orders.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. console.log()
  200. this.onSubmit({orderNo: dataset.orderno})
  201. } else {
  202. wx.navigateTo({
  203. url: '../orders/order-result?orderNo=' + dataset.orderno
  204. })
  205. }
  206. },
  207. // 购买
  208. async onSubmit(goodsInfo: any) {
  209. wx.showLoading({
  210. mask: true,
  211. title: "订单提交中...",
  212. });
  213. try {
  214. const { orderNo } = goodsInfo
  215. const {data} = await api_userPaymentOrderUnpaid({
  216. orderNo: orderNo,
  217. paymentType: 'WECHAT_MINI'
  218. })
  219. if (data.code === 200) {
  220. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  221. this.onExecutePay(paymentConfig, paymentType, orderNo)
  222. } else {
  223. this.onPayError()
  224. }
  225. } catch {
  226. wx.hideLoading()
  227. }
  228. },
  229. async onExecutePay( paymentConfig: any, paymentType: string, orderNo: string) {
  230. wx.login({
  231. success: async (wxres: any) => {
  232. const res = await api_executePayment({
  233. merOrderNo: paymentConfig.merOrderNo,
  234. paymentChannel: this.data.paymentChannel || 'wx_lite',
  235. paymentType,
  236. userId: app.globalData.userInfo?.id,
  237. code: wxres.code,
  238. wxMiniAppId: app.globalData.appId
  239. })
  240. wx.hideLoading()
  241. if(res.data.code === 200) {
  242. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  243. } else {
  244. this.onPayError(res.data.message)
  245. }
  246. },
  247. fail: () => {
  248. this.onPayError()
  249. }
  250. })
  251. },
  252. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  253. const isYeePay = paymentType.indexOf('yeepay') !== -1
  254. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  255. : paymentConfig?.expend
  256. ? JSON.parse(paymentConfig?.expend?.pay_info)
  257. : paymentConfig
  258. const that = this
  259. wx.requestPayment({
  260. timeStamp: prePayInfo.timeStamp,
  261. nonceStr: prePayInfo.nonceStr,
  262. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  263. paySign: prePayInfo.paySign,
  264. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  265. success() {
  266. wx.showToast({ title: '支付成功', icon: 'success' });
  267. // that.onRefoundComfirm()
  268. },
  269. fail(ressonInfo) {
  270. console.log('支付失败', ressonInfo)
  271. that.onPayError()
  272. }
  273. })
  274. },
  275. // 获取后台配置的支付方式
  276. async queryPayType() {
  277. try {
  278. // wxlite_payment_service_provider
  279. const { data } = await api_queryByParamName({
  280. paramName: app.globalData.appId
  281. });
  282. if (data.code == 200) {
  283. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  284. this.setData({
  285. paymentType: paramValue.vendor,
  286. paymentChannel: paramValue.channel
  287. });
  288. }
  289. } catch (error) {
  290. console.log(error, "error");
  291. }
  292. },
  293. onPayError(message?: string) {
  294. wx.hideLoading()
  295. wx.showToast({
  296. title: message || '支付取消',
  297. icon: 'none'
  298. })
  299. },
  300. async onRefounded(e: any) {
  301. const { dataset } = e.currentTarget
  302. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  303. console.log(dataset, item, 'item')
  304. if(!item) {
  305. return
  306. }
  307. if(item.wechatStatus === "REFUNDING") {
  308. try {
  309. const refundOrderId = item.refundOrderId
  310. const {data} = await api_userPaymentCancelRefund(refundOrderId)
  311. console.log(data, 'data')
  312. if(data.code == 200) {
  313. wx.showToast({ title: '取消退款成功', icon: 'none' })
  314. this.onRefoundComfirm()
  315. } else {
  316. wx.showToast({ title: data.message, icon: 'none' })
  317. }
  318. } catch {}
  319. } else {
  320. const { orderNo, studentPaymentOrderDetails } = item
  321. const goodsInfo: any = {
  322. orderNo,
  323. goods: []
  324. }
  325. if(Array.isArray(studentPaymentOrderDetails)) {
  326. studentPaymentOrderDetails.forEach((item: any) => {
  327. goodsInfo.goods.push({
  328. ...item,
  329. id: item.userPaymentOrderDetailId,
  330. currentPrice: item.paymentCashAmount
  331. })
  332. })
  333. }
  334. this.setData({
  335. goodsInfo,
  336. refoundStatus: true
  337. })
  338. }
  339. },
  340. changeRefoundStatus(e: {detail: any}) {
  341. this.setData({
  342. refoundStatus: e.detail
  343. })
  344. },
  345. onRefoundComfirm() {
  346. const that = this
  347. this.setData({
  348. refoundStatus: false
  349. })
  350. setTimeout(() => {
  351. that.setData({
  352. page: 1,
  353. maxPage: 1,
  354. recordList: [],
  355. }, () => {
  356. this.getList()
  357. })
  358. }, 1500);
  359. },
  360. })