WeappAuthService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 $data
  71. * @return array
  72. */
  73. public function login($data)
  74. {
  75. [
  76. $openid,
  77. $unionid,
  78. // $avatar,
  79. // $nickname,
  80. // $sex
  81. ] = $this->getUserInfoByCode($data[ '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. $config = ( new MemberConfigService() )->getLoginConfig();
  91. $is_auth_register = $config[ 'is_auth_register' ];
  92. $is_force_access_user_info = $config[ 'is_force_access_user_info' ];
  93. $is_bind_mobile = $config[ 'is_bind_mobile' ];
  94. $is_mobile = $config[ 'is_mobile' ];
  95. if ($member_info->isEmpty()) {
  96. // 开启自动注册会员
  97. if ($is_auth_register) {
  98. // 开启强制获取会员信息并且开启强制绑定手机号,必须获取全部信息才能进行注册
  99. if ($is_force_access_user_info && $is_bind_mobile) {
  100. if (!empty($data[ 'nickname' ]) && !empty($data[ 'headimg' ]) && !empty($data[ 'mobile' ])) {
  101. return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid, $data[ 'nickname' ], $data[ 'headimg' ]);
  102. } else {
  103. return [ 'openid' => $openid, 'unionid' => $unionid ]; // 将重要信息返回给前端保存
  104. }
  105. } else if ($is_force_access_user_info) {
  106. // 开启强制获取会员信息时,必须获取到昵称和头像才能进行注册
  107. if (!empty($data[ 'nickname' ]) && !empty($data[ 'headimg' ])) {
  108. return $this->register($openid, '', '', $unionid, $data[ 'nickname' ], $data[ 'headimg' ]);
  109. } else {
  110. return [ 'openid' => $openid, 'unionid' => $unionid ]; // 将重要信息返回给前端保存
  111. }
  112. } else if ($is_bind_mobile) {
  113. // 开启强制绑定手机号,必须获取手机号才能进行注册
  114. if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
  115. return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
  116. } else {
  117. return [ 'openid' => $openid, 'unionid' => $unionid ]; // 将重要信息返回给前端保存
  118. }
  119. } else if (!$is_force_access_user_info && !$is_bind_mobile) {
  120. // 关闭强制获取用户信息、并且关闭强制绑定手机号的情况下允许注册
  121. return $this->register($openid, '', '', $unionid);
  122. }
  123. } else {
  124. // 关闭自动注册,但是开启了强制绑定手机号,必须获取手机号才能进行注册
  125. if ($is_bind_mobile) {
  126. if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
  127. return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
  128. } else {
  129. return [ 'openid' => $openid, 'unionid' => $unionid ]; // 将重要信息返回给前端保存
  130. }
  131. } else if($is_mobile) {
  132. if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
  133. return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
  134. } else {
  135. return [ 'openid' => $openid, 'unionid' => $unionid ]; // 将重要信息返回给前端保存
  136. }
  137. }
  138. }
  139. } else {
  140. // 可能会更新用户和粉丝表
  141. $login_service = new LoginService();
  142. // 开启自动注册会员,获取到昵称和头像进行修改
  143. if ($is_auth_register) {
  144. if ($is_force_access_user_info) {
  145. if (!empty($data[ 'nickname' ])) {
  146. $member_info[ 'nickname' ] = $data[ 'nickname' ];
  147. }
  148. if (!empty($data[ 'headimg' ])) {
  149. $member_info[ 'headimg' ] = $data[ 'headimg' ];
  150. }
  151. }
  152. if ($is_bind_mobile) {
  153. // if (!empty($data[ 'mobile' ])) {
  154. // $member_info[ 'mobile' ] = $data[ 'mobile' ];
  155. // }
  156. }
  157. }
  158. return $login_service->login($member_info, MemberLoginTypeDict::WEAPP);
  159. }
  160. }
  161. /**
  162. * 注册
  163. * @param string $openid
  164. * @param string $mobile
  165. * @param string $mobile_code
  166. * @return array
  167. * @throws DataNotFoundException
  168. * @throws DbException
  169. * @throws GuzzleException
  170. * @throws InvalidConfigException
  171. * @throws ModelNotFoundException
  172. */
  173. public function register(string $openid, string $mobile = '', string $mobile_code = '', string $wx_unionid = '', $nickname = '', $headimg = '')
  174. {
  175. if(empty($openid)) throw new AuthException('AUTH_LOGIN_TAG_NOT_EXIST');
  176. if(empty($mobile)){
  177. if (!empty($mobile_code)) {
  178. $result = $this->core_weapp_serve_service->getUserPhoneNumber(0, $mobile_code);
  179. if(empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  180. $phone_info = $result['phone_info'];
  181. $mobile = $phone_info['purePhoneNumber'];
  182. if(empty($mobile)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
  183. }
  184. $is_verify_mobile = false;
  185. }else{
  186. $is_verify_mobile = true;
  187. }
  188. $member_service = new MemberService();
  189. $member_info = $member_service->findMemberInfo(['weapp_openid' => $openid]);
  190. if(!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');//账号已存在, 不能在注册
  191. if (!empty($wx_unionid)) {
  192. $member_info = $member_service->findMemberInfo([ 'wx_unionid' => $wx_unionid ]);
  193. if (!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');//账号已存在, 不能在注册
  194. }
  195. $register_service = new RegisterService();
  196. return $register_service->register($mobile ?? '',
  197. [
  198. 'weapp_openid' => $openid,
  199. 'wx_unionid' => $wx_unionid,
  200. 'nickname' => $nickname,
  201. 'headimg' => $headimg,
  202. ],
  203. MemberRegisterTypeDict::WEAPP,
  204. $is_verify_mobile ?? false
  205. );
  206. }
  207. /**
  208. * 更新openid(用于账号密码或手机号注册时未正常获取到openid时再次获取)
  209. * @param string $code
  210. * @return true
  211. */
  212. public function updateOpenid(string $code)
  213. {
  214. [
  215. $openid,
  216. $unionid,
  217. // $avatar,
  218. // $nickname,
  219. // $sex
  220. ] = $this->getUserInfoByCode($code);
  221. $member_service = new MemberService();
  222. $member = $member_service->findMemberInfo([ 'weapp_openid' => $openid ]);
  223. if (!$member->isEmpty()) throw new AuthException('MEMBER_OPENID_EXIST');//openid已存在
  224. $member_info = $member_service->findMemberInfo([ 'member_id' => $this->member_id ]);
  225. if ($member_info->isEmpty()) throw new AuthException('MEMBER_NOT_EXIST');//账号不存在
  226. $member_service->editByFind($member_info, [ 'weapp_openid' => $openid ]);
  227. return true;
  228. }
  229. }