orders.ts 9.9 KB

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