Pub.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\common\controller;
  3. use think\App;
  4. use app\enterprise\model\{User,Group};
  5. use app\index\controller\Extension;
  6. use think\facade\Session;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use GatewayClient\Gateway;
  10. use app\manage\model\Config;
  11. use thans\jwt\facade\JWTAuth;
  12. /**
  13. * 控制器基础类
  14. */
  15. class Pub
  16. {
  17. /**
  18. * Request实例
  19. * @var \think\Request
  20. */
  21. protected $request;
  22. /**
  23. * 应用实例
  24. * @var \think\App
  25. */
  26. protected $app;
  27. /**
  28. * 构造方法
  29. * @access public
  30. * @param App $app 应用对象
  31. */
  32. public function __construct(App $app)
  33. {
  34. Gateway::$registerAddress = config('gateway.registerAddress');
  35. $this->app = $app;
  36. $this->request = $this->app->request;
  37. // 控制器初始化
  38. // $this->initialize();
  39. }
  40. public function login(){
  41. $param=request()->param();
  42. $token=$param['token'] ?? '';
  43. // token一键登录
  44. if($token){
  45. $apiStatus=config('app.api_status');
  46. if(!$apiStatus){
  47. return warning('接口已关闭');
  48. }
  49. $userInfo=Cache::get($token);
  50. if(!$userInfo){
  51. return warning('TOKEN已失效!');
  52. }
  53. }else{
  54. $userInfo=User::where(['account'=> $param['account']])->withoutField('register_ip,login_count,update_time,create_time')->find();
  55. if($userInfo==null){
  56. return warning('当前用户不存在!');
  57. }
  58. if($userInfo['status']==0){
  59. return warning('您的账号已被禁用');
  60. }
  61. $password=password_hash_tp($param['password'],$userInfo['salt']);
  62. $code=$param['code'] ?? '';
  63. if($code){
  64. if($code!=Cache::get($param['account'])){
  65. return warning('验证码错误!');
  66. }
  67. Cache::delete($param['account']);
  68. }else{
  69. if($password!=$userInfo['password']){
  70. return warning('密码错误!');
  71. }
  72. }
  73. }
  74. $userInfo['avatar']=avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id']);
  75. // 如果用户已经有设置
  76. $setting=$userInfo['setting'] ?: '';
  77. if($setting){
  78. $setting['hideMessageName']= $setting['hideMessageName']=='true' ? true : false;
  79. $setting['hideMessageTime']= $setting['hideMessageTime']=='true' ? true : false;
  80. $setting['avatarCricle']= $setting['avatarCricle']=='true' ? true : false;
  81. $setting['isVoice']= $setting['isVoice']=='true' ? true : false;
  82. $setting['sendKey']=(int)$setting['sendKey'];
  83. $userInfo['setting']=$setting;
  84. }
  85. //如果登录信息中含有client——id则自动进行绑定
  86. $client_id=$this->request->param('client_id');
  87. if($client_id){
  88. $cid=$this->request->header('cid','');
  89. $this->doBindUid($userInfo['user_id'],$client_id,$cid);
  90. }
  91. $update=[
  92. 'last_login_time'=>time(),
  93. 'last_login_ip'=>$this->request->ip(),
  94. 'login_count'=>Db::raw('login_count+1')
  95. ];
  96. User::where('user_id',$userInfo['user_id'])->update($update);
  97. $userInfo['qrUrl']=request()->domain().'/scan/u/'.encryptIds($userInfo['user_id']);
  98. unset($userInfo['password'],$userInfo['salt']);
  99. $userInfo['displayName']=$userInfo['realname'];
  100. $userInfo['id']=$userInfo['user_id'];
  101. $authToken=User::refreshToken($userInfo,$param['terminal'] ?? 'web');
  102. $data=[
  103. 'sessionId'=>Session::getId(),
  104. 'authToken'=>$authToken,
  105. 'userInfo'=>$userInfo
  106. ];
  107. return success('登录成功!',$data);
  108. }
  109. //退出登录
  110. public function logout(){
  111. try {
  112. $jwtData = JWTAuth::auth();
  113. } catch (\Exception $e) {
  114. return success('退出成功!');
  115. }
  116. $userInfo = $jwtData['info']->getValue();
  117. //解密token中的用户信息
  118. $userInfo = str_encipher($userInfo,false, config('app.aes_token_key'));
  119. if (!$userInfo) {
  120. return success('退出成功!');
  121. }
  122. //解析json
  123. $userInfo = (array)json_decode($userInfo, true);
  124. if($userInfo){
  125. $client_id=$this->request->param('client_id','');
  126. if($client_id){
  127. Gateway::unbindUid($client_id,$userInfo['user_id']);
  128. }
  129. wsSendMsg(0,'isOnline',['id'=>$userInfo['user_id'],'is_online'=>0]);
  130. }
  131. JWTAuth::invalidate(JWTAuth::token()->get());
  132. return success('退出成功!');
  133. }
  134. // 注册用户
  135. public function register(){
  136. try{
  137. $data = $this->request->param();
  138. $systemInfo=Config::getSystemInfo();
  139. // 判断系统是否开启注册
  140. if($systemInfo['sysInfo']['regtype']==2){
  141. $inviteCode=$data['inviteCode'] ?? '';
  142. if(!$inviteCode){
  143. return warning('当前系统已关闭注册功能!');
  144. }
  145. if(!Cache::get($inviteCode)){
  146. return warning('邀请码已失效!');
  147. }
  148. }
  149. $code=$data['code'] ?? '';
  150. if($code){
  151. if($code!=Cache::get($data['account'])){
  152. return warning('验证码错误!');
  153. }
  154. Cache::delete($data['account']);
  155. }
  156. $user=new User();
  157. $verify=$user->checkAccount($data);
  158. if(!$verify){
  159. return warning($user->getError());
  160. }
  161. $salt=\utils\Str::random(4);
  162. $data['password'] = password_hash_tp($data['password'],$salt);
  163. $data['salt'] =$salt;
  164. $data['register_ip'] =$this->request->ip();
  165. $data['name_py'] = pinyin_sentence($data['realname']);
  166. $user->save($data);
  167. $data['user_id']=$user->user_id;
  168. // 监听用户注册后的操作
  169. event('UserRegister',$data);
  170. return success('注册成功', $data);
  171. }catch (\Exception $e){
  172. return error($e->getMessage());
  173. }
  174. }
  175. //头像生成
  176. public function avatar(){
  177. circleAvatar(input('str'),input('s')?:80,input('uid'));die;
  178. }
  179. /**
  180. * 将用户UId绑定到消息推送服务中
  181. * @return \think\response\Json
  182. */
  183. public function bindUid(){
  184. $client_id=$this->request->param('client_id');
  185. $user_id=$this->request->param('user_id');
  186. $cid=$this->request->param('cid','');
  187. try{
  188. $this->doBindUid($user_id,$client_id,$cid);
  189. }catch(\Exception $e){
  190. // 未找到用户
  191. }
  192. return success('');
  193. }
  194. // 执行绑定
  195. public function doBindUid($user_id,$client_id,$cid=''){
  196. // 如果当前ID在线,将其他地方登陆挤兑下线
  197. if(Gateway::isUidOnline($user_id)){
  198. wsSendMsg($user_id,'offline',['id'=>$user_id,'client_id'=>$client_id,'isMobile'=>$this->request->isMobile()]);
  199. }
  200. Gateway::bindUid($client_id, $user_id);
  201. // 查询团队,如果有团队则加入团队
  202. $group=Group::getMyGroup(['gu.user_id'=>$user_id,'gu.status'=>1]);
  203. if($group){
  204. $group=$group->toArray();
  205. $group_ids=arrayToString($group,'group_id',false);
  206. foreach($group_ids as $v){
  207. Gateway::joinGroup($client_id, $v);
  208. }
  209. }
  210. if($cid){
  211. bindCid($user_id,$cid);
  212. }
  213. wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>1]);
  214. }
  215. // 下线通知
  216. public function offline(){
  217. $user_id=input('user_id');
  218. try{
  219. $client_ids=Gateway::getClientIdByUid($user_id);
  220. // 一个终端登录时才发送下线通知
  221. if(count($client_ids)<2){
  222. wsSendMsg(0,'isOnline',['id'=>$user_id,'is_online'=>0]);
  223. }
  224. }catch(\Exception $e){
  225. // 未找到用户
  226. }
  227. return success('');
  228. }
  229. /**
  230. * 将用户团队绑定到消息推送服务中
  231. * @return \think\response\Json
  232. */
  233. public function bindGroup(){
  234. $client_id=input('client_id');
  235. $group_id=input('group_id');
  236. $group_id = explode('-', $group_id)[1];
  237. Gateway::joinGroup($client_id, $group_id);
  238. return success('');
  239. }
  240. // 获取系统配置信息
  241. public function getSystemInfo(){
  242. $systemInfo=Config::getSystemInfo();
  243. $systemInfo['demon_mode']=env('app.demon_mode',false);
  244. return success('',$systemInfo);
  245. }
  246. // 发送验证码
  247. public function sendCode(){
  248. $account=$this->request->param('account');
  249. $type=$this->request->param('type',1);
  250. if(in_array($type,[3,4]) && !$account){
  251. $userInfo=request()->userInfo;
  252. $acType=\utils\Regular::check_account($userInfo['account']);
  253. if($acType){
  254. $account=$userInfo['account'];
  255. }else{
  256. $account=$userInfo['email'];
  257. }
  258. };
  259. $acType=\utils\Regular::check_account($account);
  260. if(!$acType){
  261. return warning('账户必须为手机号或者邮箱');
  262. }
  263. if(Cache::get($account.'_time')) return warning('请一分钟后再试!');
  264. if($type==1){
  265. $text='登录账户';
  266. $actions="login";
  267. }elseif($type==2){
  268. $text='注册账户';
  269. $actions="register";
  270. }elseif($type==3){
  271. $text='修改密码';
  272. $actions="changePassword";
  273. }else{
  274. $text="修改账户";
  275. $actions="changeUserinfo";
  276. }
  277. $code=rand(100000,999999);
  278. Cache::set($account,$code,300);
  279. Cache::set($account.'_time',$code,60);
  280. if($acType==2){
  281. $conf=Config::where(['name'=>'smtp'])->value('value');
  282. $conf['temp']='code';
  283. $mail=new \mail\Mail($conf);
  284. $mail->sendEmail([$account],$text,$code);
  285. return success('发送成功');
  286. }else{
  287. $parmes=[
  288. 'code'=>$code
  289. ];
  290. $res=sendSms($account,$actions,$parmes);
  291. return success($res['msg']);
  292. }
  293. }
  294. // 检查app版本升级
  295. public function checkVersion(){
  296. $oldRelease=$this->request->param('release',0);
  297. $setupPage=$this->request->param('setupPage',false);
  298. $platform=$this->request->param('platform',1101);
  299. $config=Config::where('name','sysInfo')->value('value');
  300. if($platform==1101){
  301. $teminal='andriod';
  302. }else{
  303. $teminal='ios';
  304. }
  305. $versionInfo=config('version.'.$teminal);
  306. $data=[
  307. 'versionName'=>$versionInfo['version'],
  308. 'versionCode'=>$versionInfo['release'],
  309. 'updateType'=>$versionInfo['update_type'],
  310. 'versionInfo'=>$versionInfo['update_info'],
  311. 'downloadUrl'=>'',
  312. ];
  313. // 是否手动检测更新,是的话就不能强制更新或者静默更新
  314. if($setupPage){
  315. $data['updateType']='solicit';
  316. }
  317. // 如果旧版本大于等于当前版本则不更新
  318. if($oldRelease>=$versionInfo['release']){
  319. return success('',$data);
  320. }
  321. $downUrl='';
  322. $andriod=getAppDowmUrl('andriod');
  323. // 如果是ios则返回ios地址
  324. if($platform==1101){
  325. $downUrl=env('app.andriod_webclip','') ? : $andriod;
  326. }else{
  327. $downUrl=env('app.ios_webclip','');
  328. }
  329. $data['downloadUrl']=$downUrl;
  330. return success('',$data);
  331. }
  332. }