self-check.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <template>
  2. <view class="sc-page" :style="{ '--font-scale': fontScale }">
  3. <!-- 顶部说明 -->
  4. <view class="sc-hero">
  5. <text class="sc-hero-title">五维家庭自检</text>
  6. <text class="sc-hero-sub">15题 · 约5分钟 · 生成五行相生寻源建议</text>
  7. </view>
  8. <!-- 加载 -->
  9. <view v-if="loading" class="sc-loading">
  10. <text class="sc-loading-text">加载中...</text>
  11. </view>
  12. <template v-else>
  13. <!-- 维度导航 -->
  14. <view class="sc-dim-nav">
  15. <view v-for="dim in dimensions" :key="dim.code" class="sc-dim-nav-item">
  16. <view class="sc-dim-nav-dot" :style="{ background: dim.color }">
  17. <text class="sc-dim-nav-char">{{ dim.name }}</text>
  18. </view>
  19. <view class="sc-dim-nav-dots">
  20. <view
  21. v-for="q in dim.questions"
  22. :key="q.id"
  23. class="sc-dim-nav-dot-mini"
  24. :class="{ done: answers[q.id] }"
  25. :style="answers[q.id] ? 'background:' + dim.color : 'background:transparent'"
  26. ></view>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- 进度条 -->
  31. <view class="sc-progress-row">
  32. <text class="sc-progress-text">已完成 {{ answeredCount }}/{{ questions.length }} 题</text>
  33. <text class="sc-progress-pct">{{ progress }}%</text>
  34. </view>
  35. <view class="sc-progress-bar">
  36. <view class="sc-progress-fill" :style="{ width: progress + '%' }"></view>
  37. </view>
  38. <!-- 题目分组(按维度) -->
  39. <view v-for="dim in dimensions" :key="dim.code" class="sc-section">
  40. <view class="sc-section-header" :style="{ borderLeftColor: dim.color }">
  41. <text class="sc-section-name">{{ dim.name }}</text>
  42. <text class="sc-section-element" :style="{ color: dim.color }">{{ dim.element }}</text>
  43. <text class="sc-section-slogan">{{ dim.slogan }}</text>
  44. <text class="sc-section-count">{{ dim.answeredCount }}/{{ dim.questions.length }}</text>
  45. </view>
  46. <view v-for="q in dim.questions" :key="q.id" class="sc-question">
  47. <text class="sc-q-text">{{ q.text }}</text>
  48. <view class="sc-opts">
  49. <view
  50. v-for="opt in q.options"
  51. :key="opt.value"
  52. class="sc-opt"
  53. :class="{ 'sc-opt-sel': answers[q.id] === opt.value }"
  54. @click="selectOption(q.id, opt.value)"
  55. >
  56. <text class="sc-opt-label">{{ opt.label }}</text>
  57. <view class="sc-opt-radio" :class="{ 'sc-opt-radio-on': answers[q.id] === opt.value }"></view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <!-- 提交 -->
  63. <view class="sc-footer">
  64. <button class="sc-submit-btn" :disabled="!canSubmit || submitLoading" @click="submit">
  65. {{ submitLoading ? '提交中...' : '提交自检' }}
  66. </button>
  67. <text class="sc-submit-tip" v-if="!canSubmit && !submitLoading">至少完成10题,且每个维度至少回答1题</text>
  68. </view>
  69. </template>
  70. </view>
  71. </template>
  72. <script>
  73. import { BrandColors } from '../../utils/brand-colors.js'
  74. import { getSelfCheckQuestions, submitSelfCheck } from '@/utils/api'
  75. import fontScaleMixin from '@/mixins/font-scale.js'
  76. export default {
  77. mixins: [fontScaleMixin],
  78. data() {
  79. return {
  80. loading: true,
  81. submitLoading: false,
  82. isRetake: false,
  83. questions: [],
  84. answers: {},
  85. dimMeta: {
  86. body: { name: '身', element: '土', color: BrandColors.body, slogan: '主动健康' },
  87. wisdom: { name: '智', element: '金', color: BrandColors.wisdom, slogan: '面向未来' },
  88. wealth: { name: '富', element: '水', color: BrandColors.wealth, slogan: '利他创富' },
  89. action: { name: '行', element: '木', color: BrandColors.success, slogan: '人际和谐' },
  90. mind: { name: '心', element: '火', color: BrandColors.mind, slogan: '大爱相随' }
  91. }
  92. }
  93. },
  94. computed: {
  95. dimensions: function() {
  96. var order = ['body', 'wisdom', 'wealth', 'action', 'mind']
  97. var meta = this.dimMeta
  98. var result = []
  99. for (var i = 0; i < order.length; i++) {
  100. var code = order[i]
  101. var dimQuestions = this.questions.filter(function(q) { return q.dimension === code })
  102. if (dimQuestions.length === 0) {
  103. continue
  104. }
  105. var self = this
  106. var answered = dimQuestions.filter(function(q) { return self.answers[q.id] }).length
  107. var m = meta[code] || {}
  108. result.push({
  109. code: code,
  110. name: m.name || dimQuestions[0].dimensionName || code,
  111. element: m.element || dimQuestions[0].element || '',
  112. color: m.color || dimQuestions[0].color || BrandColors.primary,
  113. slogan: m.slogan || '',
  114. questions: dimQuestions,
  115. answeredCount: answered
  116. })
  117. }
  118. return result
  119. },
  120. answeredCount: function() {
  121. return Object.keys(this.answers).length
  122. },
  123. progress: function() {
  124. if (!this.questions.length) {
  125. return 0
  126. }
  127. return Math.round(this.answeredCount / this.questions.length * 100)
  128. },
  129. canSubmit: function() {
  130. if (this.answeredCount < 10) {
  131. return false
  132. }
  133. var dims = this.dimensions
  134. for (var i = 0; i < dims.length; i++) {
  135. if (dims[i].answeredCount < 1) {
  136. return false
  137. }
  138. }
  139. return true
  140. }
  141. },
  142. methods: {
  143. loadQuestions: function() {
  144. var self = this
  145. var apiCall = this.isRetake ? getSelfCheckQuestions({ retake: true }) : getSelfCheckQuestions()
  146. apiCall.then(function(res) {
  147. self.loading = false
  148. if (res.code === 200 && res.data) {
  149. self.questions = res.data || []
  150. } else {
  151. uni.showToast({ title: (res && res.message) || '加载失败', icon: 'none' })
  152. }
  153. }).catch(function() {
  154. self.loading = false
  155. uni.showToast({ title: '网络异常,请重试', icon: 'none' })
  156. })
  157. },
  158. selectOption: function(questionId, value) {
  159. var next = {}
  160. var keys = Object.keys(this.answers)
  161. for (var i = 0; i < keys.length; i++) {
  162. next[keys[i]] = this.answers[keys[i]]
  163. }
  164. next[questionId] = value
  165. this.answers = next
  166. },
  167. submit: function() {
  168. var self = this
  169. if (!this.canSubmit || this.submitLoading) {
  170. return
  171. }
  172. this.submitLoading = true
  173. var answersList = []
  174. var ids = Object.keys(this.answers)
  175. for (var i = 0; i < ids.length; i++) {
  176. answersList.push({ questionId: Number(ids[i]), answer: this.answers[ids[i]] })
  177. }
  178. submitSelfCheck({ answers: answersList }).then(function(res) {
  179. self.submitLoading = false
  180. if (res.code === 200 && res.data) {
  181. uni.setStorageSync('lastSelfCheckResult', res.data)
  182. uni.navigateTo({ url: '/pages/family/self-check-result?id=' + res.data.id })
  183. } else {
  184. uni.showToast({ title: (res && res.message) || '提交失败', icon: 'none' })
  185. }
  186. }).catch(function() {
  187. self.submitLoading = false
  188. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  189. })
  190. }
  191. },
  192. onLoad: function(options) {
  193. if (options && options.retake === '1') {
  194. this.isRetake = true
  195. }
  196. this.loadQuestions()
  197. }
  198. }
  199. </script>
  200. <style scoped lang="scss">
  201. .sc-page {
  202. min-height: 100vh;
  203. background: $facade-fill-blue-ghost;
  204. padding: 24rpx $uni-space-page 120rpx;
  205. box-sizing: border-box;
  206. }
  207. .sc-hero {
  208. padding: $uni-space-lg 10rpx 20rpx;
  209. }
  210. .sc-hero-title {
  211. display: block;
  212. font-size: calc(40rpx * var(--font-scale, 1));
  213. font-weight: 700;
  214. color: $facade-text-strong;
  215. }
  216. .sc-hero-sub {
  217. display: block;
  218. font-size: calc(24rpx * var(--font-scale, 1));
  219. color: $facade-text-muted;
  220. margin-top: 8rpx;
  221. }
  222. .sc-loading {
  223. display: flex;
  224. justify-content: center;
  225. align-items: center;
  226. height: 50vh;
  227. }
  228. .sc-loading-text {
  229. font-size: calc(28rpx * var(--font-scale, 1));
  230. color: $facade-text-muted;
  231. }
  232. /* 维度导航 */
  233. .sc-dim-nav {
  234. display: flex;
  235. justify-content: space-between;
  236. background: $app-surface;
  237. border-radius: 24rpx;
  238. padding: 24rpx 20rpx;
  239. margin-bottom: 24rpx;
  240. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  241. }
  242. .sc-dim-nav-item {
  243. display: flex;
  244. flex-direction: column;
  245. align-items: center;
  246. }
  247. .sc-dim-nav-dot {
  248. width: 56rpx;
  249. height: 56rpx;
  250. border-radius: 50%;
  251. display: flex;
  252. align-items: center;
  253. justify-content: center;
  254. }
  255. .sc-dim-nav-char {
  256. color: $app-surface;
  257. font-size: calc(26rpx * var(--font-scale, 1));
  258. font-weight: 600;
  259. }
  260. .sc-dim-nav-dots {
  261. display: flex;
  262. margin-top: 10rpx;
  263. }
  264. .sc-dim-nav-dot-mini {
  265. width: 12rpx;
  266. height: 12rpx;
  267. border-radius: 50%;
  268. background: $facade-gray-200;
  269. margin: 0 4rpx;
  270. }
  271. .sc-dim-nav-dot-mini.done {
  272. background: $brand-primary;
  273. }
  274. /* 进度 */
  275. .sc-progress-row {
  276. display: flex;
  277. justify-content: space-between;
  278. align-items: center;
  279. margin-bottom: 10rpx;
  280. }
  281. .sc-progress-text {
  282. font-size: calc(24rpx * var(--font-scale, 1));
  283. color: $facade-text-body;
  284. }
  285. .sc-progress-pct {
  286. font-size: calc(24rpx * var(--font-scale, 1));
  287. color: $brand-primary;
  288. font-weight: 600;
  289. }
  290. .sc-progress-bar {
  291. height: 10rpx;
  292. background: $facade-fill-4;
  293. border-radius: 8rpx;
  294. margin-bottom: $uni-space-lg;
  295. overflow: hidden;
  296. }
  297. .sc-progress-fill {
  298. height: 100%;
  299. background: linear-gradient(90deg, $brand-primary, $dim-body);
  300. border-radius: 8rpx;
  301. transition: width 0.3s ease;
  302. }
  303. /* 维度分组 */
  304. .sc-section {
  305. margin-bottom: $uni-space-lg;
  306. }
  307. .sc-section-header {
  308. display: flex;
  309. align-items: center;
  310. background: $app-surface;
  311. border-left: 8rpx solid $brand-primary;
  312. border-radius: 16rpx;
  313. padding: 20rpx 24rpx;
  314. margin-bottom: 16rpx;
  315. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.04);
  316. }
  317. .sc-section-name {
  318. font-size: calc(32rpx * var(--font-scale, 1));
  319. font-weight: 700;
  320. color: $facade-text-strong;
  321. margin-right: 12rpx;
  322. }
  323. .sc-section-element {
  324. font-size: calc(26rpx * var(--font-scale, 1));
  325. font-weight: 600;
  326. margin-right: 16rpx;
  327. }
  328. .sc-section-slogan {
  329. flex: 1;
  330. font-size: calc(24rpx * var(--font-scale, 1));
  331. color: $facade-text-muted;
  332. }
  333. .sc-section-count {
  334. font-size: calc(24rpx * var(--font-scale, 1));
  335. color: $brand-primary;
  336. font-weight: 600;
  337. }
  338. /* 题目卡片 */
  339. .sc-question {
  340. background: $app-surface;
  341. border-radius: 24rpx;
  342. padding: $uni-space-lg 26rpx 26rpx;
  343. margin-bottom: 16rpx;
  344. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  345. }
  346. .sc-q-text {
  347. display: block;
  348. font-size: calc(30rpx * var(--font-scale, 1));
  349. font-weight: 500;
  350. color: $facade-text-strong;
  351. line-height: 1.5;
  352. }
  353. .sc-opts {
  354. margin-top: 24rpx;
  355. }
  356. .sc-opt {
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. padding: 22rpx 24rpx;
  361. border: 2rpx solid $facade-gray-200;
  362. border-radius: 16rpx;
  363. margin-bottom: 14rpx;
  364. transition: border-color 0.2s, background-color 0.2s;
  365. }
  366. .sc-opt-sel {
  367. border-color: $brand-primary;
  368. background: $uni-primary-pale;
  369. }
  370. .sc-opt-label {
  371. flex: 1;
  372. font-size: calc(28rpx * var(--font-scale, 1));
  373. color: $facade-text-strong;
  374. line-height: 1.4;
  375. }
  376. .sc-opt-radio {
  377. width: 36rpx;
  378. height: 36rpx;
  379. border: 2rpx solid $app-border-strong;
  380. border-radius: 50%;
  381. margin-left: 20rpx;
  382. box-sizing: border-box;
  383. }
  384. .sc-opt-radio-on {
  385. border: 10rpx solid $brand-primary;
  386. }
  387. /* 提交 */
  388. .sc-footer {
  389. margin-top: 20rpx;
  390. }
  391. .sc-submit-btn {
  392. height: 92rpx;
  393. line-height: 92rpx;
  394. background: linear-gradient(135deg, $brand-primary, $dim-body);
  395. color: $app-surface;
  396. font-size: calc(32rpx * var(--font-scale, 1));
  397. font-weight: 600;
  398. border-radius: 24rpx;
  399. border: none;
  400. text-align: center;
  401. }
  402. .sc-submit-btn[disabled] {
  403. opacity: 0.5;
  404. }
  405. .sc-submit-tip {
  406. display: block;
  407. text-align: center;
  408. font-size: calc(24rpx * var(--font-scale, 1));
  409. color: $facade-text-muted;
  410. margin-top: 16rpx;
  411. }
  412. </style>