order-result.ts 11 KB

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