addon.js 28 KB

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