From 3cd85454ad10530337c5dbedeb7a7f065a6d556a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=96=E5=8F=AB?= <392494244@qq.com> Date: Thu, 7 Dec 2023 17:42:32 +0800 Subject: [PATCH] 1 --- app/api/controller/food/User.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/api/controller/food/User.php b/app/api/controller/food/User.php index 6a045a4..2d6f8db 100644 --- a/app/api/controller/food/User.php +++ b/app/api/controller/food/User.php @@ -5,6 +5,7 @@ namespace app\api\controller\food; use app\api\controller\food\Controller; use app\api\model\food\User as UserModel; use app\api\model\Setting; +use hema\Http; use hema\sms\Driver as Sms; use think\facade\Cache; use think\facade\Db; @@ -170,5 +171,31 @@ class User extends Controller return $this->renderSuccess($data, '成功'); } + public function wxLogin() + { + + $post = $this->request->post(); + if (empty($post['code'])) { + return $this->renderError('授权失败'); + } + $appid = 'wx89c12dd426a55a2e'; + $app_secret = '33e66bcf944f9810abbb5ddd7825403d'; + $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$app_secret&code={$post['code']}&grant_type=authorization_code"; + $result = json_decode(Http::get($url), true); + if (isset($result['access_token'])) { + $access_token = $result['access_token']; + $openid = $result['openid']; + + + $wx_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; + $data = json_decode(Http::get($wx_url), true); + if (isset($data['headimgurl'])) { + Db::name('user_wx')->where(['wx_openid' => $openid])->update(['headimgurl' => $data['headimgurl'], 'nickname' => $data['nickname']]); + return $this->renderSuccess($data, '成功'); + } + } + return $this->renderError('授权失败'); + } + }