purchase.vue 15 KB

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