Accounts.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. //标签
  22. if ($param['label_id'] != '' && $param['label_id'] != 0) {
  23. $where['label_id'] = $param['label_id'];
  24. }
  25. if (empty($param['start']) === false && empty($param['end']) === false && $param['start'] <= $param['end']) {
  26. $start = strtotime($param['start']);
  27. $end = strtotime($param['end'] . ' 23:59:59');
  28. $return['total'] = db('accounts')->whereBetween('add_time', [$start, $end])->count(); //总数据
  29. }
  30. $result = db('accounts')->whereBetween('add_time', [$start, $end])->where($where)->limit($offset, $limit)->order('add_time', 'desc')->select();
  31. //所有客服
  32. $users = db('users')->select();
  33. //所有标签
  34. $label = db('accountslabel')->select();
  35. foreach($result as $key=>$vo){
  36. $result[$key]['add_time'] = date('Y-m-d H:i:s',$result[$key]['add_time']);
  37. $result[$key]['last_login_time'] = date('Y-m-d H:i:s',$result[$key]['last_login_time']);
  38. for($i=0;$i<count($users);$i++){
  39. if($vo['user_id'] == $users[$i]['id']){
  40. $result[$key]['user_name'] = $users[$i]['user_name'];
  41. }
  42. }
  43. for($j=0;$j<count($label);$j++){
  44. if($vo['label_id'] == $label[$j]['id']){
  45. $result[$key]['label'] = $label[$j]['name'];
  46. }
  47. }
  48. // 生成操作按钮
  49. $result[$key]['operate'] = $this->makeBtn($vo['id']);
  50. }
  51. //$return['total'] = db('accounts')->count(); //总数据
  52. $return['rows'] = $result;
  53. return json($return);
  54. }
  55. //所有标签
  56. $label = db('accountslabel')->select();
  57. if(!empty($label)){
  58. $option = '<option value="0">全部</option>';
  59. for($i=0;$i<count($label);$i++){
  60. $option = $option.'<option value="'.$label[$i]['id'].'">'.$label[$i]['name'].'</option>';
  61. }
  62. $labeloption = '<select lay-verify="required" lay-filter="label">'.$option.'</select>';
  63. }
  64. $this->assign([
  65. 'label' => $label,
  66. 'labeloption' => $labeloption,
  67. 'status' => config('kf_status')
  68. ]);
  69. return $this->fetch();
  70. }
  71. // 重置用户密码为 123456
  72. public function resetPwd()
  73. {
  74. if(request()->isAjax()){
  75. $id = input('param.id/d');
  76. $password = md5('123456' . config('salt'));
  77. try{
  78. db('accounts')->where('id', $id)->update(['password' => $password]);
  79. }catch(\Exception $e){
  80. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  81. }
  82. return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '重置密码成功']);
  83. }
  84. }
  85. // 编辑用户
  86. public function edit()
  87. {
  88. if(request()->isAjax()){
  89. $param = input('post.');
  90. $info = array();
  91. $rules = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
  92. if(!empty($param['account_email'])){
  93. if(!preg_match($rules,$param['account_email'])){
  94. return json(['code' => -9, 'data' => '', 'msg' => '请输入正确的邮箱']);
  95. }
  96. }
  97. if(!empty($param['account_phone'])){
  98. if(!preg_match("/^1[34578]\d{9}$/", $param['account_phone'])){
  99. return json(['code' => -10, 'data' => '', 'msg' => '请输入正确的手机号码']);
  100. }
  101. }
  102. $info['nick_name'] = $param['nick_name'];
  103. $info['account_email'] = $param['account_email'];
  104. $info['account_phone'] = $param['account_phone'];
  105. $info['address'] = $param['address'];
  106. $info['label_id'] = $param['label_id'];
  107. $info['remark'] = $param['remark'];
  108. if(!empty($param['user_id']) && $param['user_id'] != 0){
  109. $info['user_id'] = $param['user_id'];
  110. }
  111. try{
  112. db('accounts')->where('id', $param['id'])->update($info);
  113. }catch(\Exception $e){
  114. return json(['code' => -6, 'data' => '', 'msg' => $e->getMessage()]);
  115. }
  116. return json(['code' => 1, 'data' => url('accounts/index'), 'msg' => '编辑用户成功']);
  117. }
  118. $id = input('param.id/d');
  119. $info = db('accounts')->where('id', $id)->find();
  120. $label = db('accountslabel')->where('id', $info['label_id'])->find();
  121. $info['label'] = $label['name'];
  122. //所有标签
  123. $label = db('accountslabel')->select();
  124. //服务记录
  125. //前一个月的时间戳
  126. $start = time()-2592000;
  127. //当前时间戳
  128. $end = time();
  129. $service_log = db('service_log a')->join('users b','a.kf_id=b.id')->field('a.servicelog_id,a.start_time,b.user_name')->whereBetween('a.start_time', [$start, $end])->where('a.user_id', $id)->select();
  130. for($i=0;$i<count($service_log);$i++){
  131. $service_log[$i]['start_time'] = date("Y-m-d H:i:s",$service_log[$i]['start_time']);
  132. }
  133. //转为日期时间格式
  134. $info['add_time'] = date('Y-m-d H:i:s',$info['add_time']);
  135. //所有客服
  136. $users = db('users')->select();
  137. $this->assign([
  138. 'info' => $info,
  139. 'users' => $users,
  140. 'label' => $label,
  141. 'service_log' => $service_log,
  142. 'status' => config('kf_status')
  143. ]);
  144. return $this->fetch();
  145. }
  146. // 删除用户
  147. public function delAccount()
  148. {
  149. if(request()->isAjax()){
  150. $id = input('param.id/d');
  151. try{
  152. db('accounts')->where('id', $id)->delete();
  153. }catch(\Exception $e){
  154. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  155. }
  156. return json(['code' => 1, 'data' => 'accounts/index', 'msg' => '删除客服成功']);
  157. }
  158. }
  159. // 生成按钮
  160. private function makeBtn($id)
  161. {
  162. //$operate = '<a href="javascript:resetPwd(' . $id . ')"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 重置密码</button></a>';
  163. $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> ';
  164. $operate .= '<a href="javascript:accountDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  165. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  166. return $operate;
  167. }
  168. // 用户标签列表
  169. public function label()
  170. {
  171. if(request()->isAjax()){
  172. $param = input('param.');
  173. $limit = $param['pageSize'];
  174. $offset = (($param['pageNumber'] - 1) * $limit);
  175. $where = [];
  176. if (empty($param['searchText']) === false) {
  177. $where['name'] = ['like', '%' . $param['searchText'] . '%'];
  178. }
  179. $result = db('accountslabel')->where($where)->limit($offset, $limit)->order('add_time', 'desc')->select();
  180. foreach($result as $key=>$vo){
  181. $result[$key]['add_time'] = date('Y-m-d H:i:s',$result[$key]['add_time']);
  182. // 生成操作按钮
  183. $result[$key]['operate'] = $this->makeBtnLabel($vo['id']);
  184. }
  185. $return['total'] = db('accountslabel')->count(); //总数据
  186. $return['rows'] = $result;
  187. return json($return);
  188. }
  189. return $this->fetch();
  190. }
  191. // 添加用户标签
  192. public function addlabel()
  193. {
  194. if (request()->isAjax()) {
  195. $param = input('post.');
  196. $has = db('accountslabel')->where('name',$param['name'])->find();
  197. if(!empty($has)){
  198. return json(['code' => -1, 'data' => '', 'msg' => '此用户标签已存在']);
  199. }
  200. $info = array();
  201. $info['name'] = $param['name'];
  202. $info['add_time'] = time();
  203. try {
  204. db('accountslabel')->insert($info);
  205. } catch (\Exception $e) {
  206. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  207. }
  208. return json(['code' => 1, 'data' => '', 'msg' => '添加用户标签成功']);
  209. }
  210. $this->assign([
  211. 'status' => config('kf_status')
  212. ]);
  213. return $this->fetch();
  214. }
  215. // 编辑用户标签
  216. public function editlabel()
  217. {
  218. // 菜单id
  219. $id = input('param.id/d');
  220. if (request()->isAjax()) {
  221. $param = input('post.');
  222. $has = db('accountslabel')->where('name',$param['name'])->where('id','<>',$id)->find();
  223. if(!empty($has)){
  224. return json(['code' => -1, 'data' => '', 'msg' => '此用户标签已存在']);
  225. }
  226. $info = array();
  227. $info['name'] = $param['name'];
  228. try {
  229. db('accountslabel')->where('id', $param['id'])->update($info);
  230. } catch (\Exception $e) {
  231. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  232. }
  233. return json(['code' => 1, 'data' => '', 'msg' => '编辑用户标签成功']);
  234. }
  235. $label = db('accountslabel')->where('id',$id)->find();
  236. $this->assign([
  237. 'label' => $label,
  238. 'id' => $id,
  239. 'status' => config('kf_status')
  240. ]);
  241. return $this->fetch();
  242. }
  243. // 删除用户标签
  244. public function delLabel()
  245. {
  246. if (request()->isAjax()) {
  247. //留言类型id
  248. $id = input('param.id/d');
  249. try {
  250. db('accountslabel')->where('id', $id)->delete();
  251. } catch (\Exception $e) {
  252. return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
  253. }
  254. return json(['code' => 1, 'data' => '', 'msg' => '删除用户标签成功']);
  255. }
  256. }
  257. // 生成标签按钮
  258. private function makeBtnLabel($id)
  259. {
  260. $operate = '<a style="margin-left:5px;" href="' . url('Accounts/editlabel', ['id' => $id]) . '"><button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 编辑</button></a> ';
  261. $operate .= '<a href="javascript:labelDel(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
  262. $operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
  263. return $operate;
  264. }
  265. }