orders.ts 11 KB

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