relationship-questionnaire.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <view class="container">
  3. <view class="page-header">
  4. <text class="page-title">关系问卷</text>
  5. <text class="page-subtitle" v-if="memberName">为「{{ memberName }}」评估关系质量</text>
  6. </view>
  7. <!-- 加载状态 -->
  8. <view v-if="loading" class="loading-wrap">
  9. <text class="loading-text">AI 正在生成问卷...</text>
  10. </view>
  11. <!-- 问卷表单 -->
  12. <view v-else-if="questions.length > 0" class="questionnaire-form">
  13. <view class="question-item" v-for="(q, index) in questions" :key="q.id">
  14. <view class="question-header">
  15. <text class="question-dimension dimension-tag" :class="q.dimension">{{ dimensionLabels[q.dimension] }}</text>
  16. <text class="question-text">{{ q.question }}</text>
  17. </view>
  18. <view class="option-list">
  19. <view class="option-item" v-for="(opt, oi) in q.options" :key="oi"
  20. :class="{selected: answers[q.id] === opt}"
  21. @click="selectOption(q.id, opt)">
  22. <text class="option-letter">{{ optionLetters[oi] }}</text>
  23. <text class="option-text">{{ opt }}</text>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="submit-bar">
  28. <button class="btn-submit" @click="onSubmit" :disabled="!allAnswered">提交问卷</button>
  29. </view>
  30. </view>
  31. <!-- 无问卷 -->
  32. <view v-else class="empty-wrap">
  33. <text class="empty-text">暂无可用问卷</text>
  34. <button class="btn-generate" @click="generateQuestionnaire">生成新问卷</button>
  35. </view>
  36. <!-- 结果展示 -->
  37. <view v-if="showResult" class="result-overlay" @click="showResult = false">
  38. <view class="result-card" @click.stop>
  39. <text class="result-title">关系质量评估结果</text>
  40. <view class="result-scores">
  41. <view class="result-score-item">
  42. <text class="result-score-label">信任</text>
  43. <view class="result-bar-bg">
  44. <view class="result-bar-fill trust" :style="{width: (result.trustScore || 0) + '%'}"></view>
  45. </view>
  46. <text class="result-score-value">{{ Math.round(result.trustScore || 0) }}</text>
  47. </view>
  48. <view class="result-score-item">
  49. <text class="result-score-label">亲密</text>
  50. <view class="result-bar-bg">
  51. <view class="result-bar-fill intimacy" :style="{width: (result.intimacyScore || 0) + '%'}"></view>
  52. </view>
  53. <text class="result-score-value">{{ Math.round(result.intimacyScore || 0) }}</text>
  54. </view>
  55. <view class="result-score-item">
  56. <text class="result-score-label">沟通</text>
  57. <view class="result-bar-bg">
  58. <view class="result-bar-fill communication" :style="{width: (result.communicationScore || 0) + '%'}"></view>
  59. </view>
  60. <text class="result-score-value">{{ Math.round(result.communicationScore || 0) }}</text>
  61. </view>
  62. </view>
  63. <button class="btn-close" @click="showResult = false">关闭</button>
  64. </view>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import { generateQuestionnaire, submitQuestionnaire, getQuestionnaireSnapshot } from '../../utils/api.js'
  70. export default {
  71. data() {
  72. return {
  73. memberId: null,
  74. memberName: '',
  75. snapshotId: null,
  76. questions: [],
  77. answers: {},
  78. loading: false,
  79. showResult: false,
  80. result: {},
  81. dimensionLabels: {
  82. trust: '信任',
  83. intimacy: '亲密',
  84. communication: '沟通'
  85. },
  86. optionLetters: ['A', 'B', 'C', 'D', 'E']
  87. }
  88. },
  89. computed: {
  90. allAnswered() {
  91. return this.questions.length > 0 && this.questions.every(function(q) {
  92. return this.answers[q.id]
  93. }, this)
  94. }
  95. },
  96. onLoad(options) {
  97. if (options.memberId) {
  98. this.memberId = Number(options.memberId)
  99. }
  100. this.loadSnapshot()
  101. },
  102. methods: {
  103. async loadSnapshot() {
  104. if (!this.memberId) return
  105. this.loading = true
  106. try {
  107. var res = await getQuestionnaireSnapshot(this.memberId)
  108. if (res.code === 200 && res.data) {
  109. this.snapshotId = res.data.id
  110. this.memberName = res.data.memberName
  111. try {
  112. this.questions = JSON.parse(res.data.aiGeneratedJson || '[]')
  113. } catch (e) {
  114. this.questions = []
  115. }
  116. } else {
  117. // 没有问卷,生成新的
  118. await this.generateQuestionnaire()
  119. }
  120. } catch (e) {
  121. uni.showToast({ title: '加载失败', icon: 'none' })
  122. } finally {
  123. this.loading = false
  124. }
  125. },
  126. async generateQuestionnaire() {
  127. if (!this.memberId) return
  128. this.loading = true
  129. try {
  130. var res = await generateQuestionnaire(this.memberId)
  131. if (res.code === 200 && res.data) {
  132. this.snapshotId = res.data.id
  133. this.memberName = res.data.memberName
  134. try {
  135. this.questions = JSON.parse(res.data.aiGeneratedJson || '[]')
  136. } catch (e) {
  137. this.questions = []
  138. }
  139. } else {
  140. uni.showToast({ title: res.message || '生成失败', icon: 'none' })
  141. }
  142. } catch (e) {
  143. uni.showToast({ title: '生成失败', icon: 'none' })
  144. } finally {
  145. this.loading = false
  146. }
  147. },
  148. selectOption(questionId, option) {
  149. this.answers[questionId] = option
  150. },
  151. async onSubmit() {
  152. if (!this.allAnswered || !this.snapshotId) {
  153. uni.showToast({ title: '请回答所有问题', icon: 'none' })
  154. return
  155. }
  156. try {
  157. var answersJson = JSON.stringify(this.answers)
  158. var res = await submitQuestionnaire({
  159. snapshotId: this.snapshotId,
  160. answersJson: answersJson
  161. })
  162. if (res.code === 200 && res.data) {
  163. this.result = res.data
  164. this.showResult = true
  165. } else {
  166. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  167. }
  168. } catch (e) {
  169. uni.showToast({ title: '提交失败', icon: 'none' })
  170. }
  171. }
  172. }
  173. }
  174. </script>
  175. <style scoped>
  176. .container {
  177. min-height: 100vh;
  178. background: #f5f5f5;
  179. padding: 24rpx;
  180. }
  181. .page-header {
  182. padding: 20rpx 0 30rpx;
  183. }
  184. .page-title {
  185. font-size: 36rpx;
  186. font-weight: bold;
  187. color: #333;
  188. display: block;
  189. }
  190. .page-subtitle {
  191. font-size: 26rpx;
  192. color: #999;
  193. margin-top: 8rpx;
  194. display: block;
  195. }
  196. .loading-wrap, .empty-wrap {
  197. text-align: center;
  198. padding: 100rpx 0;
  199. }
  200. .loading-text {
  201. font-size: 28rpx;
  202. color: #999;
  203. }
  204. .empty-text {
  205. font-size: 28rpx;
  206. color: #999;
  207. display: block;
  208. margin-bottom: 30rpx;
  209. }
  210. .btn-generate, .btn-submit {
  211. background: #F97316;
  212. color: #fff;
  213. border: none;
  214. border-radius: 12rpx;
  215. padding: 24rpx 60rpx;
  216. font-size: 30rpx;
  217. }
  218. .questionnaire-form {
  219. display: flex;
  220. flex-direction: column;
  221. gap: 24rpx;
  222. }
  223. .question-item {
  224. background: #fff;
  225. border-radius: 16rpx;
  226. padding: 28rpx;
  227. }
  228. .question-header {
  229. margin-bottom: 20rpx;
  230. }
  231. .dimension-tag {
  232. font-size: 22rpx;
  233. padding: 4rpx 12rpx;
  234. border-radius: 6rpx;
  235. margin-right: 12rpx;
  236. }
  237. .dimension-tag.trust { background: #D1FAE5; color: #059669; }
  238. .dimension-tag.intimacy { background: #FCE7F3; color: #DB2777; }
  239. .dimension-tag.communication { background: #E0E7FF; color: #4338CA; }
  240. .question-text {
  241. font-size: 30rpx;
  242. color: #333;
  243. line-height: 1.6;
  244. display: block;
  245. margin-top: 12rpx;
  246. }
  247. .option-list {
  248. display: flex;
  249. flex-direction: column;
  250. gap: 12rpx;
  251. }
  252. .option-item {
  253. display: flex;
  254. align-items: center;
  255. gap: 16rpx;
  256. padding: 20rpx;
  257. border: 2rpx solid #e0e0e0;
  258. border-radius: 12rpx;
  259. background: #fafafa;
  260. }
  261. .option-item.selected {
  262. border-color: #F97316;
  263. background: #FFF7ED;
  264. }
  265. .option-letter {
  266. width: 44rpx;
  267. height: 44rpx;
  268. line-height: 44rpx;
  269. text-align: center;
  270. background: #f0f0f0;
  271. border-radius: 50%;
  272. font-size: 24rpx;
  273. color: #666;
  274. }
  275. .option-item.selected .option-letter {
  276. background: #F97316;
  277. color: #fff;
  278. }
  279. .option-text {
  280. flex: 1;
  281. font-size: 28rpx;
  282. color: #333;
  283. }
  284. .submit-bar {
  285. margin-top: 20rpx;
  286. }
  287. .btn-submit {
  288. width: 100%;
  289. }
  290. .btn-submit[disabled] {
  291. background: #ccc;
  292. }
  293. .result-overlay {
  294. position: fixed;
  295. top: 0;
  296. left: 0;
  297. right: 0;
  298. bottom: 0;
  299. background: rgba(0,0,0,0.5);
  300. display: flex;
  301. align-items: center;
  302. justify-content: center;
  303. z-index: 100;
  304. }
  305. .result-card {
  306. width: 600rpx;
  307. background: #fff;
  308. border-radius: 24rpx;
  309. padding: 40rpx;
  310. }
  311. .result-title {
  312. font-size: 32rpx;
  313. font-weight: bold;
  314. color: #333;
  315. display: block;
  316. text-align: center;
  317. margin-bottom: 30rpx;
  318. }
  319. .result-scores {
  320. display: flex;
  321. flex-direction: column;
  322. gap: 20rpx;
  323. margin-bottom: 30rpx;
  324. }
  325. .result-score-item {
  326. display: flex;
  327. align-items: center;
  328. gap: 12rpx;
  329. }
  330. .result-score-label {
  331. width: 60rpx;
  332. font-size: 26rpx;
  333. color: #666;
  334. }
  335. .result-bar-bg {
  336. flex: 1;
  337. height: 16rpx;
  338. background: #f0f0f0;
  339. border-radius: 8rpx;
  340. overflow: hidden;
  341. }
  342. .result-bar-fill {
  343. height: 100%;
  344. border-radius: 8rpx;
  345. }
  346. .result-bar-fill.trust { background: #10B981; }
  347. .result-bar-fill.intimacy { background: #FF6B9D; }
  348. .result-bar-fill.communication { background: #6366F1; }
  349. .result-score-value {
  350. width: 50rpx;
  351. font-size: 26rpx;
  352. color: #333;
  353. text-align: right;
  354. }
  355. .btn-close {
  356. width: 100%;
  357. background: #F97316;
  358. color: #fff;
  359. border: none;
  360. border-radius: 12rpx;
  361. padding: 20rpx 0;
  362. font-size: 30rpx;
  363. }
  364. </style>