addon.js 20 KB

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