addon.js 20 KB

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