Files
sundynix-pets/pets-fe/pages/ai/ai.js
T
Blizzard 5b2f8e50bc fix: 带 date 查任务时区错位导致重复生成 + 评论弹窗重做 + AI 每日次数上限
## 首页添加的任务不显示(根因比表象严重)

time.Parse("2006-01-02", q) 返回的是 UTC 时间,dayStart 又保留了
t.Location(),于是带 ?date= 查询时:
  查询区间 = 07-29 00:00 UTC ~ 次日 = CST 的 08:00 ~ 次日 08:00
  已有任务的 task_date 是 07-29 00:00 CST,落在区间外

后果不只是「新任务不显示」——ensureDayTasks 因此认为今天没有任务,
每刷一次首页就重新生成一批。用户手动加的那条(00:00 CST)永远不在
那个错位窗口里,所以管理页看得到、首页看不到。

全项目 7 处 time.Parse 日期解析统一换成 ParseInLocation + time.Local
(任务、日计划、生日、到家日期、提醒到期日都受影响)。
实测:加一条后带 date 查得到 4 条,连查 4 次仍是 4 条不再增长。

## 评论弹窗

- 改成居中弹出。评论以输入为主,贴底弹层会被键盘顶掉大半屏
- 单行 input 换成自动撑高的 textarea,最多 500 字,带字数
- 支持配图,最多 3 张(评论表加 images 字段)
- 空评论原来会直接把弹层关掉,看起来像发成功了,改成明确提示
- 发完就地刷新列表,不再关闭弹层——连着回复更顺

## AI 每日次数上限

AI 调用是真金白银,不设上限等于把钱包交给用户。新增按「用户 + 自然日
+ 功能」计的额度,后台「社区运营」页可配问问 AI / 异常评估 / AI 计划
三档,改完立即生效。

两个刻意的设计:
- 扣额度在请求大模型之前,失败也算用掉一次。否则刷接口空转照样烧钱
- 配置读不到时回落到保守默认值,绝不「读不到就不限制」——那正是配置
  出问题时最不该发生的事

超限返回业务码 42900,小程序端明确说明「明天 0 点恢复」,不当成网络
错误让用户反复重试。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 15:50:38 +08:00

112 lines
3.5 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const { toastErr } = require('../../utils/ui.js');
const SESSION = 'chat';
const PRESETS = ['猫咪今天吐了一次怎么办?', '幼猫多久驱虫一次?', '换粮后软便怎么办?', '疫苗打完要注意什么?'];
// 打字机:每帧吐几个字。太快没效果,太慢让人等,30ms/2 字接近正常朗读速度。
const TYPE_STEP = 2;
const TYPE_INTERVAL = 30;
Page({
data: {
pet: {},
messages: [],
input: '',
presets: PRESETS,
sending: false,
loaded: false,
},
onLoad() {
this._unsub = store.subscribe((pet) => this.setData({ pet }));
store
.ready()
.then(() => {
this.setData({ pet: store.getPet() });
this.loadHistory();
})
.catch((e) => toastErr(e));
},
onUnload() {
if (this._unsub) this._unsub();
this.stopTyping();
},
loadHistory() {
const opening = {
role: 'ai',
text: '我是' + (store.getPet().name || '毛孩子') + '的养宠助手。我会优先根据档案、计划和历史记录回答,而不是泛泛聊天。',
};
api
.aiMessages(SESSION, 50)
.then((list) => {
const history = (list || []).map((m) => ({ role: m.role === 'user' ? 'user' : 'ai', text: m.text }));
this.setData({ messages: [opening].concat(history), loaded: true }, () => this.scrollToBottom());
})
.catch(() => this.setData({ messages: [opening], loaded: true }));
},
onInput(e) {
this.setData({ input: e.detail.value });
},
onSend(e) {
const preset = e.currentTarget && e.currentTarget.dataset.text;
const text = (preset || this.data.input || '').trim();
if (!text || this.data.sending) return;
// 先把用户消息和一个空的 AI 气泡放上去,AI 气泡随后逐字填充
const messages = this.data.messages.concat([
{ role: 'user', text },
{ role: 'ai', text: '', pending: true },
]);
this.setData({ messages, input: '', sending: true }, () => this.scrollToBottom());
api
.aiChat({ pet_id: store.currentPetId() || null, session: SESSION, text })
.then((res) => this.typewrite(res.reply || '(没有返回内容)'))
.catch((e) => {
const msg = (e && e.message) || '网络异常,请稍后再试。';
// 额度用完不是故障,别让用户以为是网络问题反复重试
this.typewrite(msg.indexOf('次数') >= 0 ? msg + '\n\n每天的次数是为了控制成本,明天 0 点恢复。' : msg);
});
},
// 逐字把回复填进最后一个气泡
typewrite(full) {
this.stopTyping();
const idx = this.data.messages.length - 1;
let n = 0;
this._timer = setInterval(() => {
n = Math.min(full.length, n + TYPE_STEP);
this.setData({ [`messages[${idx}].text`]: full.slice(0, n) });
if (n % 20 === 0) this.scrollToBottom();
if (n >= full.length) {
this.stopTyping();
this.setData({ [`messages[${idx}].pending`]: false, sending: false }, () => this.scrollToBottom());
}
}, TYPE_INTERVAL);
},
stopTyping() {
if (this._timer) {
clearInterval(this._timer);
this._timer = null;
}
},
// 打字过程中点一下可以跳过,不用干等
onSkip() {
if (!this.data.sending) return;
this.stopTyping();
this.setData({ sending: false });
},
scrollToBottom() {
this.setData({ scrollInto: 'chat-bottom' });
},
onShareAppMessage() {
return { title: '肉垫计划 · AI 养宠助手', path: '/pages/ai/ai' };
},
});