// +---------------------------------------------------------------------- namespace app\kefu\services; use app\kefu\controller\AuthController; use app\kefu\model\KefuModel; use app\admin\model\user\User; use service\FormBuilder as Form; use service\JsonService as Json; use service\UploadService as Upload; use think\Request; use think\Url; use app\admin\model\article\ArticleCategory as ArticleCategoryModel; use app\admin\model\article\Article as ArticleModel; use think\Session; use service\JwtService; use app\kefu\model\KefuUserRecordModel; use service\BaseService; use app\kefu\services\KefuChatService; class KefuRecordService extends BaseService { private $model; public function __construct() { $this->model = new KefuUserRecordModel; } public function getRecordList($userId, $nickname, $isTourist = 0) { [$page, $limit] = $this->getPageValue(); return $this ->model ->where([ 'user_id' => $userId, 'is_tourist' => $isTourist, 'is_kefu_send' => 1 ]) ->where(function ($q) use ($nickname) { if (trim($nickname)) { $q->where('nickname', 'like', '%' . trim($nickname) . '%'); } }) ->page($page, $limit) ->select(); } public function saveUserOnlineStatus($uid, $isOnine, $isKefu) { if ($isKefu) { $kefuUserInfo = KefuModel::get($uid); if ($kefuUserInfo) { $kefuUserInfo->online = (int)$isOnine; $kefuUserInfo->save(); } KefuUserRecordModel::update([ 'online' => (int)$isOnine ], [ 'is_kefu_send' => 0, 'to_uid' => $uid ]); } else { KefuUserRecordModel::update([ 'online' => (int)$isOnine ], [ 'is_kefu_send' => 1, 'to_uid' => $uid ]); } } public function saveUserRecord($uid, $toUid, $isKefu, $msnType = KefuChatService::MSN_TYPE_TEXT, $msg = '', $platform_type = 0) { if ($isKefu) { $sendUserInfo = KefuModel::get($uid); $recvUserInfo = User::get($toUid); } else { $sendUserInfo = User::get($uid); $recvUserInfo = KefuModel::get($toUid); } $sendRecord = $this->model->where([ 'user_id' => $uid, 'to_uid' => $toUid, 'is_kefu_send' => (int)$isKefu ])->find(); if ($sendRecord) { $sendRecord->message = $msg; $sendRecord->message_type = $msnType; $sendRecord->update_time = time(); $sendRecord->save(); } else { $data = [ 'user_id' => $uid, 'to_uid' => $toUid, 'nickname' => $recvUserInfo->nickname, 'avatar' => $recvUserInfo->avatar, 'online' => 1, 'add_time' => time(), 'update_time' => time(), 'mssage_num' => 1, 'message' => $msg, 'message_type' => KefuChatService::MSN_TYPE_TEXT, 'is_kefu_send' => (int)$isKefu ]; $this->model->create($data); } $recvRecord = $this->model->where([ 'user_id' => $toUid, 'to_uid' => $uid, 'is_kefu_send' => (int)$isKefu ? 0 : 1 ])->find(); if ($recvRecord) { $recvRecord->mssage_num++; $recvRecord->message = $msg; $recvRecord->message_type = $msnType; $recvRecord->type = $platform_type; $recvRecord->update_time = time(); $recvRecord->save(); return $recvRecord; } else { $data = [ 'user_id' => $toUid, 'to_uid' => $uid, 'nickname' => $sendUserInfo->nickname, 'avatar' => $sendUserInfo->avatar, 'online' => 1, 'add_time' => time(), 'update_time' => time(), 'mssage_num' => 1, 'message' => $msg, 'message_type' => $msnType, 'type' => $platform_type, 'is_kefu_send' => (int)$isKefu ? 0 : 1 ]; return $this->model->create($data); } } public function getLatelyMsgByUid($uid) { $result = $this->model ->where([ 'is_kefu_send' => 1, 'to_uid' => $uid ]) ->whereOr(function ($q) use ($uid) { $q->where([ 'is_kefu_send' => 0, 'user_id' => $uid ]); }) ->order("update_time", "desc") ->limit(1) ->find(); if (!$result) return 0; return $result->is_kefu_send == 1 ? $result->user_id : $result->to_uid; } }