完善收货地址管理
This commit is contained in:
266
bundle/pages/address/address.vue
Normal file
266
bundle/pages/address/address.vue
Normal file
@ -0,0 +1,266 @@
|
||||
<template>
|
||||
<view class="user-address">
|
||||
<view class="no-address column-center" v-if="!hasAddress">
|
||||
<view class="sm muted">暂无添加地址,请添加~</view>
|
||||
</view>
|
||||
<view class="address-list" v-else>
|
||||
<radio-group class="radio-group" @change="radioChange">
|
||||
<view
|
||||
v-for="(item, index) in addressList"
|
||||
:key="index"
|
||||
class="item bg-white mb20"
|
||||
:data-id="item.id"
|
||||
@tap="onSelect"
|
||||
>
|
||||
<view class="address">
|
||||
<view class="consignee md bold">
|
||||
{{ item.contact }}
|
||||
<text class="phone ml10">{{ item.telephone }}</text>
|
||||
</view>
|
||||
<view class="lighter sm mt10">
|
||||
{{ item.province }} {{ item.city }} {{ item.district }}
|
||||
{{ item.address }}
|
||||
</view>
|
||||
</view>
|
||||
<view class="operation row-between">
|
||||
<view>
|
||||
<radio
|
||||
class="radio row"
|
||||
color="#FF2C3C"
|
||||
:value="item.id + ''"
|
||||
:checked="item.is_default == '1' ? true : false"
|
||||
>
|
||||
<text>设为默认</text>
|
||||
</radio>
|
||||
</view>
|
||||
<view class="row-center">
|
||||
<view class="row mr20" @click.stop="editAddress(item.id)">
|
||||
<u-icon name="edit-pen-fill" size="32"></u-icon>
|
||||
编辑
|
||||
</view>
|
||||
<view class="row ml20" :data-id="item.id" @tap.stop="showSurePop">
|
||||
<u-icon name="trash" size="32"></u-icon>
|
||||
删除
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</radio-group>
|
||||
</view>
|
||||
<u-modal
|
||||
id="delete-dialog"
|
||||
v-model="deleteSure"
|
||||
:showCancelButton="true"
|
||||
confirm-text="删除"
|
||||
confirm-color="#FF2C3C"
|
||||
:show-title="false"
|
||||
@confirm="delAddressFun"
|
||||
@cancel="hidePop"
|
||||
>
|
||||
<view class="column-center tips-dialog">
|
||||
<view style="margin-top: 30rpx">确认删除该地址吗?</view>
|
||||
</view>
|
||||
</u-modal>
|
||||
<view class="footer row-between fixed bg-white">
|
||||
<view class="btn bg-default white md row-center br60" @click="addAddress">新增收货地址</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getAddressLists, delAddress, setDefaultAddress } from '@/api/user'
|
||||
import wechath5 from '@/utils/wechath5'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
addressList: [],
|
||||
hasAddress: true,
|
||||
deleteSure: false,
|
||||
currentId: 0
|
||||
}
|
||||
},
|
||||
|
||||
props: {},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad(options) {
|
||||
this.type = options.type
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow() {
|
||||
this.getAddressListsFun()
|
||||
},
|
||||
|
||||
methods: {
|
||||
onSelect(e) {
|
||||
if (this.type) {
|
||||
let { id } = e.currentTarget.dataset
|
||||
uni.$emit('selectaddress', {
|
||||
id
|
||||
})
|
||||
uni.navigateBack()
|
||||
}
|
||||
},
|
||||
|
||||
addAddress() {
|
||||
uni.navigateTo({
|
||||
url: '/bundle/pages/address/address_edit'
|
||||
})
|
||||
},
|
||||
|
||||
editAddress(id) {
|
||||
uni.navigateTo({
|
||||
url: `/bundle/pages/address/address_edit?id=${id}`
|
||||
})
|
||||
},
|
||||
|
||||
getAddressListsFun() {
|
||||
getAddressLists().then((res) => {
|
||||
if (res.code == 1) {
|
||||
if (res.data.length) {
|
||||
this.addressList = res.data
|
||||
this.hasAddress = true
|
||||
} else {
|
||||
this.hasAddress = false
|
||||
}
|
||||
} else {
|
||||
this.hasAddress = false
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
radioChange(e) {
|
||||
let id = e.detail.value
|
||||
console.log(e)
|
||||
setDefaultAddress(id).then((res) => {
|
||||
if (res.code == 1) this.getAddressListsFun()
|
||||
})
|
||||
},
|
||||
|
||||
onLoadFun() {
|
||||
this.getAddressListsFun()
|
||||
},
|
||||
|
||||
delAddressFun(e) {
|
||||
let id = this.currentId
|
||||
delAddress(id).then((res) => {
|
||||
if (res.code == 1) {
|
||||
this.$toast({
|
||||
title: res.msg
|
||||
})
|
||||
this.deleteSure = false
|
||||
this.getAddressListsFun()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
getWxAddressFun() {
|
||||
// #ifdef H5
|
||||
wechath5.getWxAddress().then((res) => {
|
||||
uni.setStorageSync('wxAddress', JSON.stringify(res))
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: `/pages/address_edit/address_edit`
|
||||
})
|
||||
}, 200)
|
||||
})
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.authorize({
|
||||
scope: 'scope.address',
|
||||
success: function (res) {
|
||||
uni.chooseAddress({
|
||||
success: function (res) {
|
||||
uni.setStorageSync('wxAddress', JSON.stringify(res))
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({
|
||||
url: `/pages/address_edit/address_edit`
|
||||
})
|
||||
}, 200)
|
||||
},
|
||||
fail: function (res) {
|
||||
if (res.errMsg == 'chooseAddress:cancel')
|
||||
return this.$toast({
|
||||
title: '取消选择'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: function (res) {
|
||||
uni.showModal({
|
||||
title: '您已拒绝导入微信地址权限',
|
||||
content: '是否进入权限管理,调整授权?',
|
||||
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
uni.openSetting({
|
||||
success: function (res) {}
|
||||
})
|
||||
} else if (res.cancel) {
|
||||
return this.$toast({
|
||||
title: '已取消!'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
},
|
||||
|
||||
showSurePop: function (e) {
|
||||
this.deleteSure = true
|
||||
this.currentId = e.currentTarget.dataset.id
|
||||
},
|
||||
hidePop: function (e) {
|
||||
this.deleteSure = false
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss">
|
||||
.user-address {
|
||||
padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
|
||||
.no-address {
|
||||
padding-top: 300rpx;
|
||||
text-align: center;
|
||||
}
|
||||
.address-list {
|
||||
padding: 10rpx 0;
|
||||
.item {
|
||||
padding: 0 30rpx;
|
||||
.address {
|
||||
padding: 20rpx 0;
|
||||
border-bottom: $-solid-border;
|
||||
}
|
||||
.operation {
|
||||
height: 80rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
.footer {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
height: 118rpx;
|
||||
padding: 0 30rpx;
|
||||
box-sizing: content-box;
|
||||
padding-bottom: env(safe-area-inset-bottom);
|
||||
.btn {
|
||||
flex: 1;
|
||||
height: 80rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.tips-dialog {
|
||||
height: 230rpx;
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
181
bundle/pages/address/address_edit.vue
Normal file
181
bundle/pages/address/address_edit.vue
Normal file
@ -0,0 +1,181 @@
|
||||
<template>
|
||||
<view class="address u-p-l-32 u-p-r-32">
|
||||
<u-form :model="addressObj" ref="uForm">
|
||||
<u-form-item label="收货人" label-width="160">
|
||||
<u-input v-model="addressObj.contact" placeholder="请填写收货人姓名"/>
|
||||
</u-form-item>
|
||||
<u-form-item label="联系方式" label-width="160">
|
||||
<u-input v-model="addressObj.telephone" placeholder="请填写手机号码"/>
|
||||
</u-form-item>
|
||||
<u-form-item label="所在地区" label-width="160">
|
||||
<view @tap="showPopup = true" class="row-between w-full">
|
||||
<view :class="[{'placeholder-color': !addressObj.region}]">{{ addressObj.region || '请选择地区' }}</view>
|
||||
<view>
|
||||
<u-icon name="arrow-right" size="32" color="rgb(192, 196, 204)"></u-icon>
|
||||
</view>
|
||||
</view>
|
||||
</u-form-item>
|
||||
<u-form-item label="详细地址" label-width="160" >
|
||||
<u-input v-model="addressObj.address" type="textarea" placeholder="请填写小区、街道、门牌号等信息"/>
|
||||
</u-form-item>
|
||||
<u-form-item :border-bottom="false">
|
||||
<u-checkbox v-model="addressObj.is_default" shape="circle" :active-color="themeColor">
|
||||
设为默认
|
||||
</u-checkbox>
|
||||
</u-form-item>
|
||||
|
||||
<view class="u-m-t-60">
|
||||
<u-button @click="save" hover-class="none" :customStyle="{backgroundColor: themeColor, color: '#fff', border: 'none', borderRadius: '100rpx', fontSize: '24rpx'}" :hair-line="false">保存</u-button>
|
||||
</view>
|
||||
</u-form>
|
||||
|
||||
<u-select v-model="showPopup" mode="mutil-column-auto" :list="lists" @confirm="regionChange"></u-select>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { editAddress, getOneAddress, hasRegionCode, addAddress } from '@/api/user'
|
||||
import area from '@/utils/area'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
addressObj: {
|
||||
contact: '',
|
||||
telephone: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: '',
|
||||
address: '',
|
||||
region: '',
|
||||
is_default: false
|
||||
},
|
||||
defaultRegion: ['广东省', '广州市', '番禺区'],
|
||||
defaultRegionCode: '440113',
|
||||
showPopup: false,
|
||||
lists: [],
|
||||
addressId: '',
|
||||
}
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.addressId = parseInt(options.id)
|
||||
if (options.id) {
|
||||
uni.setNavigationBarTitle({
|
||||
title: '编辑地址'
|
||||
})
|
||||
this.getOneAddressFun()
|
||||
} else {
|
||||
uni.setNavigationBarTitle({
|
||||
title: '添加地址'
|
||||
})
|
||||
}
|
||||
this.$nextTick(() => {
|
||||
this.lists = area
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload() {
|
||||
uni.removeStorageSync('wxAddress')
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 地区选择
|
||||
regionChange(region) {
|
||||
this.addressObj.province_id = region[0].value
|
||||
this.addressObj.city_id = region[1].value
|
||||
this.addressObj.district_id = region[2].value
|
||||
this.addressObj.region = region[0].label + ' ' + region[1].label + ' ' + region[2].label
|
||||
},
|
||||
|
||||
// 获取地址详情
|
||||
getOneAddressFun() {
|
||||
getOneAddress(this.addressId).then((res) => {
|
||||
if (res.code == 1) {
|
||||
let { city, province, district } = res.data
|
||||
this.addressObj = res.data
|
||||
this.addressObj.region = `${province} ${city} ${district}`
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
save() {
|
||||
let {
|
||||
addressObj,
|
||||
addressId
|
||||
} = this
|
||||
|
||||
if (!addressObj.contact) {
|
||||
return this.$toast({
|
||||
title: '请填写收货人姓名'
|
||||
})
|
||||
}
|
||||
|
||||
if (!addressObj.telephone) {
|
||||
return this.$toast({
|
||||
title: '请填写手机号码'
|
||||
})
|
||||
}
|
||||
|
||||
if (!addressObj.region) {
|
||||
return this.$toast({
|
||||
title: '请选择省、市、区'
|
||||
})
|
||||
}
|
||||
|
||||
if (!addressObj.address) {
|
||||
return this.$toast({
|
||||
title: '请填写小区、街道、门牌号等信息'
|
||||
})
|
||||
}
|
||||
|
||||
delete addressObj.region
|
||||
|
||||
if (addressId) {
|
||||
editAddress(addressObj).then((res) => {
|
||||
if (res.code == 1) {
|
||||
this.$toast( {
|
||||
title: res.msg
|
||||
}, {
|
||||
tab: 3,
|
||||
url: 1
|
||||
})
|
||||
}
|
||||
}).catch((err) => {
|
||||
return this.$toast({
|
||||
title: err
|
||||
})
|
||||
})
|
||||
} else {
|
||||
addAddress(addressObj).then((res) => {
|
||||
if (res.code == 1) {
|
||||
this.$toast({
|
||||
title: res.msg
|
||||
},{
|
||||
tab: 3,
|
||||
url: 1
|
||||
})
|
||||
}
|
||||
}).catch((err) => {
|
||||
return this.$toast({
|
||||
title: err
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
page{
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.placeholder-color {
|
||||
color: rgb(192, 196, 204);
|
||||
}
|
||||
</style>
|
||||
@ -1,7 +1,8 @@
|
||||
<template>
|
||||
<view>
|
||||
<u-cell-group :border="false">
|
||||
<u-cell-item title="退出登录" :border-bottom="false" @click="logout" hover-class="none"></u-cell-item>
|
||||
<u-cell-item title="退出登录" @click="logout" hover-class="none"></u-cell-item>
|
||||
<u-cell-item title="收货地址" @click="toAddress" :border-bottom="false" hover-class="none"></u-cell-item>
|
||||
</u-cell-group>
|
||||
|
||||
<u-modal v-model="showLogout" :content="content" :show-cancel-button="true" @confirm="confirmLogout"></u-modal>
|
||||
@ -25,7 +26,6 @@
|
||||
},
|
||||
|
||||
confirmLogout() {
|
||||
console.log(this.token)
|
||||
// 退出登录
|
||||
userLogout({
|
||||
token: this.token
|
||||
@ -42,7 +42,14 @@
|
||||
}, 500)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
toAddress() {
|
||||
uni.navigateTo({
|
||||
url: '/bundle/pages/address/address'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user