orders.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  266. wx.hideLoading()
  267. wx.showToast({
  268. title: data.message,
  269. icon: 'none'
  270. })
  271. setTimeout(() => {
  272. this.setData({
  273. page: 1,
  274. maxPage: 1,
  275. recordList: [],
  276. }, () => {
  277. this.getList(false)
  278. })
  279. }, 1000)
  280. } else {
  281. this.onPayError()
  282. }
  283. } catch {
  284. wx.hideLoading()
  285. }
  286. },
  287. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  288. wx.login({
  289. success: async (wxres: any) => {
  290. const res = await api_executePayment({
  291. merOrderNo: paymentConfig.merOrderNo,
  292. paymentChannel: this.data.paymentChannel || 'wx_lite',
  293. paymentType,
  294. userId: app.globalData.userInfo?.id,
  295. code: wxres.code,
  296. wxMiniAppId: app.globalData.appId
  297. })
  298. wx.hideLoading()
  299. if (res.data.code === 200) {
  300. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  301. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  302. wx.hideLoading()
  303. wx.showToast({
  304. title: res.data.message,
  305. icon: 'none'
  306. })
  307. setTimeout(() => {
  308. this.setData({
  309. page: 1,
  310. maxPage: 1,
  311. recordList: [],
  312. }, () => {
  313. this.getList(false)
  314. })
  315. }, 1000)
  316. } else {
  317. this.onPayError(res.data.message)
  318. }
  319. },
  320. fail: () => {
  321. this.onPayError()
  322. }
  323. })
  324. },
  325. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  326. const isYeePay = paymentType.indexOf('yeepay') !== -1
  327. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  328. : paymentConfig?.expend
  329. ? JSON.parse(paymentConfig?.expend?.pay_info)
  330. : paymentConfig
  331. const that = this
  332. wx.requestPayment({
  333. timeStamp: prePayInfo.timeStamp,
  334. nonceStr: prePayInfo.nonceStr,
  335. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  336. paySign: prePayInfo.paySign,
  337. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  338. success() {
  339. wx.showToast({ title: '支付成功', icon: 'success' });
  340. // that.onRefoundComfirm()
  341. },
  342. fail(ressonInfo) {
  343. console.log('支付失败', ressonInfo)
  344. that.onPayError()
  345. }
  346. })
  347. },
  348. // 获取后台配置的支付方式
  349. async queryPayType() {
  350. try {
  351. // wxlite_payment_service_provider
  352. const { data } = await api_queryByParamName({
  353. paramName: app.globalData.appId
  354. });
  355. if (data.code == 200) {
  356. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  357. this.setData({
  358. paymentType: paramValue.vendor,
  359. paymentChannel: paramValue.channel
  360. });
  361. }
  362. } catch (error) {
  363. console.log(error, "error");
  364. }
  365. },
  366. onPayError(message?: string) {
  367. wx.hideLoading()
  368. wx.showToast({
  369. title: message || '支付取消',
  370. icon: 'none'
  371. })
  372. },
  373. async onRefounded(e: any) {
  374. const { dataset } = e.currentTarget
  375. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  376. console.log(dataset, item, 'item')
  377. if (!item) {
  378. return
  379. }
  380. if (item.wechatStatus === "REFUNDING") {
  381. this.setData({
  382. cancelRefoundStatus: true
  383. })
  384. try {
  385. const refundOrderId = item.refundOrderId
  386. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  387. console.log(data, 'data')
  388. if (data.code == 200) {
  389. wx.showToast({ title: '取消退款成功', icon: 'none' })
  390. this.onRefoundComfirm()
  391. } else {
  392. wx.showToast({ title: data.message, icon: 'none' })
  393. this.setData({
  394. cancelRefoundStatus: false
  395. })
  396. }
  397. } catch { }
  398. } else {
  399. const { orderNo, studentPaymentOrderDetails } = item
  400. const goodsInfo: any = {
  401. orderNo,
  402. goods: []
  403. }
  404. if (Array.isArray(studentPaymentOrderDetails)) {
  405. studentPaymentOrderDetails.forEach((item: any) => {
  406. goodsInfo.goods.push({
  407. ...item,
  408. id: item.userPaymentOrderDetailId,
  409. currentPrice: item.paymentCashAmount
  410. })
  411. })
  412. }
  413. this.setData({
  414. goodsInfo,
  415. cancelRefoundStatus: true,
  416. refoundStatus: true
  417. })
  418. }
  419. },
  420. changeRefoundStatus(e: { detail: any }) {
  421. this.setData({
  422. cancelRefoundStatus: false,
  423. refoundStatus: e.detail
  424. })
  425. },
  426. onRefoundComfirm() {
  427. const that = this
  428. this.setData({
  429. refoundStatus: false
  430. })
  431. // setTimeout(() => {
  432. that.setData({
  433. cancelRefoundStatus: false,
  434. page: 1,
  435. maxPage: 1,
  436. recordList: [],
  437. }, () => {
  438. this.getList(true)
  439. })
  440. // }, 1500);
  441. },
  442. onShareAppMessage() {
  443. return {
  444. title: '音乐数字AI',
  445. path: '/pages/index/index',
  446. imageUrl: 'https://oss.dayaedu.com/ktyq/1739870592907.png'
  447. }
  448. },
  449. })