Sfoglia il codice sorgente

Make mini program AI report selection and submit feedback explicit

彭峰 2 giorni fa
parent
commit
ab244a53b1

+ 20 - 0
test/service-desk.test.cjs

@@ -28,3 +28,23 @@ test('只列出可举报的回复,上一条消息保持可选并限制长度',
   assert.equal(normalizeContactPhone(' 138-0000-1234 '),'13800001234');
   assert.equal(normalizeContactPhone(' 138-0000-1234 '),'13800001234');
   assert.equal(normalizeContactPhone('foo123'),'');
   assert.equal(normalizeContactPhone('foo123'),'');
 });
 });
+
+test('三个举报选择器有真实占位项,选第一条时产生从 0 到 1 的变化',()=>{
+  const {pickerChoices, selectedPickerIndex}=require('../utils/service-desk');
+  const one=[{id:'first',label:'只有一条'}];
+  assert.deepEqual(pickerChoices(one,'请选择').map(item=>item.label),['请选择','只有一条']);
+  assert.equal(selectedPickerIndex('0',one),-1);
+  assert.equal(selectedPickerIndex('1',one),0);
+  assert.equal(selectedPickerIndex('2',one),-1);
+  for(const index of [null,'',-1,1.5]) assert.equal(selectedPickerIndex(index,one),-1);
+  const markup=readFileSync(require.resolve('../yuban/pages/service-desk/index.wxml'),'utf8');
+  for(const index of ['sessionIndex','replyIndex','categoryIndex']) assert.ok(markup.includes('value="{{'+index+' + 1}}"'));
+});
+
+test('举报保存区分会话失效、服务端拒绝和结果待核对',()=>{
+  const {submitErrorMessage}=require('../utils/service-desk');
+  assert.equal(submitErrorMessage({code:209},'ai_reply_report'),'登录已失效,请重新登录后提交举报。');
+  assert.equal(submitErrorMessage({code:119},'ai_reply_report'),'当前账号暂不能提交这段对话的举报,请重新登录并重新选择自己的对话。');
+  assert.equal(submitErrorMessage({code:100},'ai_reply_report'),'提交结果待核对,请先刷新处理记录。');
+  assert.ok(!submitErrorMessage({message:'synthetic private server detail'},'ai_reply_report').includes('synthetic'));
+});

+ 31 - 2
utils/service-desk.js

@@ -15,6 +15,33 @@ const REPORT_CATEGORIES = [
   { value: 'other_harmful', label: '虚假或其他有害内容' },
   { value: 'other_harmful', label: '虚假或其他有害内容' },
 ];
 ];
 
 
