123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Http\Api;
- use App\Services\Login\LoginTokenService;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\MiniApp\Application;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Str;
- use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
- use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
- use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
- use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
- use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
- class WechatMiniController extends HttpBaseController
- {
- protected $app = '';
- public function __construct()
- {
- parent::__construct();
- $this->setWebsite(__NAMESPACE__);
-
- $config = config('easywechat.mini_app.default');
- try {
- $this->app = new Application($config);
- } catch (InvalidArgumentException $e) {
- }
- }
- public function loginIn(): JsonResponse
- {
- $sceneStr = Request::post('sceneStr'); // 场景值
- $code = Request::post('code');
- $wxInfo = Request::post('userInfo');
- if (empty($productMid)) {
- return responseMessage(3000, '参数错误,请重试!');
- }
- if (empty($code) || empty($wxInfo)) {
- return responseMessage(3001, '参数错误,请重试!');
- }
- $response = $this->app->getClient()->get('/sns/jscode2session', [
- 'js_code' => $code,
- 'grant_type' => 'authorization_code',
- ]);
- try {
- $result = $response->toArray();
- if ($result) {
- // 判断用户是否已经存在
- $openid = $result['openid'];
- $unionid = $result['unionid'];
- $userInfo = DB::table('user_wechat_mini_program')->where(['openid' => $openid, 'unionid' => $unionid])->first();
- if ($userInfo) {
- // 更新头像和昵称
- $data = [
- 'nick' => $wxInfo['nickname'],
- 'wx_avatar' => $wxInfo['avatar'],
- 'wx_info' => json_encode($wxInfo),
- ];
- $userId = $userInfo->id;
- $isSuccess = DB::table('user_wechat_mini_program')->where('user_id', $userId)->update($data);
- } else {
- // 插入数据
- $isSuccess = DB::transaction(function () use ($result, $wxInfo) {
- $userData = [
- 'mid' => Str::random(12),
- 'username' => $wxInfo['nickname'],
- 'real_username' => '',
- 'avatar' => $wxInfo['avatar'],
- 'roles' => json_encode([]),
- 'remember_token' => '',
- 'status' => 1,
- 'created_at' => time(),
- 'updated_at' => time(),
- ];
- $userId = DB::table('user')->insertGetId($userData);
- $data = [
- 'mid' => Str::random(12),
- 'user_id' => $userId,
- 'openid' => $result['openid'],
- 'session_key' => $result['session_key'],
- 'unionid' => $result['unionid'],
- 'nick' => $wxInfo['nickname'],
- 'wx_avatar' => $wxInfo['avatar'],
- 'wx_info' => json_encode($wxInfo),
- ];
- DB::table('user_wechat_mini_program')->insertGetId($data);
- });
- }
- if ($isSuccess) {
- // 设置登录的token
- $loginToken = new LoginTokenService('user_access_token');
- if (!in_array($sceneStr, ['web', 'mobile', 'officialAccount', 'miniProgram'])) {
- $token = $loginToken->createToken($userId, $sceneStr);
- } else {
- // 该场景值,可以为软件的mid
- $isExist = DB::table('product')->where('mid', $sceneStr)->count();
- if ($isExist) {
- $token = $loginToken->createOnlyOneToken($userId, $sceneStr);
- } else {
- return responseMessage(3003, '参数错误,请重试!');
- }
- }
- return responseMessage(1001, '操作成功!', ['token' => $token]);
- } else {
- return responseMessage(3002, '操作失败!');
- }
- }
- } catch (ClientExceptionInterface $e) {
- } catch (DecodingExceptionInterface $e) {
- } catch (RedirectionExceptionInterface $e) {
- } catch (ServerExceptionInterface $e) {
- } catch (TransportExceptionInterface $e) {
- }
- return responseMessage(3004, '操作失败,请重试!');
- }
- }
|