orders.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. refoundStatus: false,
  45. cancelRefoundStatus: false,
  46. goodsInfo: {}, // 选中的数据
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad() {
  52. },
  53. onShow() {
  54. this.setData({
  55. page: 1,
  56. maxPage: 1,
  57. recordList: [],
  58. }, () => {
  59. this.getList()
  60. })
  61. },
  62. /** 切换分类 */
  63. switchTab(e: { currentTarget: { dataset: { idx: any } } }) {
  64. const idx = e.currentTarget.dataset.idx;
  65. if (idx != this.data.tabIdx) {
  66. this.setData(
  67. {
  68. tabIdx: idx,
  69. page: 1,
  70. maxPage: 1,
  71. recordList: [],
  72. },
  73. () => {
  74. this.getList();
  75. }
  76. );
  77. }
  78. },
  79. async getList() {
  80. wx.showLoading({
  81. mask: true,
  82. title: "加载中...",
  83. });
  84. const currentPage = this.data.page,
  85. currentRow = this.data.rows,
  86. tabIdx = this.data.tabIdx;
  87. try {
  88. // @ApiModelProperty("订单状态 WAIT_PAY:待付款,WAIT_USE:待使用,SUCCESS:已完成,CLOSE:已取消")
  89. const { data } = await api_studentOrderPage({
  90. openId: app.globalData.userInfo?.liteOpenid,
  91. page: currentPage,
  92. rows: this.data.rows,
  93. wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "",
  94. })
  95. if (data.code == 200) {
  96. const { rows, total } = data.data;
  97. rows.forEach((item: any) => {
  98. item.amount = this.formatPrice(item.paymentCashAmount, 'ALL')
  99. item.statusName = this.formatOrderStatus(item.wechatStatus)
  100. const studentPaymentOrderDetails = item.studentPaymentOrderDetails || [];
  101. studentPaymentOrderDetails.forEach((student: any) => {
  102. student.originalPrice = this.formatPrice(student.paymentCashAmount, 'ALL');
  103. student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  104. })
  105. item.studentPaymentOrderDetails = studentPaymentOrderDetails
  106. });
  107. const newList = this.data.recordList.concat(rows);
  108. this.setData(
  109. {
  110. recordList: newList,
  111. maxPage: Math.ceil(total / currentRow),
  112. },
  113. () => wx.hideLoading()
  114. );
  115. } else {
  116. wx.hideLoading();
  117. }
  118. } catch(e) {
  119. console.log(e, 'e')
  120. wx.hideLoading()
  121. }
  122. },
  123. // 格式化中文
  124. formatOrderStatus(status: string) {
  125. // 订单状态 WAIT_PAY:待付款, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消
  126. const template: any = {
  127. WAIT_PAY: '等待付款',
  128. WAIT_USE: '等待使用',
  129. PAID: '交易完成',
  130. CLOSED: '交易取消',
  131. REFUNDING: '售后中',
  132. REFUNDED: '售后成功'
  133. }
  134. return template[status]
  135. },
  136. // 格式化价格
  137. formatPrice(price: number, type?: string) {
  138. const amountStr = price.toFixed(2)
  139. const [integerPart, decimalPart] = amountStr.split('.');
  140. if(type === 'ALL') {
  141. return amountStr
  142. }
  143. return {
  144. integerPart,
  145. decimalPart
  146. }
  147. },
  148. // 格式化类型
  149. formatPeriod(num: number, type: string) {
  150. if(!num || !type) {
  151. return ''
  152. }
  153. const template: any = {
  154. DAY: "天卡",
  155. MONTH: "月卡",
  156. YEAR: "年卡"
  157. }
  158. if(type === "YEAR" && num >= 99) {
  159. return '永久卡'
  160. }
  161. return num + template[type]
  162. },
  163. /** 加载更多 */
  164. loadMore() {
  165. const currentPage = this.data.page;
  166. if (this.data.page >= this.data.maxPage) {
  167. // wx.showToast({
  168. // title: "没有更多数据了",
  169. // icon: "none",
  170. // duration: 1000,
  171. // });
  172. } else {
  173. this.setData(
  174. {
  175. page: currentPage + 1,
  176. },
  177. () => {
  178. this.getList();
  179. }
  180. );
  181. }
  182. },
  183. onPay(e: any) {
  184. const { dataset } = e.currentTarget
  185. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  186. if(item) {
  187. this.onSubmit({
  188. orderNo: item.orderNo
  189. })
  190. }
  191. },
  192. onOne() {
  193. wx.redirectTo({
  194. url: '../index/index',
  195. })
  196. },
  197. onDetail(e: any) {
  198. const { dataset } = e.currentTarget
  199. if(dataset.wechatstatus === "WAIT_PAY") {
  200. this.onSubmit({orderNo: dataset.orderno})
  201. } else {
  202. wx.navigateTo({
  203. url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}`
  204. })
  205. }
  206. },
  207. // 购买
  208. async onSubmit(goodsInfo: any) {
  209. wx.showLoading({
  210. mask: true,
  211. title: "订单提交中...",
  212. });
  213. try {
  214. const { orderNo } = goodsInfo
  215. const {data} = await api_userPaymentOrderUnpaid({
  216. orderNo: orderNo,
  217. paymentType: 'WECHAT_MINI'
  218. })
  219. if (data.code === 200) {
  220. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  221. this.onExecutePay(paymentConfig, paymentType, orderNo)
  222. } else {
  223. this.onPayError()
  224. }
  225. } catch {
  226. wx.hideLoading()
  227. }
  228. },
  229. async onExecutePay( paymentConfig: any, paymentType: string, orderNo: string) {
  230. wx.login({
  231. success: async (wxres: any) => {
  232. const res = await api_executePayment({
  233. merOrderNo: paymentConfig.merOrderNo,
  234. paymentChannel: this.data.paymentChannel || 'wx_lite',
  235. paymentType,
  236. userId: app.globalData.userInfo?.id,
  237. code: wxres.code,
  238. wxMiniAppId: app.globalData.appId
  239. })
  240. wx.hideLoading()
  241. if(res.data.code === 200) {
  242. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  243. } else {
  244. this.onPayError(res.data.message)
  245. }
  246. },
  247. fail: () => {
  248. this.onPayError()
  249. }
  250. })
  251. },
  252. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  253. const isYeePay = paymentType.indexOf('yeepay') !== -1
  254. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  255. : paymentConfig?.expend
  256. ? JSON.parse(paymentConfig?.expend?.pay_info)
  257. : paymentConfig
  258. const that = this
  259. wx.requestPayment({
  260. timeStamp: prePayInfo.timeStamp,
  261. nonceStr: prePayInfo.nonceStr,
  262. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  263. paySign: prePayInfo.paySign,
  264. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  265. success() {
  266. wx.showToast({ title: '支付成功', icon: 'success' });
  267. // that.onRefoundComfirm()
  268. },
  269. fail(ressonInfo) {
  270. console.log('支付失败', ressonInfo)
  271. that.onPayError()
  272. }
  273. })
  274. },
  275. // 获取后台配置的支付方式
  276. async queryPayType() {
  277. try {
  278. // wxlite_payment_service_provider
  279. const { data } = await api_queryByParamName({
  280. paramName: app.globalData.appId
  281. });
  282. if (data.code == 200) {
  283. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  284. this.setData({
  285. paymentType: paramValue.vendor,
  286. paymentChannel: paramValue.channel
  287. });
  288. }
  289. } catch (error) {
  290. console.log(error, "error");
  291. }
  292. },
  293. onPayError(message?: string) {
  294. wx.hideLoading()
  295. wx.showToast({
  296. title: message || '支付取消',
  297. icon: 'none'
  298. })
  299. },
  300. async onRefounded(e: any) {
  301. const { dataset } = e.currentTarget
  302. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  303. console.log(dataset, item, 'item')
  304. if(!item) {
  305. return
  306. }
  307. if(item.wechatStatus === "REFUNDING") {
  308. this.setData({
  309. cancelRefoundStatus: true
  310. }, async () => {
  311. try {
  312. const refundOrderId = item.refundOrderId
  313. const {data} = await api_userPaymentCancelRefund(refundOrderId)
  314. wx.hideLoading()
  315. if(data.code == 200) {
  316. wx.showToast({ title: '取消退款成功', icon: 'none' })
  317. this.onRefoundComfirm()
  318. } else {
  319. wx.showToast({ title: data.message, icon: 'none' })
  320. }
  321. } catch {
  322. }
  323. })
  324. } else {
  325. const { orderNo, studentPaymentOrderDetails } = item
  326. const goodsInfo: any = {
  327. orderNo,
  328. goods: []
  329. }
  330. if(Array.isArray(studentPaymentOrderDetails)) {
  331. studentPaymentOrderDetails.forEach((item: any) => {
  332. goodsInfo.goods.push({
  333. ...item,
  334. id: item.userPaymentOrderDetailId,
  335. currentPrice: item.paymentCashAmount
  336. })
  337. })
  338. }
  339. this.setData({
  340. goodsInfo,
  341. cancelRefoundStatus: true,
  342. refoundStatus: true
  343. })
  344. }
  345. },
  346. changeRefoundStatus(e: {detail: any}) {
  347. this.setData({
  348. refoundStatus: e.detail,
  349. cancelRefoundStatus: false,
  350. })
  351. },
  352. onRefoundComfirm() {
  353. const that = this
  354. this.setData({
  355. refoundStatus: false
  356. })
  357. setTimeout(() => {
  358. that.setData({
  359. page: 1,
  360. maxPage: 1,
  361. cancelRefoundStatus: false,
  362. recordList: [],
  363. }, () => {
  364. this.getList()
  365. })
  366. }, 1500);
  367. },
  368. onShareAppMessage() {
  369. return {
  370. title: '器乐数字AI工具',
  371. path: '/pages/index/index',
  372. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  373. }
  374. }
  375. })