cashier-desk.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="cashier-desk">
  3. <view class="view-top">
  4. </view>
  5. <view class="payment-methods">
  6. <u-radio-group v-model="formData.payType" @change="radioGroupChange">
  7. <!-- #ifdef MP-WEIXIN -->
  8. <view class="payment-item">
  9. <view class="left">
  10. <image src="/pages-mine/static/pay1.png" class="payment-icon"></image>
  11. <text>微信支付</text>
  12. </view>
  13. <u-radio active-color="#38C148" name="2" />
  14. </view>
  15. <!-- #endif -->
  16. <!-- #ifdef MP-ALIPAY -->
  17. <view class="payment-item">
  18. <view class="left">
  19. <image src="/pages-mine/static/alipay.png" class="payment-icon"></image>
  20. <text>支付宝支付</text>
  21. </view>
  22. <u-radio active-color="#38C148" name="2" />
  23. </view>
  24. <!-- #endif -->
  25. <view class="payment-item">
  26. <view class="left">
  27. <image src="/pages-mine/static/pay2.png" class="payment-icon"></image>
  28. <text>余额支付(余额{{ orderInfo.accountMoney || 0 }})</text>
  29. </view>
  30. <u-radio active-color="#38C148" name="1" />
  31. </view>
  32. <!-- <view class="payment-item">
  33. <view class="left">
  34. <image src="/pages-mine/static/pay2.png" class="payment-icon"></image>
  35. <text>组合支付</text>
  36. </view>
  37. <u-radio active-color="#38C148" name="3" />
  38. </view> -->
  39. </u-radio-group>
  40. </view>
  41. <view class="total">
  42. <text>总计</text>
  43. <text class="amount">¥ {{ formData.payType === '1' ? orderInfo.accountDiscountMoney : orderInfo.orderMoney }}</text>
  44. </view>
  45. <button class="confirm-btn" type="primary" @click="onConfirmPayment">确认支付</button>
  46. </view>
  47. </template>
  48. <script>
  49. export default {
  50. data() {
  51. return {
  52. formData: {
  53. payType: '2',
  54. orderId: ''
  55. },
  56. orderInfo: {}
  57. }
  58. },
  59. onLoad(options) {
  60. // 从本地存储获取订单信息
  61. const info = uni.getStorageSync('orderBuyInfo');
  62. if (info) {
  63. this.orderInfo = info;
  64. this.formData.orderId = info.orderId;
  65. } else {
  66. uni.showToast({
  67. title: '订单信息不存在',
  68. icon: 'none'
  69. });
  70. setTimeout(() => {
  71. uni.navigateBack();
  72. }, 1500);
  73. }
  74. },
  75. methods: {
  76. radioGroupChange(e) {
  77. console.log(e);
  78. },
  79. // 统一支付方法
  80. handlePayment(payData) {
  81. // #ifdef MP-WEIXIN
  82. this.wxPayment(payData);
  83. // #endif
  84. // #ifdef MP-ALIPAY
  85. this.aliPayment(payData);
  86. // #endif
  87. },
  88. // 微信支付方法
  89. wxPayment(payData) {
  90. uni.requestPayment({
  91. provider: 'wxpay',
  92. timeStamp: payData.timeStamp,
  93. nonceStr: payData.nonceStr,
  94. package: `prepay_id=${payData.prepayId}`,
  95. signType: payData.signType,
  96. paySign: payData.paySign,
  97. success: (res) => {
  98. // 支付成功
  99. this.paySuccess();
  100. },
  101. fail: (err) => {
  102. console.error('微信支付失败:', err);
  103. uni.showToast({
  104. title: '支付失败',
  105. icon: 'none'
  106. });
  107. }
  108. });
  109. },
  110. // 支付宝支付方法
  111. aliPayment(payData) {
  112. my.tradePay({
  113. tradeNO: payData.tradeNo,
  114. success: (res) => {
  115. if (res.resultCode === '9000') {
  116. // 支付成功
  117. this.paySuccess();
  118. } else {
  119. uni.showToast({
  120. title: '支付失败',
  121. icon: 'none'
  122. });
  123. }
  124. },
  125. fail: (err) => {
  126. console.error('支付宝支付失败:', err);
  127. uni.showToast({
  128. title: '支付失败',
  129. icon: 'none'
  130. });
  131. }
  132. });
  133. },
  134. paySuccess() {
  135. uni.redirectTo({
  136. url: `/pages-car/pages/pay-success?orderId=${this.orderInfo.orderId}`
  137. });
  138. },
  139. // 发起支付
  140. onConfirmPayment() {
  141. uni.showLoading({
  142. title: '支付处理中'
  143. })
  144. // 构建参数
  145. const params = {
  146. payType: this.formData.payType,
  147. orderId: this.orderInfo.orderId
  148. };
  149. // 获取支付参数
  150. this.$u.api.payShopOrderAjax(params).then(res => {
  151. if (res.code === 200) {
  152. if (res.data.needPay == 1) {
  153. // 调用统一支付方法
  154. this.handlePayment(res.data);
  155. } else {
  156. // 不需要支付(如全额余额支付),直接跳转成功
  157. this.paySuccess();
  158. }
  159. } else {
  160. uni.showToast({
  161. title: res.msg || '获取支付参数失败',
  162. icon: 'none'
  163. });
  164. }
  165. }).catch(err => {
  166. console.error('获取支付参数失败:', err);
  167. uni.showToast({
  168. title: '请求失败',
  169. icon: 'none'
  170. });
  171. }).finally(() => {
  172. uni.hideLoading();
  173. })
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .cashier-desk {
  180. min-height: 100vh;
  181. background-color: #f5f5f5;
  182. .view-top {
  183. background: #38C148;
  184. height: 160rpx;
  185. width: 100%;
  186. }
  187. .payment-methods {
  188. background-color: #ffffff;
  189. border-radius: 16rpx;
  190. margin: 30rpx;
  191. margin-top: -40px;
  192. margin-bottom: 20rpx;
  193. .payment-item {
  194. display: flex;
  195. justify-content: space-between;
  196. align-items: center;
  197. padding: 30rpx;
  198. border-bottom: 1px solid #f5f5f5;
  199. &:last-child {
  200. border-bottom: none;
  201. }
  202. .left {
  203. display: flex;
  204. align-items: center;
  205. gap: 20rpx;
  206. .payment-icon {
  207. width: 40rpx;
  208. height: 40rpx;
  209. }
  210. text {
  211. font-size: 30rpx;
  212. color: #333;
  213. }
  214. }
  215. }
  216. }
  217. .total {
  218. background-color: #ffffff;
  219. border-radius: 16rpx;
  220. padding: 30rpx;
  221. display: flex;
  222. justify-content: space-between;
  223. align-items: center;
  224. margin: 30rpx;
  225. margin-bottom: 20rpx;
  226. text {
  227. font-size: 30rpx;
  228. color: #333;
  229. }
  230. .amount {
  231. color: #ff0000;
  232. font-size: 32rpx;
  233. font-weight: bold;
  234. }
  235. }
  236. .confirm-btn {
  237. width: 90%;
  238. margin-left: 5%;
  239. margin-top: 100rpx;
  240. background: #38C148;
  241. color: #fff;
  242. border-radius: 45rpx;
  243. &::after {
  244. border: none;
  245. }
  246. }
  247. }
  248. </style>