| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- namespace app\common\controller;
- use think\App;
- use app\enterprise\model\User as UserModel;
- use app\enterprise\model\Group;
- use app\admin\model\GuessAskLanguages;
- use app\admin\model\QuestionLanguages;
- use app\admin\model\UserView;
- use think\facade\Session;
- use think\facade\Db;
- use GatewayClient\Gateway;
- use Exception;
- /**
- * 控制器基础类
- */
- class User
- {
- /**
- * Request实例
- * @var \think\Request
- */
- protected $request;
- /**
- * 应用实例
- * @var \think\App
- */
- protected $app;
- protected $lang;
- /**
- * 构造方法
- * @access public
- * @param App $app 应用对象
- */
- public function __construct(App $app)
- {
- Gateway::$registerAddress = config('gateway.registerAddress');
- $this->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']]);
- } 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');
-
- //记录用户浏览
- UserView::addData($userInfo['user_id']);
- $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 guessaskClick()
- {
- try {
- $id = $this->request->param('id');
- $info = GuessAskLanguages::where('id', $id)->where('status', 1)->find();
- if ($info) {
- $info->click_num = $info->click_num + 1;
- $info->save();
- }
- } catch (Exception $e) {
- return error($e->getMessage());
- }
- return success('');
- }
- /**
- * 问题列表
- */
- 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]);
- }
- /**
- * 问题点赞、点否
- */
- function questionClick()
- {
- try {
- $id = $this->request->param('id');
- $is_like = $this->request->param('is_like');
- $question = QuestionLanguages::where('id', $id)->where('status', 1)->find();
- if ($question) {
- if ($is_like == 1) {
- $question->like_count = $question->like_count + 1;
- } else {
- $question->dislike_count = $question->dislike_count + 1;
- }
- $question->save();
- }
- } catch (Exception $e) {
- return error($e->getMessage());
- }
- return success('');
- }
- }
|