orders.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.setData({
  58. page: 1,
  59. maxPage: 1,
  60. recordList: [],
  61. }, () => {
  62. this.getList()
  63. })
  64. this.setData({
  65. serviceShow: true,
  66. })
  67. },
  68. onHide() {
  69. this.setData({
  70. serviceShow: false
  71. })
  72. },
  73. /** 切换分类 */
  74. switchTab(e: { currentTarget: { dataset: { idx: any } } }) {
  75. const idx = e.currentTarget.dataset.idx;
  76. if (idx != this.data.tabIdx) {
  77. this.setData(
  78. {
  79. tabIdx: idx,
  80. page: 1,
  81. maxPage: 1,
  82. recordList: [],
  83. },
  84. () => {
  85. this.getList();
  86. }
  87. );
  88. }
  89. },
  90. async getList() {
  91. wx.showLoading({
  92. mask: true,
  93. title: "加载中...",
  94. });
  95. const currentPage = this.data.page,
  96. currentRow = this.data.rows,
  97. tabIdx = this.data.tabIdx;
  98. try {
  99. // @ApiModelProperty("订单状态 WAIT_PAY:待付款,WAIT_USE:待使用,SUCCESS:已完成,CLOSE:已取消")
  100. const { data } = await api_studentOrderPage({
  101. version: 'V2',
  102. openId: app.globalData.userInfo?.liteOpenid,
  103. page: currentPage,
  104. rows: this.data.rows,
  105. wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "",
  106. })
  107. if (data.code == 200) {
  108. const { rows, total } = data.data;
  109. rows.forEach((item: any) => {
  110. item.amount = formatPrice(item.paymentCashAmount, 'ALL')
  111. item.statusName = this.formatOrderStatus(item.wechatStatus)
  112. let originalPrice = 0
  113. const studentPaymentOrderDetails = item.studentPaymentOrderDetails || [];
  114. studentPaymentOrderDetails.forEach((student: any) => {
  115. student.originalPrice = formatPrice(student.originalPrice, 'ALL');
  116. student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type);
  117. student.salePrice = formatPrice(student.paymentCashAmount || 0, 'ALL')
  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. wx.redirectTo({
  206. url: '../index/index',
  207. })
  208. },
  209. onDetail(e: any) {
  210. const { dataset } = e.currentTarget
  211. if (dataset.wechatstatus === "WAIT_PAY") {
  212. const orderDetail: any = this.data.recordList.find((item: any) => item.orderNo === dataset.orderno)
  213. if (!orderDetail) return
  214. const details = orderDetail.studentPaymentOrderDetails || []
  215. const params = [] as any
  216. details.forEach((item: any) => {
  217. params.push({
  218. pic: item.goodsUrl,
  219. name: item.goodsName,
  220. period: item.activationCodeInfo?.type,
  221. num: item.activationCodeInfo?.times,
  222. originalPrice: item.originalPrice,
  223. salePrice: item.paymentCashAmount,
  224. goodsType: item.goodsType, // INSTRUMENTS
  225. })
  226. })
  227. let info = JSON.stringify({
  228. ...params
  229. });
  230. info = encodeURIComponent(info);
  231. wx.navigateTo({
  232. url: `../orders/order-detail?orderInfo=${info}&orderNo=${orderDetail.orderNo}&status=${orderDetail.wechatStatus}`,
  233. });
  234. } else {
  235. wx.navigateTo({
  236. url: `../orders/order-result?orderNo=${dataset.orderno}`
  237. })
  238. }
  239. },
  240. // 购买
  241. async onSubmit(goodsInfo: any) {
  242. wx.showLoading({
  243. mask: true,
  244. title: "订单提交中...",
  245. });
  246. try {
  247. const { orderNo } = goodsInfo
  248. const { data } = await api_userPaymentOrderUnpaid({
  249. orderNo: orderNo,
  250. paymentType: 'WECHAT_MINI'
  251. })
  252. if (data.code === 200) {
  253. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  254. this.onExecutePay(paymentConfig, paymentType, orderNo)
  255. } else {
  256. this.onPayError()
  257. }
  258. } catch {
  259. wx.hideLoading()
  260. }
  261. },
  262. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  263. wx.login({
  264. success: async (wxres: any) => {
  265. const res = await api_executePayment({
  266. merOrderNo: paymentConfig.merOrderNo,
  267. paymentChannel: this.data.paymentChannel || 'wx_lite',
  268. paymentType,
  269. userId: app.globalData.userInfo?.id,
  270. code: wxres.code,
  271. wxMiniAppId: app.globalData.appId
  272. })
  273. wx.hideLoading()
  274. if (res.data.code === 200) {
  275. this.onPaying(paymentType, res.data.data.reqParams, orderNo)
  276. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  277. wx.hideLoading()
  278. wx.showToast({
  279. title: res.data.message,
  280. icon: 'none'
  281. })
  282. setTimeout(() => {
  283. this.setData(
  284. {
  285. page: 1,
  286. maxPage: 1,
  287. recordList: [],
  288. },
  289. () => {
  290. this.getList();
  291. }
  292. );
  293. }, 1000);
  294. } else {
  295. this.onPayError(res.data.message)
  296. }
  297. },
  298. fail: () => {
  299. this.onPayError()
  300. }
  301. })
  302. },
  303. onPaying(paymentType: string, paymentConfig: any, orderNo: string) {
  304. const isYeePay = paymentType.indexOf('yeepay') !== -1
  305. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  306. : paymentConfig?.expend
  307. ? JSON.parse(paymentConfig?.expend?.pay_info)
  308. : paymentConfig
  309. const that = this
  310. wx.requestPayment({
  311. timeStamp: prePayInfo.timeStamp,
  312. nonceStr: prePayInfo.nonceStr,
  313. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  314. paySign: prePayInfo.paySign,
  315. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  316. success() {
  317. wx.showToast({ title: '支付成功', icon: 'success' });
  318. },
  319. fail(ressonInfo) {
  320. console.log('支付失败', ressonInfo)
  321. that.onPayError()
  322. }
  323. })
  324. },
  325. // 获取后台配置的支付方式
  326. async queryPayType() {
  327. try {
  328. // wxlite_payment_service_provider
  329. const { data } = await api_queryByParamName({
  330. paramName: app.globalData.appId
  331. });
  332. if (data.code == 200) {
  333. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  334. this.setData({
  335. paymentType: paramValue.vendor,
  336. paymentChannel: paramValue.channel
  337. });
  338. }
  339. } catch (error) {
  340. console.log(error, "error");
  341. }
  342. },
  343. onPayError(message?: string) {
  344. wx.hideLoading()
  345. wx.showToast({
  346. title: message || '支付取消',
  347. icon: 'none'
  348. })
  349. },
  350. onShareAppMessage() {
  351. return {
  352. title: '翼时代器乐数字Ai',
  353. path: '/pages/index/index',
  354. imageUrl: 'https://oss.dayaedu.com/ktyq/1739865626350.png'
  355. }
  356. }
  357. })