addon.js 24 KB

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