orders.ts 9.4 KB

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