payment.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import OSticky from '@/components/o-sticky'
  2. import {
  3. Button,
  4. Cell,
  5. CellGroup,
  6. Checkbox,
  7. CheckboxGroup,
  8. Icon,
  9. Image,
  10. showConfirmDialog,
  11. showToast,
  12. Tag
  13. } from 'vant'
  14. import { defineComponent, nextTick, onMounted, reactive, ref } from 'vue'
  15. import styles from '../index.module.less'
  16. import radioCheck from '@/common/images/icon-radio-check.png'
  17. import radioDefault from '@/common/images/icon-radio-default.png'
  18. import { useRoute, useRouter } from 'vue-router'
  19. import request from '../../request-music'
  20. import { moneyFormat } from '@/helpers/utils'
  21. import { CountUp } from 'countup.js'
  22. export default defineComponent({
  23. name: 'payment',
  24. emits: ['next'],
  25. setup() {
  26. const route = useRoute()
  27. const router = useRouter()
  28. const state = reactive({
  29. check: [] as any, // 选中的数据
  30. checkboxRefs: [] as any,
  31. details: [] as any, //
  32. goodsInfo: {} as any, // 商品
  33. textBookInfo: {} as any, // 教材
  34. repaireInfo: {} as any, // 乐器保养
  35. vipInfo: {} as any, // 团练宝
  36. paymentOrderDetails: [] as any, // 购买状态
  37. orderInfo: {
  38. needPrice: 0,
  39. originalPrice: 0
  40. }
  41. })
  42. // 查询未支付订单
  43. const paymentOrderUnpaid = async () => {
  44. try {
  45. const { data } = await request.get('/api-student/userPaymentOrder/unpaid')
  46. console.log(data, 'data')
  47. // 判断是否有待支付订单
  48. if (data.id) {
  49. showConfirmDialog({
  50. message: '您有待支付的订单,是否继续支付',
  51. cancelButtonText: '取消订单',
  52. confirmButtonText: '继续支付'
  53. })
  54. .then(() => {
  55. const paymentConfig = data.paymentConfig
  56. router.push({
  57. path: '/orderDetail',
  58. query: {
  59. config: JSON.stringify(paymentConfig.paymentConfig),
  60. orderNo: paymentConfig.orderNo
  61. }
  62. })
  63. })
  64. .catch(async () => {
  65. console.log('cancel')
  66. try {
  67. await request.post('/api-student/userPaymentOrder/cancelPayment/' + data.orderNo)
  68. } catch {
  69. //
  70. }
  71. })
  72. }
  73. } catch {
  74. //
  75. }
  76. }
  77. // 获取商品信息
  78. const registerGoods = async () => {
  79. try {
  80. const { data } = await request.get(
  81. '/api-student/orchestraRegister/registerGoods/' + route.query.id
  82. )
  83. const details = data.details || []
  84. details.forEach((item: any) => {
  85. if (item.goodsType === 'INSTRUMENTS') {
  86. const img = item.goodsUrl ? item.goodsUrl.split(',')[0] : ''
  87. state.goodsInfo = { ...item, goodsUrl: img }
  88. } else if (item.goodsType === 'TEXTBOOK') {
  89. const img = item.goodsUrl ? item.goodsUrl.split(',')[0] : ''
  90. state.textBookInfo = { ...item, goodsUrl: img }
  91. } else if (item.goodsType === 'REPAIR') {
  92. state.repaireInfo = { ...item }
  93. } else if (item.goodsType === 'VIP') {
  94. state.vipInfo = { ...item }
  95. }
  96. state.details = details
  97. // 默认选中所有的
  98. state.check.push(item.goodsId)
  99. })
  100. state.paymentOrderDetails = data.paymentOrderDetails || []
  101. calcPrice()
  102. } catch {
  103. //
  104. }
  105. }
  106. const onSelect = (type: string) => {
  107. state.checkboxRefs[type].toggle()
  108. calcPrice()
  109. }
  110. // 初始化金额
  111. const calcPrice = () => {
  112. const details = state.details
  113. const tempPrice = {
  114. needPrice: 0, //需要支付金额
  115. originalPrice: 0 // 原价
  116. }
  117. details.forEach((item: any) => {
  118. // 是否选中
  119. if (state.check.includes(item.goodsId)) {
  120. tempPrice.needPrice += parseFloat(item.currentPrice)
  121. tempPrice.originalPrice += parseFloat(item.originalPrice)
  122. }
  123. })
  124. state.orderInfo = tempPrice
  125. initNumCountUp()
  126. }
  127. const countUpRef = reactive({
  128. needPrice: null as any,
  129. originalPrice: null as any
  130. })
  131. const initNumCountUp = () => {
  132. nextTick(() => {
  133. // 在读学生
  134. if (countUpRef.needPrice) {
  135. countUpRef.needPrice.update(state.orderInfo.needPrice)
  136. } else {
  137. countUpRef.needPrice = new CountUp('needPrice', state.orderInfo.needPrice, {
  138. decimalPlaces: 2
  139. })
  140. if (!countUpRef.needPrice.error) {
  141. countUpRef.needPrice.start()
  142. } else {
  143. console.error(countUpRef.needPrice.error)
  144. }
  145. }
  146. if (countUpRef.originalPrice) {
  147. countUpRef.originalPrice.update(state.orderInfo.originalPrice)
  148. } else {
  149. countUpRef.originalPrice = new CountUp('originalPrice', state.orderInfo.originalPrice, {
  150. decimalPlaces: 2
  151. })
  152. if (!countUpRef.originalPrice.error) {
  153. countUpRef.originalPrice.start()
  154. } else {
  155. console.error(countUpRef.originalPrice.error)
  156. }
  157. }
  158. })
  159. }
  160. // 购买
  161. const onSubmit = async () => {
  162. try {
  163. // 判断订单号
  164. if (state.check.length <= 0) {
  165. showToast('请选择您要购买的商品')
  166. return
  167. }
  168. // 重新计算金额
  169. calcPrice()
  170. const params: any = [] // 支付参数
  171. const details = state.details
  172. details.forEach((item: any) => {
  173. // 是否选中
  174. if (state.check.includes(item.goodsId)) {
  175. params.push({
  176. goodsId: item.goodsId,
  177. goodsNum: 1,
  178. goodsType: item.goodsType,
  179. paymentCashAmount: item.currentPrice, // 现金支付金额
  180. paymentCouponAmount: 0 // 优惠券金额
  181. })
  182. }
  183. })
  184. console.log({
  185. bizId: route.query.id, // 乐团编号
  186. orderType: 'ORCHESTRA',
  187. paymentCashAmount: state.orderInfo.needPrice || 0,
  188. paymentCouponAmount: 0,
  189. goodsInfos: params
  190. })
  191. // 创建订单
  192. const { data } = await request.post('/api-student/userPaymentOrder/executeOrder', {
  193. data: {
  194. bizId: route.query.id, // 乐团编号
  195. orderType: 'ORCHESTRA',
  196. paymentCashAmount: state.orderInfo.needPrice || 0,
  197. paymentCouponAmount: 0,
  198. goodsInfos: params,
  199. orderName: '订单详情',
  200. orderDesc: '订单详情'
  201. }
  202. })
  203. console.log(data)
  204. router.push({
  205. path: '/orderDetail',
  206. query: {
  207. config: JSON.stringify(data.paymentConfig),
  208. orderNo: data.orderNo
  209. }
  210. })
  211. } catch (e: any) {
  212. //
  213. console.log(e)
  214. }
  215. // router.push('/orderDetail')
  216. }
  217. onMounted(() => {
  218. // 查询未支付订单
  219. paymentOrderUnpaid()
  220. registerGoods()
  221. })
  222. return () => (
  223. <>
  224. <div class={styles.applyTitle}>报名须知</div>
  225. <div class={[styles.paymentTips, styles.mlr13]}>
  226. <p>1、您注册时所选择的乐团声部,即为乐团录取最终确认的声部,请您务必仔细填写;</p>
  227. <p>
  228. 2、所有参与乐团的学生免费赠送选报声部教材,教材随乐器一同发放,若您自备乐器,则需承担教材运费。
  229. </p>
  230. </div>
  231. <CheckboxGroup
  232. v-model={state.check}
  233. style={{ paddingBottom: '20px' }}
  234. onChange={() => {
  235. calcPrice()
  236. }}
  237. >
  238. <div class={styles.applyTitle}>乐器</div>
  239. <CellGroup
  240. inset
  241. class={[styles.mlr13, styles.sectionCell]}
  242. onClick={() => onSelect(state.goodsInfo.goodsId)}
  243. >
  244. <Cell border={false}>
  245. {{
  246. icon: () => (
  247. <Checkbox
  248. name={state.goodsInfo.goodsId}
  249. class={styles.checkbox}
  250. ref={(el: any) => (state.checkboxRefs[state.goodsInfo.goodsId] = el)}
  251. v-slots={{
  252. icon: (props: any) => (
  253. <Icon
  254. class={styles.iconChecked}
  255. name={props.checked ? radioCheck : radioDefault}
  256. />
  257. )
  258. }}
  259. />
  260. ),
  261. title: () => (
  262. <div class={styles.section}>
  263. <Image class={styles.img} src={state.goodsInfo.goodsUrl} />
  264. <div class={styles.sectionContent}>
  265. <h2>{state.goodsInfo.goodsName}</h2>
  266. <Tag type="primary">{state.goodsInfo.brandName}</Tag>
  267. <p class={styles.model}>{state.goodsInfo.desciption}</p>
  268. </div>
  269. </div>
  270. )
  271. }}
  272. </Cell>
  273. <Cell>
  274. {{
  275. title: () => (
  276. <div class={styles.extra}>
  277. <div class={styles.sectionPrice}>
  278. <p class={styles.price}>
  279. 新团特惠:<span>¥{moneyFormat(state.goodsInfo.currentPrice)}</span>
  280. </p>
  281. <p class={styles.originPrice}>
  282. 原价:<del>¥{moneyFormat(state.goodsInfo.originalPrice)}</del>
  283. </p>
  284. </div>
  285. <div class={styles.sectionTips}>
  286. 赠价值{state.repaireInfo.originalPrice}元乐器维保服务一年
  287. </div>
  288. </div>
  289. )
  290. }}
  291. </Cell>
  292. </CellGroup>
  293. <div class={styles.applyTitle}>教材</div>
  294. <CellGroup
  295. inset
  296. class={[styles.mlr13, styles.sectionCell]}
  297. onClick={() => {
  298. return
  299. // onSelect(state.textBookInfo.goodsId)
  300. }}
  301. >
  302. <Cell border={false}>
  303. {{
  304. icon: () => (
  305. <Checkbox
  306. name={state.textBookInfo.goodsId}
  307. disabled
  308. class={styles.checkbox}
  309. ref={(el: any) => (state.checkboxRefs[state.textBookInfo.goodsId] = el)}
  310. v-slots={{
  311. icon: (props: any) => (
  312. <Icon
  313. class={styles.iconChecked}
  314. name={props.checked ? radioCheck : radioDefault}
  315. />
  316. )
  317. }}
  318. />
  319. ),
  320. title: () => (
  321. <div class={styles.section}>
  322. <Image class={styles.img} src={state.textBookInfo.goodsUrl} />
  323. <div class={styles.sectionContent}>
  324. <h2>{state.textBookInfo.goodsName}</h2>
  325. <Tag type="primary">{state.textBookInfo.brandName}</Tag>
  326. <p class={styles.model}>{state.textBookInfo.description}</p>
  327. </div>
  328. </div>
  329. )
  330. }}
  331. </Cell>
  332. <Cell>
  333. {{
  334. title: () => (
  335. <div class={styles.extra}>
  336. <div class={styles.sectionPrice}>
  337. <p class={styles.price}>
  338. 新团特惠:
  339. <span class={styles.free}>
  340. {state.textBookInfo.currentPrice > 0
  341. ? moneyFormat(state.textBookInfo.currentPrice)
  342. : '免费赠送'}
  343. </span>
  344. </p>
  345. <p class={styles.originPrice}>
  346. 原价:<del>¥{moneyFormat(state.textBookInfo.originalPrice)}</del>
  347. </p>
  348. </div>
  349. </div>
  350. )
  351. }}
  352. </Cell>
  353. </CellGroup>
  354. <div class={styles.applyTitle}>乐团学习系统</div>
  355. <CellGroup
  356. inset
  357. class={[styles.mlr13, styles.sectionCell]}
  358. onClick={() => onSelect(state.vipInfo.goodsId)}
  359. >
  360. <Cell border={false}>
  361. {{
  362. icon: () => (
  363. <Checkbox
  364. name={state.vipInfo.goodsId}
  365. class={styles.checkbox}
  366. ref={(el: any) => (state.checkboxRefs[state.vipInfo.goodsId] = el)}
  367. onClick={(e: Event) => {
  368. e.stopPropagation()
  369. }}
  370. v-slots={{
  371. icon: (props: any) => (
  372. <Icon
  373. class={styles.iconChecked}
  374. name={props.checked ? radioCheck : radioDefault}
  375. />
  376. )
  377. }}
  378. />
  379. ),
  380. title: () => (
  381. <div class={styles.section}>
  382. <Image class={styles.img} src={state.vipInfo.goodsUrl} />
  383. <div class={styles.sectionContent}>
  384. <h2>{state.vipInfo.goodsName}</h2>
  385. <Tag type="primary">6个月</Tag>
  386. <p class={styles.model}>{state.vipInfo.description}</p>
  387. </div>
  388. </div>
  389. )
  390. }}
  391. </Cell>
  392. <Cell>
  393. {{
  394. title: () => (
  395. <div class={styles.extra}>
  396. <div class={styles.sectionPrice}>
  397. <p class={styles.price}>
  398. 新团特惠:<span>¥{moneyFormat(state.vipInfo.currentPrice)}</span>
  399. </p>
  400. <p class={styles.originPrice}>
  401. 原价:<del>¥{moneyFormat(state.vipInfo.originalPrice)}</del>
  402. </p>
  403. </div>
  404. </div>
  405. )
  406. }}
  407. </Cell>
  408. </CellGroup>
  409. </CheckboxGroup>
  410. <OSticky position="bottom" background="white">
  411. <div class={styles.paymentContainer}>
  412. <div class={styles.payemntPrice}>
  413. <p class={styles.needPrice}>
  414. 支付金额:
  415. <span>
  416. ¥<i style="font-style: normal" id="needPrice"></i>
  417. </span>
  418. </p>
  419. <p class={styles.allPrice}>
  420. 总原价:
  421. <del>
  422. ¥<i style="font-style: normal" id="originalPrice"></i>
  423. </del>
  424. </p>
  425. </div>
  426. <div class={styles.paymentBtn}>
  427. <Button
  428. color="linear-gradient(135deg, #FF8C4A 0%, #FF531C 100%)"
  429. round
  430. onClick={onSubmit}
  431. >
  432. 立即购买
  433. </Button>
  434. </div>
  435. </div>
  436. </OSticky>
  437. </>
  438. )
  439. }
  440. })