orders.ts 13 KB

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