addon.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 that = this;
  116. var name = $(this).closest(".operate").data("name");
  117. var userinfo = Controller.api.userinfo.get();
  118. var uid = userinfo ? userinfo.id : 0;
  119. var token = userinfo ? userinfo.token : '';
  120. var install = function (name, force) {
  121. Fast.api.ajax({
  122. url: 'addon/install',
  123. data: {name: name, force: force ? 1 : 0, uid: uid, token: token}
  124. }, function (data, ret) {
  125. Layer.closeAll();
  126. Config['addons'][data.addon.name] = ret.data.addon;
  127. Layer.alert(__('Online installed tips'), {
  128. btn: [__('OK'), __('Donate')],
  129. title: __('Warning'),
  130. icon: 1,
  131. btn2: function () {
  132. //打赏
  133. Layer.open({
  134. content: Template("paytpl", {payimg: $(that).data("donateimage")}),
  135. shade: 0.8,
  136. area: ['800px', '600px'],
  137. skin: 'layui-layer-msg layui-layer-pay',
  138. title: false,
  139. closeBtn: true,
  140. btn: false,
  141. resize: false,
  142. });
  143. }
  144. });
  145. $('.btn-refresh').trigger('click');
  146. }, function (data, ret) {
  147. //如果是需要购买的插件则弹出二维码提示
  148. if (ret && ret.code === -1) {
  149. //扫码支付
  150. Layer.open({
  151. content: Template("paytpl", ret.data),
  152. shade: 0.8,
  153. area: ['800px', '600px'],
  154. skin: 'layui-layer-msg layui-layer-pay',
  155. title: false,
  156. closeBtn: true,
  157. btn: false,
  158. resize: false,
  159. end: function () {
  160. Layer.alert(__('Pay tips'));
  161. }
  162. });
  163. } else if (ret && ret.code === -2) {
  164. //跳转支付
  165. Layer.alert(__('Pay click tips'), {
  166. btn: [__('Pay now'), __('Cancel')],
  167. icon: 0,
  168. success: function (layero) {
  169. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  170. }
  171. }, function () {
  172. Layer.alert(__('Pay new window tips'), {icon: 0});
  173. });
  174. } else if (ret && ret.code === -3) {
  175. //插件目录发现影响全局的文件
  176. Layer.open({
  177. content: Template("conflicttpl", ret.data),
  178. shade: 0.8,
  179. area: ['800px', '600px'],
  180. title: __('Warning'),
  181. btn: [__('Continue install'), __('Cancel')],
  182. end: function () {
  183. },
  184. yes: function () {
  185. install(name, true);
  186. }
  187. });
  188. } else {
  189. Layer.alert(ret.msg);
  190. }
  191. return false;
  192. });
  193. };
  194. if ($(that).data("type") !== 'free') {
  195. if (parseInt(uid) === 0) {
  196. return Layer.alert(__('Not login tips'), {
  197. title: __('Warning'),
  198. btn: [__('Login now'), __('Continue install')],
  199. yes: function (index, layero) {
  200. $(".btn-userinfo").trigger("click");
  201. },
  202. btn2: function () {
  203. install(name, false);
  204. }
  205. });
  206. }
  207. }
  208. install(name, false);
  209. });
  210. //点击卸载
  211. $(document).on("click", ".btn-uninstall", function () {
  212. var name = $(this).closest(".operate").data("name");
  213. var uninstall = function (name, force) {
  214. Fast.api.ajax({
  215. url: 'addon/uninstall',
  216. data: {name: name, force: force ? 1 : 0}
  217. }, function (data, ret) {
  218. delete Config['addons'][name];
  219. Layer.closeAll();
  220. $('.btn-refresh').trigger('click');
  221. }, function (data, ret) {
  222. if (ret && ret.code === -3) {
  223. //插件目录发现影响全局的文件
  224. Layer.open({
  225. content: Template("conflicttpl", ret.data),
  226. shade: 0.8,
  227. area: ['800px', '600px'],
  228. title: __('Warning'),
  229. btn: [__('Continue uninstall'), __('Cancel')],
  230. end: function () {
  231. },
  232. yes: function () {
  233. uninstall(name, true);
  234. }
  235. });
  236. } else {
  237. Layer.alert(ret.msg);
  238. }
  239. return false;
  240. });
  241. };
  242. Layer.confirm(__('Uninstall tips'), function () {
  243. uninstall(name, false);
  244. });
  245. });
  246. //点击配置
  247. $(document).on("click", ".btn-config", function () {
  248. var name = $(this).closest(".operate").data("name");
  249. Fast.api.open("addon/config?name=" + name, __('Setting'));
  250. });
  251. //点击启用/禁用
  252. $(document).on("click", ".btn-enable,.btn-disable", function () {
  253. var name = $(this).closest(".operate").data("name");
  254. var action = $(this).data("action");
  255. var operate = function (name, action, force) {
  256. Fast.api.ajax({
  257. url: 'addon/state',
  258. data: {name: name, action: action, force: force ? 1 : 0}
  259. }, function (data, ret) {
  260. var addon = Config['addons'][name];
  261. addon.state = action === 'enable' ? 1 : 0;
  262. Layer.closeAll();
  263. $('.btn-refresh').trigger('click');
  264. }, function (data, ret) {
  265. if (ret && ret.code === -3) {
  266. //插件目录发现影响全局的文件
  267. Layer.open({
  268. content: Template("conflicttpl", ret.data),
  269. shade: 0.8,
  270. area: ['800px', '600px'],
  271. title: __('Warning'),
  272. btn: [__('Continue operate'), __('Cancel')],
  273. end: function () {
  274. },
  275. yes: function () {
  276. operate(name, action, true);
  277. }
  278. });
  279. } else {
  280. Layer.alert(ret.msg);
  281. }
  282. return false;
  283. });
  284. };
  285. operate(name, action, false);
  286. });
  287. },
  288. add: function () {
  289. Controller.api.bindevent();
  290. },
  291. config: function () {
  292. Controller.api.bindevent();
  293. },
  294. api: {
  295. bindevent: function () {
  296. Form.api.bindevent($("form[role=form]"));
  297. },
  298. userinfo: {
  299. get: function () {
  300. var userinfo = localStorage.getItem("fastadmin_userinfo");
  301. return userinfo ? JSON.parse(userinfo) : null;
  302. },
  303. set: function (data) {
  304. if (data) {
  305. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  306. } else {
  307. localStorage.removeItem("fastadmin_userinfo");
  308. }
  309. }
  310. }
  311. }
  312. };
  313. return Controller;
  314. });