appid(); $secret = $this->config('mp_secret'); if ($appid === '' || $secret === '') { throw new RepositoryException('微信公众号未配置 AppID/Secret,请联系管理员'); } $result = Http::timeout(10)->withOptions([ 'verify' => false, // 禁用 SSL 证书验证 ])->get('https://api.weixin.qq.com/sns/oauth2/access_token', [ 'appid' => $appid, 'secret' => $secret, 'code' => $code, 'grant_type' => 'authorization_code', ])->json(); $openid = is_array($result) ? (string) ($result['openid'] ?? '') : ''; if ($openid === '') { Log::warning('微信网页授权失败', ['response' => $result]); throw new RepositoryException('微信网页授权失败:' . ($result['errmsg'] ?? '请重新授权')); } return $openid; } /** * 公众号 AppID(公众号 JSAPI 下单 appid、H5 拼网页授权链接使用) */ public function appid(): string { return $this->config('mp_appid'); } /** * 读取应用配置:后台站点配置(微信应用配置分组)优先,为空回退 config/services.php(env) */ protected function config(string $key): string { $value = site_config('wechat.' . $key, ''); if ($value === null || $value === '') { $value = match ($key) { 'mp_appid' => config('services.wechat.mp.appid', ''), 'mp_secret' => config('services.wechat.mp.secret', ''), default => '', }; } return trim((string) $value); } }