order-detail.ts 7.7 KB

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