orders.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.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. const orderDetail: any = this.data.recordList.find((item: any) => item.orderNo === dataset.orderno)
  203. if (!orderDetail) return
  204. const details = orderDetail.studentPaymentOrderDetails || []
  205. const params = [] as any
  206. details.forEach((item: any) => {
  207. params.push({
  208. pic: item.goodsUrl,
  209. name: item.goodsName,
  210. originalPrice: item.originalPrice,
  211. salePrice: item.paymentCashAmount,
  212. goodsType: item.goodsType, // INSTRUMENTS
  213. })
  214. })
  215. let info = JSON.stringify({
  216. ...params
  217. });
  218. info = encodeURIComponent(info);
  219. wx.navigateTo({
  220. url: `../orders/order-detail?orderInfo=${info}&orderNo=${orderDetail.orderNo}&status=${orderDetail.wechatStatus}`,
  221. });
  222. } else {
  223. wx.navigateTo({
  224. url: `../orders/order-result?orderNo=${dataset.orderno}`
  225. })
  226. }
  227. },
  228. // 购买
  229. async onSubmit(goodsInfo: any) {
  230. wx.showLoading({
  231. mask: true,
  232. title: "订单提交中...",
  233. });
  234. try {
  235. const { orderNo } = goodsInfo
  236. const { data } = await api_userPaymentOrderUnpaid({
  237. orderNo: orderNo,
  238. paymentType: 'WECHAT_MINI'
  239. })
  240. if (data.code === 200) {
  241. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  242. this.onExecutePay(paymentConfig, paymentType, orderNo)
  243. } else {
  244. this.onPayError()
  245. }
  246. } catch {
  247. wx.hideLoading()
  248. }
  249. },
  250. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  251. wx.login({
  252. success: async (wxres: any) => {
  253. const res = await api_executePayment({
  254. merOrderNo: paymentConfig.merOrderNo,
  255. paymentChannel: this.data.paymentChannel || 'wx_lite',
  256. paymentType,
  257. userId: app.globalData.userInfo?.id,
  258. code: wxres.code,
  259. wxMiniAppId: app.globalData.appId
  260. })
  261. wx.hideLoading()
  262. if (res.data.code === 200) {
  263. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  264. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  265. wx.hideLoading()
  266. wx.showToast({
  267. title: res.data.message,
  268. icon: 'none'
  269. })
  270. setTimeout(() => {
  271. this.setData(
  272. {
  273. page: 1,
  274. maxPage: 1,
  275. recordList: [],
  276. },
  277. () => {
  278. this.getList();
  279. }
  280. );
  281. }, 1000);
  282. } else {
  283. this.onPayError(res.data.message)
  284. }
  285. },
  286. fail: () => {
  287. this.onPayError()
  288. }
  289. })
  290. },
  291. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  292. const isYeePay = paymentType.indexOf('yeepay') !== -1
  293. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  294. : paymentConfig?.expend
  295. ? JSON.parse(paymentConfig?.expend?.pay_info)
  296. : paymentConfig
  297. const that = this
  298. wx.requestPayment({
  299. timeStamp: prePayInfo.timeStamp,
  300. nonceStr: prePayInfo.nonceStr,
  301. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  302. paySign: prePayInfo.paySign,
  303. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  304. success() {
  305. wx.showToast({ title: '支付成功', icon: 'success' });
  306. },
  307. fail(ressonInfo) {
  308. console.log('支付失败', ressonInfo)
  309. that.onPayError()
  310. }
  311. })
  312. },
  313. // 获取后台配置的支付方式
  314. async queryPayType() {
  315. try {
  316. // wxlite_payment_service_provider
  317. const { data } = await api_queryByParamName({
  318. paramName: app.globalData.appId
  319. });
  320. if (data.code == 200) {
  321. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  322. this.setData({
  323. paymentType: paramValue.vendor,
  324. paymentChannel: paramValue.channel
  325. });
  326. }
  327. } catch (error) {
  328. console.log(error, "error");
  329. }
  330. },
  331. onPayError(message?: string) {
  332. wx.hideLoading()
  333. wx.showToast({
  334. title: message || '支付取消',
  335. icon: 'none'
  336. })
  337. },
  338. onShareAppMessage() {
  339. return {
  340. title: '器乐数字AI工具',
  341. path: '/pages/index/index',
  342. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  343. }
  344. }
  345. })