orders.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import { api_executePayment, api_queryByParamName, api_studentOrderPage, api_userPaymentCancelRefund, api_userPaymentOrderUnpaid } from "../../api/login";
  2. import { formatPrice } from "../../utils/util";
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  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. // id: 6,
  37. // label: "已退款",
  38. // },
  39. ],
  40. tabIdx: 0, // 当前选中的tab索引
  41. page: 1,
  42. rows: 10,
  43. recordList: [],
  44. maxPage: 1, // 总分页数
  45. refoundStatus: false,
  46. cancelRefoundStatus: false,
  47. paymentChannel: '',
  48. goodsInfo: {}, // 选中的数据
  49. },
  50. /**
  51. * 生命周期函数--监听页面加载
  52. */
  53. onLoad() {
  54. },
  55. onShow() {
  56. this.setData({
  57. page: 1,
  58. maxPage: 1,
  59. recordList: [],
  60. }, () => {
  61. this.getList()
  62. })
  63. },
  64. /** 切换分类 */
  65. switchTab(e: { currentTarget: { dataset: { idx: any } } }) {
  66. const idx = e.currentTarget.dataset.idx;
  67. if (idx != this.data.tabIdx) {
  68. this.setData(
  69. {
  70. tabIdx: idx,
  71. page: 1,
  72. maxPage: 1,
  73. recordList: [],
  74. },
  75. () => {
  76. this.getList();
  77. }
  78. );
  79. }
  80. },
  81. async getList() {
  82. wx.showLoading({
  83. mask: true,
  84. title: "加载中...",
  85. });
  86. const currentPage = this.data.page,
  87. currentRow = this.data.rows,
  88. tabIdx = this.data.tabIdx;
  89. try {
  90. // @ApiModelProperty("订单状态 WAIT_PAY:待付款,WAIT_USE:待使用,SUCCESS:已完成,CLOSE:已取消")
  91. const { data } = await api_studentOrderPage({
  92. version: 'V2',
  93. openId: app.globalData.userInfo?.liteOpenid,
  94. page: currentPage,
  95. rows: this.data.rows,
  96. wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "",
  97. })
  98. if (data.code == 200) {
  99. const { rows, total } = data.data;
  100. rows.forEach((item: any) => {
  101. item.amount = formatPrice(item.paymentCashAmount, 'ALL')
  102. item.statusName = this.formatOrderStatus(item.wechatStatus)
  103. let originalPrice = 0
  104. const studentPaymentOrderDetails = item.studentPaymentOrderDetails || [];
  105. studentPaymentOrderDetails.forEach((student: any) => {
  106. student.originalPrice = formatPrice(student.originalPrice, 'ALL');
  107. student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  108. // 总的日常价
  109. originalPrice += Number(student.originalPrice || 0)
  110. })
  111. item.originalPrice = originalPrice
  112. item.discountPrice = formatPrice(
  113. originalPrice - item.paymentCashAmount,
  114. "ALL"
  115. )
  116. const prices: any = formatPrice(item.paymentCashAmount)
  117. item.integerPart = prices.integerPart
  118. item.decimalPart = prices.decimalPart
  119. item.studentPaymentOrderDetails = studentPaymentOrderDetails
  120. });
  121. const newList = this.data.recordList.concat(rows);
  122. this.setData(
  123. {
  124. recordList: newList,
  125. maxPage: Math.ceil(total / currentRow),
  126. },
  127. () => wx.hideLoading()
  128. );
  129. } else {
  130. wx.hideLoading();
  131. }
  132. } catch (e) {
  133. console.log(e, 'e')
  134. wx.hideLoading()
  135. }
  136. },
  137. // 格式化中文
  138. formatOrderStatus(status: string) {
  139. // 订单状态 WAIT_PAY:待付款, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消
  140. const template: any = {
  141. WAIT_PAY: '等待付款',
  142. WAIT_USE: '等待使用',
  143. PAID: '交易完成',
  144. CLOSED: '交易取消',
  145. REFUNDING: '售后中',
  146. REFUNDED: '售后成功'
  147. }
  148. return template[status]
  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}&tabIdx=${this.data.tabIdx}`
  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 if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  246. wx.hideLoading()
  247. wx.showToast({
  248. title: res.data.message,
  249. icon: 'none'
  250. })
  251. setTimeout(() => {
  252. this.getList()
  253. }, 1000);
  254. } else {
  255. this.onPayError(res.data.message)
  256. }
  257. },
  258. fail: () => {
  259. this.onPayError()
  260. }
  261. })
  262. },
  263. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  264. const isYeePay = paymentType.indexOf('yeepay') !== -1
  265. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  266. : paymentConfig?.expend
  267. ? JSON.parse(paymentConfig?.expend?.pay_info)
  268. : paymentConfig
  269. const that = this
  270. wx.requestPayment({
  271. timeStamp: prePayInfo.timeStamp,
  272. nonceStr: prePayInfo.nonceStr,
  273. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  274. paySign: prePayInfo.paySign,
  275. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  276. success() {
  277. wx.showToast({ title: '支付成功', icon: 'success' });
  278. // that.onRefoundComfirm()
  279. },
  280. fail(ressonInfo) {
  281. console.log('支付失败', ressonInfo)
  282. that.onPayError()
  283. }
  284. })
  285. },
  286. // 获取后台配置的支付方式
  287. async queryPayType() {
  288. try {
  289. // wxlite_payment_service_provider
  290. const { data } = await api_queryByParamName({
  291. paramName: app.globalData.appId
  292. });
  293. if (data.code == 200) {
  294. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  295. this.setData({
  296. paymentType: paramValue.vendor,
  297. paymentChannel: paramValue.channel
  298. });
  299. }
  300. } catch (error) {
  301. console.log(error, "error");
  302. }
  303. },
  304. onPayError(message?: string) {
  305. wx.hideLoading()
  306. wx.showToast({
  307. title: message || '支付取消',
  308. icon: 'none'
  309. })
  310. },
  311. async onRefounded(e: any) {
  312. const { dataset } = e.currentTarget
  313. const item: any = this.data.recordList.find((item: any) => item.id === dataset.id)
  314. console.log(dataset, item, 'item')
  315. if (!item) {
  316. return
  317. }
  318. if (item.wechatStatus === "REFUNDING") {
  319. this.setData({
  320. cancelRefoundStatus: true
  321. }, async () => {
  322. try {
  323. const refundOrderId = item.refundOrderId
  324. const { data } = await api_userPaymentCancelRefund(refundOrderId)
  325. wx.hideLoading()
  326. if (data.code == 200) {
  327. wx.showToast({ title: '取消退款成功', icon: 'none' })
  328. this.onRefoundComfirm()
  329. } else {
  330. wx.showToast({ title: data.message, icon: 'none' })
  331. }
  332. } catch {
  333. }
  334. })
  335. } else {
  336. const { orderNo, studentPaymentOrderDetails } = item
  337. const goodsInfo: any = {
  338. orderNo,
  339. goods: []
  340. }
  341. if (Array.isArray(studentPaymentOrderDetails)) {
  342. studentPaymentOrderDetails.forEach((item: any) => {
  343. goodsInfo.goods.push({
  344. ...item,
  345. id: item.userPaymentOrderDetailId,
  346. currentPrice: item.paymentCashAmount
  347. })
  348. })
  349. }
  350. this.setData({
  351. goodsInfo,
  352. cancelRefoundStatus: true,
  353. refoundStatus: true
  354. })
  355. }
  356. },
  357. changeRefoundStatus(e: { detail: any }) {
  358. this.setData({
  359. refoundStatus: e.detail,
  360. cancelRefoundStatus: false,
  361. })
  362. },
  363. onRefoundComfirm() {
  364. const that = this
  365. this.setData({
  366. refoundStatus: false
  367. })
  368. setTimeout(() => {
  369. that.setData({
  370. page: 1,
  371. maxPage: 1,
  372. cancelRefoundStatus: false,
  373. recordList: [],
  374. }, () => {
  375. this.getList()
  376. })
  377. }, 1500);
  378. },
  379. onShareAppMessage() {
  380. return {
  381. title: '器乐数字AI工具',
  382. path: '/pages/index/index',
  383. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  384. }
  385. }
  386. })