Accounts.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * User: nickbai
  4. * Date: 2017/10/23 13:33
  5. * Email: 1902822973@qq.com
  6. */
  7. namespace app\admin\controller;
  8. class Accounts extends Base
  9. {
  10. // 用户列表
  11. public function index()
  12. {
  13. if(request()->isAjax()){
  14. $param = input('param.');
  15. $limit = $param['pageSize'];
  16. $offset = (($param['pageNumber'] - 1) * $limit);
  17. $where = [];
  18. if (empty($param['searchText']) === false) {
  19. $where['account_name'] = ['like', '%' . $param['searchText'] . '%'];
  20. }
  21. if (empty($param['start']) === false && empty($param['end']) === false && $param['start'] <= $param['end']) {
  22. $start = strtotime($param['start']);
  23. $end = strtotime($param['end'] . ' 23:59:59');
  24. $return['total'] = db('accounts')->whereBetween('add_time', [$start, $end])->count(); //总数据
  25. }
  26. $result = db('accounts')->whereBetween('add_time', [$start, $end])->where($where)->limit($offset, $limit)->order('add_time', 'desc')->select();
  27. //所有客服
  28. $users = db('users')->select();
  29. foreach($result as $key=>$vo){
  30. $result[$key]['add_time'] = date('Y-m-d H:i:s',$result[$key]['add_time']);
  31. $result[$key]['last_login_time'] = date('Y-m-d H:i:s',$result[$key]['last_login_time']);
  32. for($i=0;$i<count($users);$i++){
  33. if($vo['user_id'] == $users[$i]['id']){
  34. $result[$key]['user_name'] = $users[$i]['user_name'];
  35. }
  36. }
  37. // 生成操作按钮
  38. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  39. }
  40. //$return['total'] = db('accounts')->count(); //总数据
  41. $return['rows'] = $result;
  42. return json($return);
  43. }
  44. return $this->fetch();
  45. }
  46. // 重置用户密码为 123456
  47. public function resetPwd()
  48. {
  49. if(request()->isAjax()){
  50. $id = input('param.id/d');
  51. $password = md5('123456' . config('salt'));
  52. try{
  53. db('accounts')->where('id', $id)->update(['password' => $password]);
  54. }catch(\Exception $e){
  55. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  56. }
  57. return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '重置密码成功']);
  58. }
  59. }
  60. // 编辑用户
  61. public function edit()
  62. {
  63. if(request()->isAjax()){
  64. $param = input('post.');
  65. $info = array();
  66. $rules = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
  67. if(!empty($param['account_email'])){
  68. if(!preg_match($rules,$param['account_email'])){
  69. return json(['code' => -9, 'data' => '', 'msg' => '请输入正确的邮箱']);
  70. }
  71. }
  72. if(!empty($param['account_phone'])){
  73. if(!preg_match("/^1[34578]\d{9}$/", $param['account_phone'])){
  74. return json(['code' => -10, 'data' => '', 'msg' => '请输入正确的手机号码']);
  75. }
  76. }
  77. $info['nick_name'] = $param['nick_name'];
  78. $info['account_email'] = $param['account_email'];
  79. $info['account_phone'] = $param['account_phone'];
  80. $info['address'] = $param['address'];
  81. $info['label'] = $param['label'];
  82. $info['remark'] = $param['remark'];
  83. if(!empty($param['user_id']) && $param['user_id'] != 0){
  84. $info['user_id'] = $param['user_id'];
  85. }
  86. try{
  87. db('accounts')->where('id', $param['id'])->update($info);
  88. }catch(\Exception $e){
  89. return json(['code' => -6, 'data' => '', 'msg' => $e->getMessage()]);
  90. }
  91. return json(['code' => 1, 'data' => '', 'msg' => '编辑用户成功']);
  92. }
  93. $id = input('param.id/d');
  94. $info = db('accounts')->where('id', $id)->find();
  95. //转为日期时间格式
  96. $info['add_time'] = date('Y-m-d H:i:s',$info['add_time']);
  97. //所有客服
  98. $users = db('users')->select();
  99. $this->assign([
  100. 'info' => $info,
  101. 'users' => $users,
  102. 'status' => config('kf_status')
  103. ]);
  104. return $this->fetch();
  105. }
  106. // 删除用户
  107. public function delAccount()
  108. {
  109. if(request()->isAjax()){
  110. $id = input('param.id/d');
  111. try{
  112. db('accounts')->where('id', $id)->delete();
  113. }catch(\Exception $e){
  114. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  115. }
  116. return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '删除客服成功']);
  117. }
  118. }
  119. // 生成按钮
  120. private function makeBtn($id)
  121. {
  122. //$operate = '<a href="javascript:resetPwd(' . $id . ')"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 重置密码</button></a>';
  123. $operate = '<a style="margin-left:5px;" href="' . url('Accounts/edit', ['id' => $id]) . '"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  124. $operate .= '<a href="javascript:accountDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  125. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  126. return $operate;
  127. }
  128. }