addon.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. Layer.alert(ret.msg);
  181. });
  182. }
  183. });
  184. }
  185. });
  186. // 点击安装
  187. $(document).on("click", ".btn-install", function () {
  188. var that = this;
  189. var name = $(this).closest(".operate").data("name");
  190. var version = $(this).data("version");
  191. var userinfo = Controller.api.userinfo.get();
  192. var uid = userinfo ? userinfo.id : 0;
  193. var token = userinfo ? userinfo.token : '';
  194. var install = function (name, force) {
  195. Fast.api.ajax({
  196. url: 'addon/install',
  197. data: {name: name, force: force ? 1 : 0, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
  198. }, function (data, ret) {
  199. Layer.closeAll();
  200. Config['addons'][data.addon.name] = ret.data.addon;
  201. Layer.alert(__('Online installed tips'), {
  202. btn: [__('OK'), __('Donate')],
  203. title: __('Warning'),
  204. icon: 1,
  205. btn2: function () {
  206. //打赏
  207. Layer.open({
  208. content: Template("paytpl", {payimg: $(that).data("donateimage")}),
  209. shade: 0.8,
  210. area: ['800px', '600px'],
  211. skin: 'layui-layer-msg layui-layer-pay',
  212. title: false,
  213. closeBtn: true,
  214. btn: false,
  215. resize: false,
  216. });
  217. }
  218. });
  219. $('.btn-refresh').trigger('click');
  220. }, function (data, ret) {
  221. //如果是需要购买的插件则弹出二维码提示
  222. if (ret && ret.code === -1) {
  223. //扫码支付
  224. Layer.open({
  225. content: Template("paytpl", ret.data),
  226. shade: 0.8,
  227. area: ['800px', '600px'],
  228. skin: 'layui-layer-msg layui-layer-pay',
  229. title: false,
  230. closeBtn: true,
  231. btn: false,
  232. resize: false,
  233. end: function () {
  234. Layer.alert(__('Pay tips'));
  235. }
  236. });
  237. } else if (ret && ret.code === -2) {
  238. //跳转支付
  239. Layer.alert(__('Pay click tips'), {
  240. btn: [__('Pay now'), __('Cancel')],
  241. icon: 0,
  242. success: function (layero) {
  243. $(".layui-layer-btn0", layero).attr("href", ret.data.payurl).attr("target", "_blank");
  244. }
  245. }, function () {
  246. Layer.alert(__('Pay new window tips'), {icon: 0});
  247. });
  248. } else if (ret && ret.code === -3) {
  249. //插件目录发现影响全局的文件
  250. Layer.open({
  251. content: Template("conflicttpl", ret.data),
  252. shade: 0.8,
  253. area: ['800px', '600px'],
  254. title: __('Warning'),
  255. btn: [__('Continue install'), __('Cancel')],
  256. end: function () {
  257. },
  258. yes: function () {
  259. install(name, true);
  260. }
  261. });
  262. } else {
  263. Layer.alert(ret.msg);
  264. }
  265. return false;
  266. });
  267. };
  268. if ($(that).data("type") !== 'free') {
  269. if (parseInt(uid) === 0) {
  270. return Layer.alert(__('Not login tips'), {
  271. title: __('Warning'),
  272. btn: [__('Login now'), __('Continue install')],
  273. yes: function (index, layero) {
  274. $(".btn-userinfo").trigger("click");
  275. },
  276. btn2: function () {
  277. install(name, false);
  278. }
  279. });
  280. }
  281. }
  282. install(name, false);
  283. });
  284. //点击卸载
  285. $(document).on("click", ".btn-uninstall", function () {
  286. var name = $(this).closest(".operate").data("name");
  287. var uninstall = function (name, force) {
  288. Fast.api.ajax({
  289. url: 'addon/uninstall',
  290. data: {name: name, force: force ? 1 : 0}
  291. }, function (data, ret) {
  292. delete Config['addons'][name];
  293. Layer.closeAll();
  294. $('.btn-refresh').trigger('click');
  295. }, function (data, ret) {
  296. if (ret && ret.code === -3) {
  297. //插件目录发现影响全局的文件
  298. Layer.open({
  299. content: Template("conflicttpl", ret.data),
  300. shade: 0.8,
  301. area: ['800px', '600px'],
  302. title: __('Warning'),
  303. btn: [__('Continue uninstall'), __('Cancel')],
  304. end: function () {
  305. },
  306. yes: function () {
  307. uninstall(name, true);
  308. }
  309. });
  310. } else {
  311. Layer.alert(ret.msg);
  312. }
  313. return false;
  314. });
  315. };
  316. Layer.confirm(__('Uninstall tips'), function () {
  317. uninstall(name, false);
  318. });
  319. });
  320. //点击配置
  321. $(document).on("click", ".btn-config", function () {
  322. var name = $(this).closest(".operate").data("name");
  323. Fast.api.open("addon/config?name=" + name, __('Setting'));
  324. });
  325. //点击启用/禁用
  326. $(document).on("click", ".btn-enable,.btn-disable", function () {
  327. var name = $(this).closest(".operate").data("name");
  328. var action = $(this).data("action");
  329. var operate = function (name, action, force) {
  330. Fast.api.ajax({
  331. url: 'addon/state',
  332. data: {name: name, action: action, force: force ? 1 : 0}
  333. }, function (data, ret) {
  334. var addon = Config['addons'][name];
  335. addon.state = action === 'enable' ? 1 : 0;
  336. Layer.closeAll();
  337. $('.btn-refresh').trigger('click');
  338. }, function (data, ret) {
  339. if (ret && ret.code === -3) {
  340. //插件目录发现影响全局的文件
  341. Layer.open({
  342. content: Template("conflicttpl", ret.data),
  343. shade: 0.8,
  344. area: ['800px', '600px'],
  345. title: __('Warning'),
  346. btn: [__('Continue operate'), __('Cancel')],
  347. end: function () {
  348. },
  349. yes: function () {
  350. operate(name, action, true);
  351. }
  352. });
  353. } else {
  354. Layer.alert(ret.msg);
  355. }
  356. return false;
  357. });
  358. };
  359. operate(name, action, false);
  360. });
  361. //点击升级
  362. $(document).on("click", ".btn-upgrade", function () {
  363. if ($(this).closest(".operate").find("a.btn-disable").size() > 0) {
  364. Layer.alert(__('Please disable addon first'), {icon: 7});
  365. return false;
  366. }
  367. var name = $(this).closest(".operate").data("name");
  368. var version = $(this).data("version");
  369. var userinfo = Controller.api.userinfo.get();
  370. var uid = userinfo ? userinfo.id : 0;
  371. var token = userinfo ? userinfo.token : '';
  372. var upgrade = function (name) {
  373. Fast.api.ajax({
  374. url: 'addon/upgrade',
  375. data: {name: name, uid: uid, token: token, version: version, faversion: Config.fastadmin.version}
  376. }, function (data, ret) {
  377. Config['addons'][name].version = version;
  378. Layer.closeAll();
  379. $('.btn-refresh').trigger('click');
  380. }, function (data, ret) {
  381. Layer.alert(ret.msg);
  382. return false;
  383. });
  384. };
  385. Layer.confirm(__('Upgrade tips'), function () {
  386. upgrade(name);
  387. });
  388. });
  389. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  390. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  391. });
  392. },
  393. add: function () {
  394. Controller.api.bindevent();
  395. },
  396. config: function () {
  397. Controller.api.bindevent();
  398. },
  399. api: {
  400. bindevent: function () {
  401. Form.api.bindevent($("form[role=form]"));
  402. },
  403. userinfo: {
  404. get: function () {
  405. var userinfo = localStorage.getItem("fastadmin_userinfo");
  406. return userinfo ? JSON.parse(userinfo) : null;
  407. },
  408. set: function (data) {
  409. if (data) {
  410. localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  411. } else {
  412. localStorage.removeItem("fastadmin_userinfo");
  413. }
  414. }
  415. }
  416. }
  417. };
  418. return Controller;
  419. });