order-detail.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid } from "../../api/login";
  3. import { formatPrice } from "../../utils/util";
  4. // 获取应用实例
  5. const app = getApp<IAppOption>()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. status: 'ing',
  12. statusList: {
  13. ing: {
  14. logo: './images/ing.png',
  15. title: '等待付款',
  16. content: '请尽快完成支付,以便我们为您处理订单'
  17. },
  18. },
  19. goodsInfo: {} as any,
  20. hasInstrument: false, // 是否有乐器
  21. receiveAddress: '', // 选择的地址信息
  22. receiveAddressInfo: {
  23. addressDetail: '',
  24. name: '',
  25. phoneNumber: ''
  26. },
  27. userBeneficiaryId: '', // 添加购买人信息
  28. paymentType: null as any, // 支付类型
  29. paymentChannel: null as any,
  30. showService: false,
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad(options: any) {
  36. this.queryPayType()
  37. if (options.orderInfo) {
  38. console.log('goods', options)
  39. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  40. console.log(goods, 'goods', options)
  41. const infos = {
  42. allSalePrice: 0,
  43. allOriginPrice: 0,
  44. allDiscountPrice: '',
  45. integerPart: '',
  46. decimalPart: '',
  47. name: '',
  48. shopId: '',
  49. orderNo: options.orderNo || '',
  50. goodsList: [] as any
  51. }
  52. // 是否有乐器
  53. let hasInstrument = false
  54. for (let i in goods) {
  55. const item = goods[i]
  56. if (item.goodsType === "INSTRUMENTS") {
  57. hasInstrument = true
  58. }
  59. infos.name = infos.name ? infos.name + '+' + item.name : item.name
  60. infos.shopId = item.shopId
  61. const afterPrice: any = formatPrice(item.salePrice)
  62. infos.goodsList.push({
  63. ...item,
  64. ...afterPrice
  65. })
  66. infos.allSalePrice += Number(item.salePrice)
  67. infos.allOriginPrice += Number(item.originalPrice)
  68. }
  69. const allAfterPrice: any = formatPrice(infos.allSalePrice)
  70. // console.log(infos.allOriginPrice, infos.allSalePrice)
  71. infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string
  72. infos.integerPart = allAfterPrice.integerPart
  73. infos.decimalPart = allAfterPrice.decimalPart
  74. // console.log(infos, 'infos')
  75. this.setData({
  76. goodsInfo: infos,
  77. status: options.status || '',
  78. hasInstrument
  79. });
  80. }
  81. },
  82. // 获取后台配置的支付方式
  83. async queryPayType() {
  84. try {
  85. // wxlite_payment_service_provider
  86. const { data } = await api_queryByParamName({
  87. paramName: app.globalData.appId
  88. });
  89. if (data.code == 200) {
  90. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  91. this.setData({
  92. paymentType: paramValue.vendor,
  93. paymentChannel: paramValue.channel
  94. });
  95. }
  96. } catch (error) {
  97. console.log(error, "error");
  98. }
  99. },
  100. /** 添加收货地址 */
  101. onSelectAddress() {
  102. wx.navigateTo({
  103. url: `../address/index`,
  104. })
  105. },
  106. onPayError(message?: string) {
  107. wx.hideLoading()
  108. wx.showToast({
  109. title: message || '支付取消',
  110. icon: 'none'
  111. })
  112. },
  113. // 购买
  114. async onSubmit() {
  115. wx.showLoading({
  116. mask: true,
  117. title: "订单提交中...",
  118. });
  119. try {
  120. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  121. if (orderNo) {
  122. const { data } = await api_userPaymentOrderUnpaid({
  123. orderNo: orderNo,
  124. paymentType: 'WECHAT_MINI'
  125. })
  126. if (data.code === 200) {
  127. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  128. this.onExecutePay(paymentConfig, paymentType, orderNo)
  129. } else {
  130. this.onPayError()
  131. }
  132. } else {
  133. const { data } = await api_executeOrder({
  134. "orderType": "WECHAT_MINI",
  135. "paymentType": this.data.paymentType,
  136. "paymentCashAmount": salePrice,
  137. "paymentCouponAmount": 0,
  138. "shopId": shopId,
  139. "openId": app.globalData.userInfo?.liteOpenid,
  140. "goodsInfos": [{
  141. "goodsId": id,
  142. "goodsNum": 1,
  143. "goodsType": "ACTIVATION_CODE",
  144. "paymentCashAmount": salePrice,
  145. "paymentCouponAmount": 0
  146. }],
  147. "orderName": name,
  148. "orderDesc": name
  149. })
  150. if (data.code === 200) {
  151. const { paymentConfig, paymentType, orderNo } = data.data
  152. this.onExecutePay(paymentConfig, paymentType, orderNo)
  153. } else if (data.code === 5200) {
  154. wx.hideLoading()
  155. wx.showToast({
  156. title: data.message,
  157. icon: 'none'
  158. })
  159. } else {
  160. this.onPayError()
  161. }
  162. }
  163. } catch {
  164. wx.hideLoading()
  165. }
  166. },
  167. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  168. wx.login({
  169. success: async (wxres: any) => {
  170. const res = await api_executePayment({
  171. merOrderNo: paymentConfig.merOrderNo,
  172. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  173. paymentType,
  174. userId: app.globalData.userInfo?.id,
  175. code: wxres.code,
  176. wxMiniAppId: app.globalData.appId
  177. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  178. // wxPubAppId: 'wxbde13f59d40cb4f2'
  179. })
  180. wx.hideLoading()
  181. if (res.data.code === 200) {
  182. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  183. } else {
  184. this.onPayError(res.data.message)
  185. }
  186. },
  187. fail: () => {
  188. this.onPayError()
  189. }
  190. })
  191. },
  192. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  193. const isYeePay = paymentType.indexOf('yeepay') !== -1
  194. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  195. : paymentConfig?.expend
  196. ? JSON.parse(paymentConfig?.expend?.pay_info)
  197. : paymentConfig
  198. const that = this
  199. wx.requestPayment({
  200. timeStamp: prePayInfo.timeStamp,
  201. nonceStr: prePayInfo.nonceStr,
  202. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  203. paySign: prePayInfo.paySign,
  204. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  205. success() {
  206. wx.showToast({ title: '支付成功', icon: 'success' });
  207. wx.redirectTo({
  208. url: '/pages/orders/order-result?orderNo=' + orderNo
  209. })
  210. },
  211. fail(ressonInfo) {
  212. console.log('支付失败', ressonInfo)
  213. that.onPayError()
  214. const goodsInfo = that.data.goodsInfo
  215. goodsInfo.orderNo = orderNo
  216. that.setData({
  217. goodsInfo
  218. })
  219. }
  220. })
  221. },
  222. /** 客服 */
  223. onService() {
  224. console.log("showService")
  225. this.setData({
  226. showService: true
  227. })
  228. },
  229. changePop(event: { detail: any }) {
  230. this.setData({
  231. showService: event.detail
  232. })
  233. },
  234. /**
  235. * 生命周期函数--监听页面初次渲染完成
  236. */
  237. onReady() {
  238. },
  239. /**
  240. * 生命周期函数--监听页面显示
  241. */
  242. onShow() {
  243. },
  244. /**
  245. * 生命周期函数--监听页面隐藏
  246. */
  247. onHide() {
  248. },
  249. /**
  250. * 生命周期函数--监听页面卸载
  251. */
  252. onUnload() {
  253. },
  254. /**
  255. * 页面相关事件处理函数--监听用户下拉动作
  256. */
  257. onPullDownRefresh() {
  258. },
  259. /**
  260. * 页面上拉触底事件的处理函数
  261. */
  262. onReachBottom() {
  263. },
  264. /**
  265. * 用户点击右上角分享
  266. */
  267. onShareAppMessage() {
  268. return {
  269. title: '器乐数字AI工具',
  270. path: '/pages/index/index',
  271. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  272. }
  273. }
  274. })