Files
danyang-match/miniapp/pages/matchmaker-service-detail/matchmaker-service-detail.js
T

96 lines
3.9 KiB
JavaScript

const api = require('../../utils/api');
Page({
data: {
serviceId: '',
detail: null,
source: 'assignment',
statusLabel: '',
followups: [],
followupTypes: ['followup', '需求了解', '候选建议', '活动协助', '其他'],
loading: true,
submitting: false,
changingStatus: false,
form: { followupType: 'followup', content: '', nextFollowupAt: '' },
},
onLoad(options) {
const serviceId = options && options.id;
if (!serviceId) {
wx.showToast({ title: '服务记录不存在', icon: 'none' });
wx.navigateBack();
return;
}
this.setData({ serviceId, source: options.source === 'management' ? 'management' : 'assignment' });
this.loadDetail();
},
onShow() {
if (this.data.serviceId && !this.data.loading) this.loadDetail();
},
tokenHeaders() {
const token = wx.getStorageSync('operatorToken');
return token ? { Authorization: `Bearer ${token}` } : {};
},
async loadDetail() {
this.setData({ loading: true });
try {
const path = this.data.source === 'management' ? '/operator/matchmaker/management/services/' : '/operator/matchmaker/services/';
const detail = await api.get(`${path}${encodeURIComponent(this.data.serviceId)}`, {}, this.tokenHeaders());
const labels = { submitted: '待处理', assigned: '已接单', in_service: '服务中', waiting_user: '待用户确认', completed: '已完成' };
this.setData({ detail: detail || null, statusLabel: labels[detail && detail.status] || (detail && detail.status) || '', followups: (detail && detail.followups) || [] });
} catch (error) {
if (error && (error.code === 'FORBIDDEN' || error.code === 'NOT_FOUND' || error.code === 'DATA_SCOPE_DENIED')) {
wx.showModal({ title: '服务权限已变更', content: '该服务对象已结束或不再属于当前服务范围。', showCancel: false,
success: () => wx.navigateBack() });
} else {
wx.showToast({ title: (error && error.message) || '加载失败', icon: 'none' });
}
} finally {
this.setData({ loading: false });
}
},
async changeStatus(e) {
const action = e.currentTarget.dataset.action;
if (this.data.changingStatus || this.data.source === 'management') return;
this.setData({ changingStatus: true });
try {
await api.post(`/operator/matchmaker/services/${encodeURIComponent(this.data.serviceId)}/${action}`, {}, this.tokenHeaders());
wx.showToast({ title: action === 'start' ? '服务已开始' : '服务已完成', icon: 'success' });
await this.loadDetail();
} catch (err) {
wx.showToast({ title: err.message || '操作失败', icon: 'none' });
} finally {
this.setData({ changingStatus: false });
}
},
onTypeChange(e) { this.setData({ 'form.followupType': this.data.followupTypes[Number(e.detail.value)] || 'followup' }); },
onContentInput(e) { this.setData({ 'form.content': e.detail.value }); },
onNextFollowupInput(e) { this.setData({ 'form.nextFollowupAt': e.detail.value }); },
async submitFollowup() {
if (this.data.submitting) return;
const content = (this.data.form.content || '').trim();
if (!content) { wx.showToast({ title: '请填写服务摘要', icon: 'none' }); return; }
this.setData({ submitting: true });
try {
await api.post(`/operator/matchmaker/services/${this.data.serviceId}/followup`, {
followupType: this.data.form.followupType,
content,
nextFollowupAt: this.data.form.nextFollowupAt || undefined,
}, this.tokenHeaders(), api.idempotencyKey('matchmaker-followup', this.data.serviceId));
wx.showToast({ title: '已记录服务', icon: 'success' });
this.setData({ 'form.content': '', 'form.nextFollowupAt': '' });
await this.loadDetail();
} catch (error) {
wx.showToast({ title: (error && error.message) || '提交失败,请重试', icon: 'none' });
} finally {
this.setData({ submitting: false });
}
},
});