purchase.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">选择测评任务模板</text>
  5. <text class="subtitle">选择适合孩子的测评任务模板</text>
  6. </view>
  7. <!-- 家庭数据区 -->
  8. <view v-if="hasFamily">
  9. <!-- 成长规划师选择 -->
  10. <view class="section" v-if="!guideId">
  11. <view class="section-title">选择成长规划师</view>
  12. <view class="guide-selector">
  13. <view
  14. class="guide-item"
  15. :class="{ 'selected': selectedGuideId === guide.guideId }"
  16. v-for="guide in availableGuides"
  17. :key="guide.guideId"
  18. @click="selectGuide(guide)"
  19. >
  20. <image class="guide-avatar" :src="guide.guideAvatar || '/static/default-avatar.png'" />
  21. <view class="guide-info">
  22. <text class="guide-name">{{ guide.guideName }}</text>
  23. <text class="guide-price">{{ formatPriceWithSymbol(guide.price) }}</text>
  24. </view>
  25. <view class="guide-check" v-if="selectedGuideId === guide.guideId">✓</view>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- 任务模板列表 -->
  30. <view class="section">
  31. <view class="section-title">测评任务模板</view>
  32. <view class="package-list">
  33. <view
  34. class="package-item"
  35. :class="{ 'selected': selectedPackageId === pkg.id }"
  36. v-for="pkg in packages"
  37. :key="pkg.id"
  38. @click="selectPackage(pkg)"
  39. >
  40. <view class="package-header">
  41. <text class="package-name">{{ pkg.name }}</text>
  42. <view class="package-price">
  43. <text class="price">{{ formatPriceWithSymbol(pkg.price) }}</text>
  44. <text class="original-price" v-if="pkg.originalPrice">{{ formatPriceWithSymbol(pkg.originalPrice) }}</text>
  45. </view>
  46. </view>
  47. <view class="package-desc">{{ pkg.description }}</view>
  48. <view class="package-features">
  49. <view class="feature-item" v-for="(feature, index) in pkg.features" :key="index">
  50. <text class="feature-icon">✓</text>
  51. <text class="feature-text">{{ feature }}</text>
  52. </view>
  53. </view>
  54. <view class="package-check" v-if="selectedPackageId === pkg.id">✓</view>
  55. </view>
  56. </view>
  57. </view>
  58. <!-- 价格信息 -->
  59. <view class="section" v-if="selectedPackageId">
  60. <view class="section-title">费用明细</view>
  61. <view class="price-breakdown">
  62. <view class="price-row">
  63. <text class="price-label">任务模板价格</text>
  64. <text class="price-value">{{ formatPriceWithSymbol(selectedPackage.price) }}</text>
  65. </view>
  66. <view class="price-row" v-if="selectedGuideId">
  67. <text class="price-label">成长规划师费用</text>
  68. <text class="price-value">{{ formatPriceWithSymbol(guidePrice) }}</text>
  69. </view>
  70. <view class="price-row" v-if="discountAmount > 0">
  71. <text class="price-label">邀请码优惠</text>
  72. <text class="price-value discount">-{{ formatPriceWithSymbol(discountAmount) }}</text>
  73. </view>
  74. <view class="price-row total">
  75. <text class="price-label">总计</text>
  76. <text class="price-value">{{ formatPriceWithSymbol(totalPrice) }}</text>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 提交按钮 -->
  81. <button class="btn-submit" @click="submit" :disabled="!selectedPackageId || submitting" :loading="submitting">确认购买</button>
  82. </view>
  83. <view v-else>
  84. <FamilyEmptyState type="card" />
  85. </view>
  86. </view>
  87. </template>
  88. <script>
  89. import { getAvailableGuides, getPackagePrice, createAssessmentOrder, createVirtualPayOrder, requestVirtualPayment, getAssessmentOrderDetail } from '../../utils/api.js'
  90. export default {
  91. data() {
  92. return {
  93. guideId: null,
  94. selectedGuideId: null,
  95. selectedGuideName: '',
  96. selectedPackageId: null,
  97. selectedPackage: null,
  98. availableGuides: [],
  99. packages: [],
  100. totalPrice: 0,
  101. guidePrice: 0,
  102. discountAmount: 0,
  103. inviteCode: '',
  104. inviteCodeInfo: null,
  105. submitting: false
  106. }
  107. },
  108. computed: {
  109. hasFamily: function() {
  110. var token = uni.getStorageSync('token')
  111. if (token) return true
  112. return this.$store.getters.hasFamily
  113. }
  114. },
  115. onLoad(options) {
  116. // 如果有邀请码参数,自动填充
  117. if (options.inviteCode) {
  118. this.inviteCode = options.inviteCode
  119. this.validateInviteCode()
  120. }
  121. // 如果有成长规划师ID参数,自动选择
  122. if (options.guideId) {
  123. this.guideId = options.guideId
  124. }
  125. this.loadPackages()
  126. this.loadAvailableGuides()
  127. },
  128. methods: {
  129. async loadPackages() {
  130. try {
  131. // TODO: 从后端获取任务模板列表
  132. // 这里简化处理,使用模拟数据
  133. this.packages = [
  134. {
  135. id: 1,
  136. name: '基础测评任务模板',
  137. description: '包含注意力、专注力、记忆力三项基础测评',
  138. price: 299,
  139. originalPrice: 399,
  140. features: ['注意力测评', '专注力测评', '记忆力测评', '测评报告', '成长建议']
  141. },
  142. {
  143. id: 2,
  144. name: '全面测评任务模板',
  145. description: '包含五项全面测评,提供详细分析报告',
  146. price: 499,
  147. originalPrice: 699,
  148. features: ['注意力测评', '专注力测评', '记忆力测评', '逻辑思维测评', '综合分析报告', '个性化成长建议', '后续跟踪']
  149. },
  150. {
  151. id: 3,
  152. name: '高级测评任务模板',
  153. name: '高级测评任务模板',
  154. description: '包含所有测评项目,提供专家一对一解读',
  155. price: 799,
  156. originalPrice: 999,
  157. features: ['所有测评项目', '专家一对一解读', '详细分析报告', '个性化成长方案', '后续跟踪服务', '家长指导建议']
  158. }
  159. ]
  160. } catch (e) {
  161. console.error('获取任务模板列表失败', e)
  162. }
  163. },
  164. async loadAvailableGuides() {
  165. try {
  166. const packageId = this.selectedPackageId ? this.selectedPackageId : 1
  167. const res = await getAvailableGuides(packageId)
  168. if (res.code === 200) {
  169. this.availableGuides = res.data || []
  170. // 如果有邀请码,自动选择对应的成长规划师
  171. if (this.inviteCodeInfo && this.inviteCodeInfo.guideId) {
  172. const guide = this.availableGuides.find(g => g.guideId === this.inviteCodeInfo.guideId)
  173. if (guide) {
  174. this.selectGuide(guide)
  175. }
  176. } else if (this.guideId) {
  177. // 如果有成长规划师ID参数,自动选择
  178. const guide = this.availableGuides.find(g => g.guideId === this.guideId)
  179. if (guide) {
  180. this.selectGuide(guide)
  181. }
  182. }
  183. }
  184. } catch (e) {
  185. console.error('获取成长规划师列表失败', e)
  186. }
  187. },
  188. selectGuide(guide) {
  189. this.selectedGuideId = guide.guideId
  190. this.selectedGuideName = guide.guideName
  191. this.guidePrice = guide.price
  192. this.calculatePrice()
  193. },
  194. selectPackage(pkg) {
  195. this.selectedPackageId = pkg.id
  196. this.selectedPackage = pkg
  197. this.calculatePrice()
  198. },
  199. calculatePrice() {
  200. if (!this.selectedPackage) {
  201. this.totalPrice = 0
  202. return
  203. }
  204. this.totalPrice = this.selectedPackage.price
  205. this.guidePrice = 0
  206. this.discountAmount = 0
  207. if (this.selectedGuideId) {
  208. const guide = this.availableGuides.find(g => g.guideId === this.selectedGuideId)
  209. if (guide) {
  210. this.guidePrice = guide.price
  211. this.totalPrice += guide.price
  212. }
  213. }
  214. if (this.inviteCodeInfo && this.inviteCodeInfo.discountAmount) {
  215. this.discountAmount = this.inviteCodeInfo.discountAmount
  216. this.totalPrice -= this.discountAmount
  217. }
  218. },
  219. async validateInviteCode() {
  220. if (!this.inviteCode) {
  221. return
  222. }
  223. try {
  224. const res = await validateInviteCode(this.inviteCode)
  225. if (res.code === 200) {
  226. this.inviteCodeInfo = res.data
  227. // 自动选择邀请码关联的成长规划师
  228. if (this.inviteCodeInfo.guideId) {
  229. const guide = this.availableGuides.find(g => g.guideId === this.inviteCodeInfo.guideId)
  230. if (guide) {
  231. this.selectGuide(guide)
  232. }
  233. }
  234. }
  235. } catch (e) {
  236. console.error('验证邀请码失败', e)
  237. }
  238. },
  239. async submit() {
  240. if (this.submitting) return
  241. if (!this.selectedPackageId) {
  242. uni.showToast({ title: '请选择任务模板', icon: 'none' })
  243. return
  244. }
  245. if (!this.selectedGuideId) {
  246. uni.showToast({ title: '请选择成长规划师', icon: 'none' })
  247. return
  248. }
  249. var memberId = uni.getStorageSync('currentChildId')
  250. if (!memberId) {
  251. uni.showToast({ title: '请先选择孩子', icon: 'none' })
  252. return
  253. }
  254. this.submitting = true
  255. uni.showLoading({ title: '创建订单中...' })
  256. var userId = uni.getStorageSync('userId')
  257. var self = this
  258. try {
  259. var createRes = await createAssessmentOrder({
  260. userId: userId,
  261. memberId: memberId,
  262. guideId: this.selectedGuideId,
  263. guideName: this.selectedGuideName,
  264. packageId: this.selectedPackageId,
  265. packageName: this.selectedPackage ? this.selectedPackage.name : '',
  266. totalPrice: this.totalPrice,
  267. discountAmount: this.discountAmount
  268. })
  269. if (createRes.code !== 200 || !createRes.data || !createRes.data.orderNo) {
  270. uni.showToast({ title: '创建订单失败', icon: 'none' })
  271. return
  272. }
  273. var orderNo = createRes.data.orderNo
  274. // 支付(内联返回 VirtualPayParamsDTO;testMode 返回 null 表示 mock 直接支付成功)
  275. var payRes = await createVirtualPayOrder(orderNo, 'ASSESSMENT')
  276. if (payRes.code !== 200) {
  277. uni.showToast({ title: payRes.message || '支付失败', icon: 'none' })
  278. return
  279. }
  280. if (!payRes.data) {
  281. // testMode:mock 已直接标记支付成功 → 跳预约页
  282. uni.showToast({ title: '支付成功', icon: 'success' })
  283. setTimeout(function() {
  284. uni.navigateTo({ url: '/pages/assessment/apply' })
  285. }, 800)
  286. return
  287. }
  288. // 真实模式:拉起虚拟支付
  289. requestVirtualPayment(payRes.data, {
  290. success: function() {
  291. uni.showToast({ title: '支付成功,确认中…', icon: 'none' })
  292. self.pollOrderStatus(orderNo)
  293. },
  294. fail: function(err) {
  295. console.error('[测评支付] requestVirtualPayment 失败', err)
  296. var errCode = err && err.errCode
  297. var msg = ''
  298. if (errCode === -15007 || errCode === -15005) msg = '登录态已过期,请重新登录后支付'
  299. else if (errCode === -15010) msg = '商品未发布,请联系客服'
  300. else if (errCode === -15013) msg = '商品价格异常'
  301. else if (errCode === -2) msg = '支付已取消'
  302. else if (err && err.errMsg) msg = err.errMsg
  303. uni.showToast({ title: msg || '支付未完成', icon: 'none' })
  304. }
  305. })
  306. } catch (e) {
  307. if (e.code === 46001) {
  308. uni.showToast({ title: '该商品暂未开放购买', icon: 'none' })
  309. } else {
  310. uni.showToast({ title: e.message || '支付失败', icon: 'none' })
  311. }
  312. } finally {
  313. this.submitting = false
  314. uni.hideLoading()
  315. }
  316. },
  317. pollOrderStatus: function(orderNo) {
  318. var self = this
  319. var times = 0
  320. var timer = setInterval(function() {
  321. times++
  322. getAssessmentOrderDetail(orderNo).then(function(res) {
  323. var order = res.data || {}
  324. if (order.status === 'paid') {
  325. clearInterval(timer)
  326. uni.showToast({ title: '支付成功', icon: 'success' })
  327. setTimeout(function() {
  328. uni.navigateTo({ url: '/pages/assessment/apply' })
  329. }, 800)
  330. } else if (times >= 5) {
  331. clearInterval(timer)
  332. uni.navigateTo({ url: '/pages/assessment/apply' })
  333. }
  334. }).catch(function() {
  335. if (times >= 5) { clearInterval(timer); uni.navigateTo({ url: '/pages/assessment/apply' }) }
  336. })
  337. }, 2000)
  338. }
  339. }
  340. }
  341. </script>
  342. <style scoped>
  343. .container {
  344. padding: 30rpx;
  345. background: #F8F8F8;
  346. min-height: 100vh;
  347. }
  348. .header {
  349. text-align: center;
  350. padding: 60rpx 0 40rpx;
  351. }
  352. .title {
  353. font-size: 48rpx;
  354. font-weight: bold;
  355. color: #333;
  356. display: block;
  357. margin-bottom: 20rpx;
  358. }
  359. .subtitle {
  360. font-size: 28rpx;
  361. color: #666;
  362. }
  363. .section {
  364. background: #fff;
  365. border-radius: 20rpx;
  366. padding: 30rpx;
  367. margin-bottom: 30rpx;
  368. }
  369. .section-title {
  370. font-size: 32rpx;
  371. font-weight: bold;
  372. color: #333;
  373. margin-bottom: 20rpx;
  374. }
  375. .guide-selector,
  376. .package-list {
  377. display: flex;
  378. flex-direction: column;
  379. gap: 20rpx;
  380. }
  381. .guide-item {
  382. position: relative;
  383. display: flex;
  384. align-items: center;
  385. background: #F8F8F8;
  386. border: 2rpx solid #E0E0E0;
  387. border-radius: 10rpx;
  388. padding: 20rpx;
  389. }
  390. .guide-item.selected {
  391. border-color: #667eea;
  392. background: #F0F4FF;
  393. }
  394. .guide-avatar {
  395. width: 60rpx;
  396. height: 60rpx;
  397. border-radius: 50%;
  398. object-fit: cover;
  399. margin-right: 20rpx;
  400. }
  401. .guide-info {
  402. flex: 1;
  403. }
  404. .guide-name {
  405. font-size: 28rpx;
  406. font-weight: bold;
  407. color: #333;
  408. display: block;
  409. margin-bottom: 5rpx;
  410. }
  411. .guide-price {
  412. font-size: 24rpx;
  413. color: #F97316;
  414. font-weight: bold;
  415. }
  416. .guide-check {
  417. width: 40rpx;
  418. height: 40rpx;
  419. background: #667eea;
  420. color: #fff;
  421. border-radius: 50%;
  422. display: flex;
  423. align-items: center;
  424. justify-content: center;
  425. font-size: 24rpx;
  426. }
  427. .package-item {
  428. position: relative;
  429. background: #F8F8F8;
  430. border: 2rpx solid #E0E0E0;
  431. border-radius: 10rpx;
  432. padding: 20rpx;
  433. }
  434. .package-item.selected {
  435. border-color: #667eea;
  436. background: #F0F4FF;
  437. }
  438. .package-header {
  439. display: flex;
  440. justify-content: space-between;
  441. align-items: center;
  442. margin-bottom: 15rpx;
  443. }
  444. .package-name {
  445. font-size: 30rpx;
  446. font-weight: bold;
  447. color: #333;
  448. }
  449. .package-price {
  450. text-align: right;
  451. }
  452. .price {
  453. font-size: 32rpx;
  454. font-weight: bold;
  455. color: #F97316;
  456. display: block;
  457. }
  458. .original-price {
  459. font-size: 24rpx;
  460. color: #999;
  461. text-decoration: line-through;
  462. margin-left: 10rpx;
  463. }
  464. .package-desc {
  465. font-size: 26rpx;
  466. color: #666;
  467. margin-bottom: 15rpx;
  468. }
  469. .package-features {
  470. display: flex;
  471. flex-wrap: wrap;
  472. gap: 10rpx;
  473. }
  474. .feature-item {
  475. display: flex;
  476. align-items: center;
  477. background: #E8F5E9;
  478. padding: 8rpx 12rpx;
  479. border-radius: 20rpx;
  480. }
  481. .feature-icon {
  482. color: #4CAF50;
  483. font-size: 24rpx;
  484. margin-right: 8rpx;
  485. }
  486. .feature-text {
  487. font-size: 24rpx;
  488. color: #333;
  489. }
  490. .package-check {
  491. position: absolute;
  492. top: 20rpx;
  493. right: 20rpx;
  494. width: 40rpx;
  495. height: 40rpx;
  496. background: #667eea;
  497. color: #fff;
  498. border-radius: 50%;
  499. display: flex;
  500. align-items: center;
  501. justify-content: center;
  502. font-size: 24rpx;
  503. }
  504. .price-breakdown {
  505. background: #F8F8F8;
  506. border-radius: 10rpx;
  507. padding: 20rpx;
  508. }
  509. .price-row {
  510. display: flex;
  511. justify-content: space-between;
  512. align-items: center;
  513. margin-bottom: 15rpx;
  514. }
  515. .price-row:last-child {
  516. margin-bottom: 0;
  517. }
  518. .price-label {
  519. font-size: 28rpx;
  520. color: #666;
  521. }
  522. .price-value {
  523. font-size: 32rpx;
  524. font-weight: bold;
  525. color: #333;
  526. }
  527. .price-value.discount {
  528. color: #F97316;
  529. }
  530. .price-row.total .price-label {
  531. font-size: 30rpx;
  532. font-weight: bold;
  533. color: #333;
  534. }
  535. .price-row.total .price-value {
  536. font-size: 36rpx;
  537. color: #F97316;
  538. }
  539. .btn-submit {
  540. width: 100%;
  541. height: 90rpx;
  542. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  543. color: #fff;
  544. border-radius: 45rpx;
  545. font-size: 32rpx;
  546. font-weight: bold;
  547. margin-top: 40rpx;
  548. }
  549. .btn-submit[disabled] {
  550. background: #CCCCCC;
  551. }
  552. </style>