order-detail.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // pages/orders/order-detail.ts
  2. import { api_executeOrderV2, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid, api_getUserReceiveAddressPage } from "../../api/login";
  3. // 获取应用实例
  4. const app = getApp<IAppOption>()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. popupShow: false,
  11. isAddressInfoTip: false,
  12. addressInfo: {} as any,
  13. serviceShow: true,
  14. status: 'ing',
  15. statusList: {
  16. ing: {
  17. logo: './images/ing.png',
  18. title: '待付款',
  19. content: '为了确保您的订单顺利进行,请尽快完成支付'
  20. },
  21. },
  22. goodsInfo: {} as any,
  23. instrumentsInfo: {} as any,
  24. memberInfo: {} as any,
  25. userNote: '', // 会员手机号
  26. paymentType: null as any, // 支付类型
  27. paymentChannel: null as any,
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad(options: any) {
  33. this.queryPayType()
  34. if (options.orderInfo) {
  35. const goods = JSON.parse(decodeURIComponent(options.orderInfo));
  36. const instrumentsInfo = JSON.parse(decodeURIComponent(options.instrumentsInfo));
  37. // const memberInfo = JSON.parse(decodeURIComponent(options.memberInfo));
  38. // console.log(goods, 'goods', instrumentsInfo, memberInfo)
  39. this.setData({
  40. goodsInfo: goods,
  41. instrumentsInfo,
  42. userNote: options.userNote,
  43. status: goods.status
  44. });
  45. }
  46. },
  47. onShow() {
  48. this.setData({
  49. serviceShow: true
  50. })
  51. },
  52. onHide() {
  53. this.setData({
  54. serviceShow: false
  55. })
  56. },
  57. async onAddressInfo() {
  58. this.setData({
  59. isAddressInfoTip: false
  60. })
  61. if (this.data.goodsInfo.orderNo) {
  62. return
  63. }
  64. try {
  65. const resData = await api_getUserReceiveAddressPage()
  66. const pageRows = resData?.data?.data?.rows || []
  67. // 当有收货地址的时候跳转到收货地址选择列表页面,当没有收货地址的时候到添加收货地址
  68. if (pageRows.length) {
  69. wx.navigateTo({
  70. url: `/pages/address/addressList?id=${this.data.addressInfo.id}`
  71. });
  72. } else {
  73. this.setData({
  74. popupShow: true
  75. })
  76. }
  77. } catch (e) {
  78. console.log(e, 'e')
  79. }
  80. },
  81. onAddAddress(e: any) {
  82. const { addressInfo } = e.detail
  83. this.setData({
  84. addressInfo
  85. })
  86. },
  87. // 获取后台配置的支付方式
  88. async queryPayType() {
  89. try {
  90. // wxlite_payment_service_provider
  91. const { data } = await api_queryByParamName({
  92. paramName: app.globalData.appId
  93. });
  94. if (data.code == 200) {
  95. const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {}
  96. this.setData({
  97. paymentType: paramValue.vendor,
  98. paymentChannel: paramValue.channel
  99. });
  100. }
  101. } catch (error) {
  102. console.log(error, "error");
  103. }
  104. },
  105. onPayError(message?: string) {
  106. wx.hideLoading()
  107. wx.showToast({
  108. title: message || '支付取消',
  109. icon: 'none'
  110. })
  111. },
  112. // 购买
  113. async onSubmit() {
  114. // 有乐器必须填收货信息
  115. if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) {
  116. wx.showToast({
  117. title: "请填写收货信息",
  118. icon: 'none'
  119. })
  120. this.setData({
  121. isAddressInfoTip: true
  122. })
  123. return
  124. }
  125. console.log(this.data.addressInfo)
  126. wx.showLoading({
  127. mask: true,
  128. title: "订单提交中...",
  129. });
  130. // 清除上一个页面的弹窗 有弹窗的时候数据返回回去数据才会刷新
  131. const pages = getCurrentPages();
  132. const prevPage = pages[pages.length - 2];
  133. prevPage.setData({ popupShow: false, memberInfo: {} });
  134. try {
  135. const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo
  136. if (orderNo) {
  137. const { data } = await api_userPaymentOrderUnpaid({
  138. orderNo: orderNo,
  139. paymentType: 'WECHAT_MINI'
  140. })
  141. if (data.code === 200) {
  142. const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig
  143. this.onExecutePay(paymentConfig, paymentType, orderNo)
  144. } else {
  145. this.onPayError()
  146. }
  147. } else {
  148. const goodsInfos = [{
  149. "goodsId": id,
  150. "goodsNum": 1,
  151. "goodsType": "ACTIVATION_CODE",
  152. "paymentCashAmount": salePrice,
  153. "paymentCouponAmount": 0
  154. }]
  155. // 乐器
  156. if (this.data.instrumentsInfo.id) {
  157. goodsInfos.push({
  158. "goodsId": this.data.instrumentsInfo.id,
  159. "goodsNum": 1,
  160. "goodsType": "INSTRUMENTS",
  161. "paymentCashAmount": this.data.instrumentsInfo.salePrice,
  162. "paymentCouponAmount": 0
  163. })
  164. }
  165. const { data } = await api_executeOrderV2({
  166. "orderType": "WECHAT_MINI",
  167. "paymentType": this.data.paymentType,
  168. "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0),
  169. "paymentCouponAmount": 0,
  170. "shopId": shopId,
  171. "openId": app.globalData.userInfo?.liteOpenid,
  172. goodsInfos,
  173. "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  174. "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""),
  175. "receiveAddress": this.data.addressInfo.id || "",
  176. userNote: this.data.userNote
  177. // "userBeneficiaryId": this.data.memberInfo.id
  178. })
  179. if (data.code === 200) {
  180. const { paymentConfig, paymentType, orderNo } = data.data
  181. this.onExecutePay(paymentConfig, paymentType, orderNo)
  182. } else if (data.code === 5435) {
  183. this.onPayError(data.message)
  184. setTimeout(() => { wx.navigateBack() }, 1000)
  185. }else {
  186. this.onPayError(data.message)
  187. }
  188. }
  189. } catch {
  190. wx.hideLoading()
  191. }
  192. },
  193. async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) {
  194. wx.login({
  195. success: async (wxres: any) => {
  196. const res = await api_executePayment({
  197. merOrderNo: paymentConfig.merOrderNo,
  198. paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', //
  199. paymentType,
  200. userId: app.globalData.userInfo?.id,
  201. code: wxres.code,
  202. wxMiniAppId: app.globalData.appId
  203. // code: '011yjYkl289aye4q2zml24UEWT3yjYkn',
  204. // wxPubAppId: 'wxbde13f59d40cb4f2'
  205. })
  206. wx.hideLoading()
  207. if (res.data.code === 200) {
  208. this.onPay(paymentType, res.data.data.reqParams, orderNo)
  209. } else if (res.data.code === 5435) {
  210. this.onPayError(res.data.message)
  211. setTimeout(() => { wx.navigateBack() }, 1000)
  212. }else {
  213. this.onPayError(res.data.message)
  214. }
  215. },
  216. fail: () => {
  217. this.onPayError()
  218. }
  219. })
  220. },
  221. onPay(paymentType: string, paymentConfig: any, orderNo: string) {
  222. const isYeePay = paymentType.indexOf('yeepay') !== -1
  223. const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn)
  224. : paymentConfig?.expend
  225. ? JSON.parse(paymentConfig?.expend?.pay_info)
  226. : paymentConfig
  227. const that = this
  228. wx.requestPayment({
  229. timeStamp: prePayInfo.timeStamp,
  230. nonceStr: prePayInfo.nonceStr,
  231. package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue,
  232. paySign: prePayInfo.paySign,
  233. signType: prePayInfo.signType ? prePayInfo.signType : 'MD5',
  234. success() {
  235. wx.showToast({ title: '支付成功', icon: 'success' });
  236. wx.redirectTo({
  237. url: '/pages/orders/order-result?orderNo=' + orderNo
  238. })
  239. },
  240. fail(ressonInfo) {
  241. console.log('支付失败', ressonInfo)
  242. that.onPayError()
  243. const goodsInfo = that.data.goodsInfo
  244. goodsInfo.orderNo = orderNo
  245. that.setData({
  246. goodsInfo
  247. })
  248. wx.redirectTo({
  249. url: '/pages/orders/order-result?orderNo=' + orderNo
  250. })
  251. }
  252. })
  253. },
  254. /**
  255. * 用户点击右上角分享
  256. */
  257. onShareAppMessage() {
  258. return {
  259. title: '乐迷器乐AI',
  260. path: '/pages/index/index',
  261. imageUrl: 'https://oss.dayaedu.com/ktyq/1739773011143.png'
  262. }
  263. }
  264. })