addon.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: Config.api_url ? Config.api_url + '/addon/index' : "addon/downloaded",
  8. add_url: '',
  9. edit_url: '',
  10. del_url: '',
  11. multi_url: ''
  12. }
  13. });
  14. var table = $("#table");
  15. // 弹窗自适应宽高
  16. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  17. table.on('load-success.bs.table', function (e, json) {
  18. if (json && typeof json.category != 'undefined' && $(".nav-category li").size() == 2) {
  19. $.each(json.category, function (i, j) {
  20. $("<li><a href='javascript:;' data-id='" + j.id + "'>" + j.name + "</a></li>").insertBefore($(".nav-category li:last"));
  21. });
  22. }
  23. });
  24. table.on('load-error.bs.table', function (e, status, res) {
  25. if (status == 404 && $(".btn-switch.active").data("type") != "local") {
  26. Layer.confirm(__('Store now available tips'), {
  27. title: __('Warmtips'),
  28. btn: [__('Switch to the local'), __('Try to reload')]
  29. }, function (index) {
  30. layer.close(index);
  31. $(".panel .nav-tabs").hide();
  32. $(".toolbar > *:not(:first)").hide();
  33. $(".btn-switch[data-type='local']").trigger("click");
  34. }, function (index) {
  35. layer.close(index);
  36. table.bootstrapTable('refresh');
  37. });
  38. return false;
  39. }
  40. });
  41. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  42. var parenttable = table.closest('.bootstrap-table');
  43. var d = $(".fixed-table-toolbar", parenttable).find(".search input");
  44. d.off("keyup drop blur");
  45. d.on("keyup", function (e) {
  46. if (e.keyCode == 13) {
  47. var that = this;
  48. var options = table.bootstrapTable('getOptions');
  49. var queryParams = options.queryParams;
  50. options.pageNumber = 1;
  51. options.queryParams = function (params) {
  52. var params = queryParams(params);
  53. params.search = $(that).val();
  54. return params;
  55. };
  56. table.bootstrapTable('refresh', {});
  57. }
  58. });
  59. });
  60. Template.helper("Moment", Moment);
  61. Template.helper("addons", Config['addons']);
  62. $("#faupload-addon").data("params", function () {
  63. var userinfo = Controller.api.userinfo.get();
  64. return {
  65. uid: userinfo ? userinfo.id : '',
  66. token: userinfo ? userinfo.token : '',
  67. version: Config.faversion
  68. };
  69. });
  70. // 初始化表格
  71. table.bootstrapTable({
  72. url: $.fn.bootstrapTable.defaults.extend.index_url,
  73. queryParams: function (params) {
  74. var userinfo = Controller.api.userinfo.get();
  75. $.extend(params, {
  76. uid: userinfo ? userinfo.id : '',
  77. token: userinfo ? userinfo.token : '',
  78. version: Config.faversion
  79. });
  80. return params;
  81. },
  82. columns: [
  83. [
  84. {field: 'id', title: 'ID', operate: false, visible: false},
  85. {
  86. field: 'home',
  87. title: __('Index'),
  88. width: '50px',
  89. formatter: Controller.api.formatter.home
  90. },
  91. {field: 'name', title: __('Name'), operate: false, visible: false, width: '120px'},
  92. {
  93. field: 'title',
  94. title: __('Title'),
  95. operate: 'LIKE',
  96. align: 'left',
  97. formatter: Controller.api.formatter.title
  98. },
  99. {field: 'intro', title: __('Intro'), operate: 'LIKE', align: 'left', class: 'visible-lg'},
  100. // {
  101. // field: 'author',
  102. // title: __('Author'),
  103. // operate: 'LIKE',
  104. // width: '100px',
  105. // formatter: Controller.api.formatter.author
  106. // },
  107. // {
  108. // field: 'price',
  109. // title: __('Price'),
  110. // operate: 'LIKE',
  111. // width: '100px',
  112. // align: 'center',
  113. // formatter: Controller.api.formatter.price
  114. // },
  115. // {
  116. // field: 'downloads',
  117. // title: __('Downloads'),
  118. // operate: 'LIKE',
  119. // width: '80px',
  120. // align: 'center',
  121. // formatter: Controller.api.formatter.downloads
  122. // },
  123. {
  124. field: 'version',
  125. title: __('Version'),
  126. operate: 'LIKE',
  127. width: '80px',
  128. align: 'center',
  129. formatter: Controller.api.formatter.version
  130. },
  131. {
  132. field: 'toggle',
  133. title: __('Status'),
  134. width: '80px',
  135. formatter: Controller.api.formatter.toggle
  136. },
  137. {
  138. field: 'id',
  139. title: __('Operate'),
  140. align: 'center',
  141. table: table,
  142. formatter: Controller.api.formatter.operate,
  143. align: 'right'
  144. },
  145. ]
  146. ],
  147. responseHandler: function (res) {
  148. $.each(res.rows, function (i, j) {
  149. j.addon = typeof Config.addons[j.name] != 'undefined' ? Config.addons[j.name] : null;
  150. });
  151. return res;
  152. },
  153. dataType: 'jsonp',
  154. templateView: false,
  155. clickToSelect: false,
  156. search: true,
  157. showColumns: false,
  158. showToggle: false,
  159. showExport: false,
  160. showSearch: false,
  161. commonSearch: true,
  162. searchFormVisible: true,
  163. searchFormTemplate: 'searchformtpl',
  164. pageSize: 50,
  165. });
  166. // 为表格绑定事件
  167. Table.api.bindevent(table);
  168. // 离线安装
  169. require(['upload'], function (Upload) {
  170. Upload.api.upload("#faupload-addon", function (data, ret) {
  171. Config['addons'][data.addon.name] = data.addon;
  172. Toastr.success(ret.msg);
  173. operate(data.addon.name, 'enable', false);
  174. return false;
  175. });
  176. });
  177. // 查看插件首页
  178. $(document).on("click", ".btn-addonindex", function () {
  179. if ($(this).attr("href") == 'javascript:;') {
  180. Layer.msg(__('Not installed tips'), {icon: 7});
  181. } else if ($(this).closest(".operate").find("a.btn-enable").size() > 0) {
  182. Layer.msg(__('Not enabled tips'), {icon: 7});
  183. return false;
  184. }
  185. });
  186. // 切换
  187. $(document).on("click", ".btn-switch", function () {
  188. $(".btn-switch").removeClass("active");
  189. $(this).addClass("active");
  190. $("form.form-commonsearch input[name='type']").val($(this).data("type"));
  191. table.bootstrapTable('refresh', {url: ($(this).data("url") ? $(this).data("url") : $.fn.bootstrapTable.defaults.extend.index_url), pageNumber: 1});
  192. return false;
  193. });
  194. $(document).on("click", ".nav-category li a", function () {
  195. $(".nav-category li").removeClass("active");
  196. $(this).parent().addClass("active");
  197. $("form.form-commonsearch input[name='category_id']").val($(this).data("id"));
  198. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  199. return false;
  200. });
  201. var tables = [];
  202. $(document).on("click", "#droptables", function () {
  203. if ($(this).prop("checked")) {
  204. Fast.api.ajax({
  205. url: "addon/get_table_list",
  206. async: false,
  207. data: {name: $(this).data("name")}
  208. }, function (data) {
  209. tables = data.tables;
  210. return false;
  211. });
  212. var html;
  213. html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">' + __('The following data tables will be deleted') + ':<br>' + tables.join("<br>") + '</div>'
  214. : '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
  215. $(html).insertAfter($(this).closest("p"));
  216. } else {
  217. $(".droptablestips").remove();
  218. }
  219. $(window).resize();
  220. });
  221. // 会员信息
  222. $(document).on("click", ".btn-userinfo", function () {
  223. var that = this;
  224. var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%'];
  225. var userinfo = Controller.api.userinfo.get();
  226. if (!userinfo) {
  227. Layer.open({
  228. content: Template("logintpl", {}),
  229. zIndex: 99,
  230. area: area,
  231. title: __('Login FastAdmin'),
  232. resize: false,
  233. btn: [__('Login'), __('Register')],
  234. yes: function (index, layero) {
  235. Fast.api.ajax({
  236. url: Config.api_url + '/user/login',
  237. dataType: 'jsonp',
  238. data: {
  239. account: $("#inputAccount", layero).val(),
  240. password: $("#inputPassword", layero).val(),
  241. _method: 'POST'
  242. }
  243. }, function (data, ret) {
  244. Controller.api.userinfo.set(data);
  245. Layer.closeAll();
  246. Layer.alert(ret.msg);
  247. }, function (data, ret) {
  248. });
  249. },
  250. btn2: function () {
  251. return false;
  252. },
  253. success: function (layero, index) {
  254. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  255. }
  256. });
  257. } else {
  258. Fast.api.ajax({
  259. url: Config.api_url + '/user/index',
  260. dataType: 'jsonp',
  261. data: {
  262. user_id: userinfo.id,
  263. token: userinfo.token,
  264. }
  265. }, function (data) {
  266. Layer.open({
  267. content: Template("userinfotpl", userinfo),
  268. area: area,
  269. title: __('Userinfo'),
  270. resize: false,
  271. btn: [__('Logout'), __('Cancel')],
  272. yes: function () {
  273. Fast.api.ajax({
  274. url: Config.api_url + '/user/logout',
  275. dataType: 'jsonp',
  276. data: {uid: userinfo.id, token: userinfo.token}
  277. }, function (data, ret) {
  278. Controller.api.userinfo.set(null);
  279. Layer.closeAll();
  280. Layer.alert(ret.msg);
  281. }, function (data, ret) {
  282. Controller.api.userinfo.set(null);
  283. Layer.closeAll();
  284. Layer.alert(ret.msg);
  285. });
  286. }
  287. });
  288. return false;
  289. }, function (data) {
  290. Controller.api.userinfo.set(null);
  291. $(that).trigger('click');
  292. return false;
  293. });
  294. }
  295. });
  296. var install = function (name, version, force) {
  297. var userinfo = Controller.api.userinfo.get();
  298. var uid = userinfo ? userinfo.id : 0;
  299. var token = userinfo ? userinfo.token : '';
  300. Fast.api.ajax({
  301. url: 'addon/install',
  302. data: {
  303. name: name,
  304. force: force ? 1 : 0,
  305. uid: uid,
  306. token: token,
  307. version: version,
  308. faversion: Config.faversion
  309. }
  310. }, function (data, ret) {
  311. Layer.closeAll();
  312. Config['addons'][data.addon.name] = ret.data.addon;
  313. Layer.alert(__('Online installed tips'), {
  314. btn: [__('OK')],
  315. title: __('Warning'),
  316. icon: 1
  317. });
  318. Controller.api.refresh(table, name);
  319. }, function (data, ret) {
  320. //如果是需要购买的插件则弹出二维码提示
  321. if (ret && ret.code === -1) {
  322. //扫码支付
  323. Layer.open({
  324. content: Template("paytpl", ret.data),
  325. shade: 0.8,
  326. area: area,
  327. skin: 'layui-layer-msg layui-layer-pay',
  328. title: false,
  329. closeBtn: true,
  330. btn: false,
  331. resize: false,
  332. end: function () {
  333. Layer.alert(__('Pay tips'));
  334. }
  335. });
  336. } else if (ret && ret.code === -2) {
  337. //如果登录已经超时,重新提醒登录
  338. if (uid && uid != ret.data.uid) {
  339. Controller.api.userinfo.set(null);
  340. $(".operate[data-name='" + name + "'] .btn-install").trigger("click");
  341. return;
  342. }
  343. top.Fast.api.open(ret.data.payurl, __('Pay now'), {
  344. area: area,
  345. end: function () {
  346. top.Layer.alert(__('Pay tips'));
  347. }
  348. });
  349. } else if (ret && ret.code === -3) {
  350. //插件目录发现影响全局的文件
  351. Layer.open({
  352. content: Template("conflicttpl", ret.data),
  353. shade: 0.8,
  354. area: area,
  355. title: __('Warning'),
  356. btn: [__('Continue install'), __('Cancel')],
  357. end: function () {
  358. },
  359. yes: function () {
  360. install(name, version, true);
  361. }
  362. });
  363. } else {
  364. Layer.alert(ret.msg);
  365. }
  366. return false;
  367. });
  368. };
  369. var uninstall = function (name, force, droptables) {
  370. Fast.api.ajax({
  371. url: 'addon/uninstall',
  372. data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
  373. }, function (data, ret) {
  374. delete Config['addons'][name];
  375. Layer.closeAll();
  376. Controller.api.refresh(table, name);
  377. }, function (data, ret) {
  378. if (ret && ret.code === -3) {
  379. //插件目录发现影响全局的文件
  380. Layer.open({
  381. content: Template("conflicttpl", ret.data),
  382. shade: 0.8,
  383. area: area,
  384. title: __('Warning'),
  385. btn: [__('Continue uninstall'), __('Cancel')],
  386. end: function () {
  387. },
  388. yes: function () {
  389. uninstall(name, true, droptables);
  390. }
  391. });
  392. } else {
  393. Layer.alert(ret.msg);
  394. }
  395. return false;
  396. });
  397. };
  398. var operate = function (name, action, force) {
  399. Fast.api.ajax({
  400. url: 'addon/state',
  401. data: {name: name, action: action, force: force ? 1 : 0}
  402. }, function (data, ret) {
  403. var addon = Config['addons'][name];
  404. addon.state = action === 'enable' ? 1 : 0;
  405. Layer.closeAll();
  406. Controller.api.refresh(table, name);
  407. }, function (data, ret) {
  408. if (ret && ret.code === -3) {
  409. //插件目录发现影响全局的文件
  410. Layer.open({
  411. content: Template("conflicttpl", ret.data),
  412. shade: 0.8,
  413. area: area,
  414. title: __('Warning'),
  415. btn: [__('Continue operate'), __('Cancel')],
  416. end: function () {
  417. },
  418. yes: function () {
  419. operate(name, action, true);
  420. }
  421. });
  422. } else {
  423. Layer.alert(ret.msg);
  424. }
  425. return false;
  426. });
  427. };
  428. var upgrade = function (name, version) {
  429. var userinfo = Controller.api.userinfo.get();
  430. var uid = userinfo ? userinfo.id : 0;
  431. var token = userinfo ? userinfo.token : '';
  432. Fast.api.ajax({
  433. url: 'addon/upgrade',
  434. data: {name: name, uid: uid, token: token, version: version, faversion: Config.faversion}
  435. }, function (data, ret) {
  436. Config['addons'][name] = data.addon;
  437. Layer.closeAll();
  438. Controller.api.refresh(table, name);
  439. }, function (data, ret) {
  440. Layer.alert(ret.msg);
  441. return false;
  442. });
  443. };
  444. // 点击安装
  445. $(document).on("click", ".btn-install", function () {
  446. var that = this;
  447. var name = $(this).closest(".operate").data("name");
  448. var version = $(this).data("version");
  449. var userinfo = Controller.api.userinfo.get();
  450. var uid = userinfo ? userinfo.id : 0;
  451. if (parseInt(uid) === 0) {
  452. return Layer.alert(__('Not login tips'), {
  453. title: __('Warning'),
  454. btn: [__('Login now')],
  455. yes: function (index, layero) {
  456. $(".btn-userinfo").trigger("click");
  457. },
  458. btn2: function () {
  459. install(name, version, false);
  460. }
  461. });
  462. }
  463. install(name, version, false);
  464. });
  465. // 点击卸载
  466. $(document).on("click", ".btn-uninstall", function () {
  467. var name = $(this).closest(".operate").data('name');
  468. if (Config['addons'][name].state == 1) {
  469. Layer.alert(__('Please disable the add before trying to uninstall'), {icon: 7});
  470. return false;
  471. }
  472. Template.helper("__", __);
  473. Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false}, function (index, layero) {
  474. uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
  475. });
  476. });
  477. // 点击配置
  478. $(document).on("click", ".btn-config", function () {
  479. var name = $(this).closest(".operate").data("name");
  480. Fast.api.open("addon/config?name=" + name, __('Setting'));
  481. });
  482. // 点击启用/禁用
  483. $(document).on("click", ".btn-enable,.btn-disable", function () {
  484. var name = $(this).data("name");
  485. var action = $(this).data("action");
  486. operate(name, action, false);
  487. });
  488. // 点击升级
  489. $(document).on("click", ".btn-upgrade", function () {
  490. var name = $(this).closest(".operate").data('name');
  491. if (Config['addons'][name].state == 1) {
  492. Layer.alert(__('Please disable the add before trying to upgrade'), {icon: 7});
  493. return false;
  494. }
  495. var version = $(this).data("version");
  496. Layer.confirm(__('Upgrade tips', Config['addons'][name].title), function () {
  497. upgrade(name, version);
  498. });
  499. });
  500. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  501. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  502. });
  503. $(document).on("click", ".view-screenshots", function () {
  504. var row = Table.api.getrowbyindex(table, parseInt($(this).data("index")));
  505. var data = [];
  506. $.each(row.screenshots, function (i, j) {
  507. data.push({
  508. "src": j
  509. });
  510. });
  511. var json = {
  512. "title": row.title,
  513. "data": data
  514. };
  515. top.Layer.photos(top.JSON.parse(JSON.stringify({photos: json})));
  516. });
  517. },
  518. add: function () {
  519. Controller.api.bindevent();
  520. },
  521. config: function () {
  522. Controller.api.bindevent();
  523. },
  524. api: {
  525. formatter: {
  526. title: function (value, row, index) {
  527. var title = '<a class="title" href="' + row.url + '" data-toggle="tooltip" title="' + __('View addon home page') + '" target="_blank">' + value + '</a>';
  528. if (row.screenshots && row.screenshots.length > 0) {
  529. // title += ' <a href="javascript:;" data-index="' + index + '" class="view-screenshots text-success" title="' + __('View addon screenshots') + '" data-toggle="tooltip"><i class="fa fa-image"></i></a>';
  530. title += ' <a href="javascript:;" data-index="' + index + '" class="view-screenshots text-success" title="' + __('View addon screenshots') + '" data-toggle="tooltip"></a>';
  531. }
  532. return title;
  533. },
  534. operate: function (value, row, index) {
  535. return Template("operatetpl", {item: row, index: index});
  536. },
  537. toggle: function (value, row, index) {
  538. if (!row.addon) {
  539. return '';
  540. }
  541. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Click to toggle status') + '" class="btn btn-toggle btn-' + (row.addon.state == 1 ? "disable" : "enable") + '" data-action="' + (row.addon.state == 1 ? "disable" : "enable") + '" data-name="' + row.name + '"><i class="fa ' + (row.addon.state == 0 ? 'fa-toggle-on fa-rotate-180 text-gray' : 'fa-toggle-on text-success') + ' fa-2x"></i></a>';
  542. },
  543. author: function (value, row, index) {
  544. var url = 'javascript:';
  545. if (typeof row.homepage !== 'undefined') {
  546. url = row.homepage;
  547. } else if (typeof row.qq !== 'undefined' && row.qq) {
  548. url = 'https://wpa.qq.com/msgrd?v=3&uin=' + row.qq + '&site=fastadmin.net&menu=yes';
  549. }
  550. return '<a href="' + url + '" target="_blank" data-toggle="tooltip" class="text-primary">' + value + '</a>';
  551. },
  552. price: function (value, row, index) {
  553. if (isNaN(value)) {
  554. return value;
  555. }
  556. return parseFloat(value) == 0 ? '<span class="text-success">' + __('Free') + '</span>' : '<span class="text-danger">¥' + value + '</span>';
  557. },
  558. downloads: function (value, row, index) {
  559. return value;
  560. },
  561. version: function (value, row, index) {
  562. return row.addon && row.addon.version != row.version ? '<a href="' + row.url + '?version=' + row.version + '" target="_blank"><span class="releasetips text-primary" data-toggle="tooltip" title="' + __('New version tips', row.version) + '">' + row.addon.version + '<i></i></span></a>' : row.version;
  563. },
  564. home: function (value, row, index) {
  565. return row.addon && parseInt(row.addon.state) > 0 ? '<a href="' + row.addon.url + '" data-toggle="tooltip" title="' + __('View addon index page') + '" target="_blank"><i class="fa fa-home text-primary"></i></a>' : '<a href="javascript:;"><i class="fa fa-home text-gray"></i></a>';
  566. },
  567. },
  568. bindevent: function () {
  569. Form.api.bindevent($("form[role=form]"));
  570. },
  571. userinfo: {
  572. get: function () {
  573. var userinfo = localStorage.getItem("fastadmin_userinfo");
  574. return userinfo ? JSON.parse(userinfo) : null;
  575. },
  576. set: function (data) {
  577. if (data) {
  578. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  579. } else {
  580. localStorage.removeItem("fastadmin_userinfo");
  581. }
  582. }
  583. },
  584. refresh: function (table, name) {
  585. //刷新左侧边栏
  586. Fast.api.refreshmenu();
  587. //刷新行数据
  588. if ($(".operate[data-name='" + name + "']").length > 0) {
  589. var index = $(".operate[data-name='" + name + "']").closest("tr[data-index]").data("index");
  590. var row = Table.api.getrowbyindex(table, index);
  591. row.addon = typeof Config['addons'][name] !== 'undefined' ? Config['addons'][name] : undefined;
  592. table.bootstrapTable("updateRow", {index: index, row: row});
  593. } else if ($(".btn-switch.active").data("type") == "local") {
  594. $(".btn-refresh").trigger("click");
  595. }
  596. }
  597. }
  598. };
  599. return Controller;
  600. });