Accounts.php 12 KB

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