Browse Source

feat(frontend): 专项调研页适配 v2 契约 - 按 code 拉取问卷 + 提交选项 id (survey-rework)

- survey.vue:loadQuestions 改调 getProblemSurveyQuestions(code),后端未绑定模板时返回内置默认问卷(不再依赖 getActiveTemplates 匹配);toggleOpt 按选项 id 选择;submit 统一 [{questionId, optionIds}]
- flow-intro.vue:surveyTemplateId 取值修正(后端返回平铺字段,兼容嵌套 surveyTemplate.id)
- api.js:新增 getProblemSurveyQuestions(code)

Ultraworked with [Sisyphus](https://github.com/OhMyOpenCode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
E2E Test Bot 2 months ago
parent
commit
8eab210678

+ 1 - 1
cfc-frontend/pages/problem/flow-intro.vue

@@ -84,7 +84,7 @@ export default {
             } else {
               self.steps = self.getDefaultSteps()
             }
-            self.surveyTemplateId = found.survey_template_id || ''
+            self.surveyTemplateId = found.surveyTemplateId || (found.surveyTemplate && found.surveyTemplate.id) || ''
           } else {
             self.steps = self.getDefaultSteps()
           }

+ 25 - 39
cfc-frontend/pages/problem/survey.vue

@@ -32,7 +32,7 @@
 </template>
 
 <script>
-import { submitProblemSurvey } from '../../utils/api.js'
+import { submitProblemSurvey, getProblemSurveyQuestions } from '../../utils/api.js'
 
 export default {
   data() {
@@ -53,24 +53,31 @@ export default {
   methods: {
     getQKey: function(qi) { return 'q' + qi },
     getOptKey: function(qi, oi) { return 'o' + qi + '_' + oi },
+    getOptId: function(q, oi) {
+      var opt = q.options && q.options[oi]
+      return opt ? (opt.id != null ? opt.id : oi) : oi
+    },
     isSelected(qi, oi) {
       var ans = this.answers[qi]
-      return ans && ans.indexOf(oi) !== -1
+      if (!ans) return false
+      var q = this.questions[qi]
+      return ans.indexOf(this.getOptId(q, oi)) !== -1
     },
     toggleOpt(qi, oi, q) {
+      var optId = this.getOptId(q, oi)
       var isMulti = q.multi !== false
       if (!this.answers[qi]) {
         this.answers[qi] = []
       }
       var arr = this.answers[qi]
-      var idx = arr.indexOf(oi)
+      var idx = arr.indexOf(optId)
       if (idx !== -1) {
         arr.splice(idx, 1)
       } else {
         if (isMulti) {
-          arr.push(oi)
+          arr.push(optId)
         } else {
-          this.answers[qi] = [oi]
+          this.answers[qi] = [optId]
         }
       }
       this.$forceUpdate()
@@ -79,30 +86,15 @@ export default {
       var self = this
       self.loading = true
       uni.showLoading({ title: '加载中...', mask: true })
-      // 从模板ID加载问卷,或通过问题域获取模板
-      var api = require('../../utils/api.js')
-      // 尝试加载模板列表找到匹配的
-      var getSurveyTemplates = api.getSurveyTemplateList || api.getActiveTemplates
-      if (getSurveyTemplates && self.templateId) {
-        getSurveyTemplates().then(function(res) {
+      // 优先按问题域 code 拉取问卷(后端未绑定模板时返回内置默认问卷)
+      if (self.code) {
+        getProblemSurveyQuestions(self.code).then(function(res) {
           uni.hideLoading()
           self.loading = false
-          var list = res && res.data
-          if (Array.isArray(list)) {
-            var found = null
-            for (var i = 0; i < list.length; i++) {
-              var item = list[i]
-              if (item.id == self.templateId || item.id === self.templateId) {
-                found = item
-                break
-              }
-            }
-            if (found && found.questionsJson) {
-              var parsed = typeof found.questionsJson === 'string' ? JSON.parse(found.questionsJson) : found.questionsJson
-              self.questions = Array.isArray(parsed) ? parsed : []
-            } else {
-              self.questions = self.getFallbackQuestions()
-            }
+          var data = res && res.data
+          if (data && Array.isArray(data.questions) && data.questions.length > 0) {
+            self.questions = data.questions
+            if (data.templateId) self.templateId = data.templateId
           } else {
             self.questions = self.getFallbackQuestions()
           }
@@ -119,8 +111,8 @@ export default {
     },
     getFallbackQuestions() {
       return [
-        { text: '请简单描述你的情况', options: [{ text: '了解中' }, { text: '已有初步了解' }], multi: false },
-        { text: '你希望获得什么帮助?', options: [{ text: '了解方案' }, { text: '购买产品' }, { text: '咨询专家' }], multi: true }
+        { id: 'q1', text: '请简单描述你的情况', multi: false, options: [{ id: 'o1', text: '了解中' }, { id: 'o2', text: '已有初步了解' }] },
+        { id: 'q2', text: '你希望获得什么帮助?', multi: true, options: [{ id: 'o1', text: '了解方案' }, { id: 'o2', text: '购买产品' }, { id: 'o3', text: '咨询专家' }] }
       ]
     },
     submitSurvey() {
@@ -141,28 +133,22 @@ export default {
       self.submitting = true
       uni.showLoading({ title: '提交中...', mask: true })
 
+      // 统一契约: [{questionId, optionIds}]
       var answerList = []
       for (var qi = 0; qi < this.questions.length; qi++) {
         var q = this.questions[qi]
         var selected = this.answers[qi] || []
         if (selected.length > 0) {
-          var texts = []
-          for (var si = 0; si < selected.length; si++) {
-            var opt = q.options[selected[si]]
-            if (opt) {
-              texts.push(opt.text)
-            }
-          }
           answerList.push({
-            questionText: q.text,
-            optionTexts: texts
+            questionId: q.id != null ? q.id : ('q' + (qi + 1)),
+            optionIds: selected
           })
         }
       }
 
       submitProblemSurvey({
         problemDomainCode: self.code,
-        templateId: self.templateId || undefined,
+        surveyTemplateId: self.templateId || undefined,
         answers: answerList
       }).then(function(res) {
         uni.hideLoading()

+ 2 - 0
cfc-frontend/utils/api.js

@@ -2706,6 +2706,8 @@ export const getNotificationList = () => request('/api/notification/list', 'POST
 export const readNotification = (data) => request('/api/notification/read', 'POST', data)
 // 专项调研
 export const submitProblemSurvey = (data) => request('/api/problem-survey/submit', 'POST', data)
+// 获取专项调研问卷(按问题域 code,未绑定模板时后端返回内置默认问卷)
+export const getProblemSurveyQuestions = (problemDomainCode) => request('/api/problem-survey/questions', 'POST', { problemDomainCode })
 // 套餐详情
 export const getProblemPackageDetail = (packageId) => request('/api/problem-package/detail', 'POST', { packageId })
 // 方案确认(生成方案任务 + 推进旅程 stage=6,packageId 可省略)