addon.js 18 KB

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