Files
training/miniprogram/src/utils/api.js
T
Pine c6def26b52 feat(miniprogram): OSS直链上传+资讯互动+详情页重设计
- API 基址支持 TARO_APP_API_BASE 覆盖;头像/入驻资料上传带 dir 业务目录(OSS 路径规范)
- 资讯详情:真实点赞(计数)/微信分享(openType=share+useShareAppMessage)/留言;封面 3.35:1 裁切;顶部色条修复
- 发布者徽章(官方/园区/OPC 三款渐变胶囊);头像有则显示无则不留位
- 动作按钮纯图标化并入留言标题行;留言区胶囊输入+渐变发送键
2026-08-28 17:20:10 +08:00

43 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Taro from '@tarojs/taro';
import { cached } from './cache';
// API 基址
// - 开发:指向本地 web 后端(project.config.json 需 urlCheck:false
// - 生产:真实备案 HTTPS 域名(小程序要求)
export const API_BASE = process.env.TARO_APP_API_BASE || 'https://opc.pinesound.cn'; // 云超服 FastAPI 后端(备案 HTTPS 域名;开发可用 TARO_APP_API_BASE 覆盖)
/**
* 统一请求封装(对齐 web 端 fetch 契约)
* 公开端点不鉴权;管理端点传 withAuth=true(带 token
*/
export function request(method, path, data = {}, withAuth = false) {
return new Promise((resolve, reject) => {
const header = { 'Content-Type': 'application/json', 'X-Client': 'miniprogram' };
if (withAuth) {
const token = Taro.getStorageSync('pine_token');
if (token) header.Authorization = `Bearer ${token}`;
}
Taro.request({
url: `${API_BASE}${path}`,
method,
data,
header,
timeout: 10000, // 后端未启动时快速失败,避免界面一直"加载中"
success: (res) => {
const d = res.data || {};
if (res.statusCode >= 200 && res.statusCode < 300 && d.ok !== false) {
resolve(d);
} else {
reject(new Error(d.detail || d.error || '请求失败,请稍后重试'));
}
},
fail: () => reject(new Error('网络连接失败,请检查网络后重试'))
});
});
}
/** 带短缓存的请求:命中未过期直接返回(用于列表类接口,减少重复请求与"空→内容"跳变) */
export function cachedRequest(method, path, ttl = 30000, data = {}, withAuth = false) {
return cached(`req:${method}:${path}:${JSON.stringify(data)}`, ttl, () => request(method, path, data, withAuth));
}