daytoutiao.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. layui.use(['laypage', 'layer', 'table', 'form'], function () {
  2. var laypage = layui.laypage //分页
  3. , $ = layui.jquery
  4. , layer = layui.layer //弹层
  5. , table = layui.table //表格
  6. , form = layui.form;
  7. var tableIds = 'toutiaolist'; //表格ID
  8. //第一个实例
  9. table.render({
  10. elem: '#' + tableIds
  11. , url: '/daytoutiao/daytoutiaoform' //数据接口
  12. , where: {token: csrfToken}
  13. , method: 'post'
  14. , page: true //开启分页
  15. , text: '数据加载中'
  16. , id: 'tablerReload'
  17. , cols: [[ //表头
  18. {type: "checkbox", width:50},
  19. {type: 'numbers', title: '序号', width: 50},
  20. {field: 'name', title: '用户名称', align: 'center'},
  21. {field: 'phone', width: 200, title: '用户电话', align: 'center'},
  22. {field: 'day_title', width: 200, title: '栏目', align: 'center'},
  23. {field: 'send_email', width: 200, title: '邮箱', align: 'center'},
  24. {field: 'efficacious', width: 150, title: '是否有效', align: 'center', templet: function (d) {
  25. switch (d.efficacious) {
  26. case '1':
  27. return '------'
  28. break;
  29. case '2':
  30. return '<span style="color: #5FB878">有效</span>'
  31. break;
  32. }
  33. }},
  34. {
  35. field: 'send_state', title: '发送状态', width: 150, align: 'center', templet: function (d) {
  36. switch (d.send_state) {
  37. case '1':
  38. return '<input type="checkbox" value="' + d.id + '" lay-skin="switch" lay-filter="switchTest" lay-text="|重新发送">'
  39. break;
  40. case '2':
  41. return '已发送'
  42. break;
  43. }
  44. }
  45. },
  46. {field: 'create_time', title: '添加时间', align: 'center'},
  47. {fixed: 'right', title: '操作', width: 180, align: 'center', toolbar: '#toutiaofunc'}
  48. ]]
  49. });
  50. table.reload('tablerReload'); //重新加载
  51. var testData = [
  52. {id:1,username:'',phone:''},
  53. {id:2,username:'',phone:''},
  54. {id:3,username:'',phone:''},
  55. {id:4,username:'',phone:''},
  56. {id:5,username:'',phone:''},
  57. {id:6,username:'',phone:''},
  58. {id:7,username:'',phone:''},
  59. {id:8,username:'',phone:''},
  60. {id:9,username:'',phone:''},
  61. {id:10,username:'',phone:''}
  62. ]
  63. table.render({
  64. elem: '#test3'
  65. , data: testData//数据接口
  66. , id: 'tablerReload3'
  67. , cols: [[ //表头
  68. {field:'id',title: '序号', width:70,align: 'center'},
  69. {field:'username',title: '姓名', width:250, sort: true, edit: 'text'},
  70. {field:'phone',title: '电话', edit: 'text', minWidth: 350}
  71. ]]
  72. });
  73. //列表操作
  74. table.on('tool(' + tableIds + ')', function (obj) {
  75. var layEvent = obj.event,
  76. data = obj.data;
  77. if (layEvent === 'del') {
  78. layer.confirm('您确定要删除吗?', {btn: ['确定', '取消']}, function () {
  79. $.post('/daytoutiao/daytoutiaodel', {id: data.id}, function (data) {
  80. layer.msg(data.msg);
  81. if(data.code == 200){
  82. table.reload('tablerReload');
  83. }
  84. })
  85. })
  86. }else if(layEvent === 'edit'){
  87. var index = layui.layer.open({
  88. type: 2,
  89. title: '添加栏目信息',
  90. closeBtn: 1, //不显示关闭按钮
  91. area: ['60%', '80%'],
  92. content: '/daytoutiao/daytoutiaoedit?id='+data.id //iframe的url,no代表不显示滚动条
  93. , success: function (layero, index) {
  94. },
  95. end: function () {
  96. table.reload('tablerReload');
  97. }
  98. });
  99. }
  100. });
  101. //监听指定开关
  102. form.on('switch(switchTest)', function (data) {
  103. let id = $(data.elem).val();
  104. layer.confirm('您确定要修改当前状态吗?', {btn: ['确认', '取消']}, function (index) {
  105. $.post('/daytoutiao/daytoutiaoanewsend',{id:id},function(res){
  106. layer.msg(res.msg)
  107. table.reload('tablerReload');
  108. },'json')
  109. }, function (index) {
  110. table.reload('tablerReload');
  111. layer.close(index)
  112. })
  113. return false;
  114. });
  115. // 新增
  116. var $ = layui.$, active = {
  117. addData: function () { //获取选中数据
  118. var index = layui.layer.open({
  119. type: 2,
  120. title: '添加栏目信息',
  121. closeBtn: 1, //不显示关闭按钮
  122. area: ['60%', '95%'],
  123. content: '/daytoutiao/daytoutiaoadd' //iframe的url,no代表不显示滚动条
  124. , success: function (layero, index) {
  125. },
  126. end: function () {
  127. table.reload('tablerReload');
  128. }
  129. });
  130. },
  131. setWx:function(){
  132. var checkStatus = table.checkStatus('tablerReload');//获取列表选中项
  133. var formData = new FormData();
  134. $.each(checkStatus.data,function (key,val) {
  135. formData.append('did[]',val.id);
  136. })
  137. $.ajax({
  138. type: "post",
  139. url: "/daytoutiao/daytoutiaosetwx",
  140. data: formData,
  141. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  142. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  143. error: function (request) {
  144. layer.msg("请联系管理员");
  145. },
  146. success: function (data) {
  147. layer.msg(data.msg);
  148. if(data.code == 200){
  149. table.reload('tablerReload');
  150. }
  151. }
  152. });
  153. return false;
  154. },
  155. reload: function () {//搜索
  156. var form = pfgs_serialize($('form'));
  157. table.reload('tablerReload', {
  158. where: {
  159. name: form.input_name,
  160. list_id: form.input_list_id,
  161. efficacious:form.input_efficacious,
  162. send_state:form.input_send_state,
  163. },
  164. page: {
  165. curr: 1
  166. }
  167. })
  168. },
  169. };
  170. $('.demoTable .layui-btn').on('click', function () {
  171. var type = $(this).data('type');
  172. active[type] ? active[type].call(this) : '';
  173. });
  174. $('#addrow').on('click',function(){
  175. let Info = table.cache.tablerReload3;
  176. let sun = (Info.length+1);
  177. Info.push({id: sun , username: '' ,phone: ''});
  178. table.reload('tablerReload3', {
  179. data:Info,
  180. limit:sun
  181. })
  182. })
  183. //添加页面提交数据
  184. form.on('submit(adds)', function (data) {
  185. let Info = table.cache.tablerReload3;
  186. let allPhone = true;
  187. let datalist = [];
  188. for(let k in Info){
  189. if(Info[k].phone.length > 0){
  190. if(!isPoneAvailable(Info[k].phone)){
  191. allPhone = false;
  192. Info[k].phone = '<span style="color: red">'+Info[k].phone+'</span>';
  193. }
  194. let d = {'name':Info[k].username,'phone':Info[k].phone}
  195. datalist.push(d);
  196. }
  197. }
  198. if(!allPhone){
  199. table.reload('tablerReload3', {
  200. data:Info,
  201. limit:Info.length
  202. })
  203. layer.msg('请填写正确的电话号码!')
  204. return false;
  205. }
  206. if(datalist.length > 0){
  207. let dataInfo = JSON.stringify(datalist);
  208. let list_id = $('select[name=list_id]').val();
  209. if(!list_id){
  210. layer.msg('请选择栏目')
  211. return false;
  212. }
  213. var index = layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8}); //数据提交提示
  214. $.post('/daytoutiao/daytoutiaoaddform',{data:dataInfo,list_id:list_id},function(res){
  215. layer.msg(res.msg)
  216. layer.close(index);
  217. },'json')
  218. }else{
  219. layer.msg('没有数据')
  220. }
  221. return false;
  222. });
  223. $('input[name=phone]').on('input',function(){
  224. if(!isPoneAvailable($(this).val())){
  225. $(this).parent().css({'border':'1px solid #ff0000'})
  226. }else{
  227. $(this).parent().css({'border':'none'})
  228. }
  229. })
  230. form.on('submit(edits)', function (data) {
  231. // var index = layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8}); //数据提交提示
  232. var formData = new FormData($('form')[0]);
  233. $.ajax({
  234. type: "post",
  235. url: "/daytoutiao/daytoutiaoeditform",
  236. data: formData,
  237. contentType: false, //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作,从而失去分界符,而使服务器不能正常解析文件
  238. processData: false, //当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
  239. error: function (request) {
  240. layer.close(index);
  241. layer.msg("请联系管理员");
  242. },
  243. success: function (data) {
  244. layer.msg(data.msg);
  245. if(data.code == 200){
  246. parent.layer.close(index); //再执行关闭
  247. }
  248. }
  249. });
  250. return false;
  251. });
  252. function isPoneAvailable(phone) {
  253. var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
  254. if (!myreg.test(phone)) {
  255. return false;
  256. } else {
  257. return true;
  258. }
  259. }
  260. })