CustomerService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\admin\controller\shopro\chat;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use app\admin\model\shopro\chat\CustomerService as ChatCustomerService;
  6. use app\admin\model\shopro\chat\CustomerServiceUser;
  7. use app\admin\model\Admin;
  8. class CustomerService extends Common
  9. {
  10. protected $noNeedRight = ['select'];
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = new ChatCustomerService;
  15. $this->adminModel = new Admin;
  16. }
  17. /**
  18. * 客服列表
  19. */
  20. public function index()
  21. {
  22. if (!$this->request->isAjax()) {
  23. return $this->view->fetch();
  24. }
  25. $customerService = $this->model->sheepFilter()->with('customer_service_user')->paginate($this->request->param('list_rows', 10));
  26. $this->success('获取成功', null, $customerService);
  27. }
  28. /**
  29. * 客服添加
  30. */
  31. public function add()
  32. {
  33. if (!$this->request->isAjax()) {
  34. return $this->view->fetch();
  35. }
  36. $params = $this->request->only(['name', 'avatar', 'room_id', 'max_num', 'auth', 'auth_id']);
  37. $this->svalidate($params, ".add");
  38. if ($this->checkHasAuthId($params['auth'], $params['auth_id'], $params['room_id'])) {
  39. error_stop('该身份已绑定其他客服');
  40. }
  41. $data = Db::transaction(function () use ($params) {
  42. $this->model->allowField(true)->save($params);
  43. $customerServiceUser = CustomerServiceUser::create([
  44. 'customer_service_id' => $this->model->id,
  45. 'auth' => $params['auth'],
  46. 'auth_id' => $params['auth_id'],
  47. ]);
  48. return $customerServiceUser;
  49. });
  50. $this->success('保存成功', null, $data);
  51. }
  52. /**
  53. * 客服详情
  54. *
  55. * @param $id
  56. */
  57. public function detail($id)
  58. {
  59. $customerService = $this->model->with(['customer_service_user'])->where('id', $id)->find();
  60. if (!$customerService) {
  61. $this->error(__('No Results were found'));
  62. }
  63. $this->success('获取成功', null, $customerService);
  64. }
  65. /**
  66. * 客服编辑
  67. *
  68. * @param $id
  69. */
  70. public function edit($id = null)
  71. {
  72. if (!$this->request->isAjax()) {
  73. return $this->view->fetch('add');
  74. }
  75. $params = $this->request->only(['name', 'avatar', 'room_id', 'max_num', 'auth', 'auth_id']);
  76. $this->svalidate($params);
  77. $id = explode(',', $id);
  78. $list = $this->model->where('id', 'in', $id)->with('customer_service_user')->select();
  79. Db::transaction(function () use ($list, $params) {
  80. foreach ($list as $customerService) {
  81. $customerService->allowField(true)->save($params);
  82. $customerServiceUser = $customerService['customer_service_user'];
  83. // 编辑了客服身份所有者
  84. if ($params['auth'] != $customerServiceUser['auth'] || $params['auth_id'] != $customerServiceUser['auth_id']) {
  85. // 验证新的身份是否已经被绑定别的客服
  86. if ($this->checkHasAuthId($params['auth'], $params['auth_id'], $params['room_id'])) {
  87. error_stop('该身份已绑定其他客服');
  88. }
  89. // 删除老的身份
  90. CustomerServiceUser::{'auth' . ucfirst($customerServiceUser['auth'])}($customerServiceUser['auth_id'])
  91. ->where('customer_service_id', $customerService['id'])->delete();
  92. // 添加新的身份
  93. $customerServiceUser = CustomerServiceUser::create([
  94. 'customer_service_id' => $customerService->id,
  95. 'auth' => $params['auth'],
  96. 'auth_id' => $params['auth_id'],
  97. ]);
  98. }
  99. }
  100. });
  101. $this->success('更新成功');
  102. }
  103. /**
  104. * 客服用语
  105. *
  106. * @param $id
  107. */
  108. public function delete($id)
  109. {
  110. if (empty($id)) {
  111. $this->error(__('Parameter %s can not be empty', 'id'));
  112. }
  113. $id = explode(',', $id);
  114. $list = $this->model->where('id', 'in', $id)->select();
  115. Db::transaction(function () use ($list) {
  116. foreach ($list as $customerService) {
  117. // 删除客服的身份
  118. CustomerServiceUser::where('customer_service_id', $customerService['id'])->delete();
  119. $customerService->delete();
  120. }
  121. });
  122. $this->success('删除成功');
  123. }
  124. /**
  125. * 检验是否已经被绑定了客服(一个管理员或者用户只能是一种客服)
  126. *
  127. * @param string $auth
  128. * @param integer $auth_id
  129. * @return void
  130. */
  131. private function checkHasAuthId($auth, $auth_id, $room_id)
  132. {
  133. $customerServiceUser = CustomerServiceUser::{'auth' . ucfirst($auth)}($auth_id)->with('customer_service')->find();
  134. if ($customerServiceUser) {
  135. $customerService = $customerServiceUser['customer_service'];
  136. if ($customerService && $customerService['room_id'] == $room_id) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. /**
  143. * 获取管理员列表
  144. *
  145. * @return void
  146. */
  147. public function select()
  148. {
  149. if (!$this->request->isAjax()) {
  150. return $this->view->fetch();
  151. }
  152. $id = $this->request->param('id', 0);
  153. $room_id = $this->request->param('room_id', 'admin');
  154. // 已经被设置为客服的管理员
  155. $adminIds = CustomerServiceUser::whereExists(function ($query) use ($room_id) {
  156. $table_name = $this->model->getQuery()->getTable();
  157. $query->table($table_name)->where('room_id', $room_id)->where('customer_service_id=id');
  158. })->where('auth', 'admin')->where('customer_service_id', '<>', $id)->column('auth_id');
  159. // 正常的,并且排除了已经设置为客服的管理员
  160. $admins = $this->adminModel->where('status', 'normal')->whereNotIn('id', $adminIds)->select();
  161. $this->success('获取成功', null, $admins);
  162. }
  163. }