addon.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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('post-body.bs.table', function (e, settings, json, xhr) {
  16. var parenttable = table.closest('.bootstrap-table');
  17. var d = $(".fixed-table-toolbar", parenttable).find(".search input");
  18. d.off("keyup drop blur");
  19. d.on("keyup", function (e) {
  20. if (e.keyCode == 13) {
  21. var that = this;
  22. var options = table.bootstrapTable('getOptions');
  23. var queryParams = options.queryParams;
  24. options.pageNumber = 1;
  25. options.queryParams = function (params) {
  26. var params = queryParams(params);
  27. params.search = $(that).val();
  28. return params;
  29. };
  30. table.bootstrapTable('refresh', {});
  31. }
  32. });
  33. });
  34. Template.helper("Moment", Moment);
  35. Template.helper("addons", Config['addons']);
  36. // 初始化表格
  37. table.bootstrapTable({
  38. url: $.fn.bootstrapTable.defaults.extend.index_url,
  39. columns: [
  40. [
  41. {field: 'id', title: 'ID', operate: false},
  42. {field: 'name', title: __('Name'), operate: false},
  43. {field: 'title', title: __('Title'), operate: 'LIKE'}
  44. ]
  45. ],
  46. dataType: 'jsonp',
  47. templateView: true,
  48. search: true,
  49. showColumns: false,
  50. showToggle: false,
  51. showExport: false,
  52. commonSearch: false,
  53. searchFormVisible: false,
  54. pageSize: 12,
  55. pagination: false,
  56. queryParams: function (params) {
  57. var filter = params.filter ? JSON.parse(params.filter) : {};
  58. var op = params.op ? JSON.parse(params.op) : {};
  59. filter.faversion = Config.fastadmin.version;
  60. op.faversion = "=";
  61. params.filter = JSON.stringify(filter);
  62. params.op = JSON.stringify(op);
  63. return params;
  64. }
  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. // 切换URL
  120. $(document).on("click", ".btn-switch", function () {
  121. $(".btn-switch").removeClass("active");
  122. $(this).addClass("active");
  123. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  124. });
  125. // 会员信息
  126. $(document).on("click", ".btn-userinfo", function () {
  127. var userinfo = Controller.api.userinfo.get();
  128. if (!userinfo) {
  129. Layer.open({
  130. content: Template("logintpl", {}),
  131. area: ['400px', '330px'],
  132. title: __('Login FastAdmin'),
  133. resize: false,
  134. btn: [__('Login'), __('Register')],
  135. yes: function (index, layero) {
  136. Fast.api.ajax({
  137. url: Config.fastadmin.api_url + '/user/login',
  138. dataType: 'jsonp',
  139. data: {account: $("#inputAccount", layero).val(), password: $("#inputPassword", layero).val(), _method: 'POST'}
  140. }, function (data, ret) {
  141. Controller.api.userinfo.set(data);
  142. Layer.closeAll();
  143. Layer.alert(ret.msg);
  144. }, function (data, ret) {
  145. Layer.alert(ret.msg);
  146. });
  147. },
  148. btn2: function () {
  149. return false;
  150. },
  151. success: function (layero, index) {
  152. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  153. }
  154. });
  155. } else {
  156. var userinfo = Controller.api.userinfo.get();
  157. if (!userinfo) {
  158. Layer.alert(__('You\'re not login'));
  159. return false;
  160. }
  161. Layer.open({
  162. content: Template("userinfotpl", userinfo),
  163. area: ['400px', '330px'],
  164. title: __('Userinfo'),
  165. resize: false,
  166. btn: [__('Logout'), __('Cancel')],
  167. yes: function () {
  168. Fast.api.ajax({
  169. url: Config.fastadmin.api_url + '/user/logout',
  170. dataType: 'jsonp',
  171. data: {uid: userinfo.id, token: userinfo.token}
  172. }, function (data, ret) {
  173. Controller.api.userinfo.set(null);
  174. Layer.closeAll();
  175. Layer.alert(ret.msg);
  176. }, function (data, ret) {
  177. Controller.api.userinfo.set(null);
  178. Layer.closeAll();
  179. Layer.alert(ret.msg);
  180. });
  181. }
  182. });
  183. }
  184. });
  185. var install = function (name, version, force) {
  186. var userinfo = Controller.api.userinfo.get();
  187. var uid = userinfo ? userinfo.id : 0;
  188. var token = userinfo ? userinfo.token : '';
  189. Fast.api.ajax({
  190. url: 'addon/install',
  191. data: {name: name, force: force ? 1 : 0, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
  192. }, function (data, ret) {
  193. Layer.closeAll();
  194. Config['addons'][data.addon.name] = ret.data.addon;
  195. Layer.alert(__('Online installed tips'), {
  196. btn: [__('OK'), __('Donate')],
  197. title: __('Warning'),
  198. icon: 1,
  199. btn2: function () {
  200. //打赏
  201. Layer.open({
  202. content: Template("paytpl", {payimg: $(that).data("donateimage")}),
  203. shade: 0.8,
  204. area: ['800px', '600px'],
  205. skin: 'layui-layer-msg layui-layer-pay',
  206. title: false,
  207. closeBtn: true,
  208. btn: false,
  209. resize: false,
  210. });
  211. }
  212. });
  213. $('.btn-refresh').trigger('click');
  214. Fast.api.refreshmenu();
  215. }, function (data, ret) {
  216. //如果是需要购买的插件则弹出二维码提示
  217. if (ret && ret.code === -1) {
  218. //扫码支付
  219. Layer.open({
  220. content: Template("paytpl", ret.data),
  221. shade: 0.8,
  222. area: ['800px', '600px'],
  223. skin: 'layui-layer-msg layui-layer-pay',
  224. title: false,
  225. closeBtn: true,
  226. btn: false,
  227. resize: false,
  228. end: function () {
  229. Layer.alert(__('Pay tips'));
  230. }
  231. });
  232. } else if (ret && ret.code === -2) {
  233. //跳转支付
  234. Layer.alert(__('Pay click tips'), {
  235. btn: [__('Pay now'), __('Cancel')],
  236. icon: 0,
  237. success: function (layero) {
  238. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  239. }
  240. }, function () {
  241. Layer.alert(__('Pay new window tips'), {icon: 0});
  242. });
  243. } else if (ret && ret.code === -3) {
  244. //插件目录发现影响全局的文件
  245. Layer.open({
  246. content: Template("conflicttpl", ret.data),
  247. shade: 0.8,
  248. area: ['800px', '600px'],
  249. title: __('Warning'),
  250. btn: [__('Continue install'), __('Cancel')],
  251. end: function () {
  252. },
  253. yes: function () {
  254. install(name, version, true);
  255. }
  256. });
  257. } else {
  258. Layer.alert(ret.msg);
  259. }
  260. return false;
  261. });
  262. };
  263. var uninstall = function (name, force) {
  264. Fast.api.ajax({
  265. url: 'addon/uninstall',
  266. data: {name: name, force: force ? 1 : 0}
  267. }, function (data, ret) {
  268. delete Config['addons'][name];
  269. Layer.closeAll();
  270. $('.btn-refresh').trigger('click');
  271. Fast.api.refreshmenu();
  272. }, function (data, ret) {
  273. if (ret && ret.code === -3) {
  274. //插件目录发现影响全局的文件
  275. Layer.open({
  276. content: Template("conflicttpl", ret.data),
  277. shade: 0.8,
  278. area: ['800px', '600px'],
  279. title: __('Warning'),
  280. btn: [__('Continue uninstall'), __('Cancel')],
  281. end: function () {
  282. },
  283. yes: function () {
  284. uninstall(name, true);
  285. }
  286. });
  287. } else {
  288. Layer.alert(ret.msg);
  289. }
  290. return false;
  291. });
  292. };
  293. var operate = function (name, action, force) {
  294. Fast.api.ajax({
  295. url: 'addon/state',
  296. data: {name: name, action: action, force: force ? 1 : 0}
  297. }, function (data, ret) {
  298. var addon = Config['addons'][name];
  299. addon.state = action === 'enable' ? 1 : 0;
  300. Layer.closeAll();
  301. $('.btn-refresh').trigger('click');
  302. Fast.api.refreshmenu();
  303. }, function (data, ret) {
  304. if (ret && ret.code === -3) {
  305. //插件目录发现影响全局的文件
  306. Layer.open({
  307. content: Template("conflicttpl", ret.data),
  308. shade: 0.8,
  309. area: ['800px', '600px'],
  310. title: __('Warning'),
  311. btn: [__('Continue operate'), __('Cancel')],
  312. end: function () {
  313. },
  314. yes: function () {
  315. operate(name, action, true);
  316. }
  317. });
  318. } else {
  319. Layer.alert(ret.msg);
  320. }
  321. return false;
  322. });
  323. };
  324. var upgrade = function (name, version) {
  325. var userinfo = Controller.api.userinfo.get();
  326. var uid = userinfo ? userinfo.id : 0;
  327. var token = userinfo ? userinfo.token : '';
  328. Fast.api.ajax({
  329. url: 'addon/upgrade',
  330. data: {name: name, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
  331. }, function (data, ret) {
  332. Config['addons'][name].version = version;
  333. Layer.closeAll();
  334. $('.btn-refresh').trigger('click');
  335. Fast.api.refreshmenu();
  336. }, function (data, ret) {
  337. Layer.alert(ret.msg);
  338. return false;
  339. });
  340. };
  341. // 点击安装
  342. $(document).on("click", ".btn-install", function () {
  343. var that = this;
  344. var name = $(this).closest(".operate").data("name");
  345. var version = $(this).data("version");
  346. var userinfo = Controller.api.userinfo.get();
  347. var uid = userinfo ? userinfo.id : 0;
  348. if ($(that).data("type") !== 'free') {
  349. if (parseInt(uid) === 0) {
  350. return Layer.alert(__('Not login tips'), {
  351. title: __('Warning'),
  352. btn: [__('Login now'), __('Continue install')],
  353. yes: function (index, layero) {
  354. $(".btn-userinfo").trigger("click");
  355. },
  356. btn2: function () {
  357. install(name, version, false);
  358. }
  359. });
  360. }
  361. }
  362. install(name, version, false);
  363. });
  364. // 点击卸载
  365. $(document).on("click", ".btn-uninstall", function () {
  366. var name = $(this).closest(".operate").data("name");
  367. Layer.confirm(__('Uninstall tips'), function () {
  368. uninstall(name, false);
  369. });
  370. });
  371. // 点击配置
  372. $(document).on("click", ".btn-config", function () {
  373. var name = $(this).closest(".operate").data("name");
  374. Fast.api.open("addon/config?name=" + name, __('Setting'));
  375. });
  376. // 点击启用/禁用
  377. $(document).on("click", ".btn-enable,.btn-disable", function () {
  378. var name = $(this).closest(".operate").data("name");
  379. var action = $(this).data("action");
  380. operate(name, action, false);
  381. });
  382. // 点击升级
  383. $(document).on("click", ".btn-upgrade", function () {
  384. if ($(this).closest(".operate").find("a.btn-disable").size() > 0) {
  385. Layer.alert(__('Please disable addon first'), {icon: 7});
  386. return false;
  387. }
  388. var name = $(this).closest(".operate").data("name");
  389. var version = $(this).data("version");
  390. Layer.confirm(__('Upgrade tips'), function () {
  391. upgrade(name, version);
  392. });
  393. });
  394. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  395. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  396. });
  397. },
  398. add: function () {
  399. Controller.api.bindevent();
  400. },
  401. config: function () {
  402. Controller.api.bindevent();
  403. },
  404. api: {
  405. bindevent: function () {
  406. Form.api.bindevent($("form[role=form]"));
  407. },
  408. userinfo: {
  409. get: function () {
  410. var userinfo = localStorage.getItem("fastadmin_userinfo");
  411. return userinfo ? JSON.parse(userinfo) : null;
  412. },
  413. set: function (data) {
  414. if (data) {
  415. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  416. } else {
  417. localStorage.removeItem("fastadmin_userinfo");
  418. }
  419. }
  420. }
  421. }
  422. };
  423. return Controller;
  424. });