book.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'clipboard'], function ($, undefined, Backend, Table, Form, Clipboard) {
  2. var contentArr = [];
  3. var Controller = {
  4. index: function () {
  5. // 初始化表格参数配置
  6. Table.api.init({
  7. extend: {
  8. index_url: 'booklistch/book/index',
  9. add_url: 'booklistch/book/add',
  10. edit_url: 'booklistch/book/edit',
  11. del_url: 'booklistch/book/del',
  12. multi_url: 'booklistch/book/multi',
  13. table: 'book_list',
  14. }
  15. });
  16. var table = $("#table");
  17. if (Config.group == 7 || Config.group == 8) {
  18. //vip或vip运营
  19. // 初始化表格
  20. table.bootstrapTable({
  21. url: $.fn.bootstrapTable.defaults.extend.index_url,
  22. pk: 'id',
  23. sortName: 'id',
  24. columns: [
  25. [
  26. {field: 'id', title: __('Id')},
  27. {field: 'title', title: __('Title')},
  28. {field: 'name', title: __('Name')},
  29. {field: 'status', title: __('Status'), visible:false, searchList: {"normal":__('normal'),"hidden":__('hidden'),"deleted":__('deleted')}},
  30. {field: 'status_text', title: __('Status'), operate:false},
  31. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  32. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  33. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:Controller.api.formatter.showBtn()}
  34. ]
  35. ]
  36. });
  37. } else {
  38. var init = true
  39. // 初始化表格
  40. table.bootstrapTable({
  41. url: $.fn.bootstrapTable.defaults.extend.index_url,
  42. pk: 'id',
  43. sortName: 'id',
  44. search:false,
  45. commonSearch: false,
  46. searchFormVisible: false,
  47. columns: [
  48. [
  49. {checkbox: true},
  50. {field: 'id', title: __('Id')},
  51. {field: 'url', title: __('Url'), formatter: Controller.api.source_url, operate: false},
  52. {field: 'title', title: __('Title')},
  53. {field: 'name', title: __('Name')},
  54. {field: 'status', title: __('Status'), visible:false, searchList: {"normal":__('normal'),"hidden":__('hidden'),"deleted":__('deleted')}},
  55. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  56. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  57. {field: 'status_text', title: __('Status'), operate:false, formatter:function (value,row, index) {
  58. if (row.status == 'normal') {
  59. return '<span style="color: green">'+value+'</span>';
  60. } else {
  61. return '<span style="color: red">'+value+'</span>';
  62. }
  63. }},
  64. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:Controller.api.formatter.showChBtn()}
  65. ]
  66. ],
  67. onLoadSuccess: function () {
  68. if (init) {
  69. var clipboard = new Clipboard('.btn-operate-copy', {
  70. target: function (trigger) {
  71. var stxt = $(trigger).parent().parent().find('.url_text i').data('clipboard-text');
  72. trigger = document.querySelector('#many_copy_dom');
  73. $('#many_copy_dom').html(stxt);
  74. return trigger;
  75. }
  76. });
  77. clipboard.on('success',function (data) {
  78. Toastr.success('复制成功');
  79. }).on('error',function (data) {
  80. Toastr.error('复制失败');
  81. });
  82. init = false;
  83. }
  84. },
  85. onPostBody: function (data) {
  86. //隐藏编辑按钮
  87. if (data.length > 0) {
  88. $.each(data, function (index, row) {
  89. if (row.self == "0") {
  90. //隐藏禁用按钮 显示解禁按钮
  91. $("a.btn-editone").eq(index).hide();
  92. }
  93. if (row.status != 'normal') {
  94. $("a.btn-operate-copy").eq(index).hide();
  95. }
  96. });
  97. }
  98. }
  99. });
  100. }
  101. $(document).on('click', '.btn-analysis', function () {
  102. Fast.api.open($(this).attr('href'), $(this).data('title') ? $(this).data('title') : '查看统计', {});
  103. return false;
  104. });
  105. // 为表格绑定事件
  106. Table.api.bindevent(table);
  107. },
  108. add: function () {
  109. Controller.api.bindevent();
  110. },
  111. edit: function () {
  112. contentArr = JSON.parse(Config.row.content);
  113. if (contentArr.length > 0) {
  114. var html = '';
  115. for (var j = 0; j < contentArr.length; j++) {
  116. var arr = contentArr[j];
  117. var id = arr.id, image = arr.image, description = arr.description;
  118. var eidtBtn = '';
  119. if (arr.self == 1) {
  120. eidtBtn = '<span><a href="javascript:;" class="btn btn-modify"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>编辑</a></span>';
  121. }
  122. html += '<div class="page-per" id="content-page'+id+'">'+
  123. '<p><img class="content-image" src="'+image+'" alt="图片"><span>'+description+'</span></p>'+
  124. '<div class="no-border btn-opreate" data-row_id="'+id+'">'+
  125. eidtBtn+
  126. '<span><a href="javascript:;" class="btn btn-deleted"><i class="fa fa-trash-o" aria-hidden="true"></i>删除</a></span>'+
  127. '</div>'+
  128. '</div>';
  129. }
  130. $(".img_txt_list").append(html);
  131. $("#content_json").val(JSON.stringify(contentArr));
  132. }
  133. Controller.api.bindevent();
  134. },
  135. api: {
  136. bindevent: function () {
  137. Form.api.bindevent($("form[role=form]"));
  138. //弹出选择
  139. $(document).on('click', '#select-resources', function () {
  140. Fast.api.open('booklistch/media/select', '选择', {
  141. callback: function (rows) {
  142. if (rows.length > 0) {
  143. for (var m=0; m < rows.length; m++) {
  144. parseData(rows[m]);
  145. }
  146. }
  147. }
  148. });
  149. });
  150. //弹出新增
  151. $(document).on('click', '#add-resources', function () {
  152. Fast.api.open('booklistch/media/add?from_window=new', '新增', {
  153. callback: function (data) {
  154. parseData(data);
  155. }
  156. });
  157. });
  158. //初始化结果
  159. var parseData = function (row) {
  160. var id = row.id;
  161. //判断是否存在
  162. if (contentArr.length > 0) {
  163. for(var i = 0; i< contentArr.length; i++) {
  164. if (contentArr[i].id == id) {
  165. //已存在 不在添加
  166. Toastr.error("书单已存在该资源,请勿重复选择");
  167. return false;
  168. break;
  169. }
  170. }
  171. }
  172. var eidtBtn = '';
  173. if (row.self == 1) {
  174. eidtBtn = '<span><a href="javascript:;" class="btn btn-modify"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>编辑</a></span>';
  175. }
  176. var html = '<div class="page-per" id="content-page'+id+'">'+
  177. '<p><img class="content-image" src="'+row.image+'" alt="图片"><span>'+row.description+'</span></p>'+
  178. '<div class="no-border btn-opreate" data-row_id="'+id+'">'+
  179. eidtBtn +
  180. '<span><a href="javascript:;" class="btn btn-deleted"><i class="fa fa-trash-o" aria-hidden="true"></i>删除</a></span>'+
  181. '</div>'+
  182. '</div>';
  183. $(".img_txt_list").append(html);
  184. contentArr.push(row);
  185. addContent();
  186. };
  187. var addContent = function() {
  188. $("#content_json").val(JSON.stringify(contentArr));
  189. };
  190. var delContent = function(id)
  191. {
  192. for(var i = 0; i< contentArr.length; i++) {
  193. if (contentArr[i].id == id) {
  194. contentArr.splice(i, 1);
  195. $("#content-page"+id).remove();
  196. break;
  197. }
  198. }
  199. $("#content_json").val(JSON.stringify(contentArr));
  200. };
  201. //编辑
  202. $(document).on('click', '.btn-modify', function () {
  203. var id = $(this).parent().parent().data('row_id');
  204. Fast.api.open('booklistch/media/edit/ids/'+id+'?from_window=edit', '编辑', {
  205. callback: function (data) {
  206. delContent(id);
  207. parseData(data);
  208. }
  209. });
  210. });
  211. //删除
  212. $(document).on('click', '.btn-deleted', function () {
  213. var id = $(this).parent().parent().data('row_id');
  214. delContent(id);
  215. });
  216. $(document).on('click', '.btn-operate-copy', function () {
  217. $(this).parent().parent().find('.url_text').click();
  218. //$(this).parent().parent().find('.url_text').trigger("click");
  219. });
  220. },
  221. source_url: function (value, row, index) {
  222. var html = [];
  223. value = row.url;
  224. if (row.status == "normal") {
  225. html.push('<div class="link_txt"></div><div class="url_text">' + value + '&nbsp;<i class="fa fa-copy copy" data-clipboard-text="' + value + '"></i>');
  226. } else {
  227. html.push('<div class="link_txt">该书单已关闭不可使用</div>');
  228. }
  229. return html.join('<br>');
  230. },
  231. formatter: {
  232. showBtn: function(){
  233. return [
  234. {
  235. name: 'clear_cache',
  236. text: '清除书单缓存',
  237. title: '清除书单缓存',
  238. classname: 'btn btn-xs btn-danger btn-ajax',
  239. url: '/admin/booklistch/book/clearcache',
  240. callback: function (data) {
  241. }
  242. },
  243. {
  244. name: 'analysis',
  245. text: '查看统计',
  246. title: '查看统计',
  247. classname: 'btn btn-xs btn-warning btn-analysis',
  248. url: '/admin/booklistch/collect/analysis',
  249. callback: function (data) {
  250. }
  251. }
  252. ];
  253. },
  254. showChBtn: function(value, row){
  255. return [
  256. {
  257. name: 'copy_url',
  258. text: '复制链接',
  259. title: '复制链接',
  260. classname: 'btn btn-click btn-operate-copy',
  261. icon: 'fa fa-copy copy',
  262. },
  263. {
  264. name: 'clear_cache',
  265. text: '清除书单缓存',
  266. title: '清除书单缓存',
  267. classname: 'btn btn-xs btn-danger btn-ajax',
  268. url: '/admin/booklistch/book/clearcache',
  269. callback:function(data){}
  270. },
  271. {
  272. name: 'analysis',
  273. text: '查看统计',
  274. title: '查看统计',
  275. classname: 'btn btn-xs btn-warning btn-analysis',
  276. url: '/admin/booklistch/collect/analysis',
  277. callback:function(data){}
  278. }
  279. ];
  280. },
  281. }
  282. }
  283. };
  284. return Controller;
  285. });