orderStatus.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { orderType } from './../../constant/index'
  2. import { reactive } from 'vue'
  3. import { state } from '@/state'
  4. import request from '@/helpers/request'
  5. import { Dialog } from 'vant'
  6. type orderType =
  7. | 'VIDEO'
  8. | 'LIVE'
  9. | 'PRACTICE'
  10. | 'GOODS'
  11. | 'VIP'
  12. | 'MUSIC'
  13. | 'PINAO_ROOM'
  14. | 'ACTI_REGIST'
  15. | ''
  16. const original = () => {
  17. return {
  18. orderType: '' as orderType, // 购买类型
  19. orderInfo: {
  20. // 订单信息
  21. orderNo: '',
  22. actualPrice: 0,
  23. payStatus: true
  24. },
  25. orderObject: {
  26. // 订单对象
  27. orderNo: '',
  28. actualPrice: 0,
  29. orderName: '',
  30. orderDesc: '',
  31. orderType: '' as orderType,
  32. recomUserId: null as any, // 推荐人编号
  33. orderList: [] as Array<any>, // 商品信息
  34. activityId: '' as any, // 活动编号
  35. couponId: '' as string, // 优惠券编号
  36. discountPrice: 0 as number // 优惠
  37. }
  38. // orderObject: {
  39. // orderNo: '',
  40. // actualPrice: 28,
  41. // orderName: '小酷Ai月度会员',
  42. // orderDesc: '小酷Ai月度会员',
  43. // orderType: 'VIP',
  44. // recomUserId: 0,
  45. // activityId: null,
  46. // orderList: [
  47. // {
  48. // orderType: 'VIP',
  49. // goodsName: '小酷Ai月度会员',
  50. // id: 2,
  51. // title: '月度会员',
  52. // price: 28,
  53. // startTime: '2023-02-28',
  54. // endTime: '2023-03-28'
  55. // }
  56. // ]
  57. // }
  58. }
  59. }
  60. export const orderStatus = reactive(original())
  61. // 重置对象
  62. export const resestState = () => {
  63. Object.assign(orderStatus, original())
  64. }
  65. export const orderInfos = () => {
  66. // 商品列表
  67. const orderList = orderStatus.orderObject.orderList || []
  68. return orderList.map((item: any) => {
  69. const params = {
  70. goodType: item.orderType,
  71. goodName: item.goodsName,
  72. recomUserId: item.recomUserId, // 推荐人id
  73. bizContent: {}
  74. }
  75. if (item.orderType === 'VIDEO') {
  76. params.bizContent = {
  77. videoLessonGroupId: item.courseGroupId,
  78. payMoney: item.coursePrice || 0
  79. }
  80. } else if (item.orderType === 'LIVE') {
  81. params.bizContent = {
  82. groupId: item.courseGroupId
  83. }
  84. } else if (item.orderType === 'PRACTICE') {
  85. const tempTime = item.classTime || []
  86. const classCourse: any = []
  87. tempTime.forEach((time: any) => {
  88. classCourse.push({
  89. classDate: time.classDate,
  90. startTime: time.startTime,
  91. endTime: time.endTime
  92. })
  93. })
  94. params.bizContent = {
  95. courseGroupName: item.courseGroupName,
  96. courseIntroduce: item.courseIntroduce,
  97. subjectId: item.subjectId,
  98. singleCourseMinutes: item.singleCourseMinutes,
  99. courseNum: item.courseNum,
  100. coursePrice: item.coursePrice,
  101. teacherId: item.teacherId,
  102. classTime: classCourse
  103. }
  104. } else if (item.orderType === 'VIP') {
  105. params.bizContent = item.id
  106. } else if (item.orderType === 'MUSIC') {
  107. params.bizContent = {
  108. musicSheetId: item.id,
  109. actualPrice: item.actualPrice || 0,
  110. clientType: state.platformType
  111. }
  112. } else if (item.orderType === 'PINAO_ROOM') {
  113. params.bizContent = item.id
  114. } else if (item.orderType === 'ACTI_REGIST') {
  115. params.bizContent = {
  116. activityId: item.activityId
  117. }
  118. }
  119. return params
  120. })
  121. }
  122. /**
  123. * @title 0元购买
  124. * @param {function} callBack 回调函数
  125. * @returns {Promise<void>}
  126. */
  127. export const onSubmitZero = async (callBack?: Function): Promise<void> => {
  128. // 正常支付
  129. try {
  130. const orderObject = orderStatus.orderObject
  131. const url =
  132. state.platformType === 'TEACHER'
  133. ? '/api-teacher/userOrder/executeOrder'
  134. : '/api-student/userOrder/executeOrder'
  135. const res = await request.post(url, {
  136. data: {
  137. orderName: orderObject.orderName,
  138. orderDesc: orderObject.orderDesc,
  139. orderType: orderObject.orderType,
  140. actualPrice: orderObject.actualPrice || 0,
  141. recomUserId: orderObject.recomUserId,
  142. activityId: orderObject.activityId,
  143. orderInfos: [...orderInfos()]
  144. }
  145. })
  146. const result = res.data || {}
  147. // 支付成功
  148. if (result.status == 'PAID') {
  149. if (callBack) {
  150. callBack()
  151. } else {
  152. Dialog.alert({
  153. message: '领取成功',
  154. confirmButtonText: '确定',
  155. confirmButtonColor: '#2dc7aa'
  156. })
  157. }
  158. } else {
  159. Dialog.alert({
  160. message: result.msg,
  161. confirmButtonText: '确定',
  162. confirmButtonColor: '#2dc7aa'
  163. })
  164. }
  165. } catch {
  166. Dialog.alert({
  167. title: '提示',
  168. message: '支付失败,请稍后重试!',
  169. confirmButtonText: '确定',
  170. confirmButtonColor: '#2dc7aa'
  171. })
  172. }
  173. }