orderStatus.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { orderType } from './../../constant/index'
  2. import { reactive } from 'vue'
  3. import { state } from '@/state'
  4. import request from '@/helpers/request'
  5. import { browser } from '@/helpers/utils'
  6. import { Dialog } from 'vant'
  7. import dayjs from 'dayjs'
  8. type orderType =
  9. | 'VIDEO'
  10. | 'LIVE'
  11. | 'GROUP'
  12. | 'PRACTICE'
  13. | 'GOODS'
  14. | 'VIP'
  15. | 'SVIP'
  16. | 'MUSIC'
  17. | 'PIANO_ROOM'
  18. | 'ACTI_REGIST'
  19. | 'VIP_COURSE'
  20. | 'ALBUM'
  21. | 'TENANT_ALBUM'
  22. | ''
  23. const original = () => {
  24. return {
  25. orderType: '' as orderType, // 购买类型
  26. orderInfo: {
  27. // 订单信息
  28. orderNo: '',
  29. actualPrice: 0,
  30. payStatus: true
  31. },
  32. orderObject: {
  33. // 订单对象
  34. orderNo: '',
  35. actualPrice: 0,
  36. orderName: '',
  37. orderDesc: '',
  38. orderType: '' as orderType,
  39. recomUserId: null as any, // 推荐人编号
  40. orderList: [] as Array<any>, // 商品信息
  41. activityId: '' as any, // 活动编号
  42. couponId: '' as string, // 优惠券编号
  43. couponAmount: 0 as number, // 所有的优惠金额 - 继续支付使用
  44. couponDiscountPrice: 0 as number, // 优惠券扣减金额
  45. discountCardPrice: 0 as number, // 畅学卡扣减金额
  46. discountPrice: 0 as number // 优惠
  47. } as any
  48. // orderObject: {
  49. // orderNo: '',
  50. // actualPrice: 28,
  51. // orderName: '小酷Ai月度会员',
  52. // orderDesc: '小酷Ai月度会员',
  53. // orderType: 'VIP',
  54. // recomUserId: 0,
  55. // activityId: null,
  56. // orderList: [
  57. // {
  58. // orderType: 'VIP',
  59. // goodsName: '小酷Ai月度会员',
  60. // id: 2,
  61. // title: '月度会员',
  62. // price: 28,
  63. // startTime: '2023-02-28',
  64. // endTime: '2023-03-28'
  65. // }
  66. // ]
  67. // }
  68. }
  69. }
  70. export const orderStatus = reactive(original())
  71. // 重置对象
  72. export const resestState = () => {
  73. Object.assign(orderStatus, original())
  74. }
  75. // 购买会员多少数量满足条件
  76. export const memberNeedNumber = (item: any) => {
  77. if (item.vipEndDays > 0) {
  78. const endTime = dayjs(item.startTime).add(item.vipEndDays, 'day')
  79. const unit = item.period === 'YEAR' ? 'years' : 'months'
  80. const months = endTime.diff(dayjs(item.startTime), unit)
  81. if (item.period === 'MONTH') {
  82. return months + 1
  83. } else if (item.period === 'QUARTERLY') {
  84. return Math.ceil((months + 1) / 3)
  85. } else if (item.period === 'YEAR_HALF') {
  86. return Math.ceil((months + 1) / 6)
  87. } else if (item.period === 'YEAR') {
  88. return months + 1
  89. } else {
  90. return 1
  91. }
  92. }
  93. return 1
  94. }
  95. export const orderInfos = () => {
  96. // 商品列表
  97. const orderList = orderStatus.orderObject.orderList || []
  98. return orderList.map((item: any) => {
  99. const params = {
  100. goodType: item.orderType,
  101. goodName: item.goodsName,
  102. recomUserId: item.recomUserId, // 推荐人id
  103. bizContent: {}
  104. } as any
  105. if (item.orderType === 'VIDEO') {
  106. params.bizContent = {
  107. videoLessonGroupId: item.courseGroupId,
  108. payMoney: item.coursePrice || 0
  109. }
  110. } else if (item.orderType === 'LIVE' || item.orderType === 'GROUP') {
  111. params.bizContent = {
  112. groupId: item.courseGroupId
  113. }
  114. } else if (item.orderType === 'PRACTICE' || item.orderType === "VIP_COURSE") {
  115. const tempTime = item.classTime || []
  116. const classCourse: any = []
  117. tempTime.forEach((time: any) => {
  118. classCourse.push({
  119. classDate: time.classDate,
  120. startTime: time.startTime,
  121. endTime: time.endTime
  122. })
  123. })
  124. params.bizContent = {
  125. courseGroupName: item.courseGroupName,
  126. courseIntroduce: item.courseIntroduce,
  127. subjectId: item.subjectId,
  128. singleCourseMinutes: item.singleCourseMinutes,
  129. courseNum: item.courseNum,
  130. coursePrice: item.coursePrice,
  131. teacherId: item.teacherId,
  132. classTime: classCourse
  133. }
  134. } else if (item.orderType === 'VIP') {
  135. params.bizContent = item.id
  136. params.goodsNum = item.num
  137. params.bizPrice = item.salePrice
  138. } else if (item.orderType === 'SVIP') {
  139. params.bizContent = item.id
  140. const needNumber = memberNeedNumber(item)
  141. params.vipEndDays = item.num > needNumber ? item.vipEndDays : null
  142. params.goodsNum = item.num
  143. params.bizPrice = item.salePrice
  144. } else if (item.orderType === 'MUSIC') {
  145. params.bizContent = {
  146. musicSheetId: item.id,
  147. actualPrice: item.actualPrice || 0,
  148. clientType: state.platformType
  149. }
  150. } else if (item.orderType === 'ALBUM') {
  151. params.bizContent = {
  152. musicSheetId: item.id,
  153. actualPrice: item.actualPrice || 0,
  154. clientType: state.platformType
  155. }
  156. } else if (item.orderType === 'PIANO_ROOM') {
  157. params.bizContent = item.id
  158. } else if (item.orderType === 'ACTI_REGIST') {
  159. params.bizContent = {
  160. activityId: item.activityId
  161. }
  162. } else if (item.orderType === 'TENANT_ALBUM') {
  163. params.bizContent = {
  164. musicSheetId: item.id,
  165. actualPrice: item.actualPrice || 0,
  166. clientType: state.platformType
  167. }
  168. }
  169. return params
  170. })
  171. }
  172. //
  173. export const orderTenantInfos = (otherGoods: any[] = []) => {
  174. // 商品列表
  175. let orderList: any[] = []
  176. if(otherGoods && otherGoods.length > 0) {
  177. orderList = otherGoods || []
  178. } else {
  179. orderList = orderStatus.orderObject.orderList || []
  180. }
  181. const activityList: any[] = [] // 活动赠品
  182. const infos = orderList.map((item: any) => {
  183. const params: any = {
  184. goodType: item.orderType,
  185. goodName: item.goodsName,
  186. goodNum: 1,
  187. bizContent: {}
  188. } as any
  189. if (item.orderType === 'VIP') {
  190. params.bizContent = item.id
  191. params.bizId = item.id
  192. params.goodsNum = item.num
  193. params.goodNum = item.num
  194. ;(item.activityList || []).map(actItem => {
  195. activityList.push(actItem)
  196. })
  197. } else if (item.orderType === 'SVIP') {
  198. params.bizContent = item.id
  199. // params.vipEndDays = item.vipEndDays
  200. const needNumber = memberNeedNumber(item)
  201. params.vipEndDays = item.num > needNumber ? item.vipEndDays : null
  202. params.goodsNum = item.num
  203. params.goodNum = item.num
  204. ;(item.activityList || []).map(actItem => {
  205. activityList.push(actItem)
  206. })
  207. } else if (item.orderType === 'MUSIC') {
  208. params.bizContent = {
  209. musicSheetId: item.id,
  210. actualPrice: item.actualPrice || 0,
  211. clientType: state.platformType
  212. }
  213. params.bizId = item.id
  214. } else if (item.orderType === 'ALBUM') {
  215. params.bizContent = {
  216. musicSheetId: item.id,
  217. actualPrice: item.actualPrice || 0,
  218. clientType: state.platformType
  219. }
  220. params.bizId = item.id
  221. } else if (item.orderType === 'TENANT_ALBUM') {
  222. console.log(item, 'item')
  223. params.bizContent = {
  224. tenantAlbumId: item.id,
  225. actualPrice: item.actualPrice || 0,
  226. buyNumber: 1,
  227. buyMultiple: 1,
  228. clientType: state.platformType
  229. }
  230. params.tenantGroupAlbumId = item.tenantGroupAlbumId
  231. params.bizId = item.id
  232. params.buyNumber = 1
  233. params.buyMultiple = 1
  234. } else if (item.orderType === 'LIVE' || item.orderType === 'GROUP') {
  235. params.bizContent = {
  236. groupId: item.courseGroupId
  237. }
  238. } else if (item.orderType === 'VIDEO') {
  239. params.bizContent = {
  240. videoLessonGroupId: item.courseGroupId,
  241. payMoney: item.coursePrice || 0
  242. }
  243. } else if (item.orderType === 'PRACTICE' || item.orderType === 'VIP_COURSE') {
  244. const tempTime = item.classTime || []
  245. const classCourse: any = []
  246. tempTime.forEach((time: any) => {
  247. classCourse.push({
  248. classDate: time.classDate,
  249. startTime: time.startTime,
  250. endTime: time.endTime
  251. })
  252. })
  253. params.bizContent = {
  254. courseGroupName: item.courseGroupName,
  255. courseIntroduce: item.courseIntroduce,
  256. teacherSubjectPriceId: item.teacherSubjectPriceId,
  257. subjectId: item.subjectId,
  258. singleCourseMinutes: item.singleCourseMinutes,
  259. courseNum: item.courseNum,
  260. coursePrice: item.coursePrice,
  261. teacherId: item.teacherId,
  262. classTime: classCourse
  263. }
  264. } else if (item.orderType === 'PIANO_ROOM') {
  265. params.bizContent = item.id
  266. } else if (item.orderType === 'ACTI_REGIST') {
  267. params.bizContent = {
  268. activityId: item.activityId
  269. }
  270. } else if(item.orderType == 'DISCOUNT') {
  271. params.bizContent = item.id
  272. params.goodsNum = item.num
  273. params.goodNum = item.num
  274. }
  275. return params
  276. })
  277. return [...infos, ...activityList]
  278. }
  279. /**
  280. * @title 0元购买
  281. * @param {function} callBack 回调函数
  282. * @returns {Promise<void>}
  283. */
  284. export const onSubmitZero = async (callBack?: Function): Promise<void> => {
  285. // 正常支付
  286. try {
  287. const orderObject = orderStatus.orderObject
  288. const url =
  289. state.platformType === 'TEACHER'
  290. ? '/api-teacher/userOrder/executeOrder'
  291. : '/api-student/userOrder/executeOrder'
  292. const res = await request.post(url, {
  293. data: {
  294. orderName: orderObject.orderName,
  295. orderDesc: orderObject.orderDesc,
  296. orderType: orderObject.orderType,
  297. actualPrice: orderObject.actualPrice || 0,
  298. recomUserId: orderObject.recomUserId,
  299. activityId: orderObject.activityId,
  300. orderInfos: [...orderInfos()]
  301. }
  302. })
  303. const result = res.data || {}
  304. // 支付成功
  305. if (result.status == 'PAID') {
  306. if (callBack) {
  307. callBack()
  308. } else {
  309. Dialog.alert({
  310. message: '领取成功',
  311. confirmButtonText: '确定',
  312. confirmButtonColor: '#2dc7aa'
  313. })
  314. }
  315. } else {
  316. Dialog.alert({
  317. message: result.msg,
  318. confirmButtonText: '确定',
  319. confirmButtonColor: '#2dc7aa'
  320. })
  321. }
  322. } catch {
  323. Dialog.alert({
  324. title: '提示',
  325. message: '支付失败,请稍后重试!',
  326. confirmButtonText: '确定',
  327. confirmButtonColor: '#2dc7aa'
  328. })
  329. }
  330. }
  331. /**
  332. * @author wxl
  333. * @description 处理基础支付功能
  334. */
  335. // 判断浏览器与支付类型,返回对应页面名称
  336. export const beforeSubmit = (pt: string) => {
  337. // const pt = state.pay_channel
  338. let payCode = 'qrCode'
  339. // 判断当前浏览器
  340. if (browser().weixin) {
  341. // 微信浏览器
  342. if (pt == 'alipay_qr' || pt == 'alipay_wap') {
  343. payCode = 'qrCode'
  344. } else if (pt == 'wx_pub') {
  345. payCode = 'pay'
  346. }
  347. } else if (browser().alipay) {
  348. // 支付宝浏览器
  349. if (pt == 'alipay_wap') {
  350. // 支付宝 H5 支付
  351. payCode = 'pay'
  352. } else {
  353. payCode = 'qrCode'
  354. }
  355. } else {
  356. payCode = 'qrCode'
  357. }
  358. return payCode == 'qrCode' ? 'payDefine' : 'payResult'
  359. }