cashier-desk.vue 9.5 KB

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