orders.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. const prices: any = this.formatPrice(student.paymentCashAmount)
  111. student.integerPart = prices.integerPart
  112. student.decimalPart = prices.decimalPart
  113. student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  114. })
  115. item.studentPaymentOrderDetails = studentPaymentOrderDetails
  116. });
  117. const newList = this.data.recordList.concat(rows);
  118. this.setData(
  119. {
  120. recordList: newList,
  121. maxPage: Math.ceil(total / currentRow),
  122. },
  123. () => wx.hideLoading()
  124. );
  125. } else {
  126. wx.hideLoading();
  127. }
  128. } catch (e) {
  129. console.log(e, 'e')
  130. wx.hideLoading()
  131. }
  132. },
  133. // 格式化中文
  134. formatOrderStatus(status: string) {
  135. // 订单状态 WAIT_PAY:待付款, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消
  136. const template: any = {
  137. WAIT_PAY: '等待支付',
  138. WAIT_USE: '待使用',
  139. PAID: '交易成功',
  140. CLOSED: '交易取消',
  141. REFUNDING: '退款中',
  142. REFUNDED: '已退款'
  143. }
  144. return template[status]
  145. },
  146. // 格式化价格
  147. formatPrice(price: number, type?: string) {
  148. const amountStr = price.toFixed(2)
  149. const [integerPart, decimalPart] = amountStr.split('.');
  150. if (type === 'ALL') {
  151. return amountStr
  152. }
  153. return {
  154. integerPart,
  155. decimalPart
  156. }
  157. },
  158. // 格式化类型
  159. formatPeriod(num: number, type: string) {
  160. if (!num || !type) {
  161. return ''
  162. }
  163. const template: any = {
  164. DAY: "天",
  165. MONTH: "月",
  166. YEAR: "年"
  167. }
  168. if (type === "YEAR" && num >= 99) {
  169. return '时长: 永久'
  170. }
  171. return "时长: " + num + template[type]
  172. },
  173. /** 加载更多 */
  174. loadMore() {
  175. const currentPage = this.data.page;
  176. if (this.data.page >= this.data.maxPage) {
  177. // wx.showToast({
  178. // title: "没有更多数据了",
  179. // icon: "none",
  180. // duration: 1000,
  181. // });
  182. } else {
  183. this.setData(
  184. {
  185. page: currentPage + 1,
  186. },
  187. () => {
  188. this.getList();
  189. }
  190. );
  191. }
  192. },
  193. onPay(e: any) {
  194. const { dataset } = e.currentTarget
  195. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  196. if (item) {
  197. this.onSubmit({
  198. orderNo: item.orderNo
  199. })
  200. }
  201. },
  202. onOne() {
  203. wx.redirectTo({
  204. url: '../index/index',
  205. })
  206. },
  207. onDetail(e: any) {
  208. const { dataset } = e.currentTarget
  209. wx.navigateTo({
  210. url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  211. })
  212. // if (dataset.wechatstatus === "WAIT_PAY") {
  213. // this.onSubmit({ orderNo: dataset.orderno })
  214. // } else {
  215. // wx.navigateTo({
  216. // url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  217. // })
  218. // }
  219. },
  220. // 购买
  221. async onSubmit(goodsInfo: any) {
  222. wx.showLoading({
  223. mask: true,
  224. title: "订单提交中...",
  225. });
  226. try {
  227. const { orderNo } = goodsInfo
  228. const { data } = await api_userPaymentOrderUnpaid({
  229. orderNo: orderNo,
  230. paymentType: 'WECHAT_MINI'
  231. })
  232. if (data.code === 200) {
  233. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  234. this.onExecutePay(paymentConfig, paymentType, orderNo)
  235. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  236. wx.hideLoading()
  237. wx.showToast({
  238. title: data.message,
  239. icon: 'none'
  240. })
  241. setTimeout(() => {
  242. this.setData(
  243. {
  244. page: 1,
  245. maxPage: 1,
  246. recordList: [],
  247. },
  248. () => {
  249. this.getList();
  250. }
  251. );
  252. }, 1000)
  253. } else {
  254. this.onPayError(data.message)
  255. }
  256. } catch {
  257. wx.hideLoading()
  258. }
  259. },
  260. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  261. wx.login({
  262. success: async (wxres: any) => {
  263. const res = await api_executePayment({
  264. merOrderNo: paymentConfig.merOrderNo,
  265. paymentChannel: this.data.paymentChannel || 'wx_lite',
  266. paymentType,
  267. userId: app.globalData.userInfo?.id,
  268. code: wxres.code,
  269. wxMiniAppId: app.globalData.appId
  270. })
  271. wx.hideLoading()
  272. if (res.data.code === 200) {
  273. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  274. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  275. wx.hideLoading()
  276. wx.showToast({
  277. title: res.data.message,
  278. icon: 'none'
  279. })
  280. setTimeout(() => {
  281. this.setData(
  282. {
  283. page: 1,
  284. maxPage: 1,
  285. recordList: [],
  286. },
  287. () => {
  288. this.getList();
  289. }
  290. );
  291. }, 1000)
  292. } else {
  293. this.onPayError(res.data.message)
  294. }
  295. },
  296. fail: () => {
  297. this.onPayError()
  298. }
  299. })
  300. },
  301. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  302. const isYeePay = paymentType.indexOf('yeepay') !== -1
  303. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  304. : paymentConfig?.expend
  305. ? JSON.parse(paymentConfig?.expend?.pay_info)
  306. : paymentConfig
  307. const that = this
  308. wx.requestPayment({
  309. timeStamp: prePayInfo.timeStamp,
  310. nonceStr: prePayInfo.nonceStr,
  311. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  312. paySign: prePayInfo.paySign,
  313. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  314. success() {
  315. wx.showToast({ title: '支付成功', icon: 'success' });
  316. // that.onRefoundComfirm()
  317. },
  318. fail(ressonInfo) {
  319. console.log('支付失败', ressonInfo)
  320. that.onPayError()
  321. }
  322. })
  323. },
  324. // 获取后台配置的支付方式
  325. async queryPayType() {
  326. try {
  327. // wxlite_payment_service_provider
  328. const { data } = await api_queryByParamName({
  329. paramName: app.globalData.appId
  330. });
  331. if (data.code == 200) {
  332. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  333. this.setData({
  334. paymentType: paramValue.vendor,
  335. paymentChannel: paramValue.channel
  336. });
  337. }
  338. } catch (error) {
  339. console.log(error, "error");
  340. }
  341. },
  342. onPayError(message?: string) {
  343. wx.hideLoading()
  344. wx.showToast({
  345. title: message || '支付取消',
  346. icon: 'none'
  347. })
  348. },
  349. async onRefounded(e: any) {
  350. const { dataset } = e.currentTarget
  351. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  352. console.log(dataset, item, 'item')
  353. if (!item) {
  354. return
  355. }
  356. if (item.wechatStatus === "REFUNDING") {
  357. this.setData({
  358. cancelRefoundStatus: true
  359. }, async () => {
  360. try {
  361. const refundOrderId = item.refundOrderId
  362. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  363. console.log(data, 'data')
  364. if (data.code == 200) {
  365. wx.showToast({ title: '你已成功取消退款', icon: 'none' })
  366. this.onRefoundComfirm()
  367. } else {
  368. wx.showToast({ title: data.message, icon: 'none' })
  369. this.setData({
  370. cancelRefoundStatus: false
  371. })
  372. }
  373. } catch { }
  374. })
  375. } else {
  376. const { orderNo, studentPaymentOrderDetails } = item
  377. const goodsInfo: any = {
  378. orderNo,
  379. goods: []
  380. }
  381. if (Array.isArray(studentPaymentOrderDetails)) {
  382. studentPaymentOrderDetails.forEach((item: any) => {
  383. goodsInfo.goods.push({
  384. ...item,
  385. id: item.userPaymentOrderDetailId,
  386. currentPrice: item.paymentCashAmount
  387. })
  388. })
  389. }
  390. this.setData({
  391. goodsInfo,
  392. cancelRefoundStatus: true,
  393. refoundStatus: true
  394. })
  395. }
  396. },
  397. changeRefoundStatus(e: { detail: any }) {
  398. this.setData({
  399. refoundStatus: e.detail,
  400. cancelRefoundStatus: false,
  401. })
  402. },
  403. onRefoundComfirm() {
  404. const that = this
  405. this.setData({
  406. refoundStatus: false,
  407. })
  408. setTimeout(() => {
  409. that.setData({
  410. page: 1,
  411. maxPage: 1,
  412. recordList: [],
  413. cancelRefoundStatus: false,
  414. }, () => {
  415. this.getList()
  416. })
  417. }, 1500);
  418. },
  419. onShareAppMessage() {
  420. return {
  421. title: ' 器乐数字ai助手',
  422. path: '/pages/index/index',
  423. imageUrl: 'https://oss.dayaedu.com/ktyq/1739929620914.png'
  424. }
  425. }
  426. })