User.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $content=lang('friend.newChat');
  84. //把机器人添加到我的联系人中
  85. $userM = new UserModel;
  86. $user=$userM->setContact(1, 0,'event',$content);//机器人客服
  87. if($user){
  88. wsSendMsg($userInfo->user_id,'appendContact',$user);
  89. }
  90. }
  91. if($userInfo['status']==0){
  92. return warning(lang('user.forbid'));
  93. }
  94. $userInfo['avatar']=avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id']);
  95. // 如果用户已经有设置
  96. $setting=$userInfo['setting'] ?: '';
  97. if($setting){
  98. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  99. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  100. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  101. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  102. $setting['sendKey']=(int)$setting['sendKey'];
  103. $userInfo['setting']=$setting;
  104. }
  105. //如果登录信息中含有client——id则自动进行绑定
  106. $client_id=$this->request->param('client_id');
  107. if($client_id){
  108. $cid=$this->request->header('cid','');
  109. $this->doBindUid($userInfo['user_id'],$client_id,$cid);
  110. }
  111. $update=[
  112. 'last_login_time'=>time(),
  113. 'last_login_ip'=>$this->request->ip(),
  114. 'login_count'=>Db::raw('login_count+1')
  115. ];
  116. UserModel::where('user_id',$userInfo['user_id'])->update($update);
  117. $userInfo['qrUrl']=getMainHost().'/scan/u/'.encryptIds($userInfo['user_id']);
  118. unset($userInfo['password'],$userInfo['salt']);
  119. $userInfo['displayName']=$userInfo['realname'];
  120. $userInfo['id']=$userInfo['user_id'];
  121. $authToken=UserModel::refreshToken($userInfo,$param['terminal'] ?? 'web');
  122. $data=[
  123. 'sessionId'=>Session::getId(),
  124. 'authToken'=>$authToken,
  125. 'userInfo'=>$userInfo
  126. ];
  127. return success(lang('user.loginOk'),$data);
  128. }
  129. // 执行绑定
  130. public function doBindUid($user_id,$client_id,$cid=''){
  131. // 如果当前ID在线,将其他地方登陆挤兑下线
  132. if(Gateway::isUidOnline($user_id)){
  133. wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>$this->request->isMobile()]);
  134. }
  135. Gateway::bindUid($client_id, $user_id);
  136. // 查询团队,如果有团队则加入团队
  137. $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]);
  138. if($group){
  139. $group=$group->toArray();
  140. $group_ids=arrayToString($group,'group_id',false);
  141. foreach($group_ids as $v){
  142. Gateway::joinGroup($client_id, $v);
  143. }
  144. }
  145. if($cid){
  146. bindCid($user_id,$cid);
  147. }
  148. wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]);
  149. }
  150. /**
  151. * 猜你想问列表
  152. */
  153. function guessask()
  154. {
  155. try {
  156. $params = $this->request->param();
  157. $page = $params['page'] ?? 1;
  158. $limit = $params['limit'] ?? 15;
  159. $language_code = $this->lang;
  160. $query = GuessAskLanguages::where('language_code', $language_code)->where('status', 1);
  161. if (!empty($params['name'])) {
  162. $query = $query->where('name', 'like', '%'.$params['name'].'%');
  163. }
  164. if (isset($params['type']) && $params['type'] != '') {
  165. $query = $query->where('type', $params['type']);
  166. }
  167. $count = $query->count();
  168. $list = $query->order('is_top','desc')
  169. ->order('is_rec','desc')
  170. ->order('click_num','desc')
  171. ->limit($limit)
  172. ->page($page)
  173. ->select();
  174. } catch (Exception $e) {
  175. return error($e->getMessage());
  176. }
  177. return success('', [ 'count' => $count, 'list' => $list]);
  178. }
  179. /**
  180. * 问题列表
  181. */
  182. function question()
  183. {
  184. try {
  185. $params = $this->request->param();
  186. $page = $params['page'] ?? 1;
  187. $limit = $params['limit'] ?? 15;
  188. $language_code = $this->lang;
  189. $query = QuestionLanguages::where('language_code', $language_code)->where('status', 1);
  190. if (isset($params['question_type']) && $params['question_type'] != '') {
  191. $query = $query->where('question_type', $params['question_type']);
  192. }
  193. $count = $query->count();
  194. $list = $query->order('weight','desc')
  195. ->limit($limit)
  196. ->page($page)
  197. ->select();
  198. } catch (Exception $e) {
  199. return error($e->getMessage());
  200. }
  201. return success('', [ 'count' => $count, 'list' => $list]);
  202. }
  203. }