init: 毛孩子计划 小程序 + Go 后端 + 内嵌后台

- pets-fe: 微信原生小程序(首页/计划/记录/报告/社区/引导),
  服务端驱动、无假数据;弹层改用 scroll-view,打开时隐藏自定义 tabBar
- pets-be: Gin + GORM(MySQL, sundynix_ 前缀) + MinIO,统一响应/分页,
  微信 code2session 登录,provider-neutral AI(DeepSeek),go:embed React 后台
- 修复:分段选择类型不匹配(字符串 vs 数字)导致选不中

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-03 15:33:31 +08:00
commit 609f7d06cf
180 changed files with 15259 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
const { request, uploadFile, setToken } = require('./request.js');
// wx.login 取 code
function wxLogin() {
return new Promise((resolve, reject) => {
wx.login({
success: (r) => (r.code ? resolve(r.code) : reject(new Error('wx.login 未返回 code'))),
fail: reject,
});
});
}
// 登录:优先微信 code2session,失败回退开发态 Mock 登录(后端 dev_login=true
async function login() {
try {
const code = await wxLogin();
const data = await request({ url: '/api/auth/wechat', method: 'POST', data: { code } });
setToken(data.token);
return data;
} catch (e) {
console.warn('[api] 微信登录失败,回退 Mock 登录:', e && e.message);
const data = await request({ url: '/api/auth/login', method: 'POST', data: { nickname: '开发用户' } });
setToken(data.token);
return data;
}
}
const api = {
login,
uploadFile,
// 用户
getProfile: () => request({ url: '/api/user/profile' }),
updateProfile: (body) => request({ url: '/api/user/profile', method: 'PUT', data: body }),
// 用户
userSummary: () => request({ url: '/api/user/summary' }),
// 宠物
getPets: () => request({ url: '/api/pets' }),
homeSummary: (id) => request({ url: `/api/pets/${id}/home-summary` }),
getPet: (id) => request({ url: `/api/pets/${id}` }),
createPet: (body) => request({ url: '/api/pets', method: 'POST', data: body }),
updatePet: (id, body) => request({ url: `/api/pets/${id}`, method: 'PUT', data: body }),
deletePet: (id) => request({ url: `/api/pets/${id}`, method: 'DELETE' }),
onboarding: (body) => request({ url: '/api/onboarding', method: 'POST', data: body }),
// 记录
getRecords: (id, type) =>
request({ url: `/api/pets/${id}/records${type ? `?type=${type}` : ''}` }),
createRecord: (id, body) => request({ url: `/api/pets/${id}/records`, method: 'POST', data: body }),
weightTrend: (id) => request({ url: `/api/pets/${id}/records/weight-trend` }),
// 任务
getTasks: (id) => request({ url: `/api/pets/${id}/tasks` }),
toggleTask: (taskId) => request({ url: `/api/tasks/${taskId}/toggle`, method: 'POST' }),
completeAllTasks: (id) => request({ url: `/api/pets/${id}/tasks/complete-all`, method: 'POST' }),
// 计划
getPlan: (id) => request({ url: `/api/pets/${id}/plan` }),
planCalendar: (id, month) =>
request({ url: `/api/pets/${id}/plan/calendar${month ? `?month=${month}` : ''}` }),
createAIPlan: (id, input) => request({ url: `/api/pets/${id}/ai-plan`, method: 'POST', data: { input } }),
applyAIPlan: (planId) => request({ url: `/api/ai-plan/${planId}/apply`, method: 'POST' }),
togglePlanTask: (taskId) => request({ url: `/api/plan-tasks/${taskId}/toggle`, method: 'POST' }),
// 提醒
getReminders: (id) => request({ url: `/api/pets/${id}/reminders` }),
// 报告
weeklyReport: (id) => request({ url: `/api/pets/${id}/report/weekly` }),
getBill: (id, period) => request({ url: `/api/pets/${id}/bill?period=${period || 'month'}` }),
healthSummary: (id) => request({ url: `/api/pets/${id}/health-summary` }),
getPoster: (id) => request({ url: `/api/pets/${id}/poster` }),
// 社区
listPosts: (tab, page) =>
request({ url: `/api/posts?tab=${encodeURIComponent(tab || '')}&page=${page || 1}&page_size=10` }),
createPost: (body) => request({ url: '/api/posts', method: 'POST', data: body }),
likePost: (id) => request({ url: `/api/posts/${id}/like`, method: 'POST' }),
listComments: (id) => request({ url: `/api/posts/${id}/comments` }),
createComment: (id, content) =>
request({ url: `/api/posts/${id}/comments`, method: 'POST', data: { content } }),
// 文章
listArticles: () => request({ url: '/api/articles' }),
getArticle: (id) => request({ url: `/api/articles/${id}` }),
// Pro
getPro: () => request({ url: '/api/pro' }),
activatePro: () => request({ url: '/api/pro/activate', method: 'POST' }),
// AI
aiChat: (body) => request({ url: '/api/ai/chat', method: 'POST', data: body }),
assessSymptom: (id, body) =>
request({ url: `/api/pets/${id}/ai/assess-symptom`, method: 'POST', data: body }),
};
module.exports = api;
+5
View File
@@ -0,0 +1,5 @@
// 后端地址。本地联调:微信开发者工具需在「详情 → 本地设置」勾选「不校验合法域名」。
// 上线时改为你的 https 域名,并在微信公众平台配置 request 合法域名。
const BASE_URL = 'http://192.168.31.4:8080';
module.exports = { BASE_URL };
+69
View File
@@ -0,0 +1,69 @@
const { BASE_URL } = require('./config.js');
const TOKEN_KEY = 'pets_token';
function getToken() {
return wx.getStorageSync(TOKEN_KEY) || '';
}
function setToken(t) {
wx.setStorageSync(TOKEN_KEY, t);
}
function clearToken() {
wx.removeStorageSync(TOKEN_KEY);
}
// 统一请求:注入 token,解析 {code,message,data},成功返回 data,失败 reject(Error)
function request({ url, method = 'GET', data, header = {} }) {
return new Promise((resolve, reject) => {
const token = getToken();
wx.request({
url: BASE_URL + url,
method,
data,
header: Object.assign(
{ 'Content-Type': 'application/json' },
token ? { Authorization: 'Bearer ' + token } : {},
header,
),
success(res) {
const body = res.data;
if (body && typeof body === 'object' && 'code' in body) {
if (body.code === 0) return resolve(body.data);
if (body.code === 40100) clearToken();
return reject(new Error(body.message || '请求失败'));
}
resolve(body);
},
fail(err) {
reject(new Error((err && err.errMsg) || '网络错误'));
},
});
});
}
// 文件上传(multipart
function uploadFile(filePath) {
return new Promise((resolve, reject) => {
const token = getToken();
wx.uploadFile({
url: BASE_URL + '/api/upload',
filePath,
name: 'file',
header: token ? { Authorization: 'Bearer ' + token } : {},
success(res) {
try {
const body = JSON.parse(res.data);
if (body.code === 0) return resolve(body.data);
reject(new Error(body.message || '上传失败'));
} catch (e) {
reject(new Error('上传响应解析失败'));
}
},
fail(err) {
reject(new Error((err && err.errMsg) || '上传失败'));
},
});
});
}
module.exports = { request, uploadFile, getToken, setToken, clearToken };
+100
View File
@@ -0,0 +1,100 @@
// 全局状态:登录、宠物列表、当前宠物;服务端驱动 + 发布订阅
const api = require('./api.js');
const state = {
user: null,
pets: [],
currentId: 0,
};
const listeners = new Set();
let bootstrapPromise = null;
function getPets() {
return state.pets;
}
function getPet() {
return state.pets.find((p) => p.id === state.currentId) || state.pets[0] || {};
}
function currentPetId() {
const p = getPet();
return p && p.id;
}
function notify() {
const pet = getPet();
listeners.forEach((fn) => {
try {
fn(pet);
} catch (e) {
/* noop */
}
});
}
function subscribe(fn) {
listeners.add(fn);
return () => listeners.delete(fn);
}
// 登录 + 拉宠物列表(只跑一次)
function ready() {
if (!bootstrapPromise) {
bootstrapPromise = (async () => {
const res = await api.login();
state.user = res.user;
await loadPets();
})().catch((e) => {
bootstrapPromise = null; // 允许重试
throw e;
});
}
return bootstrapPromise;
}
async function loadPets() {
const pets = await api.getPets();
state.pets = pets || [];
if (!state.currentId && state.pets[0]) {
state.currentId = state.pets[0].id;
}
// 当前宠物被删或不存在时回退第一只
if (!state.pets.find((p) => p.id === state.currentId) && state.pets[0]) {
state.currentId = state.pets[0].id;
}
notify();
return state.pets;
}
function switchPet(id) {
if (state.currentId === id) return;
state.currentId = id;
notify();
}
// 本地更新当前宠物字段(服务端已改动后同步 UI)
function patchCurrent(patch) {
const pet = getPet();
Object.assign(pet, patch);
notify();
}
function setUser(u) {
state.user = u;
}
function getUser() {
return state.user;
}
module.exports = {
ready,
loadPets,
getPets,
getPet,
currentPetId,
switchPet,
patchCurrent,
subscribe,
setUser,
getUser,
};