1.0
This commit is contained in:
@@ -0,0 +1,374 @@
|
||||
<template>
|
||||
<view class="container">
|
||||
<!-- 页面标题 -->
|
||||
<view class="page-header">
|
||||
<text class="page-title">收货地址</text>
|
||||
<view class="add-btn" @tap="addAddress">
|
||||
<uni-icons type="plus" size="20" color="white"></uni-icons>
|
||||
<text class="add-text">新增地址</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 地址列表 -->
|
||||
<view class="address-list" v-if="!loading && addressList.length > 0">
|
||||
<view class="address-item" v-for="(address, index) in addressList" :key="address.id">
|
||||
<!-- 默认标签 -->
|
||||
<view class="default-tag" v-if="address.is_default === 1">
|
||||
<text class="default-text">默认</text>
|
||||
</view>
|
||||
|
||||
<!-- 地址信息 -->
|
||||
<view class="address-content" @tap="editAddress(address)">
|
||||
<view class="address-header">
|
||||
<text class="consignee-name">{{ address.consignee_name }}</text>
|
||||
<text class="consignee-phone">{{ address.consignee_phone }}</text>
|
||||
</view>
|
||||
<view class="address-detail">
|
||||
<text class="address-text">{{ formatAddress(address) }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<view class="address-actions">
|
||||
<view class="action-btn default-btn"
|
||||
v-if="address.is_default !== 1"
|
||||
@tap="setDefaultAddress(address.id)">
|
||||
<text class="action-text">设为默认</text>
|
||||
</view>
|
||||
<view class="action-btn edit-btn" @tap="editAddress(address)">
|
||||
<text class="action-text">编辑</text>
|
||||
</view>
|
||||
<view class="action-btn delete-btn" @tap="deleteAddress(address.id, index)">
|
||||
<text class="action-text">删除</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 空状态 -->
|
||||
<view class="empty-state" v-if="!loading && addressList.length === 0">
|
||||
<uni-icons type="location" size="100" color="#ccc"></uni-icons>
|
||||
<text class="empty-text">暂无收货地址</text>
|
||||
<button class="add-first-btn" @tap="addAddress">添加收货地址</button>
|
||||
</view>
|
||||
|
||||
<!-- 加载状态 -->
|
||||
<view class="loading-state" v-if="loading">
|
||||
<text class="loading-text">加载中...</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import request from '@/utils/request.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
addressList: [],
|
||||
loading: false
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
// 检查登录状态
|
||||
const token = uni.getStorageSync('token')
|
||||
if (!token) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
return
|
||||
}
|
||||
this.loadAddressList()
|
||||
},
|
||||
onShow() {
|
||||
// 页面显示时重新加载地址列表(用于从编辑页面返回时刷新)
|
||||
this.loadAddressList()
|
||||
},
|
||||
methods: {
|
||||
// 加载地址列表
|
||||
async loadAddressList() {
|
||||
this.loading = true
|
||||
try {
|
||||
const res = await request.get('/back/user/addresses')
|
||||
if (res.success) {
|
||||
this.addressList = res.data || []
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载地址列表失败:', error)
|
||||
uni.showToast({
|
||||
title: '加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 格式化地址
|
||||
formatAddress(address) {
|
||||
return `${address.province} ${address.city} ${address.district} ${address.detail_address}`
|
||||
},
|
||||
|
||||
// 新增地址
|
||||
addAddress() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/address/edit'
|
||||
})
|
||||
},
|
||||
|
||||
// 编辑地址
|
||||
editAddress(address) {
|
||||
uni.navigateTo({
|
||||
url: `/pages/address/edit?id=${address.id}`
|
||||
})
|
||||
},
|
||||
|
||||
// 设置默认地址
|
||||
async setDefaultAddress(addressId) {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: '设置中...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
const res = await request.put(`/back/user/addresses/${addressId}/default`)
|
||||
if (res.success) {
|
||||
uni.showToast({
|
||||
title: '设置成功',
|
||||
icon: 'success'
|
||||
})
|
||||
// 重新加载地址列表
|
||||
this.loadAddressList()
|
||||
} else {
|
||||
throw new Error(res.message || '设置失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('设置默认地址失败:', error)
|
||||
uni.showToast({
|
||||
title: '设置失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
},
|
||||
|
||||
// 删除地址
|
||||
deleteAddress(addressId, index) {
|
||||
uni.showModal({
|
||||
title: '确认删除',
|
||||
content: '确定要删除这个收货地址吗?',
|
||||
success: async (res) => {
|
||||
if (res.confirm) {
|
||||
await this.confirmDeleteAddress(addressId, index)
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 确认删除地址
|
||||
async confirmDeleteAddress(addressId, index) {
|
||||
try {
|
||||
uni.showLoading({
|
||||
title: '删除中...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
const res = await request.delete(`/back/user/addresses/${addressId}`)
|
||||
if (res.success) {
|
||||
// 从列表中移除
|
||||
this.addressList.splice(index, 1)
|
||||
uni.showToast({
|
||||
title: '删除成功',
|
||||
icon: 'success'
|
||||
})
|
||||
} else {
|
||||
throw new Error(res.message || '删除失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('删除地址失败:', error)
|
||||
uni.showToast({
|
||||
title: '删除失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.container {
|
||||
background-color: #f5f5f5;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
background: white;
|
||||
padding: 30rpx;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
border-bottom: 1rpx solid #eee;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 36rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
background: linear-gradient(135deg, #FF4444 0%, #FF6666 100%);
|
||||
padding: 15rpx 25rpx;
|
||||
border-radius: 30rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
}
|
||||
|
||||
.add-text {
|
||||
color: white;
|
||||
font-size: 26rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.address-list {
|
||||
padding: 20rpx;
|
||||
}
|
||||
|
||||
.address-item {
|
||||
background: white;
|
||||
border-radius: 20rpx;
|
||||
margin-bottom: 20rpx;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.default-tag {
|
||||
position: absolute;
|
||||
top: 20rpx;
|
||||
right: 20rpx;
|
||||
background: #FF4444;
|
||||
padding: 8rpx 16rpx;
|
||||
border-radius: 20rpx;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.default-text {
|
||||
color: white;
|
||||
font-size: 22rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.address-content {
|
||||
padding: 30rpx;
|
||||
padding-right: 100rpx;
|
||||
}
|
||||
|
||||
.address-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 15rpx;
|
||||
}
|
||||
|
||||
.consignee-name {
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.consignee-phone {
|
||||
font-size: 28rpx;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.address-detail {
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.address-text {
|
||||
font-size: 28rpx;
|
||||
color: #666;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.address-actions {
|
||||
display: flex;
|
||||
border-top: 1rpx solid #eee;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
flex: 1;
|
||||
padding: 25rpx;
|
||||
text-align: center;
|
||||
border-right: 1rpx solid #eee;
|
||||
}
|
||||
|
||||
.action-btn:last-child {
|
||||
border-right: none;
|
||||
}
|
||||
|
||||
.default-btn {
|
||||
background: #f8f8f8;
|
||||
}
|
||||
|
||||
.edit-btn {
|
||||
background: #f0f8ff;
|
||||
}
|
||||
|
||||
.delete-btn {
|
||||
background: #fff0f0;
|
||||
}
|
||||
|
||||
.action-text {
|
||||
font-size: 26rpx;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.delete-btn .action-text {
|
||||
color: #FF4444;
|
||||
}
|
||||
|
||||
.edit-btn .action-text {
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
padding: 100rpx 50rpx;
|
||||
text-align: center;
|
||||
background: white;
|
||||
margin: 20rpx;
|
||||
border-radius: 20rpx;
|
||||
}
|
||||
|
||||
.empty-text {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
margin: 30rpx 0;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.add-first-btn {
|
||||
background: linear-gradient(135deg, #FF4444 0%, #FF6666 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 30rpx;
|
||||
padding: 20rpx 40rpx;
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.loading-state {
|
||||
padding: 100rpx;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,392 @@
|
||||
<template>
|
||||
<view class="container">
|
||||
<!-- 页面标题 -->
|
||||
<view class="page-header">
|
||||
<text class="page-title">{{ isEdit ? '编辑地址' : '新增地址' }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 地址表单 -->
|
||||
<view class="form-container">
|
||||
<!-- 收件人信息 -->
|
||||
<view class="form-section">
|
||||
<view class="section-title">收件人信息</view>
|
||||
<view class="form-item">
|
||||
<text class="form-label">收件人姓名</text>
|
||||
<input class="form-input"
|
||||
type="text"
|
||||
v-model="formData.consignee_name"
|
||||
placeholder="请输入收件人姓名"
|
||||
maxlength="20">
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-label">手机号码</text>
|
||||
<input class="form-input"
|
||||
type="number"
|
||||
v-model="formData.consignee_phone"
|
||||
placeholder="请输入手机号码"
|
||||
maxlength="11">
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 收货地址 -->
|
||||
<view class="form-section">
|
||||
<view class="section-title">收货地址</view>
|
||||
<view class="form-item">
|
||||
<text class="form-label">所在地区</text>
|
||||
<view class="region-container">
|
||||
<Region
|
||||
:width="420"
|
||||
:height="92"
|
||||
:showAllDistrict="false"
|
||||
:isRevise="hasSelectedRegion"
|
||||
:previnceId="provinceId"
|
||||
:cityId="cityId"
|
||||
:countyId="countyId"
|
||||
@region="onRegionChange"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-label">详细地址</text>
|
||||
<textarea class="form-textarea"
|
||||
v-model="formData.detail_address"
|
||||
placeholder="请输入详细地址,如道路、门牌号、小区、楼栋号等"
|
||||
maxlength="200"
|
||||
auto-height>
|
||||
</textarea>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-label">邮政编码</text>
|
||||
<input class="form-input"
|
||||
type="text"
|
||||
v-model="formData.postal_code"
|
||||
placeholder="请输入邮政编码(可选)"
|
||||
maxlength="6">
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 默认地址设置 -->
|
||||
<view class="form-section">
|
||||
<view class="form-item no-border">
|
||||
<text class="form-label">设为默认地址</text>
|
||||
<switch :checked="formData.is_default === 1" @change="onDefaultChange"></switch>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 保存按钮 -->
|
||||
<view class="save-section">
|
||||
<button class="save-btn" @tap="saveAddress" :disabled="saving">
|
||||
<text v-if="saving">保存中...</text>
|
||||
<text v-else>{{ isEdit ? '保存修改' : '保存地址' }}</text>
|
||||
</button>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import request from '@/utils/request.js'
|
||||
import Region from '@/components/k-region.vue'
|
||||
export default {
|
||||
components:{
|
||||
Region
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
isEdit: false,
|
||||
addressId: null,
|
||||
formData: {
|
||||
consignee_name: '',
|
||||
consignee_phone: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: '',
|
||||
detail_address: '',
|
||||
postal_code: '',
|
||||
is_default: 0
|
||||
},
|
||||
saving: false,
|
||||
// k-region组件需要的数据
|
||||
provinceId: 11, // 默认北京
|
||||
cityId: 1101, // 默认北京市
|
||||
countyId: 110101 // 默认东城区
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
regionText() {
|
||||
if (this.formData.province && this.formData.city && this.formData.district) {
|
||||
return `${this.formData.province} ${this.formData.city} ${this.formData.district}`
|
||||
}
|
||||
return ''
|
||||
},
|
||||
hasSelectedRegion() {
|
||||
return !!(this.formData.province && this.formData.city && this.formData.district)
|
||||
}
|
||||
},
|
||||
onLoad(options) {
|
||||
// 检查登录状态
|
||||
const token = uni.getStorageSync('token')
|
||||
if (!token) {
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是编辑还是新增
|
||||
if (options.id) {
|
||||
this.isEdit = true
|
||||
this.addressId = options.id
|
||||
this.loadAddressDetail()
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 加载地址详情
|
||||
async loadAddressDetail() {
|
||||
try {
|
||||
const res = await request.get(`/back/user/addresses/${this.addressId}`)
|
||||
if (res.success) {
|
||||
this.formData = res.data
|
||||
// 转换地区名称为代码(这里需要添加映射逻辑,暂时使用默认值)
|
||||
this.updateRegionCodes()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载地址详情失败:', error)
|
||||
uni.showToast({
|
||||
title: '加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 更新地区代码(根据地区名称)
|
||||
updateRegionCodes() {
|
||||
// 这里可以添加地区名称到代码的映射逻辑
|
||||
// 暂时使用默认值,实际应用中需要完整的地区代码映射表
|
||||
if (this.formData.province && this.formData.city && this.formData.district) {
|
||||
// 如果有具体的映射需求,可以在这里添加
|
||||
// 例如:this.provinceId = getProvinceCodeByName(this.formData.province)
|
||||
}
|
||||
},
|
||||
|
||||
// k-region组件回调
|
||||
onRegionChange(regionArray) {
|
||||
console.log('选择的地区:', regionArray)
|
||||
this.formData.province = regionArray[0] || ''
|
||||
this.formData.city = regionArray[1] || ''
|
||||
this.formData.district = regionArray[2] || ''
|
||||
},
|
||||
|
||||
// 默认地址开关变化
|
||||
onDefaultChange(e) {
|
||||
this.formData.is_default = e.detail.value ? 1 : 0
|
||||
},
|
||||
|
||||
// 表单验证
|
||||
validateForm() {
|
||||
if (!this.formData.consignee_name.trim()) {
|
||||
uni.showToast({
|
||||
title: '请输入收件人姓名',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
if (!this.formData.consignee_phone.trim()) {
|
||||
uni.showToast({
|
||||
title: '请输入手机号码',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
const phoneReg = /^1[3-9]\d{9}$/
|
||||
if (!phoneReg.test(this.formData.consignee_phone)) {
|
||||
uni.showToast({
|
||||
title: '请输入正确的手机号码',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
if (!this.formData.province || !this.formData.city || !this.formData.district) {
|
||||
uni.showToast({
|
||||
title: '请选择所在地区',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
if (!this.formData.detail_address.trim()) {
|
||||
uni.showToast({
|
||||
title: '请输入详细地址',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
},
|
||||
|
||||
// 保存地址
|
||||
async saveAddress() {
|
||||
if (!this.validateForm()) {
|
||||
return
|
||||
}
|
||||
|
||||
this.saving = true
|
||||
try {
|
||||
let res
|
||||
if (this.isEdit) {
|
||||
res = await request.put(`/back/user/addresses/${this.addressId}`, this.formData)
|
||||
} else {
|
||||
res = await request.post('/back/user/addresses', this.formData)
|
||||
}
|
||||
|
||||
if (res.success) {
|
||||
uni.showToast({
|
||||
title: this.isEdit ? '修改成功' : '添加成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 延迟返回上一页
|
||||
setTimeout(() => {
|
||||
uni.navigateBack()
|
||||
}, 1500)
|
||||
} else {
|
||||
throw new Error(res.message || '保存失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存地址失败:', error)
|
||||
uni.showToast({
|
||||
title: '保存失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.saving = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.container {
|
||||
background-color: #f5f5f5;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
background: white;
|
||||
padding: 30rpx;
|
||||
text-align: center;
|
||||
border-bottom: 1rpx solid #eee;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 36rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.form-container {
|
||||
padding: 20rpx;
|
||||
}
|
||||
|
||||
.form-section {
|
||||
background: white;
|
||||
border-radius: 20rpx;
|
||||
margin-bottom: 20rpx;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
padding: 30rpx;
|
||||
font-size: 30rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
border-bottom: 1rpx solid #f5f5f5;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 30rpx;
|
||||
border-bottom: 1rpx solid #f5f5f5;
|
||||
}
|
||||
|
||||
.form-item.no-border {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
width: 200rpx;
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.form-input {
|
||||
flex: 1;
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
margin-left: 20rpx;
|
||||
}
|
||||
|
||||
.form-textarea {
|
||||
flex: 1;
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
margin-left: 20rpx;
|
||||
min-height: 100rpx;
|
||||
}
|
||||
|
||||
.region-picker {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-left: 20rpx;
|
||||
padding: 20rpx 0;
|
||||
}
|
||||
|
||||
.region-container {
|
||||
flex: 1;
|
||||
margin-left: 20rpx;
|
||||
}
|
||||
|
||||
.region-text {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.region-placeholder {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.save-section {
|
||||
padding: 40rpx 20rpx;
|
||||
}
|
||||
|
||||
.save-btn {
|
||||
width: 100%;
|
||||
height: 90rpx;
|
||||
background: linear-gradient(135deg, #FF4444 0%, #FF6666 100%);
|
||||
border: none;
|
||||
border-radius: 45rpx;
|
||||
color: white;
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.save-btn:disabled {
|
||||
background: #ccc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
</style>
|
||||
Reference in New Issue
Block a user