User.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 app\admin\model\UserView;
  9. use think\facade\Session;
  10. use think\facade\Db;
  11. use GatewayClient\Gateway;
  12. use Exception;
  13. /**
  14. * 控制器基础类
  15. */
  16. class User
  17. {
  18. /**
  19. * Request实例
  20. * @var \think\Request
  21. */
  22. protected $request;
  23. /**
  24. * 应用实例
  25. * @var \think\App
  26. */
  27. protected $app;
  28. protected $lang;
  29. /**
  30. * 构造方法
  31. * @access public
  32. * @param App $app 应用对象
  33. */
  34. public function __construct(App $app)
  35. {
  36. Gateway::$registerAddress = config('gateway.registerAddress');
  37. $this->app = $app;
  38. $this->request = $this->app->request;
  39. $this->lang = request()->header('Lang', 'en');
  40. }
  41. //客户端登录
  42. public function login(){
  43. $params=request()->param();
  44. $where['role'] = 0;
  45. if (empty($params['account']) || empty($params['uid']) || empty($params['from'])) {
  46. return json(['code' => 400, 'msg' => '参数错误']);
  47. }
  48. if (!empty($params['account'])) {
  49. $where[] = ['account', '=', $params['account']];
  50. }
  51. if (!empty($params['from'])) {
  52. $where[] = ['from', '=', $params['from']];
  53. }
  54. if (!empty($params['uid'])) {
  55. $where[] = ['uid', '=', $params['uid']];
  56. }
  57. $userInfo=UserModel::where($where)->withoutField('register_ip,login_count,update_time,create_time')->find();
  58. if($userInfo==null){
  59. $salt = \utils\Str::random(4);
  60. $userInfo = UserModel::create([
  61. 'account'=>$params['account'],
  62. 'realname'=> !empty($params['realname']) ? $params['realname'] : '',
  63. 'name_py' => !empty($params['realname']) ? pinyin_sentence($params['realname']) : '',
  64. 'phone'=>!empty($params['phone']) ? $params['phone'] : '',
  65. 'email'=>!empty($params['email']) ? $params['email'] : '',
  66. 'from'=>$params['from'],
  67. 'uid'=>$params['uid'],
  68. 'salt' => $salt,
  69. 'password' => password_hash_tp(123456,$salt),
  70. 'cs_uid' => 0,
  71. 'role' => 0,
  72. 'status' =>1,
  73. 'register_ip' => $this->request->ip(),
  74. 'last_login_ip' => $this->request->ip(),
  75. 'last_login_time' => time(),
  76. 'login_count' => 1
  77. ]);
  78. // 监听用户注册后的操作
  79. event('UserRegister',['user_id' => $userInfo['user_id'], 'realname' => $userInfo['realname']]);
  80. } else {
  81. $userInfo['avatar']=avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id']);
  82. // 如果用户已经有设置
  83. $setting=$userInfo['setting'] ?: '';
  84. if($setting){
  85. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  86. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  87. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  88. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  89. $setting['sendKey']=(int)$setting['sendKey'];
  90. $userInfo['setting']=$setting;
  91. }
  92. }
  93. if($userInfo['status']==0){
  94. return warning(lang('user.forbid'));
  95. }
  96. //如果登录信息中含有client——id则自动进行绑定
  97. $client_id=$this->request->param('client_id');
  98. if($client_id){
  99. $cid=$this->request->header('cid','');
  100. $this->doBindUid($userInfo['user_id'],$client_id,$cid);
  101. }
  102. $update=[
  103. 'last_login_time'=>time(),
  104. 'last_login_ip'=>$this->request->ip(),
  105. 'login_count'=>Db::raw('login_count+1')
  106. ];
  107. UserModel::where('user_id',$userInfo['user_id'])->update($update);
  108. $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']);
  109. unset($userInfo['password'],$userInfo['salt']);
  110. $userInfo['displayName']=$userInfo['realname'];
  111. $userInfo['id']=$userInfo['user_id'];
  112. $authToken=UserModel::refreshToken($userInfo,$param['terminal'] ?? 'web');
  113. //记录用户浏览
  114. UserView::addData($userInfo['user_id']);
  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 guessaskClick()
  176. {
  177. try {
  178. $id = $this->request->param('id');
  179. $info = GuessAskLanguages::where('id', $id)->where('status', 1)->find();
  180. if ($info) {
  181. $info->click_num = $info->click_num + 1;
  182. $info->save();
  183. }
  184. } catch (Exception $e) {
  185. return error($e->getMessage());
  186. }
  187. return success('');
  188. }
  189. /**
  190. * 问题列表
  191. */
  192. function question()
  193. {
  194. try {
  195. $params = $this->request->param();
  196. $page = $params['page'] ?? 1;
  197. $limit = $params['limit'] ?? 15;
  198. $language_code = $this->lang;
  199. $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1);
  200. if (isset($params['question_type']) && $params['question_type'] != '') {
  201. $query = $query->where('question_type', $params['question_type']);
  202. }
  203. $count = $query->count();
  204. $list = $query->order('weight','desc')
  205. ->limit($limit)
  206. ->page($page)
  207. ->select();
  208. } catch (Exception $e) {
  209. return error($e->getMessage());
  210. }
  211. return success('', [ 'count' => $count, 'list' => $list]);
  212. }
  213. /**
  214. * 问题点赞、点否
  215. */
  216. function questionClick()
  217. {
  218. try {
  219. $id = $this->request->param('id');
  220. $is_like = $this->request->param('is_like');
  221. $question = QuestionLanguages::where('id', $id)->where('status', 1)->find();
  222. if ($question) {
  223. if ($is_like == 1) {
  224. $question->like_count = $question->like_count + 1;
  225. } else {
  226. $question->dislike_count = $question->dislike_count + 1;
  227. }
  228. $question->save();
  229. }
  230. } catch (Exception $e) {
  231. return error($e->getMessage());
  232. }
  233. return success('');
  234. }
  235. }