AuthLoginWithWechat.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by 老吴.
  4. * UserMsg:砥砺前行,扬帆起航
  5. * email:cwwx0128@qq.com
  6. * QQ:1113249273
  7. * QQ群:925283872
  8. * 微信:cww0128
  9. * Date: 2021/4/15
  10. * Time: 23:08
  11. */
  12. namespace app\api\controller;
  13. use addons\third\model\Third;
  14. use app\common\controller\Api;
  15. use fast\Http;
  16. class AuthLoginWithWechat extends Api
  17. {
  18. protected $noNeedLogin = ['code', 'login'];
  19. protected $noNeedRight = ['*'];
  20. protected $appid;
  21. protected $secret;
  22. protected $loginUrl;
  23. public function __construct()
  24. {
  25. $this->appid = get_addon_config("loginmobile")['appId'];
  26. $this->secret = get_addon_config("loginmobile")['secretId'];
  27. $this->loginUrl = get_addon_config("loginmobile")['loginUrl'];
  28. parent::__construct();
  29. }
  30. //https://hongqi-b.maoln.com/api/Auth_login_with_wechat/test
  31. /**
  32. * 登录第一步,获取openid 跟 session_key
  33. */
  34. public function code()
  35. {
  36. $code = $this->request->param('code');
  37. if (!$code) {
  38. $this->error('code不能为空');
  39. }
  40. self::getOpenid($code);
  41. }
  42. /**
  43. * @param $code 用来交换获取openid 跟 session_key
  44. */
  45. public function getOpenid($code)
  46. {
  47. $url = sprintf($this->loginUrl, $this->appid, $this->secret, $code);
  48. $result = Http::get($url);
  49. $wxResult = json_decode($result, true);
  50. if (empty($wxResult)) {
  51. $this->error('获取sessin_key及openID时异常');
  52. }
  53. if (isset($wxResult['errcode']) && $wxResult['errcode'] != 0) {
  54. $this->error($wxResult['errmsg']);
  55. }
  56. $item = [
  57. 'openid' => $wxResult['openid'],
  58. 'session_key' => $wxResult['session_key']
  59. ];
  60. $this->success('成功', $item);
  61. }
  62. /**
  63. * 用户登录
  64. */
  65. public function login()
  66. {
  67. $encryptedData = $this->request->post('encryptedData');
  68. $iv = $this->request->post('iv');
  69. $sessionKey = $this->request->post('sessionKey');
  70. $openid = $this->request->post('openid');
  71. if (empty($encryptedData) || empty($iv) || empty($sessionKey) || empty($openid)) {
  72. $this->error('缺少参数');
  73. }
  74. $errCode = self::decryptData($encryptedData, $iv, $data, $sessionKey, $this->appid);
  75. if ($errCode == 0) {
  76. $result = json_decode($data, true);
  77. $userinfo = \app\admin\model\User::where(['openid' => $openid])->find();
  78. // $ah = new Auth();
  79. if ($userinfo) {
  80. $userinfo->nickname = $result['nickName'];
  81. $userinfo->avatar = $result['avatarUrl'];
  82. $userinfo->gender = $result['gender'];
  83. $userinfo->city = $result['city'];
  84. $userinfo->province = $result['province'];
  85. $userinfo->country = $result['country'];
  86. $userinfo->save();
  87. $this->auth->direct($userinfo['id']);
  88. } else {
  89. $user = new \app\admin\model\User();
  90. $user->data([
  91. 'nickname' => $result['nickName'],
  92. 'avatar' => $result['avatarUrl'],
  93. 'gender' => $result['gender'],
  94. 'city' => $result['city'],
  95. 'province' => $result['province'],
  96. 'country' => $result['country'],
  97. 'status' => 'normal',
  98. 'openid' => $openid
  99. ]);
  100. $user->save();
  101. $this->auth->direct($user->id);
  102. }
  103. $this->success('登录成功', $this->auth->getUserinfo());
  104. } else {
  105. $this->error('登录失败' . $errCode);
  106. }
  107. }
  108. /**
  109. * 获取手机号
  110. */
  111. public function getPhone()
  112. {
  113. $iv = $this->request->post("iv", '', 'trim');
  114. $encryptedData = $this->request->post("encryptedData", '', 'trim');
  115. $sessionKey = $this->request->post('sessionKey');
  116. $datainfo = $this->auth->getUserinfo();
  117. if (!$iv || !$encryptedData) {
  118. $this->error('传参有误');
  119. }
  120. $errCode = self::decryptData($encryptedData, $iv, $data, $sessionKey, $this->appid);
  121. if ($errCode == 0) {
  122. $result = json_decode($data, true);
  123. if (isset($result['phoneNumber'])) {
  124. $user = \app\admin\model\User::get($datainfo['id']);
  125. $user->mobile = $result['phoneNumber'];
  126. $user->save();
  127. $this->success('获取成功', $result);
  128. } else {
  129. $this->error('号码获取失败');
  130. }
  131. } else {
  132. $this->error('用户信息更新失败');
  133. }
  134. }
  135. /**
  136. * 检验数据的真实性,并且获取解密后的明文.
  137. * @param $encryptedData string 加密的用户数据
  138. * @param $iv string 与用户数据一同返回的初始向量
  139. * @param $data string 解密后的原文
  140. *
  141. * @return int 成功0,失败返回对应的错误码
  142. */
  143. public function decryptData($encryptedData, $iv, &$data, $sessionKey, $appid)
  144. {
  145. if (strlen($sessionKey) != 24) {
  146. return -41001;
  147. }
  148. $aesKey = base64_decode($sessionKey);
  149. if (strlen($iv) != 24) {
  150. return -41002;
  151. }
  152. $aesIV = base64_decode($iv);
  153. $aesCipher = base64_decode($encryptedData);
  154. $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  155. $dataObj = json_decode($result);
  156. if ($dataObj == NULL) {
  157. return -41003;
  158. }
  159. if ($dataObj->watermark->appid != $appid) {
  160. return -41003;
  161. }
  162. $data = $result;
  163. return 0;
  164. }
  165. }