self-check.vue 11 KB

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