- miniprogram/config.js: BASE_URL 指向 http://192.227.237.8:3001 - 动作标题中文化(nameZh),搜索匹配 name/nameZh/nameEn - 新增计划/收藏/我的 等页面与组件 - 后端 Docker 化(Dockerfile/.dockerignore)与翻译脚本 - .gitignore 补充 .DS_Store
88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
// 业务服务层:直接映射后端 REST 接口字段,不做多余转换。
|
||
const { request } = require('../utils/request');
|
||
|
||
// 确保动作名称以中文展示(防御层:即使后端意外返回英文也能兜底)
|
||
function injectDisplayName(item) {
|
||
if (!item) return item;
|
||
// 优先级:nameZh > name(后端 normalize 已把 name 设为中文)> nameEn 不展示
|
||
item.displayName = item.nameZh || item.name || '';
|
||
return item;
|
||
}
|
||
|
||
function injectDisplayNames(list) {
|
||
if (!list || !Array.isArray(list)) return list || [];
|
||
return list.map(injectDisplayName);
|
||
}
|
||
|
||
// 将参数对象序列化为 query string(跳过空值)
|
||
function buildQuery(params) {
|
||
const qs = Object.keys(params || {})
|
||
.filter(k => params[k] !== '' && params[k] !== undefined && params[k] !== null)
|
||
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
|
||
.join('&');
|
||
return qs ? '?' + qs : '';
|
||
}
|
||
|
||
// 1) 动作列表(支持多维筛选与排序)
|
||
function listExercises(params = {}) {
|
||
return request({ url: '/api/exercises' + buildQuery(params) }).then((res) => {
|
||
if (res && res.items) res.items = injectDisplayNames(res.items);
|
||
return res;
|
||
});
|
||
}
|
||
|
||
// 2) 动作详情
|
||
function getExercise(id) {
|
||
return request({ url: '/api/exercises/' + encodeURIComponent(id) }).then(injectDisplayName);
|
||
}
|
||
|
||
// 3) 分类维度枚举
|
||
function getCategories(type) {
|
||
return request({ url: '/api/categories/' + encodeURIComponent(type) });
|
||
}
|
||
|
||
// 4) 智能推荐
|
||
function recommend(params = {}) {
|
||
return request({ url: '/api/recommend' + buildQuery(params) }).then((res) => {
|
||
if (res && res.items) res.items = injectDisplayNames(res.items);
|
||
return res;
|
||
});
|
||
}
|
||
|
||
// 5) 训练组合列表
|
||
function listCollections() {
|
||
return request({ url: '/api/collections' });
|
||
}
|
||
|
||
// 5) 训练组合下的动作
|
||
function getCollectionExercises(slug, params = {}) {
|
||
return request({ url: '/api/collections/' + encodeURIComponent(slug) + '/exercises' + buildQuery(params) }).then((res) => {
|
||
if (res && res.items) res.items = injectDisplayNames(res.items);
|
||
return res;
|
||
});
|
||
}
|
||
|
||
// 6) 搜索
|
||
function search(q) {
|
||
return request({ url: '/api/search?q=' + encodeURIComponent(q) }).then((res) => {
|
||
if (res && res.items) res.items = injectDisplayNames(res.items);
|
||
return res;
|
||
});
|
||
}
|
||
|
||
// 7) 统计概览
|
||
function getStats() {
|
||
return request({ url: '/api/stats' });
|
||
}
|
||
|
||
module.exports = {
|
||
listExercises,
|
||
getExercise,
|
||
getCategories,
|
||
recommend,
|
||
listCollections,
|
||
getCollectionExercises,
|
||
search,
|
||
getStats
|
||
};
|