addon.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. // 会员信息
  191. $(document).on("click", ".btn-userinfo", function () {
  192. var that = this;
  193. var userinfo = Controller.api.userinfo.get();
  194. if (!userinfo) {
  195. Layer.open({
  196. content: Template("logintpl", {}),
  197. zIndex: 99,
  198. area: area,
  199. title: __('Login FastAdmin'),
  200. resize: false,
  201. btn: [__('Login'), __('Register')],
  202. yes: function (index, layero) {
  203. Fast.api.ajax({
  204. url: Config.api_url + '/user/login',
  205. dataType: 'jsonp',
  206. data: {
  207. account: $("#inputAccount", layero).val(),
  208. password: $("#inputPassword", layero).val(),
  209. _method: 'POST'
  210. }
  211. }, function (data, ret) {
  212. Controller.api.userinfo.set(data);
  213. Layer.closeAll();
  214. Layer.alert(ret.msg);
  215. }, function (data, ret) {
  216. });
  217. },
  218. btn2: function () {
  219. return false;
  220. },
  221. success: function (layero, index) {
  222. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  223. }
  224. });
  225. } else {
  226. Fast.api.ajax({
  227. url: Config.api_url + '/user/index',
  228. dataType: 'jsonp',
  229. data: {
  230. user_id: userinfo.id,
  231. token: userinfo.token,
  232. }
  233. }, function (data) {
  234. Layer.open({
  235. content: Template("userinfotpl", userinfo),
  236. area: area,
  237. title: __('Userinfo'),
  238. resize: false,
  239. btn: [__('Logout'), __('Cancel')],
  240. yes: function () {
  241. Fast.api.ajax({
  242. url: Config.api_url + '/user/logout',
  243. dataType: 'jsonp',
  244. data: {uid: userinfo.id, token: userinfo.token}
  245. }, function (data, ret) {
  246. Controller.api.userinfo.set(null);
  247. Layer.closeAll();
  248. Layer.alert(ret.msg);
  249. }, function (data, ret) {
  250. Controller.api.userinfo.set(null);
  251. Layer.closeAll();
  252. Layer.alert(ret.msg);
  253. });
  254. }
  255. });
  256. return false;
  257. }, function (data) {
  258. Controller.api.userinfo.set(null);
  259. $(that).trigger('click');
  260. return false;
  261. });
  262. }
  263. });
  264. var install = function (name, version, force) {
  265. var userinfo = Controller.api.userinfo.get();
  266. var uid = userinfo ? userinfo.id : 0;
  267. var token = userinfo ? userinfo.token : '';
  268. Fast.api.ajax({
  269. url: 'addon/install',
  270. data: {
  271. name: name,
  272. force: force ? 1 : 0,
  273. uid: uid,
  274. token: token,
  275. version: version,
  276. faversion: Config.faversion
  277. }
  278. }, function (data, ret) {
  279. Layer.closeAll();
  280. Config['addons'][data.addon.name] = ret.data.addon;
  281. Layer.alert(__('Online installed tips'), {
  282. btn: [__('OK')],
  283. title: __('Warning'),
  284. icon: 1
  285. });
  286. $('.btn-refresh').trigger('click');
  287. Fast.api.refreshmenu();
  288. }, function (data, ret) {
  289. //如果是需要购买的插件则弹出二维码提示
  290. if (ret && ret.code === -1) {
  291. //扫码支付
  292. Layer.open({
  293. content: Template("paytpl", ret.data),
  294. shade: 0.8,
  295. area: area,
  296. skin: 'layui-layer-msg layui-layer-pay',
  297. title: false,
  298. closeBtn: true,
  299. btn: false,
  300. resize: false,
  301. end: function () {
  302. Layer.alert(__('Pay tips'));
  303. }
  304. });
  305. } else if (ret && ret.code === -2) {
  306. //如果登录已经超时,重新提醒登录
  307. if (uid && uid != ret.data.uid) {
  308. Controller.api.userinfo.set(null);
  309. $(".operate[data-name='" + name + "'] .btn-install").trigger("click");
  310. return;
  311. }
  312. top.Fast.api.open(ret.data.payurl, __('Pay now'), {
  313. area: area,
  314. end: function () {
  315. top.Layer.alert(__('Pay tips'));
  316. }
  317. });
  318. } else if (ret && ret.code === -3) {
  319. //插件目录发现影响全局的文件
  320. Layer.open({
  321. content: Template("conflicttpl", ret.data),
  322. shade: 0.8,
  323. area: area,
  324. title: __('Warning'),
  325. btn: [__('Continue install'), __('Cancel')],
  326. end: function () {
  327. },
  328. yes: function () {
  329. install(name, version, true);
  330. }
  331. });
  332. } else {
  333. Layer.alert(ret.msg);
  334. }
  335. return false;
  336. });
  337. };
  338. var uninstall = function (name, force) {
  339. Fast.api.ajax({
  340. url: 'addon/uninstall',
  341. data: {name: name, force: force ? 1 : 0}
  342. }, function (data, ret) {
  343. delete Config['addons'][name];
  344. Layer.closeAll();
  345. $('.btn-refresh').trigger('click');
  346. Fast.api.refreshmenu();
  347. }, function (data, ret) {
  348. if (ret && ret.code === -3) {
  349. //插件目录发现影响全局的文件
  350. Layer.open({
  351. content: Template("conflicttpl", ret.data),
  352. shade: 0.8,
  353. area: area,
  354. title: __('Warning'),
  355. btn: [__('Continue uninstall'), __('Cancel')],
  356. end: function () {
  357. },
  358. yes: function () {
  359. uninstall(name, true);
  360. }
  361. });
  362. } else {
  363. Layer.alert(ret.msg);
  364. }
  365. return false;
  366. });
  367. };
  368. var operate = function (name, action, force) {
  369. Fast.api.ajax({
  370. url: 'addon/state',
  371. data: {name: name, action: action, force: force ? 1 : 0}
  372. }, function (data, ret) {
  373. var addon = Config['addons'][name];
  374. addon.state = action === 'enable' ? 1 : 0;
  375. Layer.closeAll();
  376. $('.btn-refresh').trigger('click');
  377. Fast.api.refreshmenu();
  378. }, function (data, ret) {
  379. if (ret && ret.code === -3) {
  380. //插件目录发现影响全局的文件
  381. Layer.open({
  382. content: Template("conflicttpl", ret.data),
  383. shade: 0.8,
  384. area: area,
  385. title: __('Warning'),
  386. btn: [__('Continue operate'), __('Cancel')],
  387. end: function () {
  388. },
  389. yes: function () {
  390. operate(name, action, true);
  391. }
  392. });
  393. } else {
  394. Layer.alert(ret.msg);
  395. }
  396. return false;
  397. });
  398. };
  399. var upgrade = function (name, version) {
  400. var userinfo = Controller.api.userinfo.get();
  401. var uid = userinfo ? userinfo.id : 0;
  402. var token = userinfo ? userinfo.token : '';
  403. Fast.api.ajax({
  404. url: 'addon/upgrade',
  405. data: {name: name, uid: uid, token: token, version: version, faversion: Config.faversion}
  406. }, function (data, ret) {
  407. Config['addons'][name].version = version;
  408. Layer.closeAll();
  409. $('.btn-refresh').trigger('click');
  410. Fast.api.refreshmenu();
  411. }, function (data, ret) {
  412. Layer.alert(ret.msg);
  413. return false;
  414. });
  415. };
  416. // 点击安装
  417. $(document).on("click", ".btn-install", function () {
  418. var that = this;
  419. var name = $(this).closest(".operate").data("name");
  420. var version = $(this).data("version");
  421. var userinfo = Controller.api.userinfo.get();
  422. var uid = userinfo ? userinfo.id : 0;
  423. if (parseInt(uid) === 0) {
  424. return Layer.alert(__('Not login tips'), {
  425. title: __('Warning'),
  426. btn: [__('Login now')],
  427. yes: function (index, layero) {
  428. $(".btn-userinfo").trigger("click");
  429. },
  430. btn2: function () {
  431. install(name, version, false);
  432. }
  433. });
  434. }
  435. install(name, version, false);
  436. });
  437. // 点击卸载
  438. $(document).on("click", ".btn-uninstall", function () {
  439. var name = $(this).closest(".operate").data('name');
  440. if (Config['addons'][name].state == 1) {
  441. Layer.alert(__('Please disable addon first'), {icon: 7});
  442. return false;
  443. }
  444. Layer.confirm(__('Uninstall tips', Config['addons'][name].title), function () {
  445. uninstall(name, false);
  446. });
  447. });
  448. // 点击配置
  449. $(document).on("click", ".btn-config", function () {
  450. var name = $(this).closest(".operate").data("name");
  451. Fast.api.open("addon/config?name=" + name, __('Setting'));
  452. });
  453. // 点击启用/禁用
  454. $(document).on("click", ".btn-enable,.btn-disable", function () {
  455. var name = $(this).data("name");
  456. var action = $(this).data("action");
  457. operate(name, action, false);
  458. });
  459. // 点击升级
  460. $(document).on("click", ".btn-upgrade", function () {
  461. var name = $(this).closest(".operate").data('name');
  462. if (Config['addons'][name].state == 1) {
  463. Layer.alert(__('Please disable addon first'), {icon: 7});
  464. return false;
  465. }
  466. var version = $(this).data("version");
  467. Layer.confirm(__('Upgrade tips', Config['addons'][name].title), function () {
  468. upgrade(name, version);
  469. });
  470. });
  471. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  472. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  473. });
  474. $(document).on("click", ".view-screenshots", function () {
  475. var row = Table.api.getrowbyindex(table, parseInt($(this).data("index")));
  476. var data = [];
  477. $.each(row.screenshots, function (i, j) {
  478. data.push({
  479. "src": j
  480. });
  481. });
  482. var json = {
  483. "title": row.title,
  484. "data": data
  485. };
  486. top.Layer.photos(top.JSON.parse(JSON.stringify({photos: json})));
  487. });
  488. },
  489. add: function () {
  490. Controller.api.bindevent();
  491. },
  492. config: function () {
  493. Controller.api.bindevent();
  494. },
  495. api: {
  496. formatter: {
  497. title: function (value, row, index) {
  498. var title = '<a class="title" href="' + row.url + '" data-toggle="tooltip" title="' + __('View addon home page') + '" target="_blank">' + value + '</a>';
  499. if (row.screenshots && row.screenshots.length > 0) {
  500. 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>';
  501. }
  502. return title;
  503. },
  504. operate: function (value, row, index) {
  505. return Template("operatetpl", {item: row, index: index});
  506. },
  507. toggle: function (value, row, index) {
  508. if (!row.addon) {
  509. return '';
  510. }
  511. 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>';
  512. },
  513. author: function (value, row, index) {
  514. var url = 'javascript:';
  515. if (typeof row.homepage !== 'undefined') {
  516. url = row.homepage;
  517. } else if (typeof row.qq !== 'undefined') {
  518. url = 'https://wpa.qq.com/msgrd?v=3&uin=' + row.qq + '&site=fastadmin.net&menu=yes';
  519. }
  520. return '<a href="' + url + '" target="_blank" data-toggle="tooltip" title="' + __('Click to contact developer') + '" class="text-primary">' + value + '</a>';
  521. },
  522. price: function (value, row, index) {
  523. if (isNaN(value)) {
  524. return value;
  525. }
  526. return parseFloat(value) == 0 ? '<span class="text-success">' + __('Free') + '</span>' : '<span class="text-danger">¥' + value + '</span>';
  527. },
  528. downloads: function (value, row, index) {
  529. return value;
  530. },
  531. version: function (value, row, index) {
  532. 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;
  533. },
  534. home: function (value, row, index) {
  535. 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>';
  536. },
  537. },
  538. bindevent: function () {
  539. Form.api.bindevent($("form[role=form]"));
  540. },
  541. userinfo: {
  542. get: function () {
  543. var userinfo = localStorage.getItem("fastadmin_userinfo");
  544. return userinfo ? JSON.parse(userinfo) : null;
  545. },
  546. set: function (data) {
  547. if (data) {
  548. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  549. } else {
  550. localStorage.removeItem("fastadmin_userinfo");
  551. }
  552. }
  553. }
  554. }
  555. };
  556. return Controller;
  557. });