+function pickerChoices(items, label) {
+  return [{ label }, ...items.map(item => ({ label: item.label }))];
+}
+
+function selectedPickerIndex(value, items) {
+  if (typeof value !== 'number' && (typeof value !== 'string' || !/^\d+$/.test(value))) return -1;
+  const index = Number(value) - 1;
+  return Number.isInteger(index) && index >= 0 && index < items.length ? index : -1;
+}
+
+const INPUT_MESSAGES = [
+  '请填写有效联系电话', '请填写联系人姓名', '请填写问题描述',
+  '请先选择自己的对话', '请先选择要举报的 AI 回复', '请选择举报类型',
+];
+
+function submitErrorMessage(error, type) {
+  if (INPUT_MESSAGES.includes(error?.message)) return error.message;
+  if (type === 'ai_reply_report') {
+    const code = Number(error?.code || error?.statusCode);
+    if (code === 209 || code === 401) return '登录已失效,请重新登录后提交举报。';
+    if (code === 119 || code === 403) return '当前账号暂不能提交这段对话的举报,请重新登录并重新选择自己的对话。';
+    if (code === 101 || code === 404) return '这段对话已不可用,请重新选择。';
+    if (code === 111 || code === 142) return '举报资料未通过服务端校验,请重新选择对话和回复后提交。';
+  }
+  return '提交结果待核对,请先刷新处理记录。';
+}
+
 function normalizeContactPhone(value) {
 function normalizeContactPhone(value) {
   const phone = String(value || '').trim().replace(/[ ()-]/g, '');
   const phone = String(value || '').trim().replace(/[ ()-]/g, '');
   return /^\+?[0-9]{7,15}$/.test(phone) ? phone : '';
   return /^\+?[0-9]{7,15}$/.test(phone) ? phone : '';
@@ -123,7 +150,9 @@ async function submitTicket(scope, type, form) {
   const content = String(form.content || '').trim().slice(0, isAI ? 500 : 2000);
   const content = String(form.content || '').trim().slice(0, isAI ? 500 : 2000);
   const phone = normalizeContactPhone(form.phone);
   const phone = normalizeContactPhone(form.phone);
   const category = REPORT_CATEGORIES.find(item => item.value === form.category);
   const category = REPORT_CATEGORIES.find(item => item.value === form.category);
-  if (isAI && (!category || !form.sessionId || !form.reply?.text)) throw Error('请选择 AI 回复和举报类型');
+  if (isAI && !form.sessionId) throw Error('请先选择自己的对话');
+  if (isAI && !form.reply?.text) throw Error('请先选择要举报的 AI 回复');
+  if (isAI && !category) throw Error('请选择举报类型');
   if (!isAI && !phone) throw Error('请填写有效联系电话');
   if (!isAI && !phone) throw Error('请填写有效联系电话');
   if (type === 'cooperation' && !name) throw Error('请填写联系人姓名');
   if (type === 'cooperation' && !name) throw Error('请填写联系人姓名');
   if (type === 'feedback' && !content) throw Error('请填写问题描述');
   if (type === 'feedback' && !content) throw Error('请填写问题描述');
@@ -162,5 +191,5 @@ async function submitTicket(scope, type, form) {
 
 
 module.exports = {
 module.exports = {
   TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
   TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
-  mapRecords, replyChoices, normalizeContactPhone,
+  mapRecords, replyChoices, normalizeContactPhone, pickerChoices, selectedPickerIndex, submitErrorMessage,
 };
 };

+ 41 - 19
yuban/pages/service-desk/index.js

@@ -1,5 +1,6 @@
 const {
 const {
   TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
   TABS, REPORT_CATEGORIES, authContext, listTickets, listConversations, listReplies, submitTicket,
+  pickerChoices, selectedPickerIndex, submitErrorMessage,
 } = require('../../../utils/service-desk');
 } = require('../../../utils/service-desk');
 const { restrictionMessage, restrictionMessageForUser } = require('../../../utils/login-restrictions');
 const { restrictionMessage, restrictionMessageForUser } = require('../../../utils/login-restrictions');
 
 
@@ -7,11 +8,12 @@ const emptyForm = () => ({ name: '', organization: '', phone: '', content: '' })
 
 
 Page({
 Page({
   data: {
   data: {
-    tabs: TABS, categories: REPORT_CATEGORIES, active: 'cooperation',
+    tabs: TABS, categories: REPORT_CATEGORIES,
+    categoryChoices: pickerChoices(REPORT_CATEGORIES, '选择举报类型'), active: 'cooperation',
     signedIn: false, signingIn: false, loading: false, loadFailed: false,
     signedIn: false, signingIn: false, loading: false, loadFailed: false,
     form: emptyForm(), records: [], submitting: false, notice: '',
     form: emptyForm(), records: [], submitting: false, notice: '',
-    conversations: [], conversationsLoading: false, conversationsFailed: false, sessionIndex: -1,
-    replies: [], repliesLoading: false, repliesFailed: false, replyIndex: -1, categoryIndex: -1,
+    conversations: [], conversationChoices: [], conversationsLoading: false, conversationsFailed: false, sessionIndex: -1,
+    replies: [], replyPickerChoices: [], repliesLoading: false, repliesFailed: false, replyIndex: -1, categoryIndex: -1,
     includePrevious: false,
     includePrevious: false,
   },
   },
 
 
@@ -21,7 +23,7 @@ Page({
     if (this.owner !== owner) {
     if (this.owner !== owner) {
       this.owner = owner;
       this.owner = owner;
       this.setData({ signedIn: !!scope, records: [], notice: '', form: emptyForm(),
       this.setData({ signedIn: !!scope, records: [], notice: '', form: emptyForm(),
-        conversations: [], replies: [], sessionIndex: -1, replyIndex: -1,
+        conversations: [], conversationChoices: [], replies: [], replyPickerChoices: [], sessionIndex: -1, replyIndex: -1,
         categoryIndex: -1, includePrevious: false, conversationsLoading: false, repliesLoading: false,
         categoryIndex: -1, includePrevious: false, conversationsLoading: false, repliesLoading: false,
         conversationsFailed: false, repliesFailed: false, loading: false, loadFailed: false });
         conversationsFailed: false, repliesFailed: false, loading: false, loadFailed: false });
     }
     }
@@ -62,7 +64,7 @@ Page({
     const active = event.currentTarget.dataset.key;
     const active = event.currentTarget.dataset.key;
     if (this.data.submitting || active === this.data.active || !TABS.some(item => item.key === active)) return;
     if (this.data.submitting || active === this.data.active || !TABS.some(item => item.key === active)) return;
     this.setData({ active, records: [], notice: '', loadFailed: false, form: emptyForm(),
     this.setData({ active, records: [], notice: '', loadFailed: false, form: emptyForm(),
-      sessionIndex: -1, replies: [], replyIndex: -1, categoryIndex: -1, includePrevious: false, repliesLoading: false, repliesFailed: false });
+      sessionIndex: -1, replies: [], replyPickerChoices: [], replyIndex: -1, categoryIndex: -1, includePrevious: false, repliesLoading: false, repliesFailed: false });
     this.onShow();
     this.onShow();
   },
   },
 
 
@@ -89,7 +91,9 @@ Page({
     this.setData({ conversationsLoading: true, conversationsFailed: false });
     this.setData({ conversationsLoading: true, conversationsFailed: false });
     try {
     try {
       const conversations = await listConversations(scope);
       const conversations = await listConversations(scope);
-      if (this.matches(scope)) this.setData({ conversations });
+      if (this.matches(scope)) this.setData({
+        conversations, conversationChoices: pickerChoices(conversations, '选择自己的近期对话'),
+      });
     } catch {
     } catch {
       if (this.matches(scope)) this.setData({ conversationsFailed: true });
       if (this.matches(scope)) this.setData({ conversationsFailed: true });
     } finally {
     } finally {
@@ -97,17 +101,22 @@ Page({
     }
     }
   },
   },
 
 
-  async selectConversation(event) {
+  selectConversation(event) {
+    return this.loadReplies(selectedPickerIndex(event.detail.value, this.data.conversations));
+  },
+
+  async loadReplies(sessionIndex) {
     const scope = authContext(getApp());
     const scope = authContext(getApp());
-    const sessionIndex = Number(event.detail.value);
+    if (!scope) return;
     const selected = this.data.conversations[sessionIndex];
     const selected = this.data.conversations[sessionIndex];
-    if (!scope || !selected) return;
     const request = this.repliesRequest = (this.repliesRequest || 0) + 1;
     const request = this.repliesRequest = (this.repliesRequest || 0) + 1;
     const current = () => this.matches(scope) && request === this.repliesRequest && sessionIndex === this.data.sessionIndex;
     const current = () => this.matches(scope) && request === this.repliesRequest && sessionIndex === this.data.sessionIndex;
-    this.setData({ sessionIndex, replies: [], replyIndex: -1, includePrevious: false, repliesLoading: true, repliesFailed: false });
+    this.setData({ sessionIndex, replies: [], replyPickerChoices: [], replyIndex: -1, includePrevious: false,
+      repliesLoading: !!selected, repliesFailed: false });
+    if (!selected) return;
     try {
     try {
       const replies = await listReplies(scope, selected.id);
       const replies = await listReplies(scope, selected.id);
-      if (current()) this.setData({ replies });
+      if (current()) this.setData({ replies, replyPickerChoices: pickerChoices(replies, '选择要举报的回复') });
     } catch {
     } catch {
       if (current()) this.setData({ repliesFailed: true });
       if (current()) this.setData({ repliesFailed: true });
     } finally {
     } finally {
@@ -115,9 +124,13 @@ Page({
     }
     }
   },
   },
 
 
-  retryReplies() { void this.selectConversation({ detail: { value: this.data.sessionIndex } }); },
-  selectReply(event) { this.setData({ replyIndex: Number(event.detail.value), includePrevious: false }); },
-  selectCategory(event) { this.setData({ categoryIndex: Number(event.detail.value) }); },
+  retryReplies() { void this.loadReplies(this.data.sessionIndex); },
+  selectReply(event) {
+    this.setData({ replyIndex: selectedPickerIndex(event.detail.value, this.data.replies), includePrevious: false });
+  },
+  selectCategory(event) {
+    this.setData({ categoryIndex: selectedPickerIndex(event.detail.value, REPORT_CATEGORIES) });
+  },
   togglePrevious(event) { this.setData({ includePrevious: event.detail.value.includes('previous') }); },
   togglePrevious(event) { this.setData({ includePrevious: event.detail.value.includes('previous') }); },
 
 
   editField(event) {
   editField(event) {
@@ -127,7 +140,12 @@ Page({
 
 
   async submit() {
   async submit() {
     const scope = authContext(getApp());
     const scope = authContext(getApp());
-    if (!scope || this.data.submitting) return;
+    if (this.data.submitting) return;
+    if (!scope) {
+      this.onShow();
+      this.setData({ notice: '登录已失效,请重新登录后提交。' });
+      return;
+    }
     const { active, form, sessionIndex, conversations, replies, replyIndex, categoryIndex, includePrevious } = this.data;
     const { active, form, sessionIndex, conversations, replies, replyIndex, categoryIndex, includePrevious } = this.data;
     this.setData({ submitting: true, notice: '' });
     this.setData({ submitting: true, notice: '' });
     try {
     try {
@@ -138,11 +156,15 @@ Page({
       if (!this.matches(scope)) return;
       if (!this.matches(scope)) return;
       this.setData({ form: emptyForm(), replyIndex: -1, categoryIndex: -1, includePrevious: false,
       this.setData({ form: emptyForm(), replyIndex: -1, categoryIndex: -1, includePrevious: false,
         notice: '提交成功,工单号 ' + ticketNo + '。处理结果可在本页查看。' });
         notice: '提交成功,工单号 ' + ticketNo + '。处理结果可在本页查看。' });
-      await this.refreshRecords();
+      void this.refreshRecords();
     } catch (error) {
     } catch (error) {
-      const message = ['请填写有效联系电话', '请填写联系人姓名', '请填写问题描述', '请选择 AI 回复和举报类型'].includes(error?.message)
-        ? error.message : '提交结果待核对,请先刷新处理记录。';
-      if (this.matches(scope)) this.setData({ notice: message });
+      if (this.closed) return;
+      if (!authContext(getApp())) {
+        this.onShow();
+        this.setData({ notice: '登录已失效,请重新登录后提交。' });
+      } else if (this.matches(scope)) {
+        this.setData({ notice: submitErrorMessage(error, active) });
+      }
     } finally {
     } finally {
       if (!this.closed) this.setData({ submitting: false });
       if (!this.closed) this.setData({ submitting: false });
     }
     }

+ 4 - 4
yuban/pages/service-desk/index.wxml

@@ -27,13 +27,13 @@
         <text wx:if="{{conversationsLoading}}" class="meta">正在读取对话…</text>
         <text wx:if="{{conversationsLoading}}" class="meta">正在读取对话…</text>
         <button wx:elif="{{conversationsFailed}}" class="secondary" bindtap="refreshConversations">重新读取对话</button>
         <button wx:elif="{{conversationsFailed}}" class="secondary" bindtap="refreshConversations">重新读取对话</button>
         <text wx:elif="{{!conversations.length}}" class="meta">暂无可举报的近期对话</text>
         <text wx:elif="{{!conversations.length}}" class="meta">暂无可举报的近期对话</text>
-        <picker wx:else mode="selector" range="{{conversations}}" range-key="label" bindchange="selectConversation" disabled="{{submitting}}">
+        <picker wx:else mode="selector" range="{{conversationChoices}}" range-key="label" value="{{sessionIndex + 1}}" bindchange="selectConversation" disabled="{{submitting}}">
           <view class="pick">{{sessionIndex < 0 ? '选择自己的近期对话' : conversations[sessionIndex].label}}</view>
           <view class="pick">{{sessionIndex < 0 ? '选择自己的近期对话' : conversations[sessionIndex].label}}</view>
         </picker>
         </picker>
         <text wx:if="{{repliesLoading}}" class="meta">正在读取回复…</text>
         <text wx:if="{{repliesLoading}}" class="meta">正在读取回复…</text>
         <button wx:elif="{{repliesFailed}}" class="secondary" bindtap="retryReplies">重新读取回复</button>
         <button wx:elif="{{repliesFailed}}" class="secondary" bindtap="retryReplies">重新读取回复</button>
         <text wx:elif="{{sessionIndex >= 0 && !replies.length}}" class="meta">这段对话暂无可选回复</text>
         <text wx:elif="{{sessionIndex >= 0 && !replies.length}}" class="meta">这段对话暂无可选回复</text>
-        <picker wx:if="{{replies.length}}" mode="selector" range="{{replies}}" range-key="label" bindchange="selectReply" disabled="{{submitting}}">
+        <picker wx:if="{{replies.length}}" mode="selector" range="{{replyPickerChoices}}" range-key="label" value="{{replyIndex + 1}}" bindchange="selectReply" disabled="{{submitting}}">
           <view class="pick">{{replyIndex < 0 ? '选择要举报的回复' : replies[replyIndex].label}}</view>
           <view class="pick">{{replyIndex < 0 ? '选择要举报的回复' : replies[replyIndex].label}}</view>
         </picker>
         </picker>
         <view wx:if="{{replyIndex >= 0}}" class="excerpt">
         <view wx:if="{{replyIndex >= 0}}" class="excerpt">
@@ -44,7 +44,7 @@
             <text wx:if="{{includePrevious}}" selectable class="multiline">{{replies[replyIndex].previous.text}}</text>
             <text wx:if="{{includePrevious}}" selectable class="multiline">{{replies[replyIndex].previous.text}}</text>
           </checkbox-group>
           </checkbox-group>
         </view>
         </view>
-        <picker mode="selector" range="{{categories}}" range-key="label" bindchange="selectCategory" disabled="{{submitting}}">
+        <picker mode="selector" range="{{categoryChoices}}" range-key="label" value="{{categoryIndex + 1}}" bindchange="selectCategory" disabled="{{submitting}}">
           <view class="pick">{{categoryIndex < 0 ? '选择举报类型' : categories[categoryIndex].label}}</view>
           <view class="pick">{{categoryIndex < 0 ? '选择举报类型' : categories[categoryIndex].label}}</view>
         </picker>
         </picker>
       </block>
       </block>
@@ -53,7 +53,7 @@
         <textarea maxlength="{{active === 'ai_reply_report' ? 500 : 2000}}" value="{{form.content}}"
         <textarea maxlength="{{active === 'ai_reply_report' ? 500 : 2000}}" value="{{form.content}}"
           data-key="content" bindinput="editField" placeholder="填写具体情况" auto-height />
           data-key="content" bindinput="editField" placeholder="填写具体情况" auto-height />
       </view>
       </view>
-      <button class="primary" bindtap="submit" loading="{{submitting}}" disabled="{{submitting}}">{{active === 'ai_reply_report' ? '提交举报' : '提交'}}</button>
+      <button class="primary" bindtap="submit" loading="{{submitting}}" disabled="{{submitting}}">{{submitting ? '提交中…' : active === 'ai_reply_report' ? '提交举报' : '提交'}}</button>
       <text wx:if="{{notice}}" selectable class="notice">{{notice}}</text>
       <text wx:if="{{notice}}" selectable class="notice">{{notice}}</text>
     </view>
     </view>