addon.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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', operate: false},
  23. {field: 'name', title: __('Name'), operate: false},
  24. {field: 'title', title: __('Title'), operate: 'LIKE'}
  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. $(".btn-switch").removeClass("active");
  54. $(this).addClass("active");
  55. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  56. });
  57. // 会员信息
  58. $(document).on("click", ".btn-userinfo", function () {
  59. var userinfo = Controller.api.userinfo.get();
  60. if (!userinfo) {
  61. Layer.open({
  62. content: Template("logintpl", {}),
  63. area: ['400px', '330px'],
  64. title: __('Login FastAdmin'),
  65. resize: false,
  66. btn: [__('Login'), __('Register')],
  67. yes: function (index, layero) {
  68. Fast.api.ajax({
  69. url: Config.fastadmin.api_url + '/user/login',
  70. dataType: 'jsonp',
  71. data: {account: $("#inputAccount", layero).val(), password: $("#inputPassword", layero).val(), _method: 'POST'}
  72. }, function (data, ret) {
  73. Controller.api.userinfo.set(data);
  74. Layer.closeAll();
  75. Layer.alert(ret.msg);
  76. }, function (data, ret) {
  77. Layer.alert(ret.msg);
  78. });
  79. },
  80. btn2: function () {
  81. return false;
  82. },
  83. success: function (layero, index) {
  84. $(".layui-layer-btn1", layero).prop("href", "http://www.fastadmin.net/user/register.html").prop("target", "_blank");
  85. }
  86. });
  87. } else {
  88. var userinfo = Controller.api.userinfo.get();
  89. if (!userinfo) {
  90. Layer.alert(__('You\'re not login'));
  91. return false;
  92. }
  93. Layer.open({
  94. content: Template("userinfotpl", userinfo),
  95. area: ['400px', '330px'],
  96. title: __('Userinfo'),
  97. resize: false,
  98. btn: [__('Logout'), __('Cancel')],
  99. yes: function () {
  100. Fast.api.ajax({
  101. url: Config.fastadmin.api_url + '/user/logout',
  102. dataType: 'jsonp',
  103. data: {uid: userinfo.id, token: userinfo.token}
  104. }, function (data, ret) {
  105. Controller.api.userinfo.set(null);
  106. Layer.closeAll();
  107. Layer.alert(ret.msg);
  108. }, function (data, ret) {
  109. Layer.alert(ret.msg);
  110. });
  111. }
  112. });
  113. }
  114. });
  115. // 点击安装
  116. $(document).on("click", ".btn-install", function () {
  117. var that = this;
  118. var name = $(this).closest(".operate").data("name");
  119. var userinfo = Controller.api.userinfo.get();
  120. var uid = userinfo ? userinfo.id : 0;
  121. var token = userinfo ? userinfo.token : '';
  122. var install = function (name, force) {
  123. Fast.api.ajax({
  124. url: 'addon/install',
  125. data: {name: name, force: force ? 1 : 0, uid: uid, token: token}
  126. }, function (data, ret) {
  127. Layer.closeAll();
  128. Config['addons'][data.addon.name] = ret.data.addon;
  129. Layer.alert(__('Online installed tips'), {
  130. btn: [__('OK'), __('Donate')],
  131. title: __('Warning'),
  132. icon: 1,
  133. btn2: function () {
  134. //打赏
  135. Layer.open({
  136. content: Template("paytpl", {payimg: $(that).data("donateimage")}),
  137. shade: 0.8,
  138. area: ['800px', '600px'],
  139. skin: 'layui-layer-msg layui-layer-pay',
  140. title: false,
  141. closeBtn: true,
  142. btn: false,
  143. resize: false,
  144. });
  145. }
  146. });
  147. $('.btn-refresh').trigger('click');
  148. }, function (data, ret) {
  149. //如果是需要购买的插件则弹出二维码提示
  150. if (ret && ret.code === -1) {
  151. //扫码支付
  152. Layer.open({
  153. content: Template("paytpl", ret.data),
  154. shade: 0.8,
  155. area: ['800px', '600px'],
  156. skin: 'layui-layer-msg layui-layer-pay',
  157. title: false,
  158. closeBtn: true,
  159. btn: false,
  160. resize: false,
  161. end: function () {
  162. Layer.alert(__('Pay tips'));
  163. }
  164. });
  165. } else if (ret && ret.code === -2) {
  166. //跳转支付
  167. Layer.alert(__('Pay click tips'), {
  168. btn: [__('Pay now'), __('Cancel')],
  169. icon: 0,
  170. success: function (layero) {
  171. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  172. }
  173. }, function () {
  174. Layer.alert(__('Pay new window tips'), {icon: 0});
  175. });
  176. } else if (ret && ret.code === -3) {
  177. //插件目录发现影响全局的文件
  178. Layer.open({
  179. content: Template("conflicttpl", ret.data),
  180. shade: 0.8,
  181. area: ['800px', '600px'],
  182. title: __('Warning'),
  183. btn: [__('Continue install'), __('Cancel')],
  184. end: function () {
  185. },
  186. yes: function () {
  187. install(name, true);
  188. }
  189. });
  190. } else {
  191. Layer.alert(ret.msg);
  192. }
  193. return false;
  194. });
  195. };
  196. if ($(that).data("type") !== 'free') {
  197. if (parseInt(uid) === 0) {
  198. return Layer.alert(__('Not login tips'), {
  199. title: __('Warning'),
  200. btn: [__('Login now'), __('Continue install')],
  201. yes: function (index, layero) {
  202. $(".btn-userinfo").trigger("click");
  203. },
  204. btn2: function () {
  205. install(name, false);
  206. }
  207. });
  208. }
  209. }
  210. install(name, false);
  211. });
  212. //点击卸载
  213. $(document).on("click", ".btn-uninstall", function () {
  214. var name = $(this).closest(".operate").data("name");
  215. var uninstall = function (name, force) {
  216. Fast.api.ajax({
  217. url: 'addon/uninstall',
  218. data: {name: name, force: force ? 1 : 0}
  219. }, function (data, ret) {
  220. delete Config['addons'][name];
  221. Layer.closeAll();
  222. $('.btn-refresh').trigger('click');
  223. }, function (data, ret) {
  224. if (ret && ret.code === -3) {
  225. //插件目录发现影响全局的文件
  226. Layer.open({
  227. content: Template("conflicttpl", ret.data),
  228. shade: 0.8,
  229. area: ['800px', '600px'],
  230. title: __('Warning'),
  231. btn: [__('Continue uninstall'), __('Cancel')],
  232. end: function () {
  233. },
  234. yes: function () {
  235. uninstall(name, true);
  236. }
  237. });
  238. } else {
  239. Layer.alert(ret.msg);
  240. }
  241. return false;
  242. });
  243. };
  244. Layer.confirm(__('Uninstall tips'), function () {
  245. uninstall(name, false);
  246. });
  247. });
  248. //点击配置
  249. $(document).on("click", ".btn-config", function () {
  250. var name = $(this).closest(".operate").data("name");
  251. Fast.api.open("addon/config?name=" + name, __('Setting'));
  252. });
  253. //点击启用/禁用
  254. $(document).on("click", ".btn-enable,.btn-disable", function () {
  255. var name = $(this).closest(".operate").data("name");
  256. var action = $(this).data("action");
  257. var operate = function (name, action, force) {
  258. Fast.api.ajax({
  259. url: 'addon/state',
  260. data: {name: name, action: action, force: force ? 1 : 0}
  261. }, function (data, ret) {
  262. var addon = Config['addons'][name];
  263. addon.state = action === 'enable' ? 1 : 0;
  264. Layer.closeAll();
  265. $('.btn-refresh').trigger('click');
  266. }, function (data, ret) {
  267. if (ret && ret.code === -3) {
  268. //插件目录发现影响全局的文件
  269. Layer.open({
  270. content: Template("conflicttpl", ret.data),
  271. shade: 0.8,
  272. area: ['800px', '600px'],
  273. title: __('Warning'),
  274. btn: [__('Continue operate'), __('Cancel')],
  275. end: function () {
  276. },
  277. yes: function () {
  278. operate(name, action, true);
  279. }
  280. });
  281. } else {
  282. Layer.alert(ret.msg);
  283. }
  284. return false;
  285. });
  286. };
  287. operate(name, action, false);
  288. });
  289. },
  290. add: function () {
  291. Controller.api.bindevent();
  292. },
  293. config: function () {
  294. Controller.api.bindevent();
  295. },
  296. api: {
  297. bindevent: function () {
  298. Form.api.bindevent($("form[role=form]"));
  299. },
  300. userinfo: {
  301. get: function () {
  302. var userinfo = localStorage.getItem("fastadmin_userinfo");
  303. return userinfo ? JSON.parse(userinfo) : null;
  304. },
  305. set: function (data) {
  306. if (data) {
  307. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  308. } else {
  309. localStorage.removeItem("fastadmin_userinfo");
  310. }
  311. }
  312. }
  313. }
  314. };
  315. return Controller;
  316. });