app = $app; $this->request = $this->app->request; $this->lang = request()->header('Lang', 'en'); } public function login(){ $params=request()->param(); $where['role'] = 0; if (empty($params['account']) || empty($params['uid']) || empty($params['from'])) { return json(['code' => 400, 'msg' => '参数错误']); } if (!empty($params['account'])) { $where[] = ['account', '=', $params['account']]; } if (!empty($params['from'])) { $where[] = ['from', '=', $params['from']]; } if (!empty($params['uid'])) { $where[] = ['uid', '=', $params['uid']]; } $userInfo=UserModel::where($where)->withoutField('register_ip,login_count,update_time,create_time')->find(); if($userInfo==null){ $salt = \utils\Str::random(4); $userInfo = UserModel::create([ 'account'=>$params['account'], 'realname'=> !empty($params['realname']) ? $params['realname'] : '', 'name_py' => !empty($params['realname']) ? pinyin_sentence($params['realname']) : '', 'phone'=>!empty($params['phone']) ? $params['phone'] : '', 'email'=>!empty($params['email']) ? $params['email'] : '', 'from'=>$params['from'], 'uid'=>$params['uid'], 'salt' => $salt, 'password' => password_hash_tp(123456,$salt), 'cs_uid' => 0, 'role' => 0, 'status' =>1, 'register_ip' => $this->request->ip(), 'last_login_ip' => $this->request->ip(), 'last_login_time' => time(), 'login_count' => 1 ]); // 监听用户注册后的操作 event('UserRegister',['user_id' => $userInfo['user_id'], 'realname' => $userInfo['realname']]); // $content=lang('friend.newChat'); // //把机器人添加到我的联系人中 // $userM = new UserModel; // $user=$userM->setContact(1, 0,'event',$content);//机器人客服 // if($user){ // wsSendMsg($userInfo->user_id,'appendContact',$user); // } } else { $userInfo['avatar']=avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id']); // 如果用户已经有设置 $setting=$userInfo['setting'] ?: ''; if($setting){ $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false; $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false; $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false; $setting['isVoice']= $setting['isVoice']=='true' ? true : false; $setting['sendKey']=(int)$setting['sendKey']; $userInfo['setting']=$setting; } } if($userInfo['status']==0){ return warning(lang('user.forbid')); } //如果登录信息中含有client——id则自动进行绑定 $client_id=$this->request->param('client_id'); if($client_id){ $cid=$this->request->header('cid',''); $this->doBindUid($userInfo['user_id'],$client_id,$cid); } $update=[ 'last_login_time'=>time(), 'last_login_ip'=>$this->request->ip(), 'login_count'=>Db::raw('login_count+1') ]; UserModel::where('user_id',$userInfo['user_id'])->update($update); $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']); unset($userInfo['password'],$userInfo['salt']); $userInfo['displayName']=$userInfo['realname']; $userInfo['id']=$userInfo['user_id']; $authToken=UserModel::refreshToken($userInfo,$param['terminal'] ?? 'web'); $data=[ 'sessionId'=>Session::getId(), 'authToken'=>$authToken, 'userInfo'=>$userInfo ]; return success(lang('user.loginOk'),$data); } // 执行绑定 public function doBindUid($user_id,$client_id,$cid=''){ // 如果当前ID在线,将其他地方登陆挤兑下线 if(Gateway::isUidOnline($user_id)){ wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>$this->request->isMobile()]); } Gateway::bindUid($client_id, $user_id); // 查询团队,如果有团队则加入团队 $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]); if($group){ $group=$group->toArray(); $group_ids=arrayToString($group,'group_id',false); foreach($group_ids as $v){ Gateway::joinGroup($client_id, $v); } } if($cid){ bindCid($user_id,$cid); } wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]); } /** * 猜你想问列表 */ function guessask() { try { $params = $this->request->param(); $page = $params['page'] ?? 1; $limit = $params['limit'] ?? 15; $language_code = $this->lang; $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1); if (!empty($params['name'])) { $query = $query->where('name', 'like', '%'.$params['name'].'%'); } if (isset($params['type']) && $params['type'] != '') { $query = $query->where('type', $params['type']); } $count = $query->count(); $list = $query->order('is_top','desc') ->order('is_rec','desc') ->order('click_num','desc') ->limit($limit) ->page($page) ->select(); } catch (Exception $e) { return error($e->getMessage()); } return success('', [ 'count' => $count, 'list' => $list]); } /** * 问题列表 */ function question() { try { $params = $this->request->param(); $page = $params['page'] ?? 1; $limit = $params['limit'] ?? 15; $language_code = $this->lang; $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1); if (isset($params['question_type']) && $params['question_type'] != '') { $query = $query->where('question_type', $params['question_type']); } $count = $query->count(); $list = $query->order('weight','desc') ->limit($limit) ->page($page) ->select(); } catch (Exception $e) { return error($e->getMessage()); } return success('', [ 'count' => $count, 'list' => $list]); } }