orders.ts 9.7 KB

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