From bc4787717abaaabd1a32c54775b57d4d7d6f6566 Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 15 Sep 2020 13:16:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/api/Wechat.php | 120 +++++++++++++++++++++++++++++ app/data/controller/api/Wxapp.php | 2 +- 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 app/data/controller/api/Wechat.php diff --git a/app/data/controller/api/Wechat.php b/app/data/controller/api/Wechat.php new file mode 100644 index 000000000..cf819f447 --- /dev/null +++ b/app/data/controller/api/Wechat.php @@ -0,0 +1,120 @@ + + * + * 授权模式支持两种模块,参数 mode=0 时为静默授权,mode=1 时为完整授权 + * 注意:回跳地址默认使用 Header 中的 http_referer 获取,也可以传 source 参数 + */ +class Wechat extends Controller +{ + /** + * 授权模式 + * @var int + */ + protected $mode; + + /** + * 来源地址 + * @var string + */ + protected $source; + + /** + * 粉丝OPNEID + * @var string + */ + protected $openid; + + /** + * 网页授权配置 + * @var array + */ + protected $config; + + /** + * 微信粉丝数据 + * @var array + */ + protected $fansinfo; + /** + * 会员用户数据 + * @var array + */ + protected $userinfo; + + /** + * 加载对应JSSDK数据 + * @return \think\Response + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function jssdk() + { + $wechat = WechatService::instance(); + $this->mode = $this->request->get('mode', 1); + $this->source = $this->request->server('http_referer', $this->request->get('source')); + $user = $wechat->getWebOauthInfo($this->source ?: $this->request->url(true), $this->mode, false); + if (empty($user['openid'])) { + $content = 'alert("Wechat WebOauth failed.")'; + } else { + $this->openid = $user['openid']; + $this->config = $wechat->getWebJssdkSign($this->source); + $this->fansinfo = $user['fansinfo'] ?? []; + // 会员注册并登录生成接口令牌 + $data = $this->fansinfo; + $data['openid2'] = $data['openid']; + if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl']; + $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : ['openid2' => $this->openid]; + $this->userinfo = UserService::instance()->save($map, array_merge($map, $data), true); + $content = $this->_buildContent(); + } + return Response::create($content)->contentType('application/x-javascript'); + } + + /** + * 生成JOSN数据 + * @param mixed $data + * @return false|string + */ + private function _jsonEncode($data) + { + return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); + } + + /** + * 生成授权内容 + * @return string + */ + private function _buildContent() + { + return <<_jsonEncode($this->config)}); + wx.fansinfo={$this->_jsonEncode($this->fansinfo)}; + wx.userinfo={$this->_jsonEncode($this->userinfo)}; + wx.ready(function(){ + wx.hideOptionMenu(); + wx.hideAllNonBaseMenuItem(); + }); +} +EOF; + } + +} \ No newline at end of file diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index 0b88fc344..9c7608e42 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -10,7 +10,7 @@ use WeMini\Live; use WeMini\Qrcode; /** - * 小程序入口 + * 微信小程序入口 * Class Wxapp * @package app\data\controller\api */