orders.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import { api_executePayment, api_queryByParamName, api_studentOrderPage, api_userPaymentCancelRefund, api_userPaymentOrderUnpaid } from "../../api/login";
  2. import { formatPrice } from "../../utils/util";
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. serviceShow: true,
  11. tabList: [
  12. {
  13. id: 0,
  14. label: "全部",
  15. },
  16. {
  17. id: 1,
  18. label: "待支付",
  19. },
  20. // {
  21. // id: 2,
  22. // label: "待使用",
  23. // },
  24. {
  25. id: 3,
  26. label: "已完成",
  27. },
  28. {
  29. id: 4,
  30. label: "已取消",
  31. },
  32. // {
  33. // id: 5, // 改编号会影响详情页的退款按钮
  34. // label: "售后",
  35. // },
  36. ],
  37. paymentType: null as any, // 支付类型
  38. paymentChannel: null as any,
  39. tabIdx: 0, // 当前选中的tab索引
  40. page: 1,
  41. rows: 10,
  42. recordList: [],
  43. maxPage: 1, // 总分页数
  44. refoundStatus: false,
  45. cancelRefoundStatus: false,
  46. goodsInfo: {}, // 选中的数据
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad() {
  52. },
  53. onShow() {
  54. this.setData({
  55. serviceShow: true,
  56. page: 1,
  57. maxPage: 1,
  58. recordList: [],
  59. }, () => {
  60. this.getList(false)
  61. })
  62. },
  63. onHide() {
  64. this.setData({
  65. serviceShow: false
  66. })
  67. },
  68. /** 切换分类 */
  69. switchTab(e: { currentTarget: { dataset: { idx: any } } }) {
  70. const idx = e.currentTarget.dataset.idx;
  71. if (idx != this.data.tabIdx) {
  72. this.setData(
  73. {
  74. tabIdx: idx,
  75. page: 1,
  76. maxPage: 1,
  77. recordList: [],
  78. },
  79. () => {
  80. this.getList(false);
  81. }
  82. );
  83. }
  84. },
  85. async getList(noLoading: boolean) {
  86. if (!noLoading) {
  87. wx.showLoading({
  88. mask: true,
  89. title: "加载中...",
  90. });
  91. }
  92. const currentPage = this.data.page,
  93. currentRow = this.data.rows,
  94. tabIdx = this.data.tabIdx;
  95. try {
  96. // @ApiModelProperty("订单状态 WAIT_PAY:待支付,WAIT_USE:待使用,SUCCESS:已完成,CLOSE:已取消")
  97. const { data } = await api_studentOrderPage({
  98. openId: app.globalData.userInfo?.liteOpenid,
  99. page: currentPage,
  100. rows: this.data.rows,
  101. version: 'V2',
  102. wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "",
  103. })
  104. if (data.code == 200) {
  105. const { rows, total } = data.data;
  106. rows.forEach((item: any) => {
  107. item.amount = formatPrice(item.paymentCashAmount, 'ALL')
  108. item.statusName = this.formatOrderStatus(item.wechatStatus)
  109. let originalPrice = 0
  110. const studentPaymentOrderDetails = item.studentPaymentOrderDetails || [];
  111. studentPaymentOrderDetails.forEach((student: any) => {
  112. student.originalPrice = formatPrice(student.originalPrice || 0, 'ALL');
  113. const prices: any = formatPrice(student.paymentCashAmount || 0);
  114. student.integerPart = prices.integerPart;
  115. student.decimalPart = prices.decimalPart;
  116. // student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  117. // 格式化赠送内容
  118. // student.giftLongTime = this.formatGiftPeriod(student.giftVipDay, student.giftPeriod);
  119. // 总的日常价
  120. originalPrice += Number(student.originalPrice || 0)
  121. })
  122. item.originalPrice = originalPrice
  123. item.discountPrice = formatPrice(
  124. originalPrice - item.paymentCashAmount,
  125. "ALL"
  126. )
  127. // console.log(originalPrice, "originalPrice")
  128. item.studentPaymentOrderDetails = studentPaymentOrderDetails
  129. });
  130. const newList = this.data.recordList.concat(rows);
  131. this.setData(
  132. {
  133. recordList: newList,
  134. maxPage: Math.ceil(total / currentRow),
  135. },
  136. () => wx.hideLoading()
  137. );
  138. } else {
  139. wx.hideLoading();
  140. }
  141. } catch (e) {
  142. console.log(e, 'e')
  143. wx.hideLoading()
  144. }
  145. },
  146. // 格式化中文
  147. formatOrderStatus(status: string) {
  148. // 订单状态 WAIT_PAY:待支付, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消
  149. const template: any = {
  150. WAIT_PAY: '待支付',
  151. WAIT_USE: '待使用',
  152. PAID: '已完成',
  153. CLOSED: '已取消',
  154. REFUNDING: '退款中',
  155. REFUNDED: '退款成功'
  156. }
  157. return template[status]
  158. },
  159. // 格式化类型
  160. formatPeriod(num: number, type: string) {
  161. if (!num || !type) {
  162. return ''
  163. }
  164. const template: any = {
  165. DAY: "天卡",
  166. MONTH: "月卡",
  167. YEAR: "年卡"
  168. }
  169. if (type === "YEAR" && num >= 99) {
  170. return '永久卡'
  171. }
  172. return num + template[type]
  173. },
  174. formatGiftPeriod(num: number, type: string) {
  175. if (!num || !type) {
  176. return ''
  177. }
  178. const template: any = {
  179. DAY: "天",
  180. MONTH: "个月",
  181. YEAR: "年"
  182. }
  183. return num + template[type]
  184. },
  185. /** 加载更多 */
  186. loadMore() {
  187. const currentPage = this.data.page;
  188. if (this.data.page >= this.data.maxPage) {
  189. // wx.showToast({
  190. // title: "没有更多数据了",
  191. // icon: "none",
  192. // duration: 1000,
  193. // });
  194. } else {
  195. this.setData(
  196. {
  197. page: currentPage + 1,
  198. },
  199. () => {
  200. this.getList(false);
  201. }
  202. );
  203. }
  204. },
  205. onPay(e: any) {
  206. const { dataset } = e.currentTarget
  207. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  208. if (item) {
  209. this.onSubmit({
  210. orderNo: item.orderNo
  211. })
  212. }
  213. },
  214. onOne() {
  215. wx.redirectTo({
  216. url: '../index/index',
  217. })
  218. },
  219. onDetail(e: any) {
  220. const { dataset } = e.currentTarget
  221. if (dataset.wechatstatus === "WAIT_PAY") {
  222. const orderDetail: any = this.data.recordList.find((item: any) => item.orderNo === dataset.orderno)
  223. if (!orderDetail) return
  224. const details = orderDetail.studentPaymentOrderDetails || []
  225. const params = [] as any
  226. details.forEach((item: any) => {
  227. params.push({
  228. pic: item.goodsUrl,
  229. name: item.goodsName,
  230. originalPrice: item.originalPrice,
  231. salePrice: item.paymentCashAmount,
  232. // shopId: item.shopId,
  233. // id: item.id,
  234. goodsType: item.goodsType, // INSTRUMENTS
  235. })
  236. })
  237. let info = JSON.stringify({
  238. ...params
  239. });
  240. info = encodeURIComponent(info);
  241. wx.navigateTo({
  242. url: `../orders/order-detail?orderInfo=${info}&orderNo=${orderDetail.orderNo}&status=${orderDetail.wechatStatus}`,
  243. });
  244. } else {
  245. wx.navigateTo({
  246. url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  247. })
  248. }
  249. },
  250. // 购买
  251. async onSubmit(goodsInfo: any) {
  252. wx.showLoading({
  253. mask: true,
  254. title: "订单提交中...",
  255. });
  256. try {
  257. const { orderNo } = goodsInfo
  258. const { data } = await api_userPaymentOrderUnpaid({
  259. orderNo: orderNo,
  260. paymentType: 'WECHAT_MINI'
  261. })
  262. if (data.code === 200) {
  263. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  264. this.onExecutePay(paymentConfig, paymentType, orderNo)
  265. } else {
  266. this.onPayError()
  267. }
  268. } catch {
  269. wx.hideLoading()
  270. }
  271. },
  272. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  273. wx.login({
  274. success: async (wxres: any) => {
  275. const res = await api_executePayment({
  276. merOrderNo: paymentConfig.merOrderNo,
  277. paymentChannel: this.data.paymentChannel || 'wx_lite',
  278. paymentType,
  279. userId: app.globalData.userInfo?.id,
  280. code: wxres.code,
  281. wxMiniAppId: app.globalData.appId
  282. })
  283. wx.hideLoading()
  284. if (res.data.code === 200) {
  285. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  286. } else {
  287. this.onPayError(res.data.message)
  288. }
  289. },
  290. fail: () => {
  291. this.onPayError()
  292. }
  293. })
  294. },
  295. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  296. const isYeePay = paymentType.indexOf('yeepay') !== -1
  297. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  298. : paymentConfig?.expend
  299. ? JSON.parse(paymentConfig?.expend?.pay_info)
  300. : paymentConfig
  301. const that = this
  302. wx.requestPayment({
  303. timeStamp: prePayInfo.timeStamp,
  304. nonceStr: prePayInfo.nonceStr,
  305. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  306. paySign: prePayInfo.paySign,
  307. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  308. success() {
  309. wx.showToast({ title: '支付成功', icon: 'success' });
  310. // that.onRefoundComfirm()
  311. },
  312. fail(ressonInfo) {
  313. console.log('支付失败', ressonInfo)
  314. that.onPayError()
  315. }
  316. })
  317. },
  318. // 获取后台配置的支付方式
  319. async queryPayType() {
  320. try {
  321. // wxlite_payment_service_provider
  322. const { data } = await api_queryByParamName({
  323. paramName: app.globalData.appId
  324. });
  325. if (data.code == 200) {
  326. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  327. this.setData({
  328. paymentType: paramValue.vendor,
  329. paymentChannel: paramValue.channel
  330. });
  331. }
  332. } catch (error) {
  333. console.log(error, "error");
  334. }
  335. },
  336. onPayError(message?: string) {
  337. wx.hideLoading()
  338. wx.showToast({
  339. title: message || '支付取消',
  340. icon: 'none'
  341. })
  342. },
  343. async onRefounded(e: any) {
  344. const { dataset } = e.currentTarget
  345. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  346. console.log(dataset, item, 'item')
  347. if (!item) {
  348. return
  349. }
  350. if (item.wechatStatus === "REFUNDING") {
  351. this.setData({
  352. cancelRefoundStatus: true
  353. })
  354. try {
  355. const refundOrderId = item.refundOrderId
  356. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  357. console.log(data, 'data')
  358. if (data.code == 200) {
  359. wx.showToast({ title: '取消退款成功', icon: 'none' })
  360. this.onRefoundComfirm()
  361. } else {
  362. wx.showToast({ title: data.message, icon: 'none' })
  363. this.setData({
  364. cancelRefoundStatus: false
  365. })
  366. }
  367. } catch { }
  368. } else {
  369. const { orderNo, studentPaymentOrderDetails } = item
  370. const goodsInfo: any = {
  371. orderNo,
  372. goods: []
  373. }
  374. if (Array.isArray(studentPaymentOrderDetails)) {
  375. studentPaymentOrderDetails.forEach((item: any) => {
  376. goodsInfo.goods.push({
  377. ...item,
  378. id: item.userPaymentOrderDetailId,
  379. currentPrice: item.paymentCashAmount
  380. })
  381. })
  382. }
  383. this.setData({
  384. goodsInfo,
  385. cancelRefoundStatus: true,
  386. refoundStatus: true
  387. })
  388. }
  389. },
  390. changeRefoundStatus(e: { detail: any }) {
  391. this.setData({
  392. cancelRefoundStatus: false,
  393. refoundStatus: e.detail
  394. })
  395. },
  396. onRefoundComfirm() {
  397. const that = this
  398. this.setData({
  399. refoundStatus: false
  400. })
  401. // setTimeout(() => {
  402. that.setData({
  403. cancelRefoundStatus: false,
  404. page: 1,
  405. maxPage: 1,
  406. recordList: [],
  407. }, () => {
  408. this.getList(true)
  409. })
  410. // }, 1500);
  411. },
  412. onShareAppMessage() {
  413. return {
  414. title: '音乐数字AI',
  415. path: '/pages/index/index',
  416. imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png'
  417. }
  418. },
  419. })