Login.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\Common;
  4. use app\common\model\Config;
  5. use app\common\model\LoginLog;
  6. use app\common\model\User;
  7. use app\common\controller\Api;
  8. use EasyWeChat\Factory;
  9. use Firebase\JWT\JWT;
  10. use think\Db;
  11. use think\facade\Validate;
  12. use app\common\library\WXBizDataCrypt;
  13. /**
  14. * @title 登录注册
  15. * @controller Login
  16. * @group user
  17. */
  18. class Login extends Api
  19. {
  20. /**
  21. * @title 授权登录
  22. * @desc 授权登录
  23. * @url /api/Login/wechat_login
  24. * @method POST
  25. * @tag 基础
  26. * @header
  27. * @param name:code type:string require:1 desc:code
  28. * @param name:rawData type:string require:1 desc:rawData
  29. */
  30. public function wechat_login(){
  31. $code = input('code');
  32. $rawData = input('rawData'); //用户信息
  33. if (!$code) $this->error('code为空');
  34. $result = User::wechatLogin($code,$rawData);
  35. if ($result['code']){
  36. $this->success($result['msg'],$result['data']);
  37. }else{
  38. $this->error($result['msg']);
  39. }
  40. }
  41. public function sav(){
  42. $data['headimg']=input('avatarUrl');
  43. $data['nickname']=input('nickName');
  44. Db::table('q_user')->where('openid',input('openid'))->update($data);
  45. $this->success('操作成功');
  46. }
  47. /**
  48. * @title 绑定手机号
  49. * @desc 绑定手机号
  50. * @url /api/Login/bind_phone
  51. * @method POST
  52. * @tag 基础
  53. * @header name:Authorization require:1 default: desc:验证token
  54. *
  55. *
  56. * @param name:code type:string require:1 desc:code
  57. * @param name:iv type:string require:1 desc:iv
  58. * @param name:encryptedData type:string require:1 desc:encryptedData
  59. *
  60. */
  61. public function bind_phone(){
  62. $user_id = $this->check_login();
  63. $code = input('code');
  64. $appid = Config::get_values('small_wechat_id');
  65. $secret = '01161e8b3dcee65960d0350131e14105';
  66. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
  67. $session_key = input('session_key');
  68. /*if (!empty($session_key['session_key'])) {
  69. $session_key = $session_key['session_key'];
  70. }else{
  71. $this->error('获取session_key失败!');
  72. }*/
  73. $iv = input('iv');
  74. $encryptedData = input('encryptedData');
  75. $pc = new WXBizDataCrypt($appid, $session_key);
  76. $pc->decryptData($encryptedData, $iv, $data );
  77. $array=json_decode($data, true);
  78. $result = User::bindPhone($user_id,$array);
  79. if ($result['code']){
  80. $this->success($result['msg']);
  81. }else{
  82. $this->error($result['msg']);
  83. }
  84. }
  85. /**
  86. * 小程序手机号授权登陆
  87. */
  88. public function wx_app_login(){
  89. $config = [
  90. 'app_id' => 'wx23528ccb517d264b',
  91. 'secret' => '01161e8b3dcee65960d0350131e14105',
  92. 'response_type' => 'array',
  93. ];
  94. $app = Factory::miniProgram($config);
  95. if(app()->request->header('Authorization')){
  96. $user_id = $this->check_login();
  97. $avatar=input('avatarUrl');
  98. $nickname=input('nickname');
  99. $user=Db::table('q_user')->where('id',$user_id)->find();
  100. //$decryptedData = $app->encryptor->decryptData($user['session_key'], input('iv'), input('encryptedData'));
  101. $pc = new WXBizDataCrypt($config['app_id'], $user['session_key']);
  102. $pc->decryptData(input('encryptedData'), input('iv'), $data );
  103. dump($data);die();
  104. $array=json_decode($data, true);
  105. dump($array);die();
  106. $res=Db::table('q_user')->where('id',$user_id)->update(['headimg'=>$avatar,'nickname'=>$nickname,'phone'=>$decryptedData['phoneNumber']]);
  107. if(empty($res)){
  108. $this->success('授权信息失败,请重新授权');
  109. }else{
  110. $this->success('登陆成功');
  111. }
  112. }
  113. $code=input('code');
  114. if(empty($code)){
  115. $this->error('code参数错误');
  116. }
  117. $data=$app->auth->session($code);
  118. $user=Db::table('q_user')->where('openid',$data['openid'])->find();
  119. if(empty($user)){
  120. $id=Db::table('q_user')->insertGetId(['openid'=>$data['openid'],'session_key'=>$data['session_key']]);
  121. $user=Db::table('q_user')->where('id',$id)->find();
  122. }else{
  123. Db::table('q_user')->where('openid',$data['openid'])->update(['session_key'=>$data['session_key']]);
  124. $user['session_key']=$data['session_key'];
  125. }
  126. $token = JWT::encode($user,config('jwt.key'));
  127. $this->success('登陆成功',['token'=>$token]);
  128. }
  129. }