getInfo(['user_id' => $userId]); if ($servicerExists) { return $this->success($servicerExists); } $model = model('servicer')->add([ 'is_platform' => $isPlatform, // 此处一定为平台客服 'shop_id' => $siteId, 'user_id' => $userId, 'create_time' => time(), 'last_online_time' => time(), 'delete_time' => 0, 'client_id' => '', 'online' => $online, ]); return $this->success($model); } /** * 获取在线客服列表 * @param $site_id * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getOnlineServicers($site_id) { $list = model('servicer')->getList(['shop_id' => $site_id, 'online' => 1]); // if (empty($list) || count($list) == 0) { // return $this->error('没有在线客服'); // } return $this->success($list); } /** * 获取会员信息 * @param $where * @param bool $field * @return array|\think\Model|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getServicer($where, $field = true) { return model('servicer')->getInfo($where, $field); } /** * 设置客服在线状态 * @param $servicerId * @param $clientId * @param bool $online * @return int * @throws DbException */ public function setServicerOnlineStatus($servicerId, $clientId, $online = true) { if (!$online) { model('servicer_member')->update( ['client_id' => '', 'online' => $online], [['servicer_id', '=', $servicerId]] ); } return model('servicer')->update( ['client_id' => $online?$clientId:'', 'online' => $online, 'last_online_time' => time()], [['user_id', '=', $servicerId]] ); } /** * 获取客服列表 * @param array $condition * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getServicerList($condition = []) { $alias = 'sm'; $field = ['id']; return model('servicer')->getList($condition, $field, '', $alias); } /** * 获取当前客服服务的会员列表 * @param $user_id * @param int $page * @param int $limit * @return mixed|null * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getCurrentUserChatMembers($user_id, $page = 1, $limit = 10) { $servicer_ids = model('servicer')->getColumn(['user_id' => $user_id], 'id'); if (empty($servicer_ids)) { return null; } $alias = 'sm'; $field = ['sm.id', 'sm.member_id', 'sm.servicer_id', 'sm.member_name', 'sm.online', 'sm.create_time', 'sm.headimg']; $condition = ['sm.servicer_id', 'in', $servicer_ids]; $order = ['id' => 'desc']; return model('servicer')->pageList($condition, $field, $order, $page, 10, $alias, [], null, $limit); } /** * 删除客服 * @param $servicerId * @return array * @throws DbException */ public function delServicer($servicerId) { $del = model('servicer')->delete(['id' => $servicerId]); if ($del) { return $this->success(); } return $this->error(); } /** * 为当前会员匹配一个客服人员 * @param $site_id * @param $member_id * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getUsefulServicer($site_id, $member_id) { /** * 目前采用的是最闲优先匹配原则 */ $condition = [['online', '=', 1], ['shop_id', '=', $site_id]]; $list = model('servicer')->getList($condition, 'user_id'); if(!empty($list)){ foreach ($list as &$item){ $item_condition = [ ['servicer_id', '=', $item['user_id']], ['online', '=', 1] ]; $item_count = model('servicer_member')->getCount($item_condition); $item['chat_count'] = $item_count;//正在聊天的会员数 $condition_sd = [['member_id', '=', $member_id], ['type', '=', 1], ['servicer_id', 'in', array_column($list, 'user_id')]]; $info = model('servicer_dialogue')->getFirstData($condition_sd, 'servicer_id', 'create_time desc'); $item['last_count'] = !empty($info['servicer_id']) && $info['servicer_id']==$item['user_id'] ? 1:0; } } array_multisort(array_column($list,'last_count'),SORT_ASC,$list); array_multisort(array_column($list,'chat_count'),SORT_ASC,$list); return $list; } /** * 获取客服列表 * @param array $condition * @param int $page * @param int $page_size * @param string $order * @param bool $field * @param string $alias * @param array $join * @return array */ public function getPageList(array $condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = true, $alias = '', $join = []) { $res = model(self::TABLE_NAME)->pageList($condition, $field, $order, $page, $page_size, $alias, $join); return $this->success($res); } /** * 添加客服 * @param array $data * @param array $params * @return array */ public function add(array $data, array $params) { if (empty($data) || empty($params)) { return $this->error('', 'PARAMETER_ERROR'); } model(self::TABLE_NAME)->startTrans(); try { $user_model = new UserModel(); $res = $user_model->addUser($data, 1); if ($res['code'] < 0) { model(self::TABLE_NAME)->rollback(); return $res; } $user_info = $res['data']; $res = model(self::TABLE_NAME)->add(array_merge($params, [ 'shop_id' => 1, 'user_id' => $user_info, 'create_time' => time(), ])); model(self::TABLE_NAME)->commit(); return $this->success($res); } catch (\Exception $e) { model(self::TABLE_NAME)->rollback(); return $this->error('', '操作异常:' . $e->getMessage()); } } /** * 编辑客服 * @param array $data * @param array $condition * @param array $params * @return array */ public function edit(array $data, array $condition, array $params = []) { if (empty($data)) { return $this->error('', 'PARAMETER_ERROR'); } $user_model = new UserModel(); $user_info = $user_model->getUserInfo($condition)['data']; if (empty($user_info)) { return $this->error('', '客服不存在'); } model(self::TABLE_NAME)->startTrans(); try { $res = $user_model->editUser($data, $condition); if ($res['code'] < 0) { model(self::TABLE_NAME)->rollback(); return $res; } if (!empty($params)) { model(self::TABLE_NAME)->update($params, [['shop_id', '=', $user_info['site_id']], ['user_id', '=', $user_info['uid']]]); } model(self::TABLE_NAME)->commit(); return $this->success($res); } catch (\Exception $e) { model(self::TABLE_NAME)->rollback(); return $this->error('', '操作异常:' . $e->getMessage()); } } /** * 删除客服 * @param $site_id * @param $uid * @return array */ public function delete($site_id, $uid) { $condition = [ ['site_id', '=', $site_id], ['uid', '=', $uid], ['app_module', '=', 'servicer'] ]; $user_model = new UserModel(); $user_info = $user_model->getUserInfo($condition)['data']; if (empty($user_info)) { return $this->error('', '客服不存在'); } model(self::TABLE_NAME)->startTrans(); try { $res = $user_model->deleteUser($condition); if ($res['code'] < 0) { model(self::TABLE_NAME)->rollback(); return $res; } $res = model(self::TABLE_NAME)->delete([['shop_id', '=', $site_id], ['user_id', '=', $uid]]); model(self::TABLE_NAME)->commit(); return $this->success($res); } catch (\Exception $e) { model(self::TABLE_NAME)->rollback(); return $this->error('', '操作异常:' . $e->getMessage()); } } /** * 获取信息 * @param array $condition * @param bool $field * @return array */ public function getInfo($condition = [], $field = true) { $info = model(self::TABLE_NAME)->getInfo($condition, $field); return $this->success($info); } /** * 获取客服详情 * @param array $condition * @param bool $field * @return array */ public function getDetail($condition = [], $field = true) { $info = $this->getInfo($condition, $field)['data']; if (!empty($info)) { $user_model = new UserModel(); $info['user_info'] = $user_model->getUserInfo([['uid', '=', $info['user_id']]])['data']; } return $this->success($info); } /** * 处理客服信息 * @param array $condition * @param $site_id * @return array */ public function handleServicerInfo(array $condition, $site_id) { $res = [ 'nickname' => '', 'avatar' => '', ]; if (empty($condition)) { $info = []; } else { $info = $this->getInfo($condition, 'nickname,avatar')['data']; } return $info; } /** * 获取列表 * @param array $condition * @param bool $field * @param string $order * @param string $alias * @param array $join * @param string $group * @param null $limit * @return array */ public function getList($condition = [], $field = true, $order = 'id desc', $alias = '', $join = [], $group = '', $limit = null) { $res = model(self::TABLE_NAME)->getList($condition, $field, $order, $alias, $join, $group, $limit); return $this->success($res); } /** * 获取在线客服列表 * @param array $condition * @param string $field * @param string $order * @return array */ public function getOnlineList(array $condition, $field = 'user_id', $order = 'id desc') { $res = $this->getList(array_merge($condition, [['online', '=', 1]]), $field, $order); return $res; } /** * 分配客服 * @param array $condition * @return array */ public function assigning(array $condition) { $servicer_list = $this->getOnlineList($condition)['data']; $data = []; foreach ($servicer_list as $val) { $data[] = [ 'user_id' => $val['user_id'], 'chat_count' => model('servicer_member')->getCount([ ['servicer_id', '=', $val['user_id']], ['online', '=', 1], ]) ]; } if (!empty($data)) { // 按照服务的会员人数最少的优先分配 $sort_data = array_column($data, 'chat_count'); array_multisort($sort_data, SORT_ASC, $data); } return $data; } }