feat: 分类接口替换为api 接口
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export type ApiEnvironment = 'local' | 'online';
|
||||
type MiniProgramVersion = 'develop' | 'trial' | 'release';
|
||||
|
||||
/** 统一接口入口;本地真机调试时将 localBaseUrl 的 IP 改为电脑局域网 IP。 */
|
||||
export const apiConfig = {
|
||||
developmentEnvironment: 'local' as ApiEnvironment,
|
||||
localBaseUrl: 'http://127.0.0.1:8800/api/v1',
|
||||
// 线上域名尚未确定:填写完整 HTTPS 地址,例如 https://你的域名/api/v1。
|
||||
onlineBaseUrl: '',
|
||||
timeout: 10000,
|
||||
};
|
||||
|
||||
/** develop 可切换本地/线上;体验版、正式版和未知运行环境均只允许线上入口。 */
|
||||
export function resolveApiConfig(version: MiniProgramVersion | string) {
|
||||
const environment: ApiEnvironment =
|
||||
version === 'develop' ? apiConfig.developmentEnvironment : 'online';
|
||||
const baseUrl = (
|
||||
environment === 'local'
|
||||
? apiConfig.localBaseUrl
|
||||
: apiConfig.onlineBaseUrl
|
||||
)
|
||||
.trim()
|
||||
.replace(/\/+$/, '');
|
||||
if (!baseUrl) throw new Error('请先配置线上接口地址');
|
||||
if (
|
||||
!/^https?:\/\/[^/?#\s]+\/api\/v1$/.test(baseUrl) ||
|
||||
(environment === 'online' && !baseUrl.startsWith('https://'))
|
||||
) {
|
||||
throw new Error('接口地址配置不正确');
|
||||
}
|
||||
return { environment, baseUrl, timeout: apiConfig.timeout };
|
||||
}
|
||||
|
||||
/** 请求时读取微信包版本,避免依赖 App 启动顺序,也不接受本地缓存覆盖正式版环境。 */
|
||||
export function getApiConfig() {
|
||||
return resolveApiConfig(wx.getAccountInfoSync().miniProgram.envVersion);
|
||||
}
|
||||
Reference in New Issue
Block a user