alias('a') ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account']) ->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id') ->where(['a.account' => $params['account'], 'k.del' => 0]) ->findOrEmpty()->toArray(); } else { $kefu = (new Admin())->alias('a') ->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account']) ->join('kefu k', 'a.id = k.admin_id') ->where(['a.account' => $params['account'], 'k.shop_id' => 0, 'k.del' => 0]) ->findOrEmpty()->toArray(); } $kefu['avatar'] = !empty($kefu['avatar']) ? UrlServer::getFileUrl($kefu['avatar']) : ""; $kefu['token'] = self::createSession($kefu['id'], $kefu['shop_id'], $params['client']); return $kefu; } /** * @notes 退出登录 * @param $user_id * @param $client * @return KefuSession * @author 段誉 * @date 2021/11/23 17:14 */ public static function logout($user_id, $client) { return (new KefuSession()) ->where(['kefu_id' => $user_id, 'client' => $client]) ->update(['update_time' => time(), 'expire_time' => time()]); } /** * @notes 创建会话 * @param $admin_id * @param $client * @return string * @author 段誉 * @date 2021/11/9 16:38 */ public static function createSession($kefu_id, $shop_id, $client) { $result = KefuSession::where(['kefu_id' => $kefu_id, 'client' => $client])->findOrEmpty(); $time = time(); $expire_time = $time + Config::get('project.token_expire_time'); // 新token $token = md5($kefu_id . $client . $time); $data = [ 'shop_id' => $shop_id, 'kefu_id' => $kefu_id, 'token' => $token, 'client' => $client, 'update_time' => $time, 'expire_time' => $expire_time, ]; if ($result->isEmpty()) { KefuSession::create($data); } else { KefuSession::where([ 'kefu_id' => $kefu_id, 'shop_id' => $shop_id, 'client' => $client, ])->update($data); } return $token; } }