handler($appid); } else { # 常归接口正常服务 return ReceiveHandle::instance()->handler($appid); } } /** * 一、处理服务推送Ticket * 二、处理取消公众号授权 * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function ticket() { try { $server = WechatService::WeOpenService(); if (!($data = $server->getComonentTicket())) { return "Ticket event handling failed."; } } catch (\Exception $e) { return "Ticket event handling failed, {$e->getMessage()}"; } if (!empty($data['AuthorizerAppid']) && isset($data['InfoType'])) { # 授权成功通知 if ($data['InfoType'] === 'authorized') { $this->app->db->name('WechatServiceConfig')->where(['authorizer_appid' => $data['AuthorizerAppid']])->update(['is_deleted' => '0']); } # 接收取消授权服务事件 if ($data['InfoType'] === 'unauthorized') { $this->app->db->name('WechatServiceConfig')->where(['authorizer_appid' => $data['AuthorizerAppid']])->update(['is_deleted' => '1']); } # 授权更新通知 if ($data['InfoType'] === 'updateauthorized') { $_GET['auth_code'] = $data['PreAuthCode']; $this->applyAuth($server); } } return 'success'; } /** * 微信代网页授权 * @throws \think\Exception * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException */ public function oauth() { list($mode, $appid, $enurl, $sessid) = [ $this->request->get('mode'), $this->request->get('state'), $this->request->get('enurl'), $this->request->get('sessid'), ]; $result = WechatService::WeOpenService()->getOauthAccessToken($appid); if (empty($result['openid'])) throw new \think\Exception('网页授权失败, 无法进一步操作!'); $this->app->cache->set("{$appid}_{$sessid}_openid", $result['openid'], 3600); if (!empty($mode)) { $fans = WechatService::WeChatOauth($appid)->getUserInfo($result['access_token'], $result['openid']); if (empty($fans)) throw new \think\Exception('网页授权信息获取失败, 无法进一步操作!'); $this->app->cache->set("{$appid}_{$sessid}_fans", $fans, 3600); } $this->redirect(debase64url($enurl)); } /** * 跳转到微信服务授权页面 * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function auth() { $this->source = input('source'); if (empty($this->source)) { return '请传入回跳 source 参数 ( 请使用 enbase64url 加密 )'; } $this->sourceUrl = debase64url($this->source); if (empty($this->sourceUrl)) { return '请传入回跳 source 参数 ( 请使用 enbase64url 加密 )'; } # 预授权码不为空,则表示可以进行授权处理 $service = WechatService::WeOpenService(); if (($auth_code = $this->request->get('auth_code'))) { return $this->applyAuth($service, $this->sourceUrl); } # 生成微信授权链接,使用刷新跳转到授权网页 $redirect = url("@service/api.push/auth", [], true, true) . "?source={$this->source}"; if (($redirect = $service->getAuthRedirect($redirect))) { ob_clean(); header("Refresh:0;url={$redirect}"); return "跳转中..."; } # 生成微信授权链接失败 return "