建材商城系统20241014
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
15 KiB

8 months ago
  1. <template>
  2. <view class="hand-firm">
  3. <navbar
  4. title="确定下单"
  5. leftClick
  6. @leftClick="$utils.navigateBack"
  7. />
  8. <!-- 商品信息 -->
  9. <view class="content-wrapper">
  10. <view class="section-wrapper">
  11. <view class="section-title">
  12. <view class="title-bar"></view>
  13. <text>商品信息</text>
  14. </view>
  15. <view class="product-card">
  16. <image class="product-image" :src="productInfo.image &&
  17. productInfo.image.split(',')[0]" mode="aspectFit"></image>
  18. <view class="product-details">
  19. <view class="product-name">{{productInfo.name || ''}}</view>
  20. <view class="product-tags">
  21. <text class="tag">{{productInfo.categoryName || ''}}</text>
  22. <text class="tag">快速下单</text>
  23. </view>
  24. <view class="product-price">
  25. <text class="price-value">¥{{productInfo.price || ''}}</text>
  26. <text class="price-unit">/{{productInfo.unit || ''}}</text>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 个人信息 -->
  32. <view class="section-wrapper">
  33. <view class="section-title">
  34. <view class="title-bar"></view>
  35. <text>填写个人信息</text>
  36. </view>
  37. <view class="address-section">
  38. <view v-if="addressTotal > 0" class="has-address">
  39. <view class="address-info" @click="openAddress">
  40. <view class="address-user">
  41. <text class="name">{{address.name}}</text>
  42. <text class="phone">{{address.phone}}</text>
  43. </view>
  44. <view class="address-detail">
  45. {{address.address}} {{address.addressDetails}}
  46. </view>
  47. <view class="address-action">
  48. <text class="address-tip">已添加过的地址</text>
  49. <view class="arrow-right"></view>
  50. </view>
  51. </view>
  52. <view class="address-tag">
  53. <text>已添加过的地址</text>
  54. </view>
  55. </view>
  56. <!-- 地址表单 -->
  57. <view v-else class="no-address">
  58. <redact-address-form
  59. ref="addressForm"
  60. @saveOrUpdate="handleAddressSave"
  61. ></redact-address-form>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <!-- 底部按钮 -->
  67. <view class="order-submit">
  68. <button class="submit-btn" @click="submitOrder">快捷下单</button>
  69. </view>
  70. <!-- 地址选择弹窗 -->
  71. <uv-popup ref="addressPopup" :round="30" style="padding-bottom: 90rpx;">
  72. <addressList ref="addressList" height="60vh" @select="selectAddress" />
  73. <view class="add-btn">
  74. <view @click="$utils.navigateTo('/pages_order/mine/address?type=back')" class="button-submit">新增地址</view>
  75. </view>
  76. </uv-popup>
  77. </view>
  78. </template>
  79. <script>
  80. import redactAddressForm from '../components/address/redactAddressForm.vue';
  81. import addressList from '../components/address/addressList.vue';
  82. export default {
  83. components: {
  84. redactAddressForm,
  85. addressList
  86. },
  87. data() {
  88. return {
  89. productInfo: {}, // 商品信息
  90. orderId: '', // 订单ID
  91. address: {
  92. name: '请选择地址',
  93. address: '',
  94. phone: ''
  95. },
  96. addressTotal: 0,
  97. orderInfo: [],
  98. isLoading: false, // 加载状态
  99. shouldSubmitOrder: false // 标记是否应该在获取地址后自动提交订单
  100. };
  101. },
  102. onLoad(options) {
  103. // 获取订单ID
  104. if (options.orderId) {
  105. this.orderId = options.orderId;
  106. this.getOrderInfo();
  107. }
  108. },
  109. onShow() {
  110. // 获取地址列表
  111. this.getAddressList();
  112. },
  113. methods: {
  114. // 获取订单信息
  115. getOrderInfo() {
  116. this.$api('getOrderInfo', res => {
  117. if (res.code === 200 && res.result[0]) {
  118. // 如果返回商品信息,则设置商品信息
  119. if (res.result[0].commonShop) {
  120. this.productInfo = res.result[0].commonShop;
  121. }
  122. }
  123. });
  124. },
  125. // 获取地址列表
  126. getAddressList() {
  127. // 调用地址列表组件获取地址
  128. this.$refs.addressList.getAddressList().then(res => {
  129. if (res && res.total) {
  130. this.addressTotal = res.total;
  131. // 查找默认地址
  132. if (res.records && res.records.length > 0) {
  133. const defaultAddress = res.records.find(addr => addr.defaultFlag === '1');
  134. if (defaultAddress) {
  135. this.address = defaultAddress;
  136. } else {
  137. // 如果没有默认地址,使用第一个地址
  138. this.address = res.records[0];
  139. }
  140. // 如果标记为需要自动提交订单
  141. if (this.shouldSubmitOrder) {
  142. this.shouldSubmitOrder = false; // 重置标记
  143. this.submitOrder(true); // 执行提交订单,传入true表示不再验证地址
  144. }
  145. }
  146. } else {
  147. this.addressTotal = 0;
  148. }
  149. // 完成加载
  150. this.isLoading = false;
  151. }).catch(err => {
  152. console.error('获取地址列表失败', err);
  153. this.addressTotal = 0;
  154. this.isLoading = false;
  155. });
  156. },
  157. // 打开地址选择弹窗
  158. openAddress() {
  159. this.$refs.addressPopup.open('bottom');
  160. },
  161. // 选择地址
  162. selectAddress(address) {
  163. this.address = address;
  164. this.$refs.addressPopup.close();
  165. },
  166. // 处理地址保存
  167. handleAddressSave(address) {
  168. // 显示加载状态
  169. this.isLoading = true;
  170. // 保存地址
  171. this.$api('saveOrUpdateAddress', address, res => {
  172. if (res.code === 200) {
  173. uni.showToast({
  174. title: '地址保存成功',
  175. icon: 'success'
  176. });
  177. // 标记需要在获取地址后自动提交订单
  178. this.shouldSubmitOrder = true;
  179. // 重新获取地址列表
  180. this.getAddressList();
  181. } else {
  182. uni.showToast({
  183. title: res.message || '保存失败',
  184. icon: 'none'
  185. });
  186. this.isLoading = false;
  187. }
  188. });
  189. },
  190. // 提交订单
  191. submitOrder(skipAddressCheck = false) {
  192. if (!skipAddressCheck && this.addressTotal === 0) {
  193. const addressForm = this.$refs.addressForm;
  194. // 验证地址表单
  195. const isValid = addressForm.parameterVerification(addressForm.addressDetail);
  196. if (!isValid.auth) {
  197. uni.showToast({
  198. title: isValid.title,
  199. icon: 'none'
  200. });
  201. return;
  202. }
  203. // 显示加载状态
  204. this.isLoading = true;
  205. // 保存地址并继续
  206. addressForm.onSubmit();
  207. return;
  208. }
  209. // 显示加载中
  210. uni.showLoading({
  211. title: '提交订单中...'
  212. });
  213. // 创建订单
  214. this.$api('createOrder', {
  215. addressId: this.address.id,
  216. productId: this.productInfo.id,
  217. num : 1,
  218. payType: 1, // 默认微信支付
  219. orderId: this.orderId
  220. }, res => {
  221. uni.hideLoading();
  222. if (res.code === 200) {
  223. uni.showToast({
  224. title: '下单成功',
  225. icon: 'success',
  226. duration: 1500,
  227. success: () => {
  228. setTimeout(() => {
  229. // 跳转到订单列表页
  230. this.$utils.redirectTo('/pages_order/order/orderList');
  231. }, 1500);
  232. }
  233. });
  234. } else {
  235. uni.showToast({
  236. title: res.message || '下单失败',
  237. icon: 'none'
  238. });
  239. }
  240. });
  241. }
  242. }
  243. }
  244. </script>
  245. <style scoped lang="scss">
  246. .hand-firm {
  247. min-height: 100vh;
  248. background-color: #f6f6f6;
  249. padding-bottom: 120rpx;
  250. .content-wrapper {
  251. padding: 20rpx;
  252. }
  253. .section-wrapper {
  254. margin-bottom: 20rpx;
  255. border-radius: 12rpx;
  256. overflow: hidden;
  257. background-color: #fff;
  258. }
  259. .section-title {
  260. display: flex;
  261. align-items: center;
  262. padding: 30rpx;
  263. border-bottom: 1rpx solid #f0f0f0;
  264. .title-bar {
  265. width: 6rpx;
  266. height: 30rpx;
  267. background-color: #D03F25;
  268. margin-right: 15rpx;
  269. border-radius: 3rpx;
  270. }
  271. text {
  272. font-size: 32rpx;
  273. font-weight: 500;
  274. color: #333;
  275. }
  276. }
  277. .product-card {
  278. display: flex;
  279. padding: 30rpx;
  280. .product-image {
  281. width: 180rpx;
  282. height: 180rpx;
  283. margin-right: 30rpx;
  284. background-color: #f9f9f9;
  285. border-radius: 8rpx;
  286. }
  287. .product-details {
  288. flex: 1;
  289. display: flex;
  290. flex-direction: column;
  291. justify-content: space-between;
  292. .product-name {
  293. font-size: 32rpx;
  294. font-weight: 500;
  295. color: #333;
  296. margin-bottom: 15rpx;
  297. }
  298. .product-tags {
  299. display: flex;
  300. margin-bottom: 20rpx;
  301. .tag {
  302. font-size: 24rpx;
  303. color: #D03F25;
  304. padding: 4rpx 12rpx;
  305. background-color: rgba(208, 63, 37, 0.1);
  306. border-radius: 4rpx;
  307. margin-right: 15rpx;
  308. }
  309. }
  310. .product-price {
  311. font-size: 28rpx;
  312. color: #666;
  313. .price-value {
  314. font-size: 40rpx;
  315. font-weight: 600;
  316. color: #D03F25;
  317. }
  318. }
  319. }
  320. }
  321. .address-section {
  322. .has-address {
  323. position: relative;
  324. .address-info {
  325. padding: 30rpx;
  326. .address-user {
  327. margin-bottom: 15rpx;
  328. .name {
  329. font-size: 32rpx;
  330. font-weight: 500;
  331. color: #333;
  332. margin-right: 30rpx;
  333. }
  334. .phone {
  335. font-size: 28rpx;
  336. color: #666;
  337. }
  338. }
  339. .address-detail {
  340. font-size: 28rpx;
  341. color: #666;
  342. line-height: 1.5;
  343. margin-bottom: 20rpx;
  344. }
  345. .address-action {
  346. display: flex;
  347. justify-content: space-between;
  348. align-items: center;
  349. padding-top: 20rpx;
  350. border-top: 1px solid #eee;
  351. .address-tip {
  352. font-size: 28rpx;
  353. color: #D03F25;
  354. }
  355. .arrow-right {
  356. width: 16rpx;
  357. height: 16rpx;
  358. border-top: 2rpx solid #D03F25;
  359. border-right: 2rpx solid #D03F25;
  360. transform: rotate(45deg);
  361. }
  362. }
  363. }
  364. .address-tag {
  365. position: absolute;
  366. top: 20rpx;
  367. right: 0;
  368. background-color: #D03F25;
  369. color: #fff;
  370. font-size: 24rpx;
  371. padding: 8rpx 20rpx;
  372. border-radius: 30rpx 0 0 30rpx;
  373. }
  374. }
  375. .no-address {
  376. padding: 20rpx;
  377. }
  378. }
  379. .order-submit {
  380. position: fixed;
  381. bottom: 0;
  382. left: 0;
  383. right: 0;
  384. padding: 20rpx 30rpx;
  385. background-color: #fff;
  386. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  387. .submit-btn {
  388. width: 100%;
  389. height: 90rpx;
  390. line-height: 90rpx;
  391. text-align: center;
  392. background-color: #D03F25;
  393. color: #fff;
  394. font-size: 32rpx;
  395. border-radius: 45rpx;
  396. border: none;
  397. }
  398. }
  399. .add-btn {
  400. padding: 30rpx;
  401. .button-submit {
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. width: 100%;
  406. height: 90rpx;
  407. background-color: #D03F25;
  408. color: #fff;
  409. font-size: 32rpx;
  410. border-radius: 45rpx;
  411. }
  412. }
  413. }
  414. </style>