addon.js 25 KB

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