addon.js 26 KB

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