User.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\common\controller;
  3. use think\App;
  4. use app\enterprise\model\User as UserModel;
  5. use app\enterprise\model\Group;
  6. use app\admin\model\GuessAskLanguages;
  7. use app\admin\model\QuestionLanguages;
  8. use think\facade\Session;
  9. use think\facade\Db;
  10. use GatewayClient\Gateway;
  11. use Exception;
  12. /**
  13. * 控制器基础类
  14. */
  15. class User
  16. {
  17. /**
  18. * Request实例
  19. * @var \think\Request
  20. */
  21. protected $request;
  22. /**
  23. * 应用实例
  24. * @var \think\App
  25. */
  26. protected $app;
  27. protected $lang;
  28. /**
  29. * 构造方法
  30. * @access public
  31. * @param App $app 应用对象
  32. */
  33. public function __construct(App $app)
  34. {
  35. Gateway::$registerAddress = config('gateway.registerAddress');
  36. $this->app = $app;
  37. $this->request = $this->app->request;
  38. $this->lang = request()->header('Lang', 'en');
  39. }
  40. public function login(){
  41. $params=request()->param();
  42. $where['role'] = 0;
  43. $where['cs_uid'] = 0;
  44. if (empty($params['account']) || empty($params['uid']) || empty($params['from'])) {
  45. return json(['code' => 400, 'msg' => '参数错误']);
  46. }
  47. if (!empty($params['account'])) {
  48. $where[] = ['account', '=', $params['account']];
  49. }
  50. if (!empty($params['phone'])) {
  51. $where[] = ['phone', '=', $params['phone']];
  52. }
  53. if (!empty($params['email'])) {
  54. $where[] = ['email', '=', $params['email']];
  55. }
  56. if (!empty($params['from'])) {
  57. $where[] = ['from', '=', $params['from']];
  58. }
  59. if (!empty($params['uid'])) {
  60. $where[] = ['uid', '=', $params['uid']];
  61. }
  62. $userInfo=UserModel::where($where)->withoutField('register_ip,login_count,update_time,create_time')->find();
  63. if($userInfo==null){
  64. $salt = \utils\Str::random(4);
  65. $userInfo = UserModel::create([
  66. 'account'=>$params['account'],
  67. 'realname'=>$params['realname'],
  68. 'name_py' => pinyin_sentence($params['realname']),
  69. 'phone'=>$params['phone'],
  70. 'email'=>$params['email'],
  71. 'from'=>$params['from'],
  72. 'uid'=>$params['uid'],
  73. 'salt' => $salt,
  74. 'password' => password_hash_tp(123456,$salt),
  75. 'cs_uid' => 0,
  76. 'role' => 0,
  77. 'status' =>1,
  78. 'register_ip' => $this->request->ip(),
  79. 'last_login_ip' => $this->request->ip(),
  80. 'last_login_time' => time(),
  81. 'login_count' => 1
  82. ]);
  83. }
  84. if($userInfo['status']==0){
  85. return warning(lang('user.forbid'));
  86. }
  87. $userInfo['avatar']=avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id']);
  88. // 如果用户已经有设置
  89. $setting=$userInfo['setting'] ?: '';
  90. if($setting){
  91. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  92. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  93. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  94. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  95. $setting['sendKey']=(int)$setting['sendKey'];
  96. $userInfo['setting']=$setting;
  97. }
  98. //如果登录信息中含有client——id则自动进行绑定
  99. $client_id=$this->request->param('client_id');
  100. if($client_id){
  101. $cid=$this->request->header('cid','');
  102. $this->doBindUid($userInfo['user_id'],$client_id,$cid);
  103. }
  104. $update=[
  105. 'last_login_time'=>time(),
  106. 'last_login_ip'=>$this->request->ip(),
  107. 'login_count'=>Db::raw('login_count+1')
  108. ];
  109. UserModel::where('user_id',$userInfo['user_id'])->update($update);
  110. $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']);
  111. unset($userInfo['password'],$userInfo['salt']);
  112. $userInfo['displayName']=$userInfo['realname'];
  113. $userInfo['id']=$userInfo['user_id'];
  114. $authToken=UserModel::refreshToken($userInfo,$param['terminal'] ?? 'web');
  115. $data=[
  116. 'sessionId'=>Session::getId(),
  117. 'authToken'=>$authToken,
  118. 'userInfo'=>$userInfo
  119. ];
  120. return success(lang('user.loginOk'),$data);
  121. }
  122. // 执行绑定
  123. public function doBindUid($user_id,$client_id,$cid=''){
  124. // 如果当前ID在线,将其他地方登陆挤兑下线
  125. if(Gateway::isUidOnline($user_id)){
  126. wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>$this->request->isMobile()]);
  127. }
  128. Gateway::bindUid($client_id, $user_id);
  129. // 查询团队,如果有团队则加入团队
  130. $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]);
  131. if($group){
  132. $group=$group->toArray();
  133. $group_ids=arrayToString($group,'group_id',false);
  134. foreach($group_ids as $v){
  135. Gateway::joinGroup($client_id, $v);
  136. }
  137. }
  138. if($cid){
  139. bindCid($user_id,$cid);
  140. }
  141. wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]);
  142. }
  143. /**
  144. * 猜你想问列表
  145. */
  146. function guessask()
  147. {
  148. try {
  149. $params = $this->request->param();
  150. $page = $params['page'] ?? 1;
  151. $limit = $params['limit'] ?? 15;
  152. $language_code = $this->lang;
  153. $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1);
  154. if (!empty($params['name'])) {
  155. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  156. }
  157. if (isset($params['type']) && $params['type'] != '') {
  158. $query = $query->where('type', $params['type']);
  159. }
  160. $count = $query->count();
  161. $list = $query->order('is_top','desc')
  162. ->order('is_rec','desc')
  163. ->order('click_num','desc')
  164. ->limit($limit)
  165. ->page($page)
  166. ->select();
  167. } catch (Exception $e) {
  168. return error($e->getMessage());
  169. }
  170. return success('', [ 'count' => $count, 'list' => $list]);
  171. }
  172. /**
  173. * 问题列表
  174. */
  175. function question()
  176. {
  177. try {
  178. $params = $this->request->param();
  179. $page = $params['page'] ?? 1;
  180. $limit = $params['limit'] ?? 15;
  181. $language_code = $this->lang;
  182. $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1);
  183. if (isset($params['question_type']) && $params['question_type'] != '') {
  184. $query = $query->where('question_type', $params['question_type']);
  185. }
  186. $count = $query->count();
  187. $list = $query->order('weight','desc')
  188. ->limit($limit)
  189. ->page($page)
  190. ->select();
  191. } catch (Exception $e) {
  192. return error($e->getMessage());
  193. }
  194. return success('', [ 'count' => $count, 'list' => $list]);
  195. }
  196. }