order-result.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid, api_getUserReceiveAddressPage, api_userPaymentOrderDetail } from "../../api/login";
  3. const GRADE_ENUM = {
  4. '1': '一年级',
  5. '2': '二年级',
  6. '3': '三年级',
  7. '4': '四年级',
  8. '5': '五年级',
  9. '6': '六年级',
  10. '7': '七年级',
  11. '8': '八年级',
  12. '9': '九年级'
  13. } as any;
  14. // 获取应用实例
  15. const app = getApp<IAppOption>()
  16. Page({
  17. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. popupShow: false,
  22. isAddressInfoTip: false,
  23. addressInfo: {} as any,
  24. serviceShow: true,
  25. status: '',
  26. statusList: {
  27. WAIT_PAY: {
  28. logo: './images/ing.png',
  29. title: '待支付',
  30. content: '为了确保您的订单顺利进行,请尽快完成支付'
  31. },
  32. PAID: {
  33. logo: './images/success.png',
  34. title: '已完成',
  35. content: '订单流程已结束,感谢您的支持与参与!'
  36. },
  37. CLOSED: {
  38. logo: './images/error.png',
  39. title: '已取消',
  40. content: '您的订单已被关闭,如有需要请重新下单'
  41. },
  42. WAIT_USE: {
  43. logo: './images/wait.png',
  44. title: '待使用',
  45. content: '为了顺利使用,请尽快扫描下方二维码进行激活'
  46. },
  47. REFUNDING: {
  48. logo: './images/refounding.png',
  49. title: '退款中',
  50. content: '您的订单正在退款中,预计7个工作日内审核完'
  51. },
  52. REFUNDED: {
  53. logo: './images/refounded.png',
  54. title: '已退款',
  55. content: '您的订单已成功退款,感谢您的耐心等待'
  56. }
  57. },
  58. orderNo: "",
  59. goodsInfo: {} as any,
  60. instrumentsInfo: {} as any,
  61. memberInfo: {} as any,
  62. orderInfo: {} as any, //订单信息
  63. paymentType: null as any, // 支付类型
  64. paymentChannel: null as any,
  65. },
  66. onTimeing() {
  67. if (!app.globalData.isLogin) {
  68. setTimeout(() => {
  69. this.onTimeing()
  70. }, 500);
  71. } else {
  72. this.getDetail()
  73. }
  74. },
  75. /**
  76. * 生命周期函数--监听页面加载
  77. */
  78. onLoad(options: any) {
  79. this.queryPayType()
  80. // if (options.orderInfo) {
  81. // const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  82. // const instrumentsInfo = JSON.parse(decodeURIComponent(options.instrumentsInfo));
  83. // const memberInfo = JSON.parse(decodeURIComponent(options.memberInfo));
  84. // console.log(goods, 'goods', instrumentsInfo, memberInfo)
  85. // this.setData({
  86. // goodsInfo: goods,
  87. // instrumentsInfo,
  88. // memberInfo,
  89. // status: goods.status
  90. // });
  91. // }
  92. if (options.orderNo) {
  93. this.setData({
  94. orderNo: options.orderNo
  95. }, () => { this.onTimeing() })
  96. }
  97. },
  98. async getDetail(callback?: any) {
  99. try {
  100. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  101. if (data.code == 200) {
  102. const { goodsInfos, addresses, beneficiary, wechatStatus, createTime } = data.data
  103. const goodsInfo = {}
  104. if (goodsInfos[0]) {
  105. goodsInfo.pic = goodsInfos[0].goodsUrl
  106. goodsInfo.name = goodsInfos[0].goodsName
  107. goodsInfo.originalPrice = goodsInfos[0].originalPrice
  108. goodsInfo.salePrice = goodsInfos[0].paymentCashAmount
  109. goodsInfo.typeName = this.formatPeriod(goodsInfos[0].activationCodeInfo.times, goodsInfos[0].activationCodeInfo.type)
  110. goodsInfo.orderNo = this.data.orderNo
  111. }
  112. const instrumentsInfo = {}
  113. if (goodsInfos[1]) {
  114. instrumentsInfo.pic = goodsInfos[1].goodsUrl
  115. instrumentsInfo.name = goodsInfos[1].goodsName
  116. instrumentsInfo.originalPrice = goodsInfos[1].originalPrice
  117. instrumentsInfo.salePrice = goodsInfos[1].paymentCashAmount
  118. instrumentsInfo.id = goodsInfos[1].id
  119. }
  120. const addressInfo = {}
  121. if (addresses && instrumentsInfo.id) {
  122. addressInfo.id = addresses.id
  123. addressInfo.name = addresses.name
  124. addressInfo.phoneNumber = addresses.phoneNumber
  125. addressInfo.addressDes = addresses.detailAddress
  126. }
  127. const memberInfo = {}
  128. if (beneficiary) {
  129. memberInfo.name = beneficiary.name
  130. memberInfo.phone = beneficiary.phone
  131. memberInfo.schoolInfo = beneficiary.provinceName + beneficiary.cityName + beneficiary.regionName + beneficiary.schoolAreaName + GRADE_ENUM[beneficiary.currentGradeNum] + beneficiary.currentClass + "班"
  132. }
  133. const orderInfo = {
  134. createTime,
  135. orderNo: this.data.orderNo
  136. }
  137. this.setData({
  138. goodsInfo,
  139. instrumentsInfo,
  140. addressInfo,
  141. memberInfo,
  142. status: wechatStatus,
  143. orderInfo
  144. })
  145. }
  146. } catch (error) {
  147. console.log(error, "error");
  148. }
  149. },
  150. // 格式化类型
  151. formatPeriod(num: number, type: string) {
  152. const template: any = {
  153. DAY: "天卡",
  154. MONTH: "月卡",
  155. YEAR: "年卡"
  156. }
  157. if (type === "YEAR" && num >= 99) {
  158. return '永久卡'
  159. }
  160. return num + template[type]
  161. },
  162. onCopy(e: { currentTarget: any }) {
  163. wx.setClipboardData({
  164. data: e.currentTarget.dataset.orderno,
  165. success: () => {
  166. wx.showToast({ title: '复制成功', icon: 'none' })
  167. },
  168. fail: () => {
  169. wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' })
  170. }
  171. })
  172. },
  173. // 获取后台配置的支付方式
  174. async queryPayType() {
  175. try {
  176. // wxlite_payment_service_provider
  177. const { data } = await api_queryByParamName({
  178. paramName: app.globalData.appId
  179. });
  180. if (data.code == 200) {
  181. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  182. this.setData({
  183. paymentType: paramValue.vendor,
  184. paymentChannel: paramValue.channel
  185. });
  186. }
  187. } catch (error) {
  188. console.log(error, "error");
  189. }
  190. },
  191. onPayError(message?: string) {
  192. wx.hideLoading()
  193. wx.showToast({
  194. title: message || '支付取消',
  195. icon: 'none'
  196. })
  197. },
  198. // 购买
  199. async onSubmit() {
  200. // 有乐器必须填收货信息
  201. // if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  202. // wx.showToast({
  203. // title: "请填写收货信息",
  204. // icon: 'none'
  205. // })
  206. // this.setData({
  207. // isAddressInfoTip: true
  208. // })
  209. // return
  210. // }
  211. console.log(this.data.addressInfo)
  212. wx.showLoading({
  213. mask: true,
  214. title: "订单提交中...",
  215. });
  216. try {
  217. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  218. if (orderNo) {
  219. const { data } = await api_userPaymentOrderUnpaid({
  220. orderNo: orderNo,
  221. paymentType: 'WECHAT_MINI'
  222. })
  223. if (data.code === 200) {
  224. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  225. this.onExecutePay(paymentConfig, paymentType, orderNo)
  226. } else {
  227. this.onPayError()
  228. }
  229. } else {
  230. const goodsInfos = [{
  231. "goodsId": id,
  232. "goodsNum": 1,
  233. "goodsType": "ACTIVATION_CODE",
  234. "paymentCashAmount": salePrice,
  235. "paymentCouponAmount": 0
  236. }]
  237. // 乐器
  238. if (this.data.instrumentsInfo.id) {
  239. goodsInfos.push({
  240. "goodsId": this.data.instrumentsInfo.id,
  241. "goodsNum": 1,
  242. "goodsType": "INSTRUMENTS",
  243. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  244. "paymentCouponAmount": 0
  245. })
  246. }
  247. const { data } = await api_executeOrder({
  248. "orderType": "WECHAT_MINI",
  249. "paymentType": this.data.paymentType,
  250. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  251. "paymentCouponAmount": 0,
  252. "shopId": shopId,
  253. "openId": app.globalData.userInfo?.liteOpenid,
  254. goodsInfos,
  255. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  256. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  257. "receiveAddress": this.data.addressInfo.id || "",
  258. "userBeneficiaryId": this.data.memberInfo.id
  259. })
  260. if (data.code === 200) {
  261. const { paymentConfig, paymentType, orderNo } = data.data
  262. this.onExecutePay(paymentConfig, paymentType, orderNo)
  263. } else if (data.code === 5200) {
  264. wx.hideLoading()
  265. wx.showToast({
  266. title: data.message,
  267. icon: 'none'
  268. })
  269. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  270. wx.hideLoading()
  271. wx.showToast({
  272. title: data.message,
  273. icon: 'none'
  274. })
  275. setTimeout(() => { wx.navigateBack() }, 1000)
  276. } else {
  277. this.onPayError(data.message)
  278. }
  279. }
  280. } catch {
  281. wx.hideLoading()
  282. }
  283. },
  284. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  285. wx.login({
  286. success: async (wxres: any) => {
  287. const res = await api_executePayment({
  288. merOrderNo: paymentConfig.merOrderNo,
  289. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  290. paymentType,
  291. userId: app.globalData.userInfo?.id,
  292. code: wxres.code,
  293. wxMiniAppId: app.globalData.appId
  294. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  295. // wxPubAppId: 'wxbde13f59d40cb4f2'
  296. })
  297. wx.hideLoading()
  298. if (res.data.code === 200) {
  299. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  300. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  301. wx.hideLoading()
  302. wx.showToast({
  303. title: res.data.message,
  304. icon: 'none'
  305. })
  306. setTimeout(() => { wx.navigateBack() }, 1000)
  307. } else {
  308. this.onPayError(res.data.message)
  309. }
  310. },
  311. fail: () => {
  312. this.onPayError()
  313. }
  314. })
  315. },
  316. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  317. const isYeePay = paymentType.indexOf('yeepay') !== -1
  318. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  319. : paymentConfig?.expend
  320. ? JSON.parse(paymentConfig?.expend?.pay_info)
  321. : paymentConfig
  322. const that = this
  323. wx.requestPayment({
  324. timeStamp: prePayInfo.timeStamp,
  325. nonceStr: prePayInfo.nonceStr,
  326. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  327. paySign: prePayInfo.paySign,
  328. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  329. success() {
  330. wx.showToast({ title: '支付成功', icon: 'success' });
  331. setTimeout(() => {
  332. that.getDetail()
  333. }, 1500)
  334. },
  335. fail(ressonInfo) {
  336. console.log('支付失败', ressonInfo)
  337. that.onPayError()
  338. const goodsInfo = that.data.goodsInfo
  339. goodsInfo.orderNo = orderNo
  340. that.setData({
  341. goodsInfo
  342. })
  343. }
  344. })
  345. },
  346. /**
  347. * 用户点击右上角分享
  348. */
  349. onShareAppMessage() {
  350. return {
  351. title: '乐迷器乐AI',
  352. path: '/pages/index/index',
  353. imageUrl: 'https://oss.dayaedu.com/ktyq/1739773011143.png'
  354. }
  355. }
  356. })