123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace app\service\api\weapp;
- use app\dict\member\MemberLoginTypeDict;
- use app\dict\member\MemberRegisterTypeDict;
- use app\service\api\login\LoginService;
- use app\service\api\login\RegisterService;
- use app\service\api\member\MemberConfigService;
- use app\service\api\member\MemberService;
- use app\service\core\weapp\CoreWeappAuthService;
- use core\base\BaseApiService;
- use core\exception\ApiException;
- use core\exception\AuthException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use GuzzleHttp\Exception\GuzzleException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class WeappAuthService extends BaseApiService
- {
- public $core_weapp_serve_service;
- public function __construct()
- {
- parent::__construct();
- $this->core_weapp_serve_service = new CoreWeappAuthService();
- }
-
- public function getUserInfoByCode(string $code)
- {
- $result = $this->core_weapp_serve_service->session(0, $code);
- $openid = $result['openid'] ?? '';
- $unionid = $result['unionid'] ?? '';
- if(empty($openid)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
-
- return [
- $openid,
- $unionid,
- ];
- }
-
- public function login($data)
- {
- [
- $openid,
- $unionid,
- ] = $this->getUserInfoByCode($data[ 'code' ]);
- $member_service = new MemberService();
- $member_info = $member_service->findMemberInfo([ 'weapp_openid' => $openid ]);
- if ($member_info->isEmpty() && !empty($unionid)) {
- $member_info = $member_service->findMemberInfo([ 'wx_unionid' => $unionid ]);
- if (!$member_info->isEmpty()) {
- $member_info->weapp_openid = $openid;
- }
- }
- $config = ( new MemberConfigService() )->getLoginConfig();
- $is_auth_register = $config[ 'is_auth_register' ];
- $is_force_access_user_info = $config[ 'is_force_access_user_info' ];
- $is_bind_mobile = $config[ 'is_bind_mobile' ];
- $is_mobile = $config[ 'is_mobile' ];
- if ($member_info->isEmpty()) {
-
- if ($is_auth_register) {
-
- if ($is_force_access_user_info && $is_bind_mobile) {
- if (!empty($data[ 'nickname' ]) && !empty($data[ 'headimg' ]) && !empty($data[ 'mobile' ])) {
- return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid, $data[ 'nickname' ], $data[ 'headimg' ]);
- } else {
- return [ 'openid' => $openid, 'unionid' => $unionid ];
- }
- } else if ($is_force_access_user_info) {
-
- if (!empty($data[ 'nickname' ]) && !empty($data[ 'headimg' ])) {
- return $this->register($openid, '', '', $unionid, $data[ 'nickname' ], $data[ 'headimg' ]);
- } else {
- return [ 'openid' => $openid, 'unionid' => $unionid ];
- }
- } else if ($is_bind_mobile) {
-
- if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
- return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
- } else {
- return [ 'openid' => $openid, 'unionid' => $unionid ];
- }
- } else if (!$is_force_access_user_info && !$is_bind_mobile) {
-
- return $this->register($openid, '', '', $unionid);
- }
- } else {
-
- if ($is_bind_mobile) {
- if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
- return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
- } else {
- return [ 'openid' => $openid, 'unionid' => $unionid ];
- }
- } else if($is_mobile) {
- if (!empty($data[ 'mobile' ]) || !empty($data[ 'mobile_code' ])) {
- return $this->register($openid, $data[ 'mobile' ], $data[ 'mobile_code' ], $unionid);
- } else {
- return [ 'openid' => $openid, 'unionid' => $unionid ];
- }
- }
- }
- } else {
-
- $login_service = new LoginService();
-
- if ($is_auth_register) {
- if ($is_force_access_user_info) {
- if (!empty($data[ 'nickname' ])) {
- $member_info[ 'nickname' ] = $data[ 'nickname' ];
- }
- if (!empty($data[ 'headimg' ])) {
- $member_info[ 'headimg' ] = $data[ 'headimg' ];
- }
- }
- if ($is_bind_mobile) {
- }
- }
- return $login_service->login($member_info, MemberLoginTypeDict::WEAPP);
- }
- }
-
- public function register(string $openid, string $mobile = '', string $mobile_code = '', string $wx_unionid = '', $nickname = '', $headimg = '')
- {
- if(empty($openid)) throw new AuthException('AUTH_LOGIN_TAG_NOT_EXIST');
- if(empty($mobile)){
- if (!empty($mobile_code)) {
- $result = $this->core_weapp_serve_service->getUserPhoneNumber(0, $mobile_code);
- if(empty($result)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
- $phone_info = $result['phone_info'];
- $mobile = $phone_info['purePhoneNumber'];
- if(empty($mobile)) throw new ApiException('WECHAT_EMPOWER_NOT_EXIST');
- }
- $is_verify_mobile = false;
- }else{
- $is_verify_mobile = true;
- }
- $member_service = new MemberService();
- $member_info = $member_service->findMemberInfo(['weapp_openid' => $openid]);
- if(!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');
- if (!empty($wx_unionid)) {
- $member_info = $member_service->findMemberInfo([ 'wx_unionid' => $wx_unionid ]);
- if (!$member_info->isEmpty()) throw new AuthException('MEMBER_IS_EXIST');
- }
- $register_service = new RegisterService();
- return $register_service->register($mobile ?? '',
- [
- 'weapp_openid' => $openid,
- 'wx_unionid' => $wx_unionid,
- 'nickname' => $nickname,
- 'headimg' => $headimg,
- ],
- MemberRegisterTypeDict::WEAPP,
- $is_verify_mobile ?? false
- );
- }
-
- public function updateOpenid(string $code)
- {
- [
- $openid,
- $unionid,
- ] = $this->getUserInfoByCode($code);
- $member_service = new MemberService();
- $member = $member_service->findMemberInfo([ 'weapp_openid' => $openid ]);
- if (!$member->isEmpty()) throw new AuthException('MEMBER_OPENID_EXIST');
- $member_info = $member_service->findMemberInfo([ 'member_id' => $this->member_id ]);
- if ($member_info->isEmpty()) throw new AuthException('MEMBER_NOT_EXIST');
- $member_service->editByFind($member_info, [ 'weapp_openid' => $openid ]);
- return true;
- }
- }
|