user.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. layui.use([ 'laypage', 'layer', 'table', 'element','form','transfer','util'], function(){
  2. var laypage = layui.laypage //分页
  3. ,layer = layui.layer //弹层
  4. ,table = layui.table //表格
  5. ,transfer = layui.transfer
  6. ,util = layui.util
  7. ,element = layui.element //元素操作
  8. ,form = layui.form;
  9. //第一个实例
  10. table.render({
  11. elem: '#userlist'
  12. ,url: '/user/homefrom' //数据接口
  13. ,where:{token:csrfToken}
  14. ,method:'post'
  15. ,id:'tablerReload'
  16. ,page: true //开启分页
  17. ,text:'数据加载中'
  18. ,limit : 15
  19. ,cols: [[ //表头
  20. // {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left',align:'center'},
  21. {type:'numbers',fixed:"left",title:'序号'},
  22. {field: 'user', title: '账号', align:'center'}
  23. ,{field: 'name', title: '姓名', width:150,align:'center'}
  24. ,{field: 'status', title: '状态', width:80,align:'center', templet: function (d) {
  25. if (d.status == 2) {
  26. return '<input type="checkbox" value="' + d.uid + '" data-status="1" lay-skin="switch" lay-filter="switchTest3" lay-text="开启|关闭">'
  27. } else {
  28. return '<input type="checkbox" checked value="' + d.uid + '" data-status="2" lay-skin="switch" lay-filter="switchTest3" lay-text="开启|关闭">'
  29. }
  30. }}
  31. ,{field: 'create_at', title: '注册时间', width:200,align:'center'}
  32. ,{field: 'login_num', title: '登录次数', width:150,align:'center'}
  33. ,{field: 'login_time', title: '上次登录时间', width:200,align:'center'}
  34. ,{fixed: 'right',title:'操作', width: 250, align:'center', toolbar: '#operate'}
  35. ]]
  36. });
  37. table.reload('tablerReload'); //重新加载
  38. //列表操作
  39. table.on('tool(userlist)', function(obj){
  40. var layEvent = obj.event,
  41. data = obj.data;
  42. if(layEvent === 'edit'){
  43. layer.open({
  44. type: 2,
  45. title: '修改用户信息',
  46. area: ['600px', '500px'],
  47. anim: 2,
  48. content: '/user/edit?id='+data.uid //iframe的url,no代表不显示滚动条
  49. });
  50. } else if(layEvent === 'userrole') {
  51. layer.open({
  52. type: 2,
  53. title: '用户添加角色',
  54. closeBtn: 1, //不显示关闭按钮
  55. // shade: [0],
  56. area: ['700px', '550px'],
  57. anim: 2,
  58. content:'/user/adduserrole?uid='+data.uid,
  59. });
  60. }
  61. });
  62. //监听指定开关
  63. form.on('switch(switchTest3)', function (data) {
  64. var id = data.value
  65. ,status = $(this).data('status')
  66. layer.confirm('确定要修改此操作吗?', {btn: ['确定', '取消']}, function () {
  67. $.post('/user/usersetstatus', {id:id, status: status}, function (data) {
  68. layer.msg(data.msg);
  69. table.reload('tablerReload');
  70. })
  71. }, function (aa) {
  72. table.reload('tablerReload'); //重新加载
  73. })
  74. });
  75. var $ = layui.$, active = {
  76. addData: function(){ //获取选中数据
  77. layer.open({
  78. type: 2,
  79. title: '添加账号',
  80. closeBtn: 1, //不显示关闭按钮
  81. shade: [0],
  82. area: ['600px', '500px'],
  83. anim: 2,
  84. content: ['/user/adduser', 'no'], //iframe的url,no代表不显示滚动条
  85. });
  86. }
  87. };
  88. $('.demoTable .layui-btn').on('click', function(){
  89. var type = $(this).data('type');
  90. active[type] ? active[type].call(this) : '';
  91. });
  92. //添加
  93. form.on('submit(userSubmit)', function(data){
  94. var index = top.layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  95. $.post('/user/adduserfrom',{'data':data.field},function (data) {
  96. if(data.code == 200)
  97. {
  98. top.layer.close(index); //关闭数据提交信息
  99. layer.msg(data.msg);
  100. parent.layui.table.reload('userlist');
  101. TableClose();
  102. } else if(data.code == 300 ) {
  103. top.layer.close(index); //关闭数据提交信息
  104. layer.msg(data.msg);
  105. }
  106. })
  107. return false;
  108. });
  109. /**
  110. * 2020.6.24 lyy 用户管理优化
  111. * 用户状态修改
  112. * 用户添加角色
  113. *begin
  114. * ****/
  115. var data1 = [];
  116. var data2 = [];
  117. $('#userrolelist .y_role').each(function () {
  118. let t = {"value": $(this).data('id'), "title": $(this).data('name')};
  119. data1.push(t);
  120. })
  121. $('#userrolelist .c_role').each(function () {
  122. data2.push($(this).data('id'))
  123. })
  124. //显示搜索框
  125. transfer.render({
  126. elem: '#test4'
  127. , data: data1
  128. , value: data2
  129. , title: ['未赋予', '已赋予']
  130. , showSearch: true
  131. , id: "keyuserrole"
  132. })
  133. util.event('lay-demoTransferActive', {
  134. getData: function (othis) {
  135. var getData = transfer.getData('keyuserrole'); //获取右侧数据
  136. var rid = [];
  137. for(var v of getData){
  138. rid.push(v.value);
  139. }
  140. var index = top.layer.msg('数据提交中,请稍候', {icon: 16, time: false, shade: 0.8}); //数据提交提示
  141. $.post('/user/adduserrolefrom', {'data': {rid:rid,uid:$(this).data('uid'),type:$(this).data('type')}}, function (data) {
  142. if (data.code == 200) {
  143. top.layer.close(index); //关闭数据提交信息
  144. layer.msg(data.msg);
  145. parent.layui.table.reload('userlist');
  146. TableClose();
  147. } else if (data.code == 300) {
  148. top.layer.close(index); //关闭数据提交信息
  149. layer.msg(data.msg);
  150. }
  151. })
  152. }
  153. , reload: function () {
  154. //实例重载
  155. transfer.reload('keyuserrole', {
  156. title: ['未赋予', '已赋予']
  157. , data: data1
  158. , value: data2
  159. , showSearch: true
  160. })
  161. }
  162. });
  163. //用户添加角色
  164. // form.on('submit(add_user_role)', function(data){
  165. // var index = top.layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  166. // $.post('/user/adduserrolefrom',{'data':data.field},function (data) {
  167. // if(data.code == 200)
  168. // {
  169. // top.layer.close(index); //关闭数据提交信息
  170. // layer.msg(data.msg);
  171. // TableClose();
  172. // } else if(data.code == 300 ) {
  173. // top.layer.close(index); //关闭数据提交信息
  174. // layer.msg(data.msg);
  175. // }
  176. // })
  177. // return false;
  178. // });
  179. //给角色添加节点
  180. form.on('submit(userEditSubmit)', function(data){
  181. $.post('/user/editform',{'data':data.field},function (data) {
  182. if(data.code == 200)
  183. {
  184. layer.msg(data.msg);
  185. TableClose();
  186. } else if(data.code == 300 ) {
  187. layer.msg(data.msg);
  188. }
  189. });
  190. return false;
  191. });
  192. //个人修改密码
  193. form.on('submit(userPwdSubmit)', function(data){
  194. var index = top.layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8}); //数据提交提示
  195. $.post('/user/pwdsaveform',{'data':data.field},function (data) {
  196. top.layer.close(index); //关闭数据提交信息
  197. if(data.code == 200)
  198. {
  199. layer.msg(data.msg);
  200. window.parent.location.href = '/public/login';
  201. } else if(data.code == 300 ) {
  202. layer.msg(data.msg);
  203. }
  204. })
  205. top.layer.close(index); //关闭数据提交信息
  206. return false;
  207. });
  208. function TableClose() {
  209. setTimeout(function () {
  210. var index = parent.layer.getFrameIndex(window.name);
  211. parent.layer.close(index);
  212. },1000)
  213. }
  214. });