addon.js 26 KB

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