17 changed files with 1422 additions and 726 deletions
@ -0,0 +1,317 @@ |
|||
<template> |
|||
<el-dialog |
|||
:title="`${type === 'add' ? '新增' : '编辑'}收货地址`" |
|||
:visible="visible" |
|||
width="700px" |
|||
@close="handleClose" |
|||
> |
|||
<el-form |
|||
:model="form" |
|||
:rules="rules" |
|||
ref="addressForm" |
|||
label-width="100px" |
|||
size="small" |
|||
> |
|||
<el-form-item label="收货人" prop="username"> |
|||
<el-input |
|||
v-model="form.username" |
|||
placeholder="请填写收货人姓名,限制10个字符" |
|||
maxlength="10" |
|||
></el-input> |
|||
</el-form-item> |
|||
<el-form-item label="所在地区" prop="region"> |
|||
<div class="region-selector"> |
|||
<el-select |
|||
v-model="form.province_id" |
|||
placeholder="选择省份/直辖市" |
|||
@change="handleProvinceChange" |
|||
class="region-select" |
|||
> |
|||
<el-option |
|||
v-for="province in provinces" |
|||
:key="province.id" |
|||
:label="province.name" |
|||
:value="province.id" |
|||
></el-option> |
|||
</el-select> |
|||
|
|||
<el-select |
|||
v-model="form.city_id" |
|||
placeholder="选择城市" |
|||
@change="handleCityChange" |
|||
class="region-select" |
|||
:disabled="!form.province_id" |
|||
> |
|||
<el-option |
|||
v-for="city in cities" |
|||
:key="city.id" |
|||
:label="city.name" |
|||
:value="city.id" |
|||
></el-option> |
|||
</el-select> |
|||
|
|||
<el-select |
|||
v-model="form.district_id" |
|||
placeholder="选择县区" |
|||
class="region-select" |
|||
:disabled="!form.city_id" |
|||
> |
|||
<el-option |
|||
v-for="district in districts" |
|||
:key="district.id" |
|||
:label="district.name" |
|||
:value="district.id" |
|||
></el-option> |
|||
</el-select> |
|||
</div> |
|||
</el-form-item> |
|||
<el-form-item label="详细地址" prop="detail_addr"> |
|||
<el-input |
|||
v-model="form.detail_addr" |
|||
placeholder="请填写详细地址,限制500个字符" |
|||
maxlength="500" |
|||
></el-input> |
|||
</el-form-item> |
|||
<el-form-item label="手机号码" prop="mobile"> |
|||
<el-input |
|||
v-model="form.mobile" |
|||
placeholder="请填写手机号码,限制11位数字" |
|||
maxlength="11" |
|||
oninput="value=value.replace(/[^\d]/g,'')" |
|||
></el-input> |
|||
</el-form-item> |
|||
</el-form> |
|||
<div slot="footer" class="dialog-footer"> |
|||
<el-button size="small" @click="handleClose">取消</el-button> |
|||
<el-button size="small" type="primary" @click="saveAddress" |
|||
>保存</el-button |
|||
> |
|||
</div> |
|||
</el-dialog> |
|||
</template> |
|||
|
|||
<script> |
|||
export default { |
|||
name: "AddressFormDialog", |
|||
props: { |
|||
visible: { |
|||
type: Boolean, |
|||
default: false, |
|||
}, |
|||
type: { |
|||
type: String, |
|||
required: true, |
|||
validator: (val) => ["add", "edit"].includes(val), |
|||
}, |
|||
initialData: { |
|||
type: Object, |
|||
default: () => ({}), |
|||
}, |
|||
}, |
|||
data() { |
|||
const validateMobile = (rule, value, callback) => { |
|||
if (!value) { |
|||
return callback(new Error("请输入手机号码")); |
|||
} else if (!/^1[3-9]\d{9}$/.test(value)) { |
|||
return callback(new Error("请输入正确的11位手机号码")); |
|||
} else { |
|||
callback(); |
|||
} |
|||
}; |
|||
|
|||
const validateRegion = (rule, value, callback) => { |
|||
if ( |
|||
!this.form.province_id || |
|||
!this.form.city_id || |
|||
!this.form.district_id |
|||
) { |
|||
return callback(new Error("请完整选择省市区")); |
|||
} |
|||
callback(); |
|||
}; |
|||
|
|||
return { |
|||
form: { |
|||
id: null, |
|||
username: "", |
|||
detail_addr: "", |
|||
mobile: "", |
|||
province_id: null, |
|||
city_id: null, |
|||
district_id: null, |
|||
region: "", |
|||
}, |
|||
rules: { |
|||
username: [ |
|||
{ required: true, message: "请输入收货人姓名", trigger: "blur" }, |
|||
{ max: 10, message: "姓名不能超过10个字符", trigger: "blur" }, |
|||
], |
|||
region: [ |
|||
{ required: true, validator: validateRegion, trigger: "change" }, |
|||
], |
|||
detail_addr: [ |
|||
{ required: true, message: "请输入详细地址", trigger: "blur" }, |
|||
{ max: 500, message: "详细地址不能超过500个字符", trigger: "blur" }, |
|||
], |
|||
mobile: [ |
|||
{ required: true, validator: validateMobile, trigger: "blur" }, |
|||
], |
|||
}, |
|||
regionData: [] |
|||
}; |
|||
}, |
|||
computed: { |
|||
provinces() { |
|||
return this.regionData.filter((item) => item.level === 1); |
|||
}, |
|||
cities() { |
|||
if (!this.form.province_id) return []; |
|||
return this.regionData.filter( |
|||
(item) => item.level === 2 && item.pid === this.form.province_id |
|||
); |
|||
}, |
|||
districts() { |
|||
if (!this.form.city_id) return []; |
|||
return this.regionData.filter( |
|||
(item) => item.level === 3 && item.pid === this.form.city_id |
|||
); |
|||
}, |
|||
}, |
|||
watch: { |
|||
visible(val) { |
|||
if (val) { |
|||
this.initForm(); |
|||
} |
|||
}, |
|||
initialData: { |
|||
handler() { |
|||
if (this.visible) { |
|||
this.initForm(); |
|||
} |
|||
}, |
|||
deep: true, |
|||
}, |
|||
}, |
|||
mounted() { |
|||
this.getRegionData(); |
|||
}, |
|||
methods: { |
|||
// 获取地区数据 |
|||
async getRegionData() { |
|||
try { |
|||
const res = await this.post({}, "/api/uservice/user/getAreas"); |
|||
if (res && res.data) { |
|||
this.regionData = res.data; |
|||
} else { |
|||
this.$message.error("获取地区数据失败"); |
|||
} |
|||
} catch (error) { |
|||
console.error("获取地区数据出错:", error); |
|||
this.$message.error("获取地区数据时发生错误"); |
|||
} |
|||
}, |
|||
initForm() { |
|||
this.$nextTick(() => { |
|||
if (this.$refs.addressForm) { |
|||
this.$refs.addressForm.resetFields(); |
|||
} |
|||
|
|||
this.form = { |
|||
id: this.type === "edit" ? this.initialData.id : null, |
|||
username: this.initialData.username || "", |
|||
detail_addr: this.initialData.detail_addr || "", |
|||
mobile: this.initialData.mobile || "", |
|||
province_id: this.initialData.province_id || null, |
|||
city_id: this.initialData.city_id || null, |
|||
district_id: this.initialData.district_id || null, |
|||
region: this.initialData.region || "", |
|||
}; |
|||
|
|||
// 如果没有ID信息再尝试通过文本匹配 |
|||
if ( |
|||
this.type === "edit" && |
|||
this.initialData.region && |
|||
!this.form.province_id |
|||
) { |
|||
this.echoRegionSelection(this.initialData.region); |
|||
} |
|||
}); |
|||
}, |
|||
echoRegionSelection(regionText) { |
|||
const regions = regionText.split(" "); |
|||
if (regions.length < 3) return; |
|||
|
|||
const province = this.provinces.find((p) => p.name === regions[0]); |
|||
if (province) { |
|||
this.form.province_id = province.id; |
|||
|
|||
this.$nextTick(() => { |
|||
const city = this.cities.find((c) => c.name === regions[1]); |
|||
if (city) { |
|||
this.form.city_id = city.id; |
|||
|
|||
this.$nextTick(() => { |
|||
const district = this.districts.find( |
|||
(d) => d.name === regions[2] |
|||
); |
|||
if (district) { |
|||
this.form.district_id = district.id; |
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
} |
|||
}, |
|||
handleProvinceChange() { |
|||
this.form.city_id = null; |
|||
this.form.district_id = null; |
|||
}, |
|||
handleCityChange() { |
|||
this.form.district_id = null; |
|||
}, |
|||
saveAddress() { |
|||
this.$refs.addressForm.validate(async (valid) => { |
|||
if (!valid) return; |
|||
|
|||
const province = this.provinces.find( |
|||
(p) => p.id === this.form.province_id |
|||
); |
|||
const city = this.cities.find((c) => c.id === this.form.city_id); |
|||
const district = this.districts.find( |
|||
(d) => d.id === this.form.district_id |
|||
); |
|||
|
|||
if (!(province && city && district)) { |
|||
this.$message.warning("地区信息不完整"); |
|||
return; |
|||
} |
|||
|
|||
const formData = { |
|||
...this.form, |
|||
region: `${province.name} ${city.name} ${district.name}`, |
|||
}; |
|||
|
|||
this.$emit("save", formData); |
|||
this.handleClose(); |
|||
}); |
|||
}, |
|||
handleClose() { |
|||
this.$emit("update:visible", false); |
|||
}, |
|||
}, |
|||
}; |
|||
</script> |
|||
|
|||
<style lang="scss" scoped> |
|||
.region-selector { |
|||
display: flex; |
|||
gap: 10px; |
|||
flex-wrap: wrap; |
|||
} |
|||
|
|||
.region-select { |
|||
min-width: 180px; |
|||
flex: 1; |
|||
max-width: 30%; |
|||
} |
|||
</style> |
@ -0,0 +1,213 @@ |
|||
<template> |
|||
<!-- 加入购物车弹框 --> |
|||
<el-dialog |
|||
title="加入购物车" |
|||
:visible.sync="dialogVisible" |
|||
width="360px" |
|||
:close-on-click-modal="false" |
|||
:show-close="false" |
|||
> |
|||
<!-- 商品信息 --> |
|||
<div class="cart-dialog__product"> |
|||
<img v-lazy="product.headimg" alt="商品图片" class="product-img" /> |
|||
<div class="product-info"> |
|||
<h4 class="product-name">{{ product.sku_name || "商品名称" }}</h4> |
|||
<p class="product-price">¥{{ product.price / 100 }}</p> |
|||
</div> |
|||
</div> |
|||
|
|||
<!-- 数量选择器 --> |
|||
<div class="cart-dialog__quantity"> |
|||
<label>购买数量:</label> |
|||
<el-input-number |
|||
v-model="buyQuantity" |
|||
:min="1" |
|||
:max="product.stock" |
|||
:step="1" |
|||
class="quantity-input" |
|||
@change="handleQuantityChange" |
|||
></el-input-number> |
|||
<span class="stock-tip" v-if="product.stock"> |
|||
库存仅剩 {{ product.stock }} 件 |
|||
</span> |
|||
</div> |
|||
|
|||
<!-- 底部按钮区 --> |
|||
<span slot="footer" class="dialog-footer"> |
|||
<el-button @click="dialogVisible = false" class="cancel-btn"> |
|||
取消 |
|||
</el-button> |
|||
<el-button |
|||
type="primary" |
|||
@click="handleAddToCart" |
|||
:loading="loading" |
|||
class="confirm-btn" |
|||
> |
|||
加入购物车 |
|||
</el-button> |
|||
</span> |
|||
</el-dialog> |
|||
</template> |
|||
|
|||
<script> |
|||
import { mapActions } from "vuex"; |
|||
|
|||
export default { |
|||
name: "AddToCartDialog", |
|||
props: { |
|||
// 接收父组件传入的商品数据(必填) |
|||
product: { |
|||
type: Object, |
|||
required: true, |
|||
default: () => ({ |
|||
id: "", |
|||
name: "", |
|||
price: 0, |
|||
avatar: "https://picsum.photos/100", // 默认占位图 |
|||
stock: 99, // 默认库存 |
|||
}), |
|||
}, |
|||
// 控制弹框显示/隐藏 |
|||
visible: { |
|||
type: Boolean, |
|||
default: false, |
|||
}, |
|||
buyQuantity: { |
|||
type: Number, |
|||
default: false, |
|||
}, |
|||
}, |
|||
data() { |
|||
return { |
|||
loading: false, // 按钮加载状态 |
|||
}; |
|||
}, |
|||
computed: { |
|||
// 双向绑定弹框显示状态 |
|||
dialogVisible: { |
|||
get() { |
|||
return this.visible; |
|||
}, |
|||
set(val) { |
|||
this.$emit("update:visible", val); |
|||
}, |
|||
}, |
|||
}, |
|||
methods: { |
|||
// 映射 Vuex 的加入购物车 action |
|||
...mapActions(["addToCart"]), |
|||
|
|||
// 数量变更时校验 |
|||
handleQuantityChange(val) { |
|||
if (val > this.product.stock) { |
|||
this.buyQuantity = this.product.stock; |
|||
this.$message.warning(`库存不足,最多可购买 ${this.product.stock} 件`); |
|||
} |
|||
}, |
|||
|
|||
// 加入购物车逻辑 |
|||
async handleAddToCart() { |
|||
this.loading = true; |
|||
try { |
|||
// 调用 Vuex action 加入购物车(携带数量) |
|||
await this.addToCart({ |
|||
...this.product, |
|||
quantity: this.buyQuantity, |
|||
}); |
|||
|
|||
// 关闭弹框 |
|||
this.dialogVisible = false; |
|||
|
|||
// 重置状态(下次打开时恢复默认) |
|||
this.buyQuantity = 1; |
|||
} catch (err) { |
|||
this.$message.error(err.message || "加入购物车失败,请重试"); |
|||
} finally { |
|||
this.loading = false; |
|||
} |
|||
}, |
|||
}, |
|||
}; |
|||
</script> |
|||
|
|||
<style lang="scss" scoped> |
|||
.cart-dialog__product { |
|||
display: flex; |
|||
align-items: center; |
|||
margin-bottom: 20px; |
|||
padding-bottom: 15px; |
|||
border-bottom: 1px solid #f5f5f5; |
|||
|
|||
.product-img { |
|||
width: 80px; |
|||
height: 80px; |
|||
object-fit: cover; |
|||
border-radius: 4px; |
|||
margin-right: 15px; |
|||
} |
|||
|
|||
.product-info { |
|||
flex: 1; |
|||
|
|||
.product-name { |
|||
font-size: 16px; |
|||
color: #333; |
|||
margin: 0 0 8px; |
|||
white-space: nowrap; |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
} |
|||
|
|||
.product-price { |
|||
font-size: 18px; |
|||
color: #ff4400; |
|||
margin: 0; |
|||
font-weight: 500; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.cart-dialog__quantity { |
|||
display: flex; |
|||
align-items: center; |
|||
margin-bottom: 10px; |
|||
|
|||
label { |
|||
font-size: 14px; |
|||
color: #666; |
|||
margin-right: 15px; |
|||
} |
|||
|
|||
.quantity-input { |
|||
width: 120px; |
|||
} |
|||
|
|||
.stock-tip { |
|||
font-size: 12px; |
|||
color: #999; |
|||
margin-left: 15px; |
|||
} |
|||
} |
|||
|
|||
.dialog-footer { |
|||
display: flex; |
|||
justify-content: center; |
|||
padding-top: 10px; |
|||
|
|||
.cancel-btn { |
|||
margin-right: 10px; |
|||
border-color: #e5e5e5; |
|||
color: #666; |
|||
} |
|||
|
|||
.confirm-btn { |
|||
background-color: #6a8a27; |
|||
border-color: #6a8a27; |
|||
|
|||
&:hover { |
|||
background-color: #5a7a1f; |
|||
border-color: #5a7a1f; |
|||
} |
|||
} |
|||
} |
|||
</style> |
Loading…
Reference in new issue