response.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte'], function ($, undefined, Backend, Table, Form, Adminlte) {
  2. //去掉特殊字符和转义字符
  3. function excludeSpecial(s) {
  4. s = s.replace(/[\\\/\b\f\n\r\t\'\[\]\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/g,'');
  5. return s;
  6. };
  7. $(document).on('keyup','.description',function () {
  8. var descrp = $(this).val();
  9. $(this).val(excludeSpecial(descrp));
  10. });
  11. var Controller = {
  12. index: function () {
  13. // 初始化表格参数配置
  14. Table.api.init({
  15. extend: {
  16. index_url: 'wechat/response/index',
  17. add_url: 'wechat/response/add',
  18. edit_url: 'wechat/response/edit',
  19. del_url: 'wechat/response/del',
  20. multi_url: 'wechat/response/multi',
  21. }
  22. });
  23. var table = $("#table");
  24. // 初始化表格
  25. table.bootstrapTable({
  26. url: $.fn.bootstrapTable.defaults.extend.index_url,
  27. sortName: 'id',
  28. columns: [
  29. [
  30. {field: 'state', checkbox: true, },
  31. // {field: 'id', title: 'ID'},
  32. {field: 'type', title: __('Type'),formatter:function(value){
  33. var ob = {'text':'文本','image':'图片', 'news':'图文'};
  34. return ob[value];
  35. }, searchList:{'text':'文本', 'image':'图片', 'news':'图文'}},
  36. {field: 'title', title: __('Resource title'),operate:'LIKE'},
  37. // {field: 'eventkey', title: __('Event key')},
  38. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false},
  39. {field: 'is_subscribe', title: __('是否支持多图文'), formatter: function (value) {
  40. var o = {'1':'支持多图文','0':'不支持多图文'};
  41. return o[value];
  42. }, searchList: {'1':'支持多图文','0':'不支持多图文'}},
  43. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  44. ]
  45. ]
  46. });
  47. // 为表格绑定事件
  48. Table.api.bindevent(table);
  49. },
  50. select: function () {
  51. //console.log(window.location.search);
  52. // 初始化表格参数配置
  53. Table.api.init({
  54. extend: {
  55. index_url: 'wechat/response/index',
  56. }
  57. });
  58. var table = $("#table");
  59. // 初始化表格
  60. table.bootstrapTable({
  61. url: $.fn.bootstrapTable.defaults.extend.index_url + window.location.search,
  62. sortName: 'id',
  63. columns: [
  64. [
  65. {field: 'state', checkbox: true, },
  66. //{field: 'id', title: 'ID'},
  67. {field: 'type', title: __('Type'),formatter:function(value){
  68. var ob = {'text':'文本','image':'图片', 'news':'图文'};
  69. return ob[value];
  70. }, searchList:{'text':'文本', 'image':'图片', 'news':'图文'}},
  71. {field: 'title', title: __('Resource title'),operate:'LIKE'},
  72. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false},
  73. {field: 'is_subscribe', title: __('是否支持多图文'), formatter: function (value) {
  74. var o = {'1':'支持多图文','0':'不支持多图文'};
  75. return o[value];
  76. }, searchList: {'1':'支持多图文','0':'不支持多图文'}},
  77. {field: 'operate', title: __('Operate'), events: {
  78. 'click .btn-chooseone': function (e, value, row, index) {
  79. Fast.api.close(row);
  80. },
  81. }, formatter: function () {
  82. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  83. }}
  84. ]
  85. ]
  86. });
  87. // 为表格绑定事件
  88. Table.api.bindevent(table);
  89. },
  90. add: function () {
  91. Controller.api.bindevent();
  92. },
  93. edit: function () {
  94. Controller.api.bindevent();
  95. },
  96. api: {
  97. bindevent: function () {
  98. //资源管理
  99. var ore={
  100. arrma:[
  101. {
  102. 'title':'',
  103. 'image':'',
  104. 'url':'http://',
  105. 'description':'',
  106. }
  107. ],
  108. imglis:[],
  109. titlist:[],
  110. sli:'<div class="img_txt_li img_txt_main">\
  111. <div class="img_txt_one_tit">\
  112. <i class="fa fa-pencil img_txt_edit_tit"></i>\
  113. <div class="img_txt_one_h3"><input type="text" value="sli_value" /></div>\
  114. <i class="fa fa-chain img_txt_edit_url"></i>\
  115. <i class="fa fa-book img_txt_edit_book"></i>\
  116. <div class="img_txt_li_url"><input type="text" value="click_url" /></div>\
  117. </div>\
  118. <div class="img_txt_one_img img_txt_edit_img"><img src="sli_img" /></div>\
  119. <div class="form-group group-type-news group-type">\
  120. <label for="description" class="control-label col-xs-12 col-sm-2">简介:</label>\
  121. <div class="col-xs-12">\
  122. <textarea type="text" class="form-control description" rows="4" maxlength="120">sli_description</textarea>\
  123. </div>\
  124. </div>\
  125. <div class="fa img_txt_remvoe"></div>\
  126. </div>'
  127. };
  128. /**
  129. * form表单验证
  130. */
  131. $("form[role=form]").data("validator-options", {
  132. ignore: ':hidden',
  133. beforeSubmit: function (form) {
  134. if($('input[name="row[type]"]:checked').val() == 'news'){
  135. var $imgTxtMain = $('.img_txt_main');
  136. var reg = /^(http[s]?|ftp):\/\/[^\/\.]+?\..+\w/;
  137. var sList = '';
  138. ore.arrma = [];
  139. for(var i=0; i<$imgTxtMain.length; i++){
  140. ore.arrma.push({});
  141. //标题
  142. if($('.img_txt_main:eq('+ i +') input:eq(0)').val().length > 0){
  143. ore.arrma[i].title = $('.img_txt_main:eq('+ i +') input:eq(0)').val();
  144. }else{
  145. Toastr.error("图文标题不能为空");
  146. return false;
  147. }
  148. //链接
  149. if(reg.test($('.img_txt_main:eq('+ i +') input:eq(1)').val())){
  150. ore.arrma[i].url = $('.img_txt_main:eq('+ i +') input:eq(1)').val();
  151. }else{
  152. Toastr.error("请填写图文正确的跳转链接");
  153. return false;
  154. }
  155. //图片
  156. if($('.img_txt_main:eq('+ i +') img').attr('src').length > 0){
  157. ore.arrma[i].image = $('.img_txt_main:eq('+ i +') img').attr('src');
  158. }else{
  159. Toastr.error("请填写图文正确的跳转链接");
  160. return false;
  161. }
  162. ore.arrma[i].description = $('.img_txt_main:eq('+ i +') textarea').val();
  163. }
  164. sList = JSON.stringify(ore.arrma);
  165. $('.img_txt_hidde_input').val(sList);
  166. // console.log('验证');
  167. // return false;
  168. }
  169. }
  170. });
  171. Form.api.bindevent($("form[role=form]"),
  172. function (data) {
  173. Fast.api.close(data);
  174. },
  175. function (data) {
  176. console.log('error', data);
  177. });
  178. /**
  179. * 资源类型管理
  180. */
  181. $(document).on('click', "input[name='row[type]']", function () {
  182. var type = $(this).val();
  183. $('.group-type').addClass('hide');
  184. $('.group-type input').attr('disabled');
  185. $('.group-type-' + type).removeClass('hide');
  186. $('.group-type-' + type+' input').removeAttr('disabled');
  187. });
  188. //是否多图文
  189. $(document).on('click',"input[name='row[is_subscribe]']",function(){
  190. var subscribe = $(this).val();
  191. $('.group-type-subscribe').addClass('hide');
  192. if(subscribe==1){
  193. $('.group-type-subscribe').removeClass('hide');
  194. $('.img_txt_li').removeClass('hide');
  195. }else{
  196. $('.img_txt_li').addClass('hide');
  197. }
  198. });
  199. /**
  200. * 插入关注自动回复模板
  201. */
  202. $(document).on('click', ".btn_automatic_reply", function () {
  203. var textarea = $("textarea[name='row[content]']");
  204. var cursorPos = textarea.prop('selectionStart');
  205. var v = textarea.val();
  206. var textBefore = v.substring(0, cursorPos);
  207. var textAfter = v.substring(cursorPos, v.length);
  208. var txttar = '欢迎关注,您上次阅读的小说《{$book_name}》\n\n<a href="{$book_url}">【点此继续阅读】</a>\n\n为方便下次阅读,请置顶公众号';
  209. textarea.val(textBefore + txttar + textAfter);
  210. });
  211. /**
  212. * 插入链接
  213. */
  214. $(document).on('click', ".btn-insertlink", function () {
  215. var textarea = $("textarea[name='row[content]']");
  216. var cursorPos = textarea.prop('selectionStart');
  217. var v = textarea.val();
  218. var textBefore = v.substring(0, cursorPos);
  219. var textAfter = v.substring(cursorPos, v.length);
  220. Layer.prompt({title: '请输入显示的文字', formType: 3}, function (text, index) {
  221. text = $.trim(text);
  222. Layer.close(index);
  223. Layer.prompt({title: '请输入跳转的链接URL(包含http/https)', formType: 3}, function (link, index) {
  224. link = link.replace(/\s+/g, "");
  225. link = link.replace(/[\r\n]/g, "");
  226. text = text == '' ? link : text;
  227. textarea.val(textBefore + '<a href="' + link + '">' + text + '</a>' + textAfter);
  228. Layer.close(index);
  229. });
  230. });
  231. });
  232. $.ajax({
  233. type:'get',
  234. url:'/admin/manage/cover/ajax?sort=id&order=desc&offset=0&limit=10&filter={"status":"normal"}',
  235. cache:false,
  236. async:false,
  237. success:function(data){
  238. // console.log(data)
  239. ore.imglis = data.rows;
  240. },
  241. error:function(err){
  242. }
  243. })
  244. $.ajax({
  245. type:'get',
  246. url:'/admin/manage/title/ajax?sort=id&order=desc&offset=0&limit=10&filter={"status":"normal"}',
  247. cache:false,
  248. async:false,
  249. success:function(data){
  250. // console.log(data)
  251. ore.titlist = data.rows;
  252. ore.arrma[0]['image'] = ore.imglis[parseInt(Math.random()*ore.imglis.length)]['image'];
  253. ore.arrma[0]['title'] = ore.titlist[parseInt(Math.random()*ore.titlist.length)]['title'];
  254. if($('.img_txt_hidde_input').val().length > 0){
  255. ore.arrma = JSON.parse($('.img_txt_hidde_input').val());
  256. console.log(ore.arrma);
  257. for(var i=0; i<$('.img_txt_hidde_input').val().length; i++){
  258. if(i==0){
  259. $('.img_txt_list').html('<div class="img_txt_one img_txt_main">\
  260. <div class="img_txt_one_tit">\
  261. <i class="fa fa-pencil img_txt_edit_tit"></i>\
  262. <div class="img_txt_one_h3"><input type="text" value="' + ore.arrma[0]['title'] + '" /></div>\
  263. <i class="fa fa-chain img_txt_edit_url"></i>\
  264. <i class="fa fa-book img_txt_edit_book"></i>\
  265. <div class="img_txt_one_url"><input type="text" value="' + ore.arrma[0]['url'] + '" /></div>\
  266. </div>\
  267. <div class="img_txt_one_img img_txt_edit_img"><img src="' + ore.arrma[0]['image'] + '" /></div>\
  268. <div class="form-group group-type-news group-type">\
  269. <label for="description" class="control-label col-xs-12 col-sm-2">简介:</label>\
  270. <div class="col-xs-12">\
  271. <textarea type="text" class="form-control description" rows="4" maxlength="120">' + ore.arrma[0]['description'] + '</textarea>\
  272. </div>\
  273. </div>\
  274. </div>');
  275. }else{
  276. var sLiImg=ore.sli.replace(/sli_img/g,ore.arrma[i]['image']);
  277. sLiImg=sLiImg.replace(/sli_value/g,ore.arrma[i]['title']);
  278. sLiImg=sLiImg.replace(/click_url/g,ore.arrma[i]['url']);
  279. sLiImg=sLiImg.replace(/sli_description/g,ore.arrma[i]['description']);
  280. $('.img_txt_list').append(sLiImg);
  281. }
  282. }
  283. }else{
  284. $('.img_txt_list').html('<div class="img_txt_one img_txt_main">\
  285. <div class="img_txt_one_tit">\
  286. <i class="fa fa-pencil img_txt_edit_tit"></i>\
  287. <div class="img_txt_one_h3"><input type="text" value="' + ore.arrma[0]['title'] + '" /></div>\
  288. <i class="fa fa-chain img_txt_edit_url"></i>\
  289. <i class="fa fa-book img_txt_edit_book"></i>\
  290. <div class="img_txt_one_url"><input type="text" value="' + ore.arrma[0]['url'] + '" /></div>\
  291. </div>\
  292. <div class="img_txt_one_img img_txt_edit_img"><img src="' + ore.arrma[0]['image'] + '" /></div>\
  293. <div class="form-group group-type-news group-type">\
  294. <label for="description" class="control-label col-xs-12 col-sm-2">简介:</label>\
  295. <div class="col-xs-12">\
  296. <textarea type="text" class="form-control description" rows="4" maxlength="120">' + ore.arrma[0]['description'] + '</textarea>\
  297. </div>\
  298. </div>\
  299. </div>');
  300. }
  301. },
  302. error:function(err){
  303. }
  304. })
  305. $(document).on('click','.img_txt_add_but',function(){
  306. var $List=$('.img_txt_main');
  307. var oNowData = {
  308. image:ore.imglis[parseInt(Math.random()*ore.imglis.length)]['image'],
  309. title:ore.titlist[parseInt(Math.random()*ore.titlist.length)]['title'],
  310. url:'http://',
  311. description:''
  312. }
  313. var sLiImg=ore.sli.replace(/sli_img/g,oNowData['image']);
  314. sLiImg=sLiImg.replace(/sli_value/g,oNowData['title']);
  315. sLiImg=sLiImg.replace(/click_url/g,'http://');
  316. sLiImg=sLiImg.replace(/sli_description/g,oNowData['description']);
  317. if($List.length<5){
  318. $('.img_txt_list').append(sLiImg);
  319. ore.arrma.push(oNowData);
  320. }else{
  321. Toastr.error("不能超过5篇");
  322. }
  323. });
  324. $(document).on('click','.img_txt_remvoe',function(){
  325. var $this=$(this);
  326. $this.parent().remove();
  327. });
  328. //修改title
  329. $(document).on('click','.img_txt_edit_tit',function(){
  330. var $this=$(this);
  331. Fast.api.open('manage/title/select', '选择', {
  332. callback: function (data) {
  333. console.log('展示title回调', data);
  334. $this.parent().find('input').eq(0).val(data.title);
  335. }
  336. });
  337. });
  338. //修改url
  339. $(document).on('click','.img_txt_edit_url',function(){
  340. var $this=$(this);
  341. Fast.api.open('referral/referral/select?one=1', '选择', {
  342. callback: function (data) {
  343. var data = data[0];
  344. // console.log('展示title回调', data);
  345. $this.parent().find('textarea').val(data['book']['description']);
  346. $this.parent().find('input').eq(1).val(data.source_url);
  347. }
  348. });
  349. });
  350. //修改book
  351. $(document).on('click','.img_txt_edit_book',function(){
  352. var $this=$(this);
  353. Fast.api.open('book.book/select?one=1', '选择', {
  354. callback: function (data) {
  355. var data = data[0];
  356. $this.parent().find('input').eq(1).val(data.current_book_url);
  357. $this.parent().parent().find('textarea').eq(0).val(data.description);
  358. }
  359. });
  360. });
  361. //修改图片
  362. $(document).on('click','.img_txt_edit_img',function(){
  363. var $this=$(this);
  364. Fast.api.open('manage/cover/select', '选择', {
  365. callback: function (data) {
  366. // console.log('展示image回调', data);
  367. $this.find('img').attr('src',data.image);
  368. }
  369. });
  370. });
  371. //修改跳转链接
  372. $(document).on('click','.img_txt_main a',function(){
  373. var $this=$(this);
  374. var nIndex = $this.parents('.img_txt_main').index();
  375. layer.prompt({
  376. formType: 2,
  377. value: ore.arrma[nIndex].url,
  378. title: '请输入跳转链接地址(http://)',
  379. area: ['300px', '100px'] //自定义文本域宽高
  380. }, function(value, index, elem){
  381. //var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
  382. var reg=/^(http[s]?|ftp):\/\/[^\/\.]+?\..+\w/;
  383. if(reg.test(value)){
  384. layer.close(index);
  385. ore.arrma[nIndex].url = value;
  386. $this.html(value);
  387. }else{
  388. Toastr.error("请输入正确的跳转地址。");
  389. }
  390. });
  391. });
  392. }
  393. }
  394. };
  395. return Controller;
  396. });