static::getAppid(), 'token' => sConf('wechat.token'), 'appsecret' => sConf('wechat.secret'), 'encodingaeskey' => sConf('wechat.aeskey'), 'mch_id' => sConf('wechat.mch_id'), 'mch_key' => sConf('wechat.mch_key') ]; } /** * 通过网页授权获取粉丝信息 * @param string $source 回跳URL地址 * @param integer $isfull 获取资料模式 * @param boolean $redirect 是否直接跳转 * @return array */ public static function getWebOauthInfo(string $source, int $isfull = 0, bool $redirect = true): array { try { $appid = static::getAppid(); $openid = request()->session()->get("{$appid}_openid"); $userinfo = request()->session()->get("{$appid}_fansinfo"); if ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($userinfo))) { return ['openid' => $openid,"url" => "", 'fansinfo' => $userinfo]; } // 解析 GET 参数 $queryString = parse_url($source, PHP_URL_QUERY); // 初始化参数数组 $params = []; // 只有当有查询字符串时才解析 if ($queryString !== null && $queryString !== '') { parse_str($queryString, $params); } // parse_str(parse_url($source, PHP_URL_QUERY), $params); $getVars = [ 'code' => $params['code'] ?? input('code', ''), 'rcode' => $params['rcode'] ?? input('rcode', ''), 'state' => $params['state'] ?? input('state', ''), ]; $wechat = static::WeChatOauth(); if ($getVars['state'] !== $appid || empty($getVars['code'])) { $params['rcode'] = enbase64url($source); $location = strstr("{$source}?", '?', true) . '?' . http_build_query($params); $oauthurl = $wechat->getOauthRedirect($location, $appid, $isfull ? 'snsapi_userinfo' : 'snsapi_base'); return ['openid' => "","url" => $oauthurl ,"fansinfo" => ""]; } elseif (($token = $wechat->getOauthAccessToken($getVars['code'])) && isset($token['openid'])) { request()->session()->set("{$appid}_openid", $openid = $token['openid']); if ($isfull && isset($token['access_token'])) { $userinfo = $wechat->getUserInfo($token['access_token'], $openid); request()->session()->set("{$appid}_fansinfo", $userinfo); } } if ($getVars['rcode']) { $location = debase64url($getVars['rcode']); return ['openid' => "","url" => $location ,"fansinfo" => ""]; } elseif ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($userinfo))) { return ['openid' => $openid, 'url' => '', 'fansinfo' => $userinfo]; } else { throw new \Exception('Query params [rcode] not find.'); } } catch (\Throwable $th) { echo $th->getMessage(); } } // // /** // * 获取微信网页JSSDK签名参数 // * @param null|string $location 签名地址 // * @return array // * @throws \WeChat\Exceptions\InvalidResponseException // * @throws \WeChat\Exceptions\LocalCacheException // * @throws \think\admin\Exception // */ // public static function getWebJssdkSign(?string $location = null): array // { // $location = $location ?: Library::$sapp->request->url(true); // if (static::getType() === 'api') { // return static::WeChatScript()->getJsSign($location); // } else { // return static::ThinkServiceConfig()->jsSign($location); // } // } }