order-result.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /**
  67. * 生命周期函数--监听页面加载
  68. */
  69. onLoad(options: any) {
  70. this.queryPayType()
  71. // if (options.orderInfo) {
  72. // const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  73. // const instrumentsInfo = JSON.parse(decodeURIComponent(options.instrumentsInfo));
  74. // const memberInfo = JSON.parse(decodeURIComponent(options.memberInfo));
  75. // console.log(goods, 'goods', instrumentsInfo, memberInfo)
  76. // this.setData({
  77. // goodsInfo: goods,
  78. // instrumentsInfo,
  79. // memberInfo,
  80. // status: goods.status
  81. // });
  82. // }
  83. if (options.orderNo) {
  84. this.setData({
  85. orderNo: options.orderNo
  86. })
  87. this.getDetail()
  88. }
  89. },
  90. async getDetail(callback?: any) {
  91. try {
  92. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  93. if (data.code == 200) {
  94. const { goodsInfos, addresses, beneficiary, wechatStatus, createTime } = data.data
  95. const goodsInfo = {}
  96. if (goodsInfos[0]) {
  97. goodsInfo.pic = goodsInfos[0].goodsUrl
  98. goodsInfo.name = goodsInfos[0].goodsName
  99. goodsInfo.originalPrice = goodsInfos[0].originalPrice
  100. goodsInfo.salePrice = goodsInfos[0].paymentCashAmount
  101. goodsInfo.typeName = this.formatPeriod(goodsInfos[0].activationCodeInfo.times, goodsInfos[0].activationCodeInfo.type)
  102. goodsInfo.orderNo = this.data.orderNo
  103. }
  104. const instrumentsInfo = {}
  105. if (goodsInfos[1]) {
  106. instrumentsInfo.pic = goodsInfos[1].goodsUrl
  107. instrumentsInfo.name = goodsInfos[1].goodsName
  108. instrumentsInfo.originalPrice = goodsInfos[1].originalPrice
  109. instrumentsInfo.salePrice = goodsInfos[1].paymentCashAmount
  110. instrumentsInfo.id = goodsInfos[1].id
  111. }
  112. const addressInfo = {}
  113. if (addresses && instrumentsInfo.id) {
  114. addressInfo.id = addresses.id
  115. addressInfo.name = addresses.name
  116. addressInfo.phoneNumber = addresses.phoneNumber
  117. addressInfo.addressDes = addresses.detailAddress
  118. }
  119. const memberInfo = {}
  120. if (beneficiary) {
  121. memberInfo.name = beneficiary.name
  122. memberInfo.phone = beneficiary.phone
  123. memberInfo.schoolInfo = beneficiary.provinceName + beneficiary.cityName + beneficiary.regionName + beneficiary.schoolAreaName + GRADE_ENUM[beneficiary.currentGradeNum] + beneficiary.currentClass + "班"
  124. }
  125. const orderInfo = {
  126. createTime,
  127. orderNo: this.data.orderNo
  128. }
  129. this.setData({
  130. goodsInfo,
  131. instrumentsInfo,
  132. addressInfo,
  133. memberInfo,
  134. status: wechatStatus,
  135. orderInfo
  136. })
  137. }
  138. } catch (error) {
  139. console.log(error, "error");
  140. }
  141. },
  142. // 格式化类型
  143. formatPeriod(num: number, type: string) {
  144. if (!num || !type) {
  145. return ''
  146. }
  147. const template: any = {
  148. DAY: "天",
  149. MONTH: "月",
  150. YEAR: "年"
  151. }
  152. if (type === "YEAR" && num >= 99) {
  153. return '时长: 永久'
  154. }
  155. return "时长: " + num + template[type]
  156. },
  157. onCopy(e: { currentTarget: any }) {
  158. wx.setClipboardData({
  159. data: e.currentTarget.dataset.orderno,
  160. success: () => {
  161. wx.showToast({ title: '复制成功', icon: 'none' })
  162. },
  163. fail: () => {
  164. wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' })
  165. }
  166. })
  167. },
  168. // 获取后台配置的支付方式
  169. async queryPayType() {
  170. try {
  171. // wxlite_payment_service_provider
  172. const { data } = await api_queryByParamName({
  173. paramName: app.globalData.appId
  174. });
  175. if (data.code == 200) {
  176. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  177. this.setData({
  178. paymentType: paramValue.vendor,
  179. paymentChannel: paramValue.channel
  180. });
  181. }
  182. } catch (error) {
  183. console.log(error, "error");
  184. }
  185. },
  186. onPayError(message?: string) {
  187. wx.hideLoading()
  188. wx.showToast({
  189. title: message || '支付取消',
  190. icon: 'none'
  191. })
  192. },
  193. // 购买
  194. async onSubmit() {
  195. // 有乐器必须填收货信息
  196. // if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  197. // wx.showToast({
  198. // title: "请填写收货信息",
  199. // icon: 'none'
  200. // })
  201. // this.setData({
  202. // isAddressInfoTip: true
  203. // })
  204. // return
  205. // }
  206. console.log(this.data.addressInfo)
  207. wx.showLoading({
  208. mask: true,
  209. title: "订单提交中...",
  210. });
  211. try {
  212. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  213. if (orderNo) {
  214. const { data } = await api_userPaymentOrderUnpaid({
  215. orderNo: orderNo,
  216. paymentType: 'WECHAT_MINI'
  217. })
  218. if (data.code === 200) {
  219. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  220. this.onExecutePay(paymentConfig, paymentType, orderNo)
  221. } else {
  222. this.onPayError()
  223. }
  224. } else {
  225. const goodsInfos = [{
  226. "goodsId": id,
  227. "goodsNum": 1,
  228. "goodsType": "ACTIVATION_CODE",
  229. "paymentCashAmount": salePrice,
  230. "paymentCouponAmount": 0
  231. }]
  232. // 乐器
  233. if (this.data.instrumentsInfo.id) {
  234. goodsInfos.push({
  235. "goodsId": this.data.instrumentsInfo.id,
  236. "goodsNum": 1,
  237. "goodsType": "INSTRUMENTS",
  238. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  239. "paymentCouponAmount": 0
  240. })
  241. }
  242. const { data } = await api_executeOrder({
  243. "orderType": "WECHAT_MINI",
  244. "paymentType": this.data.paymentType,
  245. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  246. "paymentCouponAmount": 0,
  247. "shopId": shopId,
  248. "openId": app.globalData.userInfo?.liteOpenid,
  249. goodsInfos,
  250. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  251. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  252. "receiveAddress": this.data.addressInfo.id || "",
  253. "userBeneficiaryId": this.data.memberInfo.id
  254. })
  255. if (data.code === 200) {
  256. const { paymentConfig, paymentType, orderNo } = data.data
  257. this.onExecutePay(paymentConfig, paymentType, orderNo)
  258. } else if (data.code === 5200) {
  259. wx.hideLoading()
  260. wx.showToast({
  261. title: data.message,
  262. icon: 'none'
  263. })
  264. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  265. wx.hideLoading()
  266. wx.showToast({
  267. title: data.message,
  268. icon: 'none'
  269. })
  270. setTimeout(() => { wx.navigateBack() }, 1000)
  271. } else {
  272. this.onPayError(data.message)
  273. }
  274. }
  275. } catch {
  276. wx.hideLoading()
  277. }
  278. },
  279. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  280. wx.login({
  281. success: async (wxres: any) => {
  282. const res = await api_executePayment({
  283. merOrderNo: paymentConfig.merOrderNo,
  284. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  285. paymentType,
  286. userId: app.globalData.userInfo?.id,
  287. code: wxres.code,
  288. wxMiniAppId: app.globalData.appId
  289. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  290. // wxPubAppId: 'wxbde13f59d40cb4f2'
  291. })
  292. wx.hideLoading()
  293. if (res.data.code === 200) {
  294. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  295. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  296. wx.hideLoading()
  297. wx.showToast({
  298. title: res.data.message,
  299. icon: 'none'
  300. })
  301. setTimeout(() => { wx.navigateBack() }, 1000)
  302. } else {
  303. this.onPayError(res.data.message)
  304. }
  305. },
  306. fail: () => {
  307. this.onPayError()
  308. }
  309. })
  310. },
  311. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  312. const isYeePay = paymentType.indexOf('yeepay') !== -1
  313. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  314. : paymentConfig?.expend
  315. ? JSON.parse(paymentConfig?.expend?.pay_info)
  316. : paymentConfig
  317. const that = this
  318. wx.requestPayment({
  319. timeStamp: prePayInfo.timeStamp,
  320. nonceStr: prePayInfo.nonceStr,
  321. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  322. paySign: prePayInfo.paySign,
  323. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  324. success() {
  325. wx.showToast({ title: '支付成功', icon: 'success' });
  326. setTimeout(() => {
  327. that.getDetail()
  328. }, 1500)
  329. },
  330. fail(ressonInfo) {
  331. console.log('支付失败', ressonInfo)
  332. that.onPayError()
  333. const goodsInfo = that.data.goodsInfo
  334. goodsInfo.orderNo = orderNo
  335. that.setData({
  336. goodsInfo
  337. })
  338. }
  339. })
  340. },
  341. /**
  342. * 用户点击右上角分享
  343. */
  344. onShareAppMessage() {
  345. return {
  346. title: ' 器乐数字ai助手',
  347. path: '/pages/index/index',
  348. imageUrl: 'https://oss.dayaedu.com/ktyq/1739929620914.png'
  349. }
  350. }
  351. })