System.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * User: nickbai
  4. * Date: 2017/10/31 12:47
  5. * Email: 1902822973@qq.com
  6. */
  7. namespace app\admin\controller;
  8. class System extends Base
  9. {
  10. // 自动回复设置
  11. public function reply()
  12. {
  13. if(request()->isPost()){
  14. $param = input('post.');
  15. if(empty($param['word'])){
  16. return json(['code' => -1, 'data' => '', 'msg' => '回复内容不能为空']);
  17. }
  18. try{
  19. db('reply')->where('id', 1)->update($param);
  20. }catch(\Exception $e){
  21. return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
  22. }
  23. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  24. }
  25. $info = db('reply')->where('id', 1)->find();
  26. $this->assign([
  27. 'info' => $info,
  28. 'status' => config('kf_status')
  29. ]);
  30. return $this->fetch();
  31. }
  32. // 客服设置
  33. public function customerService()
  34. {
  35. if(request()->isPost()){
  36. $param = input('post.');
  37. db('kf_config')->where('id', 1)->update($param);
  38. return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
  39. }
  40. $this->assign([
  41. 'config' => db('kf_config')->where('id', 1)->find(),
  42. 'status' => config('kf_status')
  43. ]);
  44. return $this->fetch();
  45. }
  46. // 历史会话记录
  47. public function wordsLog()
  48. {
  49. if(request()->isAjax()){
  50. $param = input('param.');
  51. $limit = $param['pageSize'];
  52. $offset = ($param['pageNumber'] - 1) * $limit;
  53. // 默认显示最近7天
  54. $start = input('param.start');
  55. $end = input('param.end');
  56. $user_id = input('param.user_id');
  57. $group_id = input('param.group_id');
  58. $temp = db('service_log');
  59. $countTmp = db('service_log');
  60. if(!empty($param['searchText'])){
  61. $user = db('users')->where('user_name', $param['searchText'])->find();
  62. $temp = $temp->where('kf_id', $user['id']);
  63. $countTmp = $countTmp->where('kf_id', $user['id']);
  64. }
  65. //日期
  66. if(!empty($start) && !empty($end) && $start <= $end){
  67. $temp = $temp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  68. $countTmp = $countTmp->whereBetween('start_time', [strtotime($start), strtotime($end . ' 23:59:59')]);
  69. }
  70. //结束时间为空
  71. if(!empty($start) && empty($end)){
  72. $temp = $temp->where('start_time','>',strtotime($start));
  73. $countTmp = $temp->where('start_time','>',strtotime($start));
  74. }
  75. //开始时间为空
  76. if(empty($start) && !empty($end)){
  77. $temp = $temp->where('start_time','<',strtotime($end . ' 23:59:59'));
  78. $countTmp = $temp->where('start_time','<',strtotime($end . ' 23:59:59'));
  79. }
  80. //开始时间/结束时间都为空(默认查七天)
  81. if(empty($start) && empty($end)){
  82. $temp = $temp->where('start_time','<',time())->where('start_time','>',time()-604800);
  83. $countTmp = $temp->where('start_time','<',time())->where('start_time','>',time()-604800);
  84. }
  85. //客服
  86. if($user_id != 0){
  87. $temp = $temp->where('kf_id', $user_id);
  88. $countTmp = $countTmp->where('kf_id', $user_id);
  89. }
  90. //客服组
  91. if($group_id != 0){
  92. $temp = $temp->where('group_id', $group_id);
  93. $countTmp = $temp->where('group_id', $group_id);
  94. }
  95. $result = $temp->limit($offset, $limit)->order('start_time', 'desc')->select();
  96. //所有客服
  97. $users = db('users')->select();
  98. //满意度
  99. $evaluate = db('evaluate')->select();
  100. foreach($result as $key=>$vo){
  101. if($result[$key]['intime'] != 0){
  102. $result[$key]['intime'] = date('Y-m-d H:i:s', $vo['intime']);
  103. }else{
  104. $result[$key]['intime'] = '-';
  105. }
  106. if($result[$key]['start_time'] != 0){
  107. $result[$key]['start_time'] = date('Y-m-d H:i:s', $vo['start_time']);
  108. }else{
  109. $result[$key]['start_time'] = '-';
  110. }
  111. if($result[$key]['end_time'] != 0){
  112. $result[$key]['end_time'] = date('Y-m-d H:i:s', $vo['start_time']);
  113. }else{
  114. $result[$key]['end_time'] = '-';
  115. }
  116. //客服名称
  117. for($i=0;$i<count($users);$i++){
  118. if($result[$key]['kf_id'] == $users[$i]['id']){
  119. $result[$key]['kefu_name'] = $users[$i]['user_name'];
  120. }
  121. }
  122. //满意度
  123. for($j=0;$j<count($evaluate);$j++){
  124. if($result[$key]['evaluate_id'] == $evaluate[$j]['evaluate_id']){
  125. $result[$key]['evaluate_name'] = $evaluate[$j]['evaluate_name'];
  126. }
  127. }
  128. // 生成操作按钮
  129. if(0 != $vo['servicelog_id']){
  130. $result[$key]['operate'] = $this->makeBtn($vo['servicelog_id']);
  131. }
  132. }
  133. $return['total'] = $countTmp->count(); //总数据
  134. $return['rows'] = $result;
  135. return json($return);
  136. }
  137. //所有客服
  138. $users = db('users')->select();
  139. if(!empty($users)){
  140. $option = '<option value="0">全部客服</option>';
  141. for($i=0;$i<count($users);$i++){
  142. $option = $option.'<option value="'.$users[$i]['id'].'">'.$users[$i]['user_name'].'</option>';
  143. }
  144. $useroption = '<select name="user_id" id="user_id" class="form-control">'.$option.'</select>';
  145. }
  146. //所有客服组
  147. $groups = db('groups')->select();
  148. if(!empty($groups)){
  149. $option = '<option value="0">全部客服组</option>';
  150. for($j=0;$j<count($groups);$j++){
  151. $option = $option.'<option value="'.$groups[$j]['id'].'">'.$groups[$j]['name'].'</option>';
  152. }
  153. $groupoption = '<select name="group_id" id="group_id" class="form-control">'.$option.'</select>';
  154. }
  155. $this->assign([
  156. 'useroption' => $useroption,
  157. 'groupoption' => $groupoption
  158. ]);
  159. return $this->fetch();
  160. }
  161. // 历史会话记录详情
  162. public function detail($id)
  163. {
  164. $chat = db('chat_log')->where('servicelog_id',$id)->order('time_line')->select();
  165. $html = '';
  166. for($i=0;$i<count($chat);$i++){
  167. $chat[$i]['time_line'] = date('H:i',$chat[$i]['time_line']);
  168. if(!empty(strstr($chat[$i]['to_id'], 'KF'))){
  169. $html = $html . '<div style="margin-top:15px;width: 30%;"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  170. $html = $html . '<p style="margin-top:5px;" class="form-content">'.$chat[$i]['content'].'</p></div>';
  171. }else{
  172. $html = $html . '<div style="margin-top:15px;width: 30%;margin-left:70%;text-align:right;"><div>'.$chat[$i]['from_name'].'&nbsp&nbsp&nbsp'.$chat[$i]['time_line'].'</div>';
  173. $html = $html . '<p style="margin-top:5px;" class="form-content">'.$chat[$i]['content'].'</p></div>';
  174. }
  175. }
  176. $servicelog = db('service_log')->where('servicelog_id',$id)->find();
  177. $evaluate = db('evaluate')->where('evaluate_id',$servicelog['evaluate_id'])->find();
  178. $evaluate = $evaluate['evaluate_name'];
  179. $this->assign([
  180. 'html' => $html,
  181. 'evaluate' => $evaluate
  182. ]);
  183. return $this->fetch();
  184. }
  185. // 生成按钮
  186. private function makeBtn($id)
  187. {
  188. $operate = '<a href="' . url('system/detail', ['id' => $id]) . '">';
  189. $operate .= '<button type="button" class="btn btn-primary btn-sm"><i class="fa fa-paste"></i> 详情</button></a> ';
  190. return $operate;
  191. }
  192. }