orderStatus.ts 5.1 KB

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