orders.ts 9.6 KB

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