tradeOrder.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { memberType } from '@/constant'
  2. import request from '@/helpers/request'
  3. import { state } from '@/state'
  4. import { orderStatus } from '@/views/order-detail/orderStatus'
  5. import dayjs from 'dayjs'
  6. const apiSuffix =
  7. state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  8. // LIVE: '直播课',
  9. // PRACTICE: '陪练课',
  10. // VIDEO: '视频课',
  11. // VIP: '开通会员',
  12. // MUSIC: '单曲点播'
  13. interface IAmount {
  14. couponAmount: number
  15. discountPrice: number
  16. }
  17. export const formatOrderDetail = async (item: any, amount?: IAmount) => {
  18. const type = item.goodType
  19. let tempList: any = {}
  20. switch (type) {
  21. case 'LIVE':
  22. {
  23. try {
  24. const live = await getLiveDetail(item.bizId)
  25. const courseInfo: any[] = []
  26. const coursePlanList = live.planList || []
  27. coursePlanList.forEach((item: any) => {
  28. const startTime = item.startTime || new Date()
  29. const endTime = item.endTime || new Date()
  30. courseInfo.push({
  31. courseTime: `${dayjs(startTime).format('YYYY-MM-DD')} ${dayjs(
  32. startTime
  33. ).format('HH:mm')}~${dayjs(endTime).format('HH:mm')}`,
  34. coursePlan: item.plan,
  35. id: item.courseId
  36. })
  37. })
  38. tempList = {
  39. orderType: item.goodType,
  40. goodName: item.goodName,
  41. courseGroupId: live.courseGroupId,
  42. courseGroupName: live.courseGroupName,
  43. coursePrice: live.coursePrice,
  44. teacherName: live.userName || `游客${live.teacherId || ''}`,
  45. teacherId: live.teacherId,
  46. avatar: live.avatar,
  47. courseInfo
  48. }
  49. } catch (e: any) {
  50. throw new Error(e.message)
  51. }
  52. }
  53. break
  54. case 'PRACTICE': {
  55. const bizContent: any = JSON.parse(item.bizContent)
  56. tempList = {
  57. ...bizContent,
  58. teacherName: item.username,
  59. starGrade: item.starGrade,
  60. avatar: item.avatar
  61. }
  62. break
  63. }
  64. case 'VIDEO': {
  65. try {
  66. const res = await getVideoDetail(item.bizId)
  67. const { lessonGroup, detailList } = res
  68. tempList = {
  69. orderType: item.goodType,
  70. goodName: item.goodName,
  71. courseGroupId: lessonGroup.id,
  72. courseGroupName: lessonGroup.lessonName,
  73. coursePrice: lessonGroup.lessonPrice,
  74. teacherName: lessonGroup.username,
  75. teacherId: lessonGroup.teacherId,
  76. avatar: lessonGroup.avatar,
  77. courseInfo: detailList
  78. }
  79. } catch (e: any) {
  80. throw new Error(e.message)
  81. }
  82. break
  83. }
  84. case 'VIP':
  85. {
  86. try {
  87. const res = await getVipDetail(item.id)
  88. tempList = {
  89. orderType: item.goodType,
  90. goodName: item.goodName,
  91. id: item.id,
  92. title: memberType[res.period] || '',
  93. // 判断是否有优惠金额
  94. price: amount?.couponAmount
  95. ? Number(
  96. (
  97. res.salePrice -
  98. amount.couponAmount +
  99. amount.discountPrice
  100. ).toFixed(2)
  101. )
  102. : res.salePrice || item.actualPrice,
  103. startTime: dayjs(res.startTime).format('YYYY-MM-DD'),
  104. endTime: dayjs(res.endTime).format('YYYY-MM-DD')
  105. }
  106. } catch (e: any) {
  107. throw new Error(e.message)
  108. }
  109. }
  110. break
  111. case 'MUSIC':
  112. {
  113. try {
  114. const res = await getMusicDetail(item.bizId)
  115. tempList = {
  116. orderType: item.goodType,
  117. goodName: item.goodName,
  118. ...res
  119. }
  120. } catch (e: any) {
  121. throw new Error(e.message)
  122. }
  123. }
  124. break
  125. case 'ALBUM':
  126. {
  127. console.log(item)
  128. try {
  129. const res = await getAlbumDetail(item.bizId)
  130. tempList = {
  131. orderType: item.goodType,
  132. goodName: item.goodName,
  133. ...res
  134. }
  135. } catch (e: any) {
  136. throw new Error(e.message)
  137. }
  138. }
  139. break
  140. case 'ACTI_REGIST':
  141. {
  142. try {
  143. const res = await getMusicActiveTrack(item.bizId)
  144. tempList = {
  145. orderType: item.goodType,
  146. goodsName: res.activityName,
  147. activityId: res.id,
  148. actualPrice: res.registrationPrice
  149. }
  150. } catch (e: any) {
  151. throw new Error(e.message)
  152. }
  153. }
  154. break
  155. }
  156. tempList.orderType = type
  157. tempList.goodName = item.goodName
  158. orderStatus.orderObject.orderList.push(tempList)
  159. }
  160. // 获取视频课详情
  161. export const getVideoDetail = async (groupId: any) => {
  162. try {
  163. const res = await request.get(
  164. `${apiSuffix}/videoLesson/selectVideoLesson`,
  165. {
  166. params: {
  167. groupId
  168. }
  169. }
  170. )
  171. return res.data
  172. } catch {
  173. throw new Error('获取视频课详情失败')
  174. }
  175. }
  176. // 获取直播课详情
  177. export const getLiveDetail = async (groupId: any) => {
  178. try {
  179. const res = await request.get(
  180. `${apiSuffix}/courseGroup/queryLiveCourseInfo`,
  181. {
  182. params: {
  183. groupId
  184. }
  185. }
  186. )
  187. return res.data
  188. } catch {
  189. throw new Error('获取直播课详情失败')
  190. }
  191. }
  192. // 获取会员详情
  193. export const getVipDetail = async (id: any) => {
  194. try {
  195. const setting = await request.get(`${apiSuffix}/vipCardRecord/detail/` + id)
  196. return setting.data || []
  197. } catch {
  198. throw new Error('获取会员详情失败')
  199. }
  200. }
  201. // 获取曲目详情
  202. export const getMusicDetail = async (id: any) => {
  203. try {
  204. const res = await request.get(`${apiSuffix}/music/sheet/detail/${id}`)
  205. return res.data
  206. } catch {
  207. throw new Error('获取曲目详情失败')
  208. }
  209. }
  210. // 活动列表
  211. // 获取曲目详情
  212. export const getMusicActiveTrack = async (id: any) => {
  213. try {
  214. const res = await request.post(`${apiSuffix}/open/activity/info/${id}`)
  215. return res.data
  216. } catch {
  217. throw new Error('获取曲目详情失败')
  218. }
  219. }
  220. // 获取专辑详情
  221. export const getAlbumDetail = async (id: any) => {
  222. try {
  223. const res = await request.post(`${apiSuffix}/music/album/detail`, {
  224. data: { id }
  225. })
  226. return res.data
  227. } catch {
  228. throw new Error('获取专辑详情失败')
  229. }
  230. }
  231. // 为了处理继续支付逻辑
  232. export const tradeOrder = (result: any, callBack?: any) => {
  233. const {
  234. orderNo,
  235. actualPrice,
  236. orderDesc,
  237. orderName,
  238. orderType,
  239. orderDetailList,
  240. couponAmount, // 优惠金额
  241. discountPrice,
  242. paymentConfig
  243. } = result
  244. orderStatus.orderObject.orderType = orderType
  245. orderStatus.orderObject.orderName = orderName
  246. orderStatus.orderObject.orderDesc = orderDesc
  247. orderStatus.orderObject.actualPrice = actualPrice
  248. orderStatus.orderObject.orderNo = orderNo
  249. orderStatus.orderObject.discountPrice = discountPrice
  250. orderStatus.orderObject.paymentConfig = paymentConfig
  251. orderStatus.orderObject.orderList = []
  252. try {
  253. orderDetailList.forEach(async (item: any) => {
  254. await formatOrderDetail(item, {
  255. couponAmount,
  256. discountPrice
  257. })
  258. })
  259. callBack && callBack()
  260. } catch {
  261. //
  262. }
  263. }