123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2024/3/25 17:00
- */
- namespace App\Http\Services\Frontend\Client\Auth;
- use App\Exceptions\ApiException;
- use App\Http\Services\Frontend\Client\Common\AuthService;
- use App\Http\Services\Frontend\Client\Common\SmsService;
- use App\Http\Services\Service;
- use App\Models\Member\User;
- use App\Models\WechatUser;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Response;
- use Illuminate\Support\Facades\DB;
- use Overtrue\LaravelWeChat\EasyWeChat;
- class WechatAuthenticatedService extends Service
- {
- /**
- * @throws InvalidArgumentException
- */
- public function oauth($data): array
- {
- $wechat = EasyWeChat::officialAccount();
- $oauth = $wechat->getOAuth();
- $scopes = $data['scopes'] ?: 'snsapi_base';
- //callback_url 是授权回调的URL
- //生成完整的授权URL
- $redirectUrl = $oauth->scopes([$scopes])->redirect($data['redirect_url']);
- return ['redirectUrl' => $redirectUrl];
- }
- /**
- * @throws InvalidArgumentException
- */
- public function callback(array $params): array
- {
- $wechat = EasyWeChat::officialAccount();
- $oauth = $wechat->getOAuth();
- // 获取 OAuth 授权用户信息
- $wechat_user = $oauth->userFromCode($params['code']);
- //$wechat_user = $user->toArray();
- $data = [
- 'openid' => $wechat_user->getId() ?? $wechat_user->getTokenResponse()['openid'],
- 'bindUser' => true
- ];
- $avatar = $wechat_user->getAvatar();
- $avatar && ($data['avatar'] = $avatar);
- $nickname = $wechat_user->getNickname();
- $nickname && ($data['nickname'] = $nickname);
- $userIsExists = User::query()->where('openid', $data['openid'])->exists();
- if (!$userIsExists) {
- // $user_id = User::query()->create($data)->id;
- // $user = User::query()->find($user_id);
- $data['bindUser'] = false;
- }
- // $isExits = $this->hasOpenID($data['openid']);
- // 是否已存在
- // !$isExits && $this->create($data);
- // $wechatUser = WechatUser::query()->with('user.info')->where('openid', $data['openid'])->first();
- // return $this->success('', ['wechatUser' => $wechatUser->toArray()]);
- return $data;
- }
- private function hasOpenID($openId): bool
- {
- return false;
- // return WechatUser::query()->where('openid', $openId)->exists();
- }
- /**
- * @throws ApiException
- */
- public function bind(array $params): void
- {
- $mobile = $params['mobile'];
- $code = $params['code'];
- $openid = $params['open_id'];
- // 验证验证码
- $verifyCodeResult = (new SmsService())->verifyCode($mobile, $code);
- !$verifyCodeResult && self::error('验证码错误!', 400);
- // 获取微信信息
- // $data = [
- // 'openid' => $params['openid'],
- // 'avatar' => $params['avatar'],
- // 'nickname' => $params['nickname'],
- // ];
- $register_ip = request()->getClientIp();
- // 获取绑定用户
- User::query()->updateOrCreate(['openid' => $openid], ['openid' => $openid, 'mobile' => $mobile, 'register_ip' => $register_ip]);
- }
- /**
- * @throws ApiException
- */
- public function store($data): array
- {
- // 公众号OpenID
- $openID = $data['open_id'];
- $autoRegister = $data['auto_register'] ?? 0;
- $user = User::query()->where('open_id', $openID)->first();
- if (!$user && $autoRegister) {
- // 获取微信信息
- // 创建用户信息
- $user_id = User::query()->create(['openid' => $openID])->id;
- $user = User::query()->find($user_id);
- }
- $token = (new AuthService)->store($user);
- return ['token' => $token];
- }
- public function create1($data): Response
- {
- // 判断验证码
- (new SmsService())->verify($data['mobile'], $data['code']);
- $where = ['mobile' => $data['mobile'], 'type' => 0];
- $exists = User::query()->where($where)->exists();
- if (!$exists) User::query()->create($where);
- // 绑定公众号
- $user = User::query()->where($where)->first();
- $user->open_id = $data['open_id'];
- $user->save();
- return (new AuthService)->store($user);
- }
- // 发送短信
- public function send($data): Response
- {
- $mobile = $data['mobile'];
- $sms = new SmsService();
- return $sms->send($mobile, 0, 2);
- }
- }
|