orders.ts 10.0 KB

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