[更新]微信网页授权

This commit is contained in:
Anyon 2020-12-08 13:51:23 +08:00
parent 5a9efac167
commit 651074a92f

View File

@ -19,40 +19,30 @@ use think\Response;
*/ */
class Wechat extends Controller class Wechat extends Controller
{ {
/**
* 授权模式
* @var int
*/
protected $mode;
/**
* 来源地址
* @var string
*/
protected $source;
/** /**
* 粉丝OPNEID * 粉丝OPNEID
* @var string * @var string
*/ */
protected $openid; private $openid;
/** /**
* 网页授权配置 * 网页授权配置
* @var array * @var array
*/ */
protected $config; private $config;
/** /**
* 微信粉丝数据 * 微信粉丝数据
* @var array * @var array
*/ */
protected $fansInfo; private $fansInfo;
/** /**
* 用户用户数据 * 前端用户数据
* @var array * @var array
*/ */
protected $userInfo; private $userInfo;
/** /**
* 加载对应JSSDK数据 * 加载对应JSSDK数据
@ -64,18 +54,17 @@ class Wechat extends Controller
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function jssdk() public function jssdk(): Response
{ {
$wechat = WechatService::instance(); $wechat = WechatService::instance();
$this->mode = $this->request->get('mode', 1); $source = $this->request->server('http_referer', $this->request->get('source', ''));
$this->source = $this->request->server('http_referer', $this->request->get('source', '')); $result = $wechat->getWebOauthInfo($source ?: $this->request->url(true), input('mode', 1), false);
$user = $wechat->getWebOauthInfo($this->source ?: $this->request->url(true), $this->mode, false); if (empty($result['openid'])) {
if (empty($user['openid'])) {
$content = 'alert("Wechat WebOauth failed.")'; $content = 'alert("Wechat WebOauth failed.")';
} else { } else {
$this->openid = $user['openid']; $this->openid = $result['openid'];
$this->config = $wechat->getWebJssdkSign($this->source); $this->fansInfo = $result['fansinfo'] ?? [];
$this->fansInfo = $user['fansinfo'] ?? []; $this->config = $wechat->getWebJssdkSign($source);
// 用户注册并登录生成接口令牌 // 用户注册并登录生成接口令牌
$data = $this->fansInfo; $data = $this->fansInfo;
$data['openid2'] = $data['openid']; $data['openid2'] = $data['openid'];