orders.ts 9.9 KB

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