orders.ts 9.7 KB

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