orders.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. tabList: [
  10. {
  11. id: 0,
  12. label: "全部订单",
  13. },
  14. {
  15. id: 1,
  16. label: "待支付",
  17. },
  18. // {
  19. // id: 2,
  20. // label: "待使用",
  21. // },
  22. {
  23. id: 3,
  24. label: "已完成",
  25. },
  26. {
  27. id: 4,
  28. label: "已取消",
  29. },
  30. // {
  31. // id: 5,
  32. // label: "售后",
  33. // },
  34. // {
  35. // id: 6,
  36. // label: "已退款",
  37. // },
  38. ],
  39. tabIdx: 0, // 当前选中的tab索引
  40. page: 1,
  41. rows: 10,
  42. recordList: [],
  43. maxPage: 1, // 总分页数
  44. serviceShow: true,
  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()
  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();
  82. }
  83. );
  84. }
  85. },
  86. async getList() {
  87. wx.showLoading({
  88. mask: true,
  89. title: "加载中...",
  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. "version": "V2",
  98. openId: app.globalData.userInfo?.liteOpenid,
  99. page: currentPage,
  100. rows: this.data.rows,
  101. wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "",
  102. })
  103. if (data.code == 200) {
  104. const { rows, total } = data.data;
  105. rows.forEach((item: any) => {
  106. item.amount = this.formatPrice(item.paymentCashAmount, 'ALL')
  107. item.statusName = this.formatOrderStatus(item.wechatStatus)
  108. const studentPaymentOrderDetails = item.studentPaymentOrderDetails || [];
  109. studentPaymentOrderDetails.forEach((student: any) => {
  110. student.originalPrice = this.formatPrice(student.paymentCashAmount, 'ALL');
  111. const prices: any = this.formatPrice(student.paymentCashAmount)
  112. student.integerPart = prices.integerPart
  113. student.decimalPart = prices.decimalPart
  114. student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  115. })
  116. item.studentPaymentOrderDetails = studentPaymentOrderDetails
  117. });
  118. const newList = this.data.recordList.concat(rows);
  119. this.setData(
  120. {
  121. recordList: newList,
  122. maxPage: Math.ceil(total / currentRow),
  123. },
  124. () => wx.hideLoading()
  125. );
  126. } else {
  127. wx.hideLoading();
  128. }
  129. } catch (e) {
  130. console.log(e, 'e')
  131. wx.hideLoading()
  132. }
  133. },
  134. // 格式化中文
  135. formatOrderStatus(status: string) {
  136. // 订单状态 WAIT_PAY:待付款, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消
  137. const template: any = {
  138. WAIT_PAY: '待支付',
  139. WAIT_USE: '待使用',
  140. PAID: '已完成',
  141. CLOSED: '已取消',
  142. REFUNDING: '退款中',
  143. REFUNDED: '已退款'
  144. }
  145. return template[status]
  146. },
  147. // 格式化价格
  148. formatPrice(price: number, type?: string) {
  149. const amountStr = price.toFixed(2)
  150. const [integerPart, decimalPart] = amountStr.split('.');
  151. if (type === 'ALL') {
  152. return amountStr
  153. }
  154. return {
  155. integerPart,
  156. decimalPart
  157. }
  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. /** 加载更多 */
  175. loadMore() {
  176. const currentPage = this.data.page;
  177. if (this.data.page >= this.data.maxPage) {
  178. // wx.showToast({
  179. // title: "没有更多数据了",
  180. // icon: "none",
  181. // duration: 1000,
  182. // });
  183. } else {
  184. this.setData(
  185. {
  186. page: currentPage + 1,
  187. },
  188. () => {
  189. this.getList();
  190. }
  191. );
  192. }
  193. },
  194. onPay(e: any) {
  195. const { dataset } = e.currentTarget
  196. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  197. if (item) {
  198. this.onSubmit({
  199. orderNo: item.orderNo
  200. })
  201. }
  202. },
  203. onOne() {
  204. wx.redirectTo({
  205. url: '../index/index',
  206. })
  207. },
  208. onDetail(e: any) {
  209. const { dataset } = e.currentTarget
  210. wx.navigateTo({
  211. url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  212. })
  213. // if (dataset.wechatstatus === "WAIT_PAY") {
  214. // this.onSubmit({ orderNo: dataset.orderno })
  215. // } else {
  216. // wx.navigateTo({
  217. // url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  218. // })
  219. // }
  220. },
  221. // 购买
  222. async onSubmit(goodsInfo: any) {
  223. wx.showLoading({
  224. mask: true,
  225. title: "订单提交中...",
  226. });
  227. try {
  228. const { orderNo } = goodsInfo
  229. const { data } = await api_userPaymentOrderUnpaid({
  230. orderNo: orderNo,
  231. paymentType: 'WECHAT_MINI'
  232. })
  233. if (data.code === 200) {
  234. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  235. this.onExecutePay(paymentConfig, paymentType, orderNo)
  236. } else {
  237. this.onPayError()
  238. this.setData(
  239. {
  240. page: 1,
  241. maxPage: 1,
  242. recordList: [],
  243. },
  244. () => {
  245. this.getList();
  246. }
  247. );
  248. }
  249. } catch {
  250. wx.hideLoading()
  251. }
  252. },
  253. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  254. wx.login({
  255. success: async (wxres: any) => {
  256. const res = await api_executePayment({
  257. merOrderNo: paymentConfig.merOrderNo,
  258. paymentChannel: this.data.paymentChannel || 'wx_lite',
  259. paymentType,
  260. userId: app.globalData.userInfo?.id,
  261. code: wxres.code,
  262. wxMiniAppId: app.globalData.appId
  263. })
  264. wx.hideLoading()
  265. if (res.data.code === 200) {
  266. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  267. } else {
  268. this.setData(
  269. {
  270. page: 1,
  271. maxPage: 1,
  272. recordList: [],
  273. },
  274. () => {
  275. this.getList();
  276. }
  277. );
  278. this.onPayError(res.data.message)
  279. }
  280. },
  281. fail: () => {
  282. this.onPayError()
  283. }
  284. })
  285. },
  286. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  287. const isYeePay = paymentType.indexOf('yeepay') !== -1
  288. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  289. : paymentConfig?.expend
  290. ? JSON.parse(paymentConfig?.expend?.pay_info)
  291. : paymentConfig
  292. const that = this
  293. wx.requestPayment({
  294. timeStamp: prePayInfo.timeStamp,
  295. nonceStr: prePayInfo.nonceStr,
  296. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  297. paySign: prePayInfo.paySign,
  298. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  299. success() {
  300. wx.showToast({ title: '支付成功', icon: 'success' });
  301. // that.onRefoundComfirm()
  302. },
  303. fail(ressonInfo) {
  304. console.log('支付失败', ressonInfo)
  305. that.onPayError()
  306. }
  307. })
  308. },
  309. // 获取后台配置的支付方式
  310. async queryPayType() {
  311. try {
  312. // wxlite_payment_service_provider
  313. const { data } = await api_queryByParamName({
  314. paramName: app.globalData.appId
  315. });
  316. if (data.code == 200) {
  317. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  318. this.setData({
  319. paymentType: paramValue.vendor,
  320. paymentChannel: paramValue.channel
  321. });
  322. }
  323. } catch (error) {
  324. console.log(error, "error");
  325. }
  326. },
  327. onPayError(message?: string) {
  328. wx.hideLoading()
  329. wx.showToast({
  330. title: message || '支付取消',
  331. icon: 'none'
  332. })
  333. },
  334. async onRefounded(e: any) {
  335. const { dataset } = e.currentTarget
  336. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  337. console.log(dataset, item, 'item')
  338. if (!item) {
  339. return
  340. }
  341. if (item.wechatStatus === "REFUNDING") {
  342. this.setData({
  343. cancelRefoundStatus: true
  344. }, async () => {
  345. try {
  346. const refundOrderId = item.refundOrderId
  347. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  348. console.log(data, 'data')
  349. if (data.code == 200) {
  350. wx.showToast({ title: '你已成功取消退款', icon: 'none' })
  351. this.onRefoundComfirm()
  352. } else {
  353. wx.showToast({ title: data.message, icon: 'none' })
  354. this.setData({
  355. cancelRefoundStatus: false
  356. })
  357. }
  358. } catch { }
  359. })
  360. } else {
  361. const { orderNo, studentPaymentOrderDetails } = item
  362. const goodsInfo: any = {
  363. orderNo,
  364. goods: []
  365. }
  366. if (Array.isArray(studentPaymentOrderDetails)) {
  367. studentPaymentOrderDetails.forEach((item: any) => {
  368. goodsInfo.goods.push({
  369. ...item,
  370. id: item.userPaymentOrderDetailId,
  371. currentPrice: item.paymentCashAmount
  372. })
  373. })
  374. }
  375. this.setData({
  376. goodsInfo,
  377. cancelRefoundStatus: true,
  378. refoundStatus: true
  379. })
  380. }
  381. },
  382. changeRefoundStatus(e: { detail: any }) {
  383. this.setData({
  384. refoundStatus: e.detail,
  385. cancelRefoundStatus: false,
  386. })
  387. },
  388. onRefoundComfirm() {
  389. const that = this
  390. this.setData({
  391. refoundStatus: false,
  392. })
  393. setTimeout(() => {
  394. that.setData({
  395. page: 1,
  396. maxPage: 1,
  397. recordList: [],
  398. cancelRefoundStatus: false,
  399. }, () => {
  400. this.getList()
  401. })
  402. }, 1500);
  403. },
  404. onShareAppMessage() {
  405. return {
  406. title: '音乐数字AI器乐工具',
  407. path: '/pages/index/index',
  408. imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png'
  409. }
  410. }
  411. })