addon.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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},
  49. {field: 'name', title: __('Name'), operate: false},
  50. {field: 'title', title: __('Title'), operate: 'LIKE'}
  51. ]
  52. ],
  53. dataType: 'jsonp',
  54. templateView: true,
  55. search: true,
  56. showColumns: false,
  57. showToggle: false,
  58. showExport: false,
  59. showSearch: false,
  60. commonSearch: true,
  61. searchFormVisible: true,
  62. searchFormTemplate: 'searchformtpl',
  63. pageSize: 12,
  64. pagination: false,
  65. });
  66. // 为表格绑定事件
  67. Table.api.bindevent(table);
  68. table.on('click', '.btn-addoninfo', function (event) {
  69. var index = parseInt($(this).data("index"));
  70. var data = table.bootstrapTable("getData");
  71. var item = data[index];
  72. var addon = typeof Config.addons[item.name] != 'undefined' ? Config.addons[item.name] : null;
  73. Layer.alert(Template("addoninfotpl", {item: item, addon: addon}), {
  74. btn: [__('OK'), __('Donate'), __('Feedback'), __('Document')],
  75. title: __('Detail'),
  76. area: ['450px', '490px'],
  77. btn2: function () {
  78. //打赏
  79. Layer.open({
  80. content: Template("paytpl", {payimg: item.donateimage}),
  81. shade: 0.8,
  82. area: ['800px', '600px'],
  83. skin: 'layui-layer-msg layui-layer-pay',
  84. title: false,
  85. closeBtn: true,
  86. btn: false,
  87. resize: false,
  88. });
  89. },
  90. btn3: function () {
  91. return false;
  92. },
  93. btn4: function () {
  94. return false;
  95. },
  96. success: function (layero, index) {
  97. $(".layui-layer-btn2", layero).attr("href", "http://forum.fastadmin.net/t/bug?ref=addon&name=" + item.name).attr("target", "_blank");
  98. $(".layui-layer-btn3", layero).attr("href", "http://www.fastadmin.net/store/" + item.name + ".html?ref=addon").attr("target", "_blank");
  99. }
  100. });
  101. });
  102. // 离线安装
  103. require(['upload'], function (Upload) {
  104. Upload.api.plupload("#plupload-addon", function (data, ret) {
  105. Config['addons'][data.addon.name] = data.addon;
  106. Toastr.success(ret.msg);
  107. operate(data.addon.name, 'enable', false);
  108. });
  109. });
  110. // 查看插件首页
  111. $(document).on("click", ".btn-addonindex", function () {
  112. if ($(this).attr("href") == 'javascript:;') {
  113. Layer.msg(__('Not installed tips'), {icon: 7});
  114. } else if ($(this).closest(".operate").find("a.btn-enable").size() > 0) {
  115. Layer.msg(__('Not enabled tips'), {icon: 7});
  116. return false;
  117. }
  118. });
  119. // 切换
  120. $(document).on("click", ".btn-switch", function () {
  121. $(".btn-switch").removeClass("active");
  122. $(this).addClass("active");
  123. $("form.form-commonsearch input[name='type']").val($(this).data("type"));
  124. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  125. return false;
  126. });
  127. $(document).on("click", ".nav-category li a", function () {
  128. $(".nav-category li").removeClass("active");
  129. $(this).parent().addClass("active");
  130. $("form.form-commonsearch input[name='category_id']").val($(this).data("id"));
  131. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  132. return false;
  133. });
  134. // 会员信息
  135. $(document).on("click", ".btn-userinfo", function () {
  136. var userinfo = Controller.api.userinfo.get();
  137. if (!userinfo) {
  138. Layer.open({
  139. content: Template("logintpl", {}),
  140. area: ['400px', '330px'],
  141. title: __('Login FastAdmin'),
  142. resize: false,
  143. btn: [__('Login'), __('Register')],
  144. yes: function (index, layero) {
  145. Fast.api.ajax({
  146. url: Config.fastadmin.api_url + '/user/login',
  147. dataType: 'jsonp',
  148. data: {
  149. account: $("#inputAccount", layero).val(),
  150. password: $("#inputPassword", layero).val(),
  151. _method: 'POST'
  152. }
  153. }, function (data, ret) {
  154. Controller.api.userinfo.set(data);
  155. Layer.closeAll();
  156. Layer.alert(ret.msg);
  157. }, function (data, ret) {
  158. Layer.alert(ret.msg);
  159. });
  160. },
  161. btn2: function () {
  162. return false;
  163. },
  164. success: function (layero, index) {
  165. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  166. }
  167. });
  168. } else {
  169. var userinfo = Controller.api.userinfo.get();
  170. if (!userinfo) {
  171. Layer.alert(__('You\'re not login'));
  172. return false;
  173. }
  174. Layer.open({
  175. content: Template("userinfotpl", userinfo),
  176. area: ['400px', '330px'],
  177. title: __('Userinfo'),
  178. resize: false,
  179. btn: [__('Logout'), __('Cancel')],
  180. yes: function () {
  181. Fast.api.ajax({
  182. url: Config.fastadmin.api_url + '/user/logout',
  183. dataType: 'jsonp',
  184. data: {uid: userinfo.id, token: userinfo.token}
  185. }, function (data, ret) {
  186. Controller.api.userinfo.set(null);
  187. Layer.closeAll();
  188. Layer.alert(ret.msg);
  189. }, function (data, ret) {
  190. Controller.api.userinfo.set(null);
  191. Layer.closeAll();
  192. Layer.alert(ret.msg);
  193. });
  194. }
  195. });
  196. }
  197. });
  198. var install = function (name, version, force) {
  199. var userinfo = Controller.api.userinfo.get();
  200. var uid = userinfo ? userinfo.id : 0;
  201. var token = userinfo ? userinfo.token : '';
  202. Fast.api.ajax({
  203. url: 'addon/install',
  204. data: {
  205. name: name,
  206. force: force ? 1 : 0,
  207. uid: uid,
  208. token: token,
  209. version: version,
  210. faversion: Config.fastadmin.version
  211. }
  212. }, function (data, ret) {
  213. Layer.closeAll();
  214. Config['addons'][data.addon.name] = ret.data.addon;
  215. Layer.alert(__('Online installed tips'), {
  216. btn: [__('OK'), __('Donate')],
  217. title: __('Warning'),
  218. icon: 1,
  219. btn2: function () {
  220. //打赏
  221. Layer.open({
  222. content: Template("paytpl", {payimg: $(that).data("donateimage")}),
  223. shade: 0.8,
  224. area: ['800px', '600px'],
  225. skin: 'layui-layer-msg layui-layer-pay',
  226. title: false,
  227. closeBtn: true,
  228. btn: false,
  229. resize: false,
  230. });
  231. }
  232. });
  233. $('.btn-refresh').trigger('click');
  234. Fast.api.refreshmenu();
  235. }, function (data, ret) {
  236. //如果是需要购买的插件则弹出二维码提示
  237. if (ret && ret.code === -1) {
  238. //扫码支付
  239. Layer.open({
  240. content: Template("paytpl", ret.data),
  241. shade: 0.8,
  242. area: ['800px', '600px'],
  243. skin: 'layui-layer-msg layui-layer-pay',
  244. title: false,
  245. closeBtn: true,
  246. btn: false,
  247. resize: false,
  248. end: function () {
  249. Layer.alert(__('Pay tips'));
  250. }
  251. });
  252. } else if (ret && ret.code === -2) {
  253. //跳转支付
  254. Layer.alert(__('Pay click tips'), {
  255. btn: [__('Pay now'), __('Cancel')],
  256. icon: 0,
  257. success: function (layero) {
  258. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  259. }
  260. }, function () {
  261. Layer.alert(__('Pay new window tips'), {icon: 0});
  262. });
  263. } else if (ret && ret.code === -3) {
  264. //插件目录发现影响全局的文件
  265. Layer.open({
  266. content: Template("conflicttpl", ret.data),
  267. shade: 0.8,
  268. area: ['800px', '600px'],
  269. title: __('Warning'),
  270. btn: [__('Continue install'), __('Cancel')],
  271. end: function () {
  272. },
  273. yes: function () {
  274. install(name, version, true);
  275. }
  276. });
  277. } else {
  278. Layer.alert(ret.msg);
  279. }
  280. return false;
  281. });
  282. };
  283. var uninstall = function (name, force) {
  284. Fast.api.ajax({
  285. url: 'addon/uninstall',
  286. data: {name: name, force: force ? 1 : 0}
  287. }, function (data, ret) {
  288. delete Config['addons'][name];
  289. Layer.closeAll();
  290. $('.btn-refresh').trigger('click');
  291. Fast.api.refreshmenu();
  292. }, function (data, ret) {
  293. 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 uninstall'), __('Cancel')],
  301. end: function () {
  302. },
  303. yes: function () {
  304. uninstall(name, true);
  305. }
  306. });
  307. } else {
  308. Layer.alert(ret.msg);
  309. }
  310. return false;
  311. });
  312. };
  313. var operate = function (name, action, force) {
  314. Fast.api.ajax({
  315. url: 'addon/state',
  316. data: {name: name, action: action, force: force ? 1 : 0}
  317. }, function (data, ret) {
  318. var addon = Config['addons'][name];
  319. addon.state = action === 'enable' ? 1 : 0;
  320. Layer.closeAll();
  321. $('.btn-refresh').trigger('click');
  322. Fast.api.refreshmenu();
  323. }, function (data, ret) {
  324. if (ret && ret.code === -3) {
  325. //插件目录发现影响全局的文件
  326. Layer.open({
  327. content: Template("conflicttpl", ret.data),
  328. shade: 0.8,
  329. area: ['800px', '600px'],
  330. title: __('Warning'),
  331. btn: [__('Continue operate'), __('Cancel')],
  332. end: function () {
  333. },
  334. yes: function () {
  335. operate(name, action, true);
  336. }
  337. });
  338. } else {
  339. Layer.alert(ret.msg);
  340. }
  341. return false;
  342. });
  343. };
  344. var upgrade = function (name, version) {
  345. var userinfo = Controller.api.userinfo.get();
  346. var uid = userinfo ? userinfo.id : 0;
  347. var token = userinfo ? userinfo.token : '';
  348. Fast.api.ajax({
  349. url: 'addon/upgrade',
  350. data: {name: name, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
  351. }, function (data, ret) {
  352. Config['addons'][name].version = version;
  353. Layer.closeAll();
  354. $('.btn-refresh').trigger('click');
  355. Fast.api.refreshmenu();
  356. }, function (data, ret) {
  357. Layer.alert(ret.msg);
  358. return false;
  359. });
  360. };
  361. // 点击安装
  362. $(document).on("click", ".btn-install", function () {
  363. var that = this;
  364. var name = $(this).closest(".operate").data("name");
  365. var version = $(this).data("version");
  366. var userinfo = Controller.api.userinfo.get();
  367. var uid = userinfo ? userinfo.id : 0;
  368. if ($(that).data("type") !== 'free') {
  369. if (parseInt(uid) === 0) {
  370. return Layer.alert(__('Not login tips'), {
  371. title: __('Warning'),
  372. btn: [__('Login now'), __('Continue install')],
  373. yes: function (index, layero) {
  374. $(".btn-userinfo").trigger("click");
  375. },
  376. btn2: function () {
  377. install(name, version, false);
  378. }
  379. });
  380. }
  381. }
  382. install(name, version, false);
  383. });
  384. // 点击卸载
  385. $(document).on("click", ".btn-uninstall", function () {
  386. var name = $(this).closest(".operate").data("name");
  387. Layer.confirm(__('Uninstall tips'), function () {
  388. uninstall(name, false);
  389. });
  390. });
  391. // 点击配置
  392. $(document).on("click", ".btn-config", function () {
  393. var name = $(this).closest(".operate").data("name");
  394. Fast.api.open("addon/config?name=" + name, __('Setting'));
  395. });
  396. // 点击启用/禁用
  397. $(document).on("click", ".btn-enable,.btn-disable", function () {
  398. var name = $(this).closest(".operate").data("name");
  399. var action = $(this).data("action");
  400. operate(name, action, false);
  401. });
  402. // 点击升级
  403. $(document).on("click", ".btn-upgrade", function () {
  404. if ($(this).closest(".operate").find("a.btn-disable").size() > 0) {
  405. Layer.alert(__('Please disable addon first'), {icon: 7});
  406. return false;
  407. }
  408. var name = $(this).closest(".operate").data("name");
  409. var version = $(this).data("version");
  410. Layer.confirm(__('Upgrade tips'), function () {
  411. upgrade(name, version);
  412. });
  413. });
  414. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  415. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  416. });
  417. },
  418. add: function () {
  419. Controller.api.bindevent();
  420. },
  421. config: function () {
  422. Controller.api.bindevent();
  423. },
  424. api: {
  425. bindevent: function () {
  426. Form.api.bindevent($("form[role=form]"));
  427. },
  428. userinfo: {
  429. get: function () {
  430. var userinfo = localStorage.getItem("fastadmin_userinfo");
  431. return userinfo ? JSON.parse(userinfo) : null;
  432. },
  433. set: function (data) {
  434. if (data) {
  435. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  436. } else {
  437. localStorage.removeItem("fastadmin_userinfo");
  438. }
  439. }
  440. }
  441. }
  442. };
  443. return Controller;
  444. });