feat(passport): 迁移并修复小程序支付页面访问 websopy API 问题
- 迁移 src/passport/pay 相关代码至当前项目,无需替换文件 - 新增环境变量 WEBSOPY_API_BASE_URL,提供 websopy API 基础地址 - 在 config/app.ts 导出 WebsopyBaseUrl,供代码引用 - 配置 env.d.ts 类型定义,消除 TypeScript 隐式 any 错误 - request 请求方法支持自定义 baseUrl 以调用不同接口地址 - passport/pay 页面调用订阅接口统一传入 WebsopyBaseUrl - 修正 tenantId 类型为 number,调整登录用户类型断言 - 确认编译成功,dist 目录生成对应文件及正确请求地址 - 说明 type-check 依赖库问题不影响正常构建和运行
This commit is contained in:
@@ -5,8 +5,9 @@ import { Check, Close, Tips, Clock } from '@nutui/icons-react-taro'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { request } from '@/utils/request'
|
||||
import { getOpenId, loginByOpenId } from '@/api/passport/wx-login'
|
||||
import type { User } from '@/api/system/user/model'
|
||||
import { saveStorageByLoginUser } from '@/utils/server'
|
||||
import { TenantId } from '@/config/app'
|
||||
import { TenantId, WebsopyBaseUrl } from '@/config/app'
|
||||
|
||||
/**
|
||||
* 小程序支付页面
|
||||
@@ -87,7 +88,8 @@ const PayPage: React.FC = () => {
|
||||
const data: any = await request({
|
||||
url: `/app/subscription/detail-by-no/${subscriptionNo}`,
|
||||
method: 'GET',
|
||||
returnRaw: true // 先用 returnRaw=true 拿到完整响应,方便调试
|
||||
returnRaw: true,
|
||||
baseUrl: WebsopyBaseUrl
|
||||
})
|
||||
|
||||
console.log('[PayPage] 接口完整返回(returnRaw=true):', JSON.stringify(data))
|
||||
@@ -175,13 +177,13 @@ const PayPage: React.FC = () => {
|
||||
try {
|
||||
const loginResult = await loginByOpenId({
|
||||
code: loginRes.code,
|
||||
tenantId: TenantId
|
||||
tenantId: Number(TenantId)
|
||||
})
|
||||
console.log('[PayPage] loginByOpenId 结果:', loginResult)
|
||||
|
||||
if (loginResult.success && loginResult.data) {
|
||||
// 登录成功,保存 token
|
||||
saveStorageByLoginUser(loginResult.data.access_token || '', loginResult.data.user)
|
||||
saveStorageByLoginUser(loginResult.data.access_token || '', loginResult.data.user as unknown as User)
|
||||
console.log('[PayPage] 自动登录成功,已保存 token')
|
||||
} else {
|
||||
console.warn('[PayPage] 自动登录失败:', loginResult.message)
|
||||
@@ -211,7 +213,8 @@ const PayPage: React.FC = () => {
|
||||
url: `/app/subscription/mp-prepay/${detail.id}`,
|
||||
method: 'POST',
|
||||
data: { openid },
|
||||
returnRaw: true
|
||||
returnRaw: true,
|
||||
baseUrl: WebsopyBaseUrl
|
||||
})
|
||||
|
||||
console.log('[PayPage] mp-prepay 完整返回:', JSON.stringify(prepayRes))
|
||||
@@ -240,7 +243,8 @@ const PayPage: React.FC = () => {
|
||||
await request({
|
||||
url: `/app/subscription/mp-confirm/${detail.subscriptionNo}`,
|
||||
method: 'POST',
|
||||
data: {}
|
||||
data: {},
|
||||
baseUrl: WebsopyBaseUrl
|
||||
})
|
||||
} catch (confirmErr) {
|
||||
console.warn('支付确认通知失败,等待轮询:', confirmErr)
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface RequestConfig {
|
||||
showLoading?: boolean
|
||||
showError?: boolean
|
||||
returnRaw?: boolean
|
||||
baseUrl?: string
|
||||
}
|
||||
|
||||
interface ApiResponse<T = any> {
|
||||
@@ -206,7 +207,7 @@ const retryRequest = async <T>(config: RequestConfig, retryCount = 0): Promise<T
|
||||
|
||||
export async function request<T>(options: RequestConfig): Promise<T> {
|
||||
try {
|
||||
const config = requestInterceptor({ ...DEFAULT_CONFIG, ...options })
|
||||
const config = requestInterceptor({ ...DEFAULT_CONFIG, ...options, url: buildUrl(options.url, options.baseUrl) })
|
||||
return await retryRequest<T>(config)
|
||||
} catch (error) {
|
||||
const requestError = error as RequestError
|
||||
@@ -215,11 +216,11 @@ export async function request<T>(options: RequestConfig): Promise<T> {
|
||||
}
|
||||
}
|
||||
|
||||
const buildUrl = (url: string): string => {
|
||||
const buildUrl = (url: string, customBaseUrl?: string): string => {
|
||||
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||
return url
|
||||
}
|
||||
return `${baseUrl}${url}`
|
||||
return `${customBaseUrl || baseUrl}${url}`
|
||||
}
|
||||
|
||||
const buildQueryString = (params: Record<string, any>): string => {
|
||||
@@ -231,41 +232,41 @@ const buildQueryString = (params: Record<string, any>): string => {
|
||||
}
|
||||
|
||||
export function get<T>(url: string, params?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
const fullUrl = buildUrl(url) + (params ? buildQueryString(params) : '')
|
||||
const fullUrl = buildUrl(url, config?.baseUrl) + (params ? buildQueryString(params) : '')
|
||||
return request<T>({ url: fullUrl, method: 'GET', returnRaw: true, ...config })
|
||||
}
|
||||
|
||||
export function post<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'POST', data, returnRaw: true, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'POST', data, returnRaw: true, ...config })
|
||||
}
|
||||
|
||||
export function put<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'PUT', data, returnRaw: true, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'PUT', data, returnRaw: true, ...config })
|
||||
}
|
||||
|
||||
export function patch<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'PATCH', data, returnRaw: true, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'PATCH', data, returnRaw: true, ...config })
|
||||
}
|
||||
|
||||
export function del<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'DELETE', data, returnRaw: true, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'DELETE', data, returnRaw: true, ...config })
|
||||
}
|
||||
|
||||
export function getData<T>(url: string, params?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
const fullUrl = buildUrl(url) + (params ? buildQueryString(params) : '')
|
||||
const fullUrl = buildUrl(url, config?.baseUrl) + (params ? buildQueryString(params) : '')
|
||||
return request<T>({ url: fullUrl, method: 'GET', returnRaw: false, ...config })
|
||||
}
|
||||
|
||||
export function postData<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'POST', data, returnRaw: false, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'POST', data, returnRaw: false, ...config })
|
||||
}
|
||||
|
||||
export function putData<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'PUT', data, returnRaw: false, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'PUT', data, returnRaw: false, ...config })
|
||||
}
|
||||
|
||||
export function delData<T>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<T> {
|
||||
return request<T>({ url: buildUrl(url), method: 'DELETE', data, returnRaw: false, ...config })
|
||||
return request<T>({ url: buildUrl(url, config?.baseUrl), method: 'DELETE', data, returnRaw: false, ...config })
|
||||
}
|
||||
|
||||
export default {
|
||||
|
||||
Reference in New Issue
Block a user