order-result.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. 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. onCopy(e: { currentTarget: any }) {
  166. wx.setClipboardData({
  167. data: e.currentTarget.dataset.orderno,
  168. success: () => {
  169. wx.showToast({ title: '复制成功', icon: 'none' })
  170. },
  171. fail: () => {
  172. wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' })
  173. }
  174. })
  175. },
  176. // 获取后台配置的支付方式
  177. async queryPayType() {
  178. try {
  179. // wxlite_payment_service_provider
  180. const { data } = await api_queryByParamName({
  181. paramName: app.globalData.appId
  182. });
  183. if (data.code == 200) {
  184. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  185. this.setData({
  186. paymentType: paramValue.vendor,
  187. paymentChannel: paramValue.channel
  188. });
  189. }
  190. } catch (error) {
  191. console.log(error, "error");
  192. }
  193. },
  194. onPayError(message?: string) {
  195. wx.hideLoading()
  196. wx.showToast({
  197. title: message || '支付取消',
  198. icon: 'none'
  199. })
  200. },
  201. // 购买
  202. async onSubmit() {
  203. // 有乐器必须填收货信息
  204. // if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  205. // wx.showToast({
  206. // title: "请填写收货信息",
  207. // icon: 'none'
  208. // })
  209. // this.setData({
  210. // isAddressInfoTip: true
  211. // })
  212. // return
  213. // }
  214. console.log(this.data.addressInfo)
  215. wx.showLoading({
  216. mask: true,
  217. title: "订单提交中...",
  218. });
  219. try {
  220. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  221. if (orderNo) {
  222. const { data } = await api_userPaymentOrderUnpaid({
  223. orderNo: orderNo,
  224. paymentType: 'WECHAT_MINI'
  225. })
  226. if (data.code === 200) {
  227. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  228. this.onExecutePay(paymentConfig, paymentType, orderNo)
  229. } else {
  230. this.onPayError()
  231. }
  232. } else {
  233. const goodsInfos = [{
  234. "goodsId": id,
  235. "goodsNum": 1,
  236. "goodsType": "ACTIVATION_CODE",
  237. "paymentCashAmount": salePrice,
  238. "paymentCouponAmount": 0
  239. }]
  240. // 乐器
  241. if (this.data.instrumentsInfo.id) {
  242. goodsInfos.push({
  243. "goodsId": this.data.instrumentsInfo.id,
  244. "goodsNum": 1,
  245. "goodsType": "INSTRUMENTS",
  246. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  247. "paymentCouponAmount": 0
  248. })
  249. }
  250. const { data } = await api_executeOrder({
  251. "orderType": "WECHAT_MINI",
  252. "paymentType": this.data.paymentType,
  253. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  254. "paymentCouponAmount": 0,
  255. "shopId": shopId,
  256. "openId": app.globalData.userInfo?.liteOpenid,
  257. goodsInfos,
  258. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  259. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  260. "receiveAddress": this.data.addressInfo.id || "",
  261. "userBeneficiaryId": this.data.memberInfo.id
  262. })
  263. if (data.code === 200) {
  264. const { paymentConfig, paymentType, orderNo } = data.data
  265. this.onExecutePay(paymentConfig, paymentType, orderNo)
  266. } else if (data.code === 5200) {
  267. wx.hideLoading()
  268. wx.showToast({
  269. title: data.message,
  270. icon: 'none'
  271. })
  272. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) {
  273. wx.hideLoading()
  274. wx.showToast({
  275. title: data.message,
  276. icon: 'none'
  277. })
  278. setTimeout(() => { wx.navigateBack() }, 1000)
  279. } else {
  280. this.onPayError(data.message)
  281. }
  282. }
  283. } catch {
  284. wx.hideLoading()
  285. }
  286. },
  287. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  288. wx.login({
  289. success: async (wxres: any) => {
  290. const res = await api_executePayment({
  291. merOrderNo: paymentConfig.merOrderNo,
  292. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  293. paymentType,
  294. userId: app.globalData.userInfo?.id,
  295. code: wxres.code,
  296. wxMiniAppId: app.globalData.appId
  297. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  298. // wxPubAppId: 'wxbde13f59d40cb4f2'
  299. })
  300. wx.hideLoading()
  301. if (res.data.code === 200) {
  302. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  303. } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) {
  304. wx.hideLoading()
  305. wx.showToast({
  306. title: res.data.message,
  307. icon: 'none'
  308. })
  309. setTimeout(() => { wx.navigateBack() }, 1000)
  310. } else {
  311. this.onPayError(res.data.message)
  312. }
  313. },
  314. fail: () => {
  315. this.onPayError()
  316. }
  317. })
  318. },
  319. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  320. const isYeePay = paymentType.indexOf('yeepay') !== -1
  321. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  322. : paymentConfig?.expend
  323. ? JSON.parse(paymentConfig?.expend?.pay_info)
  324. : paymentConfig
  325. const that = this
  326. wx.requestPayment({
  327. timeStamp: prePayInfo.timeStamp,
  328. nonceStr: prePayInfo.nonceStr,
  329. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  330. paySign: prePayInfo.paySign,
  331. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  332. success() {
  333. wx.showToast({ title: '支付成功', icon: 'success' });
  334. setTimeout(() => {
  335. that.getDetail()
  336. }, 1500)
  337. },
  338. fail(ressonInfo) {
  339. console.log('支付失败', ressonInfo)
  340. that.onPayError()
  341. const goodsInfo = that.data.goodsInfo
  342. goodsInfo.orderNo = orderNo
  343. that.setData({
  344. goodsInfo
  345. })
  346. }
  347. })
  348. },
  349. /**
  350. * 用户点击右上角分享
  351. */
  352. onShareAppMessage() {
  353. return {
  354. title: ' 器乐数字ai助手',
  355. path: '/pages/index/index',
  356. imageUrl: 'https://oss.dayaedu.com/ktyq/1739929620914.png'
  357. }
  358. }
  359. })