orders.ts 10 KB

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