Files
ai_wht_wechat/miniprogram/miniprogram/config/api.ts
2025-12-19 22:36:48 +08:00

143 lines
3.9 KiB
TypeScript

// API配置文件
// 统一管理后端接口地址
/**
* 环境类型
* dev: 开发环境(本地)
* test: 测试环境(服务器测试)
* prod: 生产环境
*/
type EnvType = 'dev' | 'test' | 'prod';
/**
* 环境配置接口
*/
interface EnvConfig {
baseURL: string; // 主服务地址
pythonURL?: string; // Python服务地址(可选)
timeout: number; // 请求超时时间
}
/**
* 多环境配置
* 修改这里的配置即可切换不同环境的后端地址
*/
const API_CONFIG: Record<EnvType, EnvConfig> = {
// 开发环境 - 本地开发
dev: {
baseURL: 'http://localhost:8080', // 本地Go服务
pythonURL: 'http://localhost:8000', // 本地Python服务
timeout: 90000
},
// 测试环境 - 服务器测试
test: {
baseURL: 'https://lehang.tech', // 测试服务器Go服务
pythonURL: 'https://lehang.tech', // 测试服务器Python服务
timeout: 90000
},
// 生产环境
prod: {
baseURL: 'https://lehang.tech', // 生产环境Go服务
pythonURL: 'https://lehang.tech', // 生产环境Python服务
timeout: 90000
}
};
/**
* 自动检测环境
* 根据小程序的运行环境自动判断:
* - 开发版(develop) → dev
* - 体验版(trial) → test
* - 正式版(release) → prod
*/
function detectEnvironment(): EnvType {
try {
const accountInfo = wx.getAccountInfoSync();
const envVersion = accountInfo.miniProgram.envVersion;
switch (envVersion) {
case 'develop':
return 'dev'; // 开发版
case 'trial':
return 'test'; // 体验版
case 'release':
return 'prod'; // 正式版
default:
return 'dev';
}
} catch (error) {
console.warn('无法检测环境,使用默认开发环境', error);
return 'dev';
}
}
// 当前环境(自动检测)
const currentEnv: EnvType = detectEnvironment();
const currentConfig = (): EnvConfig => API_CONFIG[currentEnv];
// 输出环境信息
console.log(`[API Config] 当前环境: ${currentEnv}`);
console.log(`[API Config] 主服务: ${currentConfig().baseURL}`);
if (currentConfig().pythonURL) {
console.log(`[API Config] Python服务: ${currentConfig().pythonURL}`);
}
// API端点
export const API = {
// 基础配置(动态获取)
get baseURL(): string {
return currentConfig().baseURL;
},
get pythonURL(): string | undefined {
return currentConfig().pythonURL;
},
get timeout(): number {
return currentConfig().timeout;
},
// 登录接口
auth: {
wechatLogin: '/api/login/wechat', // 微信登录
phoneLogin: '/api/login/phone' // 手机号登录
},
// 员工端接口
employee: {
profile: '/api/employee/profile', // 获取个人信息
bindXHS: '/api/employee/bind-xhs', // 绑定小红书
unbindXHS: '/api/employee/unbind-xhs', // 解绑小红书
availableCopies: '/api/employee/available-copies', // 获取可领取文案
claimCopy: '/api/employee/claim-copy', // 领取文案
claimRandomCopy: '/api/employee/claim-random-copy', // 随机领取文案
publish: '/api/employee/publish', // 发布内容
myPublishRecords: '/api/employee/my-publish-records' // 我的发布记录
},
// 公开接口(不需要登录)
public: {
products: '/api/products' // 获取产品列表
},
// 小红书相关接口
xhs: {
sendCode: '/api/xhs/send-code' // 发送小红书验证码
}
};
// 构建完整URL
export function buildURL(path: string, usePython = false): string {
const baseURL = usePython ? (API.pythonURL || API.baseURL) : API.baseURL;
return `${baseURL}${path}`;
}
// 获取请求头
export function getHeaders(): Record<string, string> {
const token = wx.getStorageSync('token') || '';
return {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : ''
};
}