index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <!-- 结算页面 -->
  2. <template>
  3. <view class="container">
  4. <!-- <u-navbar title="确认订单" autoBack></u-navbar> -->
  5. <!-- 收货地址 -->
  6. <view class="address-section" @click="chooseWxAddress">
  7. <template v-if="address">
  8. <view class="address-content">
  9. <view class="info">
  10. <text class="name">{{address.userName}}</text>
  11. <text class="phone">{{address.telNumber}}</text>
  12. </view>
  13. <view class="address">
  14. {{address.provinceName}}{{address.cityName}}{{address.countyName}}{{address.detailInfo}}
  15. </view>
  16. </view>
  17. </template>
  18. <template v-else>
  19. <view class="address-empty">
  20. <text>请选择收货地址</text>
  21. <u-icon name="arrow-right" color="#999"></u-icon>
  22. </view>
  23. </template>
  24. </view>
  25. <!-- 商品信息 -->
  26. <view class="goods-section" v-if="sku">
  27. <view class="goods-item" v-for="(item, index) in Array.isArray(sku) ? sku : [sku]" :key="index">
  28. <image :src="item.skuImage" mode="aspectFill" class="goods-img"></image>
  29. <view class="goods-info">
  30. <view class="goods-name">{{item.productName}}</view>
  31. <view class="goods-spec">{{item.skuName}}</view>
  32. <view class="price-quantity">
  33. <text class="price">¥{{item.price}}</text>
  34. <text class="quantity">x{{item.count}}</text>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- 空状态 -->
  40. <view class="empty-state" v-else>
  41. <text>商品信息不存在</text>
  42. </view>
  43. <!-- 支付方式 -->
  44. <view class="payment-section">
  45. <view class="section-title">支付方式</view>
  46. <view class="payment-list">
  47. <view class="payment-item" @click="selectPayment('wxpay')" :class="{active: paymentMethod === 'wxpay'}">
  48. <view class="left">
  49. <image src="/static/images/wxpay.png" mode="aspectFit" class="payment-icon"></image>
  50. <text>微信支付</text>
  51. </view>
  52. <u-icon v-if="paymentMethod === 'wxpay'" name="checkmark" color="#F95B5B"></u-icon>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 订单金额 -->
  57. <view class="amount-section">
  58. <view class="amount-item">
  59. <text>商品金额</text>
  60. <text class="price">¥{{totalProductAmount}}</text>
  61. </view>
  62. <view class="amount-item">
  63. <text>运费</text>
  64. <text class="price">¥{{freightAmount}}</text>
  65. </view>
  66. <view class="amount-item total">
  67. <text>实付款</text>
  68. <text class="price">¥{{totalAmount}}</text>
  69. </view>
  70. </view>
  71. <!-- 底部提交栏 -->
  72. <view class="submit-bar">
  73. <view class="total-amount">
  74. <text>合计:</text>
  75. <text class="price">¥{{totalAmount}}</text>
  76. </view>
  77. <button class="submit-btn" @click="submitOrder" :loading="submitting" :disabled="submitting">
  78. {{submitting ? '提交中...' : '提交订单'}}
  79. </button>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import {
  85. mapGetters
  86. } from 'vuex';
  87. import {
  88. submitOrder,
  89. saveCartOrder
  90. } from '../../../config/api.js';
  91. export default {
  92. data() {
  93. return {
  94. sku: null,
  95. address: null,
  96. paymentMethod: 'wxpay',
  97. submitting: false,
  98. freightAmount: 0,
  99. isFromCart: false
  100. }
  101. },
  102. computed: {
  103. ...mapGetters(['isLogin']),
  104. totalProductAmount() {
  105. if (!this.sku) return 0;
  106. if (Array.isArray(this.sku)) {
  107. return this.sku.reduce((total, item) => total + (item.price * item.count), 0);
  108. }
  109. return this.sku.price * this.sku.count;
  110. },
  111. totalAmount() {
  112. return this.totalProductAmount + this.freightAmount;
  113. }
  114. },
  115. onLoad(options) {
  116. if (options.sku) {
  117. try {
  118. this.sku = JSON.parse(decodeURIComponent(options.sku));
  119. this.isFromCart = false;
  120. } catch (error) {
  121. console.error('解析商品数据失败:', error);
  122. uni.showToast({
  123. title: '商品数据错误',
  124. icon: 'none'
  125. });
  126. setTimeout(() => {
  127. uni.navigateBack();
  128. }, 1500);
  129. }
  130. } else if (options.items) {
  131. try {
  132. const items = JSON.parse(decodeURIComponent(options.items));
  133. this.isFromCart = true;
  134. // 将购物车商品数据转换为结算页面需要的格式
  135. this.sku = items.map(item => ({
  136. id: item.id,
  137. skuId: item.skuId,
  138. skuImage: item.images,
  139. productName: item.productName,
  140. skuName: item.skuName,
  141. price: item.price,
  142. count: item.count
  143. }));
  144. } catch (error) {
  145. console.error('解析购物车数据失败:', error);
  146. uni.showToast({
  147. title: '商品数据错误',
  148. icon: 'none'
  149. });
  150. setTimeout(() => {
  151. uni.navigateBack();
  152. }, 1500);
  153. }
  154. } else {
  155. uni.showToast({
  156. title: '商品数据不存在',
  157. icon: 'none'
  158. });
  159. setTimeout(() => {
  160. uni.navigateBack();
  161. }, 1500);
  162. }
  163. },
  164. methods: {
  165. // 选择微信收货地址
  166. chooseWxAddress() {
  167. uni.chooseAddress({
  168. success: (res) => {
  169. this.address = res;
  170. },
  171. fail: (err) => {
  172. console.error('获取收货地址失败:', err);
  173. if (err.errMsg.includes('auth deny')) {
  174. uni.showModal({
  175. title: '提示',
  176. content: '需要您的授权才能获取收货地址,是否前往设置?',
  177. success: (res) => {
  178. if (res.confirm) {
  179. uni.openSetting();
  180. }
  181. }
  182. });
  183. } else {
  184. uni.$u.toast('获取收货地址失败');
  185. }
  186. }
  187. });
  188. },
  189. // 选择支付方式
  190. selectPayment(method) {
  191. this.paymentMethod = method;
  192. },
  193. // 提交订单
  194. async submitOrder() {
  195. if (!this.address) {
  196. return uni.$u.toast('请选择收货地址');
  197. }
  198. if (this.submitting) return;
  199. this.submitting = true;
  200. try {
  201. if (this.isFromCart) {
  202. // 购物车批量下单
  203. const orderList = this.sku.map(item => ({
  204. province: this.address.provinceName,
  205. city: this.address.cityName,
  206. area: this.address.countyName,
  207. address: this.address.detailInfo,
  208. mobile: this.address.telNumber,
  209. name: this.address.userName,
  210. skuId: item.skuId,
  211. count: item.count,
  212. payType: this.paymentMethod === 'wxpay' ? 1 : 2
  213. }));
  214. const res = await saveCartOrder(orderList);
  215. if (res.code === 200) {
  216. // 跳转到支付页面
  217. uni.navigateTo({
  218. url: `/packageOrder/pages/payment/index?orderId=${res.data.id}`
  219. });
  220. } else {
  221. uni.$u.toast(res.msg || '提交订单失败');
  222. }
  223. } else {
  224. // 单个商品下单
  225. const orderData = {
  226. mobile: this.address.telNumber,
  227. name: this.address.userName,
  228. province: this.address.provinceName,
  229. city: this.address.cityName,
  230. area: this.address.countyName,
  231. address: this.address.detailInfo,
  232. postalCode: this.address.postalCode,
  233. skuId: this.sku.id,
  234. count: this.sku.count,
  235. paymentMethod: this.paymentMethod
  236. };
  237. const res = await submitOrder(orderData);
  238. if (res.code === 200) {
  239. // 跳转到支付页面
  240. uni.navigateTo({
  241. url: `/packageOrder/pages/payment/index?orderId=${res.data.id}`
  242. });
  243. } else {
  244. uni.$u.toast(res.msg || '提交订单失败');
  245. }
  246. }
  247. } catch (error) {
  248. console.error('提交订单失败:', error);
  249. uni.$u.toast('提交订单失败,请重试');
  250. } finally {
  251. this.submitting = false;
  252. }
  253. }
  254. }
  255. }
  256. </script>
  257. <style lang="scss">
  258. .container {
  259. min-height: 100vh;
  260. background-color: #f5f5f5;
  261. padding-bottom: 120rpx;
  262. }
  263. .address-section {
  264. background-color: #fff;
  265. margin: 20rpx;
  266. padding: 30rpx;
  267. border-radius: 12rpx;
  268. .address-content {
  269. .info {
  270. margin-bottom: 20rpx;
  271. .name {
  272. font-size: 32rpx;
  273. font-weight: bold;
  274. margin-right: 20rpx;
  275. }
  276. .phone {
  277. font-size: 28rpx;
  278. color: #666;
  279. }
  280. }
  281. .address {
  282. font-size: 28rpx;
  283. color: #333;
  284. }
  285. }
  286. .address-empty {
  287. display: flex;
  288. align-items: center;
  289. justify-content: space-between;
  290. font-size: 28rpx;
  291. color: #999;
  292. }
  293. }
  294. .goods-section {
  295. background-color: #fff;
  296. margin: 20rpx;
  297. padding: 30rpx;
  298. border-radius: 12rpx;
  299. .goods-item {
  300. display: flex;
  301. .goods-img {
  302. width: 160rpx;
  303. height: 160rpx;
  304. border-radius: 8rpx;
  305. margin-bottom: 10rpx;
  306. }
  307. .goods-info {
  308. flex: 1;
  309. margin-left: 20rpx;
  310. .goods-name {
  311. font-size: 28rpx;
  312. color: #333;
  313. margin-bottom: 10rpx;
  314. }
  315. .goods-spec {
  316. font-size: 24rpx;
  317. color: #999;
  318. margin-bottom: 20rpx;
  319. }
  320. .price-quantity {
  321. display: flex;
  322. justify-content: space-between;
  323. align-items: center;
  324. .price {
  325. font-size: 32rpx;
  326. color: #F95B5B;
  327. font-weight: bold;
  328. }
  329. .quantity {
  330. font-size: 26rpx;
  331. color: #999;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. .payment-section {
  338. background-color: #fff;
  339. margin: 20rpx;
  340. padding: 30rpx;
  341. border-radius: 12rpx;
  342. .section-title {
  343. font-size: 28rpx;
  344. color: #333;
  345. margin-bottom: 20rpx;
  346. }
  347. .payment-item {
  348. display: flex;
  349. justify-content: space-between;
  350. align-items: center;
  351. padding: 20rpx 0;
  352. &.active {
  353. .left {
  354. text {
  355. color: #F95B5B;
  356. }
  357. }
  358. }
  359. .left {
  360. display: flex;
  361. align-items: center;
  362. .payment-icon {
  363. width: 40rpx;
  364. height: 40rpx;
  365. margin-right: 20rpx;
  366. }
  367. text {
  368. font-size: 28rpx;
  369. color: #333;
  370. }
  371. }
  372. }
  373. }
  374. .amount-section {
  375. background-color: #fff;
  376. margin: 20rpx;
  377. padding: 30rpx;
  378. border-radius: 12rpx;
  379. .amount-item {
  380. display: flex;
  381. justify-content: space-between;
  382. margin-bottom: 20rpx;
  383. font-size: 28rpx;
  384. color: #666;
  385. &.total {
  386. margin-top: 20rpx;
  387. padding-top: 20rpx;
  388. border-top: 1px solid #f5f5f5;
  389. color: #333;
  390. font-weight: bold;
  391. .price {
  392. color: #F95B5B;
  393. font-size: 32rpx;
  394. }
  395. }
  396. .price {
  397. color: #333;
  398. }
  399. }
  400. }
  401. .submit-bar {
  402. position: fixed;
  403. bottom: 0;
  404. left: 0;
  405. right: 0;
  406. height: 100rpx;
  407. background-color: #fff;
  408. display: flex;
  409. align-items: center;
  410. padding: 0 30rpx;
  411. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  412. .total-amount {
  413. flex: 1;
  414. font-size: 28rpx;
  415. .price {
  416. color: #F95B5B;
  417. font-size: 36rpx;
  418. font-weight: bold;
  419. }
  420. }
  421. .submit-btn {
  422. width: 240rpx;
  423. height: 72rpx;
  424. line-height: 72rpx;
  425. background-color: #F95B5B;
  426. color: #fff;
  427. font-size: 28rpx;
  428. border-radius: 36rpx;
  429. text-align: center;
  430. &[disabled] {
  431. background-color: #ccc;
  432. }
  433. }
  434. }
  435. .empty-state {
  436. text-align: center;
  437. padding: 40rpx;
  438. color: #999;
  439. font-size: 28rpx;
  440. }
  441. </style>