orders.ts 10 KB

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