tradeOrder.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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': case "GROUP":
  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. try {
  128. const res = await getAlbumDetail(item.bizId)
  129. tempList = {
  130. orderType: item.goodType,
  131. goodName: item.goodName,
  132. ...res
  133. }
  134. } catch (e: any) {
  135. throw new Error(e.message)
  136. }
  137. }
  138. break
  139. case 'TENANT_ALBUM':
  140. {
  141. try {
  142. const res = await getTenantAlbumDetail(item.bizId)
  143. tempList = {
  144. orderType: item.goodType,
  145. goodName: item.goodName,
  146. ...res,
  147. purchaseNum: item.bizInfo?.bizValidTime || res.purchaseNum
  148. }
  149. } catch (e: any) {
  150. throw new Error(e.message)
  151. }
  152. }
  153. break
  154. case 'ACTI_REGIST':
  155. {
  156. try {
  157. const res = await getMusicActiveTrack(item.bizId)
  158. tempList = {
  159. orderType: item.goodType,
  160. goodsName: res.activityName,
  161. activityId: res.id,
  162. actualPrice: res.registrationPrice
  163. }
  164. } catch (e: any) {
  165. throw new Error(e.message)
  166. }
  167. }
  168. break
  169. }
  170. tempList.orderType = type
  171. tempList.goodName = item.goodName
  172. orderStatus.orderObject.orderList.push(tempList)
  173. }
  174. // 获取视频课详情
  175. export const getVideoDetail = async (groupId: any) => {
  176. try {
  177. const res = await request.get(
  178. `${apiSuffix}/videoLesson/selectVideoLesson`,
  179. {
  180. params: {
  181. groupId
  182. }
  183. }
  184. )
  185. return res.data
  186. } catch {
  187. throw new Error('获取视频课详情失败')
  188. }
  189. }
  190. // 获取直播课详情
  191. export const getLiveDetail = async (groupId: any) => {
  192. try {
  193. const res = await request.get(
  194. `${apiSuffix}/courseGroup/queryLiveCourseInfo`,
  195. {
  196. params: {
  197. groupId
  198. }
  199. }
  200. )
  201. return res.data
  202. } catch {
  203. throw new Error('获取直播课详情失败')
  204. }
  205. }
  206. // 获取会员详情
  207. export const getVipDetail = async (id: any) => {
  208. try {
  209. const setting = await request.get(`${apiSuffix}/vipCardRecord/detail/` + id)
  210. return setting.data || []
  211. } catch {
  212. throw new Error('获取会员详情失败')
  213. }
  214. }
  215. // 获取曲目详情
  216. export const getMusicDetail = async (id: any) => {
  217. try {
  218. const res = await request.get(`${apiSuffix}/music/sheet/detail/${id}`)
  219. return res.data
  220. } catch {
  221. throw new Error('获取曲目详情失败')
  222. }
  223. }
  224. // 活动列表
  225. // 获取曲目详情
  226. export const getMusicActiveTrack = async (id: any) => {
  227. try {
  228. const res = await request.post(`${apiSuffix}/open/activity/info/${id}`)
  229. return res.data
  230. } catch {
  231. throw new Error('获取曲目详情失败')
  232. }
  233. }
  234. // 获取专辑详情
  235. export const getAlbumDetail = async (id: any) => {
  236. try {
  237. const res = await request.post(`${apiSuffix}/music/album/detail`, {
  238. data: { id }
  239. })
  240. return res.data
  241. } catch {
  242. throw new Error('获取专辑详情失败')
  243. }
  244. }
  245. // 获取机构专辑详情
  246. export const getTenantAlbumDetail = async (id: any) => {
  247. try {
  248. const res = await request.post(
  249. `${apiSuffix}/userTenantAlbumRecord/detail`,
  250. {
  251. requestType: 'form',
  252. data: { albumId: id }
  253. }
  254. )
  255. return res.data
  256. } catch {
  257. throw new Error('获取机构专辑详情失败')
  258. }
  259. }
  260. // 为了处理继续支付逻辑
  261. export const tradeOrder = (result: any, callBack?: any) => {
  262. const {
  263. orderNo,
  264. actualPrice,
  265. orderDesc,
  266. orderName,
  267. orderType,
  268. orderDetailList,
  269. couponAmount, // 优惠金额
  270. discountPrice,
  271. paymentConfig, // v2 类型的订单才会用
  272. paymentVendor,
  273. paymentVersion
  274. } = result
  275. orderStatus.orderObject.orderType = orderType
  276. orderStatus.orderObject.orderName = orderName
  277. orderStatus.orderObject.orderDesc = orderDesc
  278. orderStatus.orderObject.actualPrice = actualPrice
  279. orderStatus.orderObject.orderNo = orderNo
  280. orderStatus.orderObject.discountPrice = discountPrice
  281. orderStatus.orderObject.orderList = []
  282. orderStatus.orderObject.paymentConfig = {
  283. ...paymentConfig,
  284. paymentVendor,
  285. paymentVersion
  286. }
  287. try {
  288. orderDetailList.forEach(async (item: any) => {
  289. await formatOrderDetail(item, {
  290. couponAmount,
  291. discountPrice
  292. })
  293. })
  294. callBack && callBack()
  295. } catch {
  296. //
  297. }
  298. }