orders.ts 11 KB

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