addon.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. Template.helper("Moment", Moment);
  16. Template.helper("addons", Config['addons']);
  17. // 初始化表格
  18. table.bootstrapTable({
  19. url: location.protocol === "https:" ? "addon/downloaded" : $.fn.bootstrapTable.defaults.extend.index_url,
  20. columns: [
  21. [
  22. {field: 'id', title: 'ID'},
  23. {field: 'name', title: __('Name')},
  24. {field: 'title', title: __('Title')}
  25. ]
  26. ],
  27. dataType: 'jsonp',
  28. templateView: true,
  29. search: false,
  30. showColumns: false,
  31. showToggle: false,
  32. showExport: false,
  33. commonSearch: true,
  34. searchFormVisible: false,
  35. pageSize: 10
  36. });
  37. // 为表格绑定事件
  38. Table.api.bindevent(table);
  39. // 如果是https则启用提示
  40. if (location.protocol === "https:") {
  41. $("#warmtips").removeClass("hide");
  42. $(".btn-switch,.btn-userinfo").addClass("disabled");
  43. }
  44. require(['upload'], function (Upload) {
  45. Upload.api.plupload("#plupload-addon", function (data, ret) {
  46. Config['addons'][data.addon.name] = data.addon;
  47. $('.btn-refresh').trigger('click');
  48. Toastr.success(ret.msg);
  49. });
  50. });
  51. //切换URL
  52. $(document).on("click", ".btn-switch", function () {
  53. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  54. });
  55. // 会员信息
  56. $(document).on("click", ".btn-userinfo", function () {
  57. var userinfo = Controller.api.userinfo.get();
  58. if (!userinfo) {
  59. Layer.open({
  60. content: Template("logintpl", {}),
  61. area: ['400px', '330px'],
  62. title: __('Login FastAdmin'),
  63. resize: false,
  64. btn: [__('Login'), __('Register')],
  65. yes: function (index, layero) {
  66. Fast.api.ajax({
  67. url: Config.fastadmin.api_url + '/user/login',
  68. dataType: 'jsonp',
  69. data: {account: $("#inputAccount", layero).val(), password: $("#inputPassword", layero).val(), _method: 'POST'}
  70. }, function (data, ret) {
  71. Controller.api.userinfo.set(data);
  72. Layer.closeAll();
  73. Layer.alert(ret.msg);
  74. }, function (data, ret) {
  75. Layer.alert(ret.msg);
  76. });
  77. },
  78. btn2: function () {
  79. return false;
  80. },
  81. success: function (layero, index) {
  82. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  83. }
  84. });
  85. } else {
  86. var userinfo = Controller.api.userinfo.get();
  87. if (!userinfo) {
  88. Layer.alert(__('You\'re not login'));
  89. return false;
  90. }
  91. Layer.open({
  92. content: Template("userinfotpl", userinfo),
  93. area: ['400px', '330px'],
  94. title: __('Userinfo'),
  95. resize: false,
  96. btn: [__('Logout'), __('Cancel')],
  97. yes: function () {
  98. Fast.api.ajax({
  99. url: Config.fastadmin.api_url + '/user/logout',
  100. dataType: 'jsonp',
  101. data: {uid: userinfo.id, token: userinfo.token}
  102. }, function (data, ret) {
  103. Controller.api.userinfo.set(null);
  104. Layer.closeAll();
  105. Layer.alert(ret.msg);
  106. }, function (data, ret) {
  107. Layer.alert(ret.msg);
  108. });
  109. }
  110. });
  111. }
  112. });
  113. // 点击安装
  114. $(document).on("click", ".btn-install", function () {
  115. var name = $(this).closest(".operate").data("name");
  116. var userinfo = Controller.api.userinfo.get();
  117. var uid = userinfo ? userinfo.id : 0;
  118. var token = userinfo ? userinfo.token : '';
  119. var install = function (name, force) {
  120. Fast.api.ajax({
  121. url: 'addon/install',
  122. data: {name: name, force: force ? 1 : 0, uid: uid, token: token}
  123. }, function (data, ret) {
  124. Layer.closeAll();
  125. Config['addons'][data.addon.name] = ret.data.addon;
  126. $('.btn-refresh').trigger('click');
  127. }, function (data, ret) {
  128. //如果是需要购买的插件则弹出二维码提示
  129. if (ret && ret.code === -1) {
  130. //扫码支付
  131. Layer.open({
  132. content: Template("paytpl", ret.data),
  133. shade: 0.8,
  134. area: ['800px', '600px'],
  135. skin: 'layui-layer-msg layui-layer-pay',
  136. title: false,
  137. closeBtn: true,
  138. btn: false,
  139. resize: false,
  140. end: function () {
  141. Layer.alert(__('Pay tips'));
  142. }
  143. });
  144. } else if (ret && ret.code === -2) {
  145. //跳转支付
  146. Layer.alert(__('Pay click tips'), {
  147. btn: [__('Pay now'), __('Cancel')],
  148. icon: 0,
  149. success: function (layero) {
  150. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  151. }
  152. }, function () {
  153. Layer.alert(__('Pay new window tips'), {icon: 0});
  154. });
  155. } else if (ret && ret.code === -3) {
  156. //插件目录发现影响全局的文件
  157. Layer.open({
  158. content: Template("conflicttpl", ret.data),
  159. shade: 0.8,
  160. area: ['800px', '600px'],
  161. title: __('Warning'),
  162. btn: [__('Continue install'), __('Cancel')],
  163. end: function () {
  164. },
  165. yes: function () {
  166. install(name, true);
  167. }
  168. });
  169. } else {
  170. Layer.alert(ret.msg);
  171. }
  172. return false;
  173. });
  174. };
  175. install(name, false);
  176. });
  177. //点击卸载
  178. $(document).on("click", ".btn-uninstall", function () {
  179. var name = $(this).closest(".operate").data("name");
  180. var uninstall = function (name, force) {
  181. Fast.api.ajax({
  182. url: 'addon/uninstall',
  183. data: {name: name, force: force ? 1 : 0}
  184. }, function (data, ret) {
  185. delete Config['addons'][name];
  186. Layer.closeAll();
  187. $('.btn-refresh').trigger('click');
  188. }, function (data, ret) {
  189. if (ret && ret.code === -3) {
  190. //插件目录发现影响全局的文件
  191. Layer.open({
  192. content: Template("conflicttpl", ret.data),
  193. shade: 0.8,
  194. area: ['800px', '600px'],
  195. title: __('Warning'),
  196. btn: [__('Continue uninstall'), __('Cancel')],
  197. end: function () {
  198. },
  199. yes: function () {
  200. uninstall(name, true);
  201. }
  202. });
  203. } else {
  204. Layer.alert(ret.msg);
  205. }
  206. return false;
  207. });
  208. };
  209. Layer.confirm(__('Uninstall tips'), function () {
  210. uninstall(name, false);
  211. });
  212. });
  213. //点击配置
  214. $(document).on("click", ".btn-config", function () {
  215. var name = $(this).closest(".operate").data("name");
  216. Fast.api.open("addon/config?name=" + name, __('Setting'));
  217. });
  218. //点击启用/禁用
  219. $(document).on("click", ".btn-enable,.btn-disable", function () {
  220. var name = $(this).closest(".operate").data("name");
  221. var action = $(this).data("action");
  222. var operate = function (name, action, force) {
  223. Fast.api.ajax({
  224. url: 'addon/state',
  225. data: {name: name, action: action, force: force ? 1 : 0}
  226. }, function (data, ret) {
  227. var addon = Config['addons'][name];
  228. addon.state = action === 'enable' ? 1 : 0;
  229. Layer.closeAll();
  230. $('.btn-refresh').trigger('click');
  231. }, function (data, ret) {
  232. if (ret && ret.code === -3) {
  233. //插件目录发现影响全局的文件
  234. Layer.open({
  235. content: Template("conflicttpl", ret.data),
  236. shade: 0.8,
  237. area: ['800px', '600px'],
  238. title: __('Warning'),
  239. btn: [__('Continue operate'), __('Cancel')],
  240. end: function () {
  241. },
  242. yes: function () {
  243. operate(name, action, true);
  244. }
  245. });
  246. } else {
  247. Layer.alert(ret.msg);
  248. }
  249. return false;
  250. });
  251. };
  252. operate(name, action, false);
  253. });
  254. },
  255. add: function () {
  256. Controller.api.bindevent();
  257. },
  258. config: function () {
  259. Controller.api.bindevent();
  260. },
  261. api: {
  262. bindevent: function () {
  263. Form.api.bindevent($("form[role=form]"));
  264. },
  265. userinfo: {
  266. get: function () {
  267. var userinfo = localStorage.getItem("fastadmin_userinfo");
  268. return userinfo ? JSON.parse(userinfo) : null;
  269. },
  270. set: function (data) {
  271. if (data) {
  272. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  273. } else {
  274. localStorage.removeItem("fastadmin_userinfo");
  275. }
  276. }
  277. }
  278. }
  279. };
  280. return Controller;
  281. });