WeappAuthService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\weapp;
  12. use app\dict\member\MemberLoginTypeDict;
  13. use app\dict\member\MemberRegisterTypeDict;
  14. use app\service\api\login\LoginService;
  15. use app\service\api\login\RegisterService;
  16. use app\service\api\member\MemberConfigService;
  17. use app\service\api\member\MemberService;
  18. use app\service\core\weapp\CoreWeappAuthService;
  19. use core\base\BaseApiService;
  20. use core\exception\ApiException;
  21. use core\exception\AuthException;
  22. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  23. use GuzzleHttp\Exception\GuzzleException;
  24. use think\db\exception\DataNotFoundException;
  25. use think\db\exception\DbException;
  26. use think\db\exception\ModelNotFoundException;
  27. /**
  28. * 微信配置模型
  29. * Class WechatConfigService
  30. * @package app\service\core\wechat
  31. */
  32. class WeappAuthService extends BaseApiService
  33. {
  34. public $core_weapp_serve_service;
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. $this->core_weapp_serve_service = new CoreWeappAuthService();
  39. }
  40. /**
  41. * 通过code获取微信小程序用户信息
  42. * @param string $code
  43. * @return array
  44. * @throws InvalidConfigException
  45. */
  46. public function getUserInfoByCode(string $code)
  47. {
  48. // $iv = $this->request->param('iv', '');
  49. // $encrypted_data = $this->request->param('encrypted_data', '');
  50. $result = $this->core_weapp_serve_service->session(0, $code);
  51. // if(empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  52. // $userinfo = $this->core_weapp_serve_service->decryptData($result['session_key'], $iv, $encrypted_data);
  53. $openid = $result['openid'] ?? '';//对应微信的 openid
  54. $unionid = $result['unionid'] ?? '' ;//对应微信的 unionid
  55. if(empty($openid)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  56. //todo 这儿还可能会获取用户昵称 头像 性别 ....用以更新会员信息
  57. // $nickname = $userinfo['nickName'] ?? '';//对应微信的 nickname
  58. // $avatar = $userinfo['avatarUrl'] ?? '';//对应微信的 头像地址
  59. // $sex = $userinfo['gender'];//性别
  60. return [
  61. $openid,
  62. $unionid,
  63. // $avatar,
  64. // $nickname,
  65. // $sex
  66. ];
  67. }
  68. /**
  69. * 登录
  70. * @param string $code
  71. * @return array
  72. */
  73. public function login(string $code)
  74. {
  75. [
  76. $openid,
  77. $unionid,
  78. // $avatar,
  79. // $nickname,
  80. // $sex
  81. ] = $this->getUserInfoByCode($code);
  82. $member_service = new MemberService();
  83. $member_info = $member_service->findMemberInfo(['weapp_openid' => $openid]);
  84. if ($member_info->isEmpty() && !empty($unionid)) {
  85. $member_info = $member_service->findMemberInfo([ 'wx_unionid' => $unionid ]);
  86. if (!$member_info->isEmpty()) {
  87. $member_info->weapp_openid = $openid;
  88. }
  89. }
  90. if($member_info->isEmpty()){
  91. $config = (new MemberConfigService())->getLoginConfig();
  92. $is_auth_register = $config['is_auth_register'];
  93. // 去掉强制绑定手机号判断,否则开启强制绑定的情况下小程序第三方注册无法注册
  94. if($is_auth_register == 1){
  95. return $this->register($openid, wx_unionid: $unionid);
  96. }else{
  97. return ['openid' => $openid, 'unionid' => $unionid];
  98. }
  99. }else{
  100. //可能会更新用户和粉丝表
  101. $login_service = new LoginService();
  102. return $login_service->login($member_info, MemberLoginTypeDict::WEAPP);
  103. }
  104. //todo 业务落地
  105. }
  106. /**
  107. * 注册
  108. * @param string $openid
  109. * @param string $mobile
  110. * @param string $mobile_code
  111. * @return array
  112. * @throws DataNotFoundException
  113. * @throws DbException
  114. * @throws GuzzleException
  115. * @throws InvalidConfigException
  116. * @throws ModelNotFoundException
  117. */
  118. public function register(string $openid, string $mobile = '', string $mobile_code = '', string $wx_unionid = '')
  119. {
  120. if(empty($openid)) throw new AuthException('AUTH_LOGIN_TAG_NOT_EXIST');
  121. if(empty($mobile)){
  122. if (!empty($mobile_code)) {
  123. $result = $this->core_weapp_serve_service->getUserPhoneNumber(0, $mobile_code);
  124. if(empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  125. $phone_info = $result['phone_info'];
  126. $mobile = $phone_info['purePhoneNumber'];
  127. if(empty($mobile)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  128. }
  129. $is_verify_mobile = false;
  130. }else{
  131. $is_verify_mobile = true;
  132. }
  133. $member_service = new MemberService();
  134. $member_info = $member_service->findMemberInfo(['weapp_openid' => $openid]);
  135. if(!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');//账号已存在, 不能在注册
  136. if (!empty($wx_unionid)) {
  137. $member_info = $member_service->findMemberInfo([ 'wx_unionid' => $wx_unionid ]);
  138. if (!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');//账号已存在, 不能在注册
  139. }
  140. $register_service = new RegisterService();
  141. return $register_service->register($mobile ?? '',
  142. [
  143. 'weapp_openid' => $openid,
  144. 'wx_unionid' => $wx_unionid
  145. ],
  146. MemberRegisterTypeDict::WEAPP,
  147. $is_verify_mobile ?? false
  148. );
  149. }
  150. /**
  151. * 更新openid(用于账号密码或手机号注册时未正常获取到openid时再次获取)
  152. * @param string $code
  153. * @return true
  154. */
  155. public function updateOpenid(string $code)
  156. {
  157. [
  158. $openid,
  159. $unionid,
  160. // $avatar,
  161. // $nickname,
  162. // $sex
  163. ] = $this->getUserInfoByCode($code);
  164. $member_service = new MemberService();
  165. $member = $member_service->findMemberInfo([ 'weapp_openid' => $openid ]);
  166. if (!$member->isEmpty()) throw new AuthException('MEMBER_OPENID_EXIST');//openid已存在
  167. $member_info = $member_service->findMemberInfo([ 'member_id' => $this->member_id ]);
  168. if ($member_info->isEmpty()) throw new AuthException('MEMBER_NOT_EXIST');//账号不存在
  169. $member_service->editByFind($member_info, [ 'weapp_openid' => $openid ]);
  170. return true;
  171. }
  172. }