orders.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. }
  239. } catch {
  240. wx.hideLoading()
  241. }
  242. },
  243. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  244. wx.login({
  245. success: async (wxres: any) => {
  246. const res = await api_executePayment({
  247. merOrderNo: paymentConfig.merOrderNo,
  248. paymentChannel: this.data.paymentChannel || 'wx_lite',
  249. paymentType,
  250. userId: app.globalData.userInfo?.id,
  251. code: wxres.code,
  252. wxMiniAppId: app.globalData.appId
  253. })
  254. wx.hideLoading()
  255. if (res.data.code === 200) {
  256. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  257. } else {
  258. this.onPayError(res.data.message)
  259. }
  260. },
  261. fail: () => {
  262. this.onPayError()
  263. }
  264. })
  265. },
  266. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  267. const isYeePay = paymentType.indexOf('yeepay') !== -1
  268. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  269. : paymentConfig?.expend
  270. ? JSON.parse(paymentConfig?.expend?.pay_info)
  271. : paymentConfig
  272. const that = this
  273. wx.requestPayment({
  274. timeStamp: prePayInfo.timeStamp,
  275. nonceStr: prePayInfo.nonceStr,
  276. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  277. paySign: prePayInfo.paySign,
  278. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  279. success() {
  280. wx.showToast({ title: '支付成功', icon: 'success' });
  281. // that.onRefoundComfirm()
  282. },
  283. fail(ressonInfo) {
  284. console.log('支付失败', ressonInfo)
  285. that.onPayError()
  286. }
  287. })
  288. },
  289. // 获取后台配置的支付方式
  290. async queryPayType() {
  291. try {
  292. // wxlite_payment_service_provider
  293. const { data } = await api_queryByParamName({
  294. paramName: app.globalData.appId
  295. });
  296. if (data.code == 200) {
  297. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  298. this.setData({
  299. paymentType: paramValue.vendor,
  300. paymentChannel: paramValue.channel
  301. });
  302. }
  303. } catch (error) {
  304. console.log(error, "error");
  305. }
  306. },
  307. onPayError(message?: string) {
  308. wx.hideLoading()
  309. wx.showToast({
  310. title: message || '支付取消',
  311. icon: 'none'
  312. })
  313. },
  314. async onRefounded(e: any) {
  315. const { dataset } = e.currentTarget
  316. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  317. console.log(dataset, item, 'item')
  318. if (!item) {
  319. return
  320. }
  321. if (item.wechatStatus === "REFUNDING") {
  322. this.setData({
  323. cancelRefoundStatus: true
  324. }, async () => {
  325. try {
  326. const refundOrderId = item.refundOrderId
  327. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  328. console.log(data, 'data')
  329. if (data.code == 200) {
  330. wx.showToast({ title: '你已成功取消退款', icon: 'none' })
  331. this.onRefoundComfirm()
  332. } else {
  333. wx.showToast({ title: data.message, icon: 'none' })
  334. this.setData({
  335. cancelRefoundStatus: false
  336. })
  337. }
  338. } catch { }
  339. })
  340. } else {
  341. const { orderNo, studentPaymentOrderDetails } = item
  342. const goodsInfo: any = {
  343. orderNo,
  344. goods: []
  345. }
  346. if (Array.isArray(studentPaymentOrderDetails)) {
  347. studentPaymentOrderDetails.forEach((item: any) => {
  348. goodsInfo.goods.push({
  349. ...item,
  350. id: item.userPaymentOrderDetailId,
  351. currentPrice: item.paymentCashAmount
  352. })
  353. })
  354. }
  355. this.setData({
  356. goodsInfo,
  357. cancelRefoundStatus: true,
  358. refoundStatus: true
  359. })
  360. }
  361. },
  362. changeRefoundStatus(e: { detail: any }) {
  363. this.setData({
  364. refoundStatus: e.detail,
  365. cancelRefoundStatus: false,
  366. })
  367. },
  368. onRefoundComfirm() {
  369. const that = this
  370. this.setData({
  371. refoundStatus: false,
  372. })
  373. setTimeout(() => {
  374. that.setData({
  375. page: 1,
  376. maxPage: 1,
  377. recordList: [],
  378. cancelRefoundStatus: false,
  379. }, () => {
  380. this.getList()
  381. })
  382. }, 1500);
  383. },
  384. onShareAppMessage() {
  385. return {
  386. title: '音乐数字AI器乐工具',
  387. path: '/pages/index/index',
  388. imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png'
  389. }
  390. }
  391. })