define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte'], function ($, undefined, Backend, Table, Form, Adminlte) { //去掉特殊字符和转义字符 function excludeSpecial(s) { s = s.replace(/[\\\/\b\f\n\r\t\'\[\]\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/g,''); return s; }; $(document).on('keyup','.description',function () { var descrp = $(this).val(); $(this).val(excludeSpecial(descrp)); }); var Controller = { index: function () { // 初始化表格参数配置 Table.api.init({ extend: { index_url: 'wechat/response/index', add_url: 'wechat/response/add', edit_url: 'wechat/response/edit', del_url: 'wechat/response/del', multi_url: 'wechat/response/multi', } }); var table = $("#table"); // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, sortName: 'id', columns: [ [ {field: 'state', checkbox: true, }, // {field: 'id', title: 'ID'}, {field: 'type', title: __('Type'),formatter:function(value){ var ob = {'text':'文本','image':'图片', 'news':'图文'}; return ob[value]; }, searchList:{'text':'文本', 'image':'图片', 'news':'图文'}}, {field: 'title', title: __('Resource title'),operate:'LIKE'}, // {field: 'eventkey', title: __('Event key')}, {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false}, {field: 'is_subscribe', title: __('是否支持多图文'), formatter: function (value) { var o = {'1':'支持多图文','0':'不支持多图文'}; return o[value]; }, searchList: {'1':'支持多图文','0':'不支持多图文'}}, {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} ] ] }); // 为表格绑定事件 Table.api.bindevent(table); }, select: function () { //console.log(window.location.search); // 初始化表格参数配置 Table.api.init({ extend: { index_url: 'wechat/response/index', } }); var table = $("#table"); // 初始化表格 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url + window.location.search, sortName: 'id', columns: [ [ {field: 'state', checkbox: true, }, //{field: 'id', title: 'ID'}, {field: 'type', title: __('Type'),formatter:function(value){ var ob = {'text':'文本','image':'图片', 'news':'图文'}; return ob[value]; }, searchList:{'text':'文本', 'image':'图片', 'news':'图文'}}, {field: 'title', title: __('Resource title'),operate:'LIKE'}, {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, operate:false}, {field: 'is_subscribe', title: __('是否支持多图文'), formatter: function (value) { var o = {'1':'支持多图文','0':'不支持多图文'}; return o[value]; }, searchList: {'1':'支持多图文','0':'不支持多图文'}}, {field: 'operate', title: __('Operate'), events: { 'click .btn-chooseone': function (e, value, row, index) { Fast.api.close(row); }, }, formatter: function () { return ' ' + __('Choose') + ''; }} ] ] }); // 为表格绑定事件 Table.api.bindevent(table); }, add: function () { Controller.api.bindevent(); }, edit: function () { Controller.api.bindevent(); }, api: { bindevent: function () { //资源管理 var ore={ arrma:[ { 'title':'', 'image':'', 'url':'http://', 'description':'', } ], imglis:[], titlist:[], sli:'
\
\ \
\ \ \
\
\
\
\ \
\ \
\
\
\
' }; /** * form表单验证 */ $("form[role=form]").data("validator-options", { ignore: ':hidden', beforeSubmit: function (form) { if($('input[name="row[type]"]:checked').val() == 'news'){ var $imgTxtMain = $('.img_txt_main'); var reg = /^(http[s]?|ftp):\/\/[^\/\.]+?\..+\w/; var sList = ''; ore.arrma = []; for(var i=0; i<$imgTxtMain.length; i++){ ore.arrma.push({}); //标题 if($('.img_txt_main:eq('+ i +') input:eq(0)').val().length > 0){ ore.arrma[i].title = $('.img_txt_main:eq('+ i +') input:eq(0)').val(); }else{ Toastr.error("图文标题不能为空"); return false; } //链接 if(reg.test($('.img_txt_main:eq('+ i +') input:eq(1)').val())){ ore.arrma[i].url = $('.img_txt_main:eq('+ i +') input:eq(1)').val(); }else{ Toastr.error("请填写图文正确的跳转链接"); return false; } //图片 if($('.img_txt_main:eq('+ i +') img').attr('src').length > 0){ ore.arrma[i].image = $('.img_txt_main:eq('+ i +') img').attr('src'); }else{ Toastr.error("请填写图文正确的跳转链接"); return false; } ore.arrma[i].description = $('.img_txt_main:eq('+ i +') textarea').val(); } sList = JSON.stringify(ore.arrma); $('.img_txt_hidde_input').val(sList); // console.log('验证'); // return false; } } }); Form.api.bindevent($("form[role=form]"), function (data) { Fast.api.close(data); }, function (data) { console.log('error', data); }); /** * 资源类型管理 */ $(document).on('click', "input[name='row[type]']", function () { var type = $(this).val(); $('.group-type').addClass('hide'); $('.group-type input').attr('disabled'); $('.group-type-' + type).removeClass('hide'); $('.group-type-' + type+' input').removeAttr('disabled'); }); //是否多图文 $(document).on('click',"input[name='row[is_subscribe]']",function(){ var subscribe = $(this).val(); $('.group-type-subscribe').addClass('hide'); if(subscribe==1){ $('.group-type-subscribe').removeClass('hide'); $('.img_txt_li').removeClass('hide'); }else{ $('.img_txt_li').addClass('hide'); } }); /** * 插入关注自动回复模板 */ $(document).on('click', ".btn_automatic_reply", function () { var textarea = $("textarea[name='row[content]']"); var cursorPos = textarea.prop('selectionStart'); var v = textarea.val(); var textBefore = v.substring(0, cursorPos); var textAfter = v.substring(cursorPos, v.length); var txttar = '欢迎关注,您上次阅读的小说《{$book_name}》\n\n【点此继续阅读】\n\n为方便下次阅读,请置顶公众号'; textarea.val(textBefore + txttar + textAfter); }); /** * 插入链接 */ $(document).on('click', ".btn-insertlink", function () { var textarea = $("textarea[name='row[content]']"); var cursorPos = textarea.prop('selectionStart'); var v = textarea.val(); var textBefore = v.substring(0, cursorPos); var textAfter = v.substring(cursorPos, v.length); Layer.prompt({title: '请输入显示的文字', formType: 3}, function (text, index) { text = $.trim(text); Layer.close(index); Layer.prompt({title: '请输入跳转的链接URL(包含http/https)', formType: 3}, function (link, index) { link = link.replace(/\s+/g, ""); link = link.replace(/[\r\n]/g, ""); text = text == '' ? link : text; textarea.val(textBefore + '' + text + '' + textAfter); Layer.close(index); }); }); }); $.ajax({ type:'get', url:'/admin/manage/cover/ajax?sort=id&order=desc&offset=0&limit=10&filter={"status":"normal"}', cache:false, async:false, success:function(data){ // console.log(data) ore.imglis = data.rows; }, error:function(err){ } }) $.ajax({ type:'get', url:'/admin/manage/title/ajax?sort=id&order=desc&offset=0&limit=10&filter={"status":"normal"}', cache:false, async:false, success:function(data){ // console.log(data) ore.titlist = data.rows; ore.arrma[0]['image'] = ore.imglis[parseInt(Math.random()*ore.imglis.length)]['image']; ore.arrma[0]['title'] = ore.titlist[parseInt(Math.random()*ore.titlist.length)]['title']; if($('.img_txt_hidde_input').val().length > 0){ ore.arrma = JSON.parse($('.img_txt_hidde_input').val()); console.log(ore.arrma); for(var i=0; i<$('.img_txt_hidde_input').val().length; i++){ if(i==0){ $('.img_txt_list').html('
\
\ \
\ \ \
\
\
\
\ \
\ \
\
\
'); }else{ var sLiImg=ore.sli.replace(/sli_img/g,ore.arrma[i]['image']); sLiImg=sLiImg.replace(/sli_value/g,ore.arrma[i]['title']); sLiImg=sLiImg.replace(/click_url/g,ore.arrma[i]['url']); sLiImg=sLiImg.replace(/sli_description/g,ore.arrma[i]['description']); $('.img_txt_list').append(sLiImg); } } }else{ $('.img_txt_list').html('
\
\ \
\ \ \
\
\
\
\ \
\ \
\
\
'); } }, error:function(err){ } }) $(document).on('click','.img_txt_add_but',function(){ var $List=$('.img_txt_main'); var oNowData = { image:ore.imglis[parseInt(Math.random()*ore.imglis.length)]['image'], title:ore.titlist[parseInt(Math.random()*ore.titlist.length)]['title'], url:'http://', description:'' } var sLiImg=ore.sli.replace(/sli_img/g,oNowData['image']); sLiImg=sLiImg.replace(/sli_value/g,oNowData['title']); sLiImg=sLiImg.replace(/click_url/g,'http://'); sLiImg=sLiImg.replace(/sli_description/g,oNowData['description']); if($List.length<5){ $('.img_txt_list').append(sLiImg); ore.arrma.push(oNowData); }else{ Toastr.error("不能超过5篇"); } }); $(document).on('click','.img_txt_remvoe',function(){ var $this=$(this); $this.parent().remove(); }); //修改title $(document).on('click','.img_txt_edit_tit',function(){ var $this=$(this); Fast.api.open('manage/title/select', '选择', { callback: function (data) { console.log('展示title回调', data); $this.parent().find('input').eq(0).val(data.title); } }); }); //修改url $(document).on('click','.img_txt_edit_url',function(){ var $this=$(this); Fast.api.open('referral/referral/select?one=1', '选择', { callback: function (data) { var data = data[0]; // console.log('展示title回调', data); $this.parent().find('textarea').val(data['book']['description']); $this.parent().find('input').eq(1).val(data.source_url); } }); }); //修改book $(document).on('click','.img_txt_edit_book',function(){ var $this=$(this); Fast.api.open('book.book/select?one=1', '选择', { callback: function (data) { var data = data[0]; $this.parent().find('input').eq(1).val(data.current_book_url); $this.parent().parent().find('textarea').eq(0).val(data.description); } }); }); //修改图片 $(document).on('click','.img_txt_edit_img',function(){ var $this=$(this); Fast.api.open('manage/cover/select', '选择', { callback: function (data) { // console.log('展示image回调', data); $this.find('img').attr('src',data.image); } }); }); //修改跳转链接 $(document).on('click','.img_txt_main a',function(){ var $this=$(this); var nIndex = $this.parents('.img_txt_main').index(); layer.prompt({ formType: 2, value: ore.arrma[nIndex].url, title: '请输入跳转链接地址(http://)', area: ['300px', '100px'] //自定义文本域宽高 }, function(value, index, elem){ //var reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/; var reg=/^(http[s]?|ftp):\/\/[^\/\.]+?\..+\w/; if(reg.test(value)){ layer.close(index); ore.arrma[nIndex].url = value; $this.html(value); }else{ Toastr.error("请输入正确的跳转地址。"); } }); }); } } }; return Controller; });