This commit is contained in:
尖叫 2023-12-07 17:42:32 +08:00
parent b533f4e73b
commit 3cd85454ad

View File

@ -5,6 +5,7 @@ namespace app\api\controller\food;
use app\api\controller\food\Controller; use app\api\controller\food\Controller;
use app\api\model\food\User as UserModel; use app\api\model\food\User as UserModel;
use app\api\model\Setting; use app\api\model\Setting;
use hema\Http;
use hema\sms\Driver as Sms; use hema\sms\Driver as Sms;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Db; use think\facade\Db;
@ -170,5 +171,31 @@ class User extends Controller
return $this->renderSuccess($data, '成功'); 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('授权失败');
}
} }