response.js 14 KB

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