422 lines
12 KiB
Vue
422 lines
12 KiB
Vue
<template>
|
|
<u-popup :show="show" @close="close" mode="center" round="16" closeable>
|
|
<view class="dialog-container">
|
|
<view class="dialog-title">填写收货地址</view>
|
|
|
|
<u-form :model="form" ref="uForm" labelPosition="left" labelWidth="100">
|
|
<u-form-item label="姓名:">
|
|
<u-input v-model="userAddress.realName" placeholder="请输入姓名" required border="none"/>
|
|
</u-form-item>
|
|
|
|
<u-form-item label="联系电话:" >
|
|
<u-input maxlength="11" v-model="userAddress.phone" placeholder="请输入联系电话" required border="none"/>
|
|
</u-form-item>
|
|
|
|
<u-form-item label="所在地区:" >
|
|
<CitySelect
|
|
ref="cityselect"
|
|
:defaultValue="addressText"
|
|
@callback="result"
|
|
:items="district"
|
|
></CitySelect>
|
|
</u-form-item>
|
|
|
|
<u-form-item label="详细地址:">
|
|
<u-input v-model="userAddress.detail" placeholder="请输入详细地址" required border="none" />
|
|
</u-form-item>
|
|
</u-form>
|
|
<view class="address-analysis-wrap">
|
|
<view class="inp">
|
|
<textarea
|
|
v-model.trim="addressInput"
|
|
placeholder="请粘贴收件人手机号、姓名、收货地址并用空格区分,系统将自动识别地址信息"
|
|
placeholder-style="font-size:24rpx;color:#C2C5CC;"
|
|
class="inp-textarea"
|
|
/>
|
|
</view>
|
|
<view class="btn-wrap">
|
|
<view
|
|
class="txt"
|
|
@click="clearHandle"
|
|
>
|
|
清空
|
|
</view>
|
|
<view
|
|
:class="addressInput ? '' : 'disable'"
|
|
class="btn"
|
|
@click="distinguishHandle"
|
|
>
|
|
识别
|
|
</view>
|
|
</view>
|
|
</view>
|
|
<view class="dialog-footer">
|
|
<u-button type="primary" color="#C52733" shape="circle" @click="handleSave">立即保存</u-button>
|
|
</view>
|
|
</view>
|
|
</u-popup>
|
|
</template>
|
|
|
|
<script>
|
|
import CitySelect from '@/components/CitySelect'
|
|
import config from '@/utils/mapConfig'
|
|
import attrs, { chs_extphone, required } from '@/utils/validate'
|
|
import { validatorDefaultCatch } from '@/utils/dialog'
|
|
import { getAddress, getCity, postAddress, analysisAddress } from '@/api/user'
|
|
import { isWeixin } from '@/utils'
|
|
export default {
|
|
components: {
|
|
CitySelect
|
|
},
|
|
data() {
|
|
return {
|
|
district: [],
|
|
id: null,
|
|
// 是否选择地址模式:0-不是,1-来源订单创建页面,2-来源购物车
|
|
choosMode: 0,
|
|
userAddress: {
|
|
realName: '',
|
|
phone: '',
|
|
detail: '',
|
|
isDefault: 0
|
|
},
|
|
address: {},
|
|
isWechat: isWeixin(),
|
|
addressText: "",
|
|
pageKeyId: Object.freeze('userAddressEdit'),
|
|
addressInput: '',
|
|
show: false,
|
|
showRegionPicker: false,
|
|
form: {
|
|
name: '',
|
|
phone: '',
|
|
region: '',
|
|
address: ''
|
|
},
|
|
rules: {
|
|
name: [
|
|
{ required: true, message: '请输入姓名', trigger: 'blur' }
|
|
],
|
|
phone: [
|
|
{ required: true, message: '请输入联系电话', trigger: 'blur' },
|
|
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
|
|
],
|
|
region: [
|
|
{ required: true, message: '请选择所在地区', trigger: 'change' }
|
|
],
|
|
address: [
|
|
{ required: true, message: '请输入详细地址', trigger: 'blur' }
|
|
]
|
|
}
|
|
}
|
|
},
|
|
onReady() {
|
|
this.$refs.uForm.setRules(this.rules);
|
|
},
|
|
methods: {
|
|
clearHandle() {
|
|
if (!this.addressInput) return
|
|
this.addressInput = ''
|
|
},
|
|
// 识别
|
|
distinguishHandle() {
|
|
if (!this.addressInput) return
|
|
uni.showLoading()
|
|
analysisAddress({
|
|
content: this.addressInput
|
|
}).then((res) => {
|
|
uni.hideLoading()
|
|
const { success, data } = res
|
|
if (success) {
|
|
this.address = {
|
|
province: data.province || '',
|
|
city: data.city || '',
|
|
district: data.district || '',
|
|
city_id: data.cityId
|
|
}
|
|
this.userAddress.realName = data.realName || ''
|
|
this.userAddress.phone = data.phone || ''
|
|
this.userAddress.detail = data.detail || ''
|
|
this.addressText = `${this.address.province} ${this.address.city} ${this.address.district}`
|
|
}
|
|
}).catch((err) => {
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
title: err.msg + '',
|
|
icon: 'none',
|
|
mask: false,
|
|
duration: 1500
|
|
})
|
|
})
|
|
},
|
|
open() {
|
|
this.show = true
|
|
this.getCityList()
|
|
},
|
|
close() {
|
|
this.show = false
|
|
},
|
|
result(values) {
|
|
this.address = {
|
|
province: values.province.name || "",
|
|
city: values.city.name || "",
|
|
district: values.district.name || "",
|
|
city_id: values.city.id
|
|
};
|
|
this.addressText = `${this.address.province} ${this.address.city} ${this.address.district}`;
|
|
// this.addressText =
|
|
// this.address.province + this.address.city + this.address.district;
|
|
},
|
|
handleRegionConfirm(e) {
|
|
this.form.region = e.province.label + ' ' + e.city.label + ' ' + e.area.label
|
|
},
|
|
async handleSave() {
|
|
const name = this.userAddress.realName || ''
|
|
const phone = this.userAddress.phone || ''
|
|
const addressText = this.addressText || ''
|
|
const detail = this.userAddress.detail || ''
|
|
const isDefault = this.userAddress.isDefault
|
|
if (phone.length !== 11) {
|
|
uni.showToast({
|
|
title: "请输入11位手机号码",
|
|
icon: "none"
|
|
})
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await this.$validator({
|
|
name: [
|
|
required(required.message("姓名")),
|
|
attrs.range([2, 16], attrs.range.message("姓名"))
|
|
],
|
|
phone: [
|
|
required(required.message("联系电话")),
|
|
chs_extphone(chs_extphone.message())
|
|
//chs_phone(chs_phone.message())
|
|
],
|
|
addressText: [required("请选择地址")],
|
|
detail: [required(required.message("具体地址"))]
|
|
}).validate({name, phone, addressText, detail});
|
|
} catch (e) {
|
|
return validatorDefaultCatch(e);
|
|
}
|
|
try {
|
|
let that = this,
|
|
data = {
|
|
id: that.id,
|
|
real_name: name,
|
|
phone: phone,
|
|
address: this.address,
|
|
detail: detail,
|
|
is_default: isDefault ? true : false,
|
|
post_code: ""
|
|
};
|
|
postAddress(data).then((res) => {
|
|
uni.showToast({
|
|
title: res.msg,
|
|
icon: "none",
|
|
duration: 2000
|
|
});
|
|
this.$emit('save', res.data.id)
|
|
});
|
|
} catch (err) {
|
|
uni.showToast({
|
|
title: err.msg || err.response.data.msg || err.response.data.message,
|
|
icon: "none",
|
|
duration: 2000
|
|
});
|
|
}
|
|
},
|
|
//获取当前定位地址
|
|
getCurAddress() {
|
|
var that = this;
|
|
//定位
|
|
uni.showLoading({
|
|
title: '定位中...'
|
|
});
|
|
//this.getLocation();
|
|
uni.getLocation({
|
|
type: 'wgs84',
|
|
success: function (res) {
|
|
let latitude = res.latitude;
|
|
let longitude = res.longitude;
|
|
|
|
console.log('latitude:' + latitude + 'longitude:' + longitude)
|
|
|
|
// #ifdef H5
|
|
Vue.jsonp(
|
|
'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + config.key,
|
|
{
|
|
//callbackName: 'QQmap',
|
|
output: 'jsonp',
|
|
}).then(json => {
|
|
// Success.
|
|
uni.hideLoading();
|
|
|
|
that.addressText =
|
|
json.result.ad_info.province + " " + json.result.ad_info.city + " " + json.result.ad_info.district;
|
|
that.address.province = json.result.ad_info.province;
|
|
that.address.city = json.result.ad_info.city;
|
|
that.address.district = json.result.ad_info.district;
|
|
that.$set(that.userAddress, 'detail', json.result.formatted_addresses.recommend);
|
|
//that.userAddress.detail = json.result.formatted_addresses.recommend;
|
|
// that.$refs.cityselect.adjustDefaultValue();
|
|
|
|
//设置city_id
|
|
var pIdx = that.getIndex(that.address.province, that.district);
|
|
if (pIdx > -1) {
|
|
var citys = that.district[pIdx].c;
|
|
var cIdx = that.getIndex(that.address.city, citys);
|
|
if (cIdx > -1) {
|
|
var city = citys[cIdx];
|
|
that.address.city_id = city.v;
|
|
console.log('city_id:' + city.v);
|
|
}
|
|
}
|
|
|
|
|
|
}).catch(err => {
|
|
// Failed.
|
|
console.log('地址解析失败:' + JSON.stringify(err));
|
|
uni.hideLoading();
|
|
})
|
|
// #endif
|
|
|
|
// #ifndef H5
|
|
uni.request({
|
|
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + config.key,
|
|
success: function (res) {
|
|
// console.log('res:'+JSON.stringify(res.data));
|
|
uni.hideLoading();
|
|
that.addressText =
|
|
res.data.result.ad_info.province + " " + res.data.result.ad_info.city + " " + res.data.result.ad_info.district;
|
|
that.address.province = res.data.result.ad_info.province;
|
|
that.address.city = res.data.result.ad_info.city;
|
|
that.address.district = res.data.result.ad_info.district;
|
|
//that.userAddress.detail = res.data.result.formatted_addresses.recommend;
|
|
that.$set(that.userAddress, 'detail', res.data.result.formatted_addresses.recommend);
|
|
// that.$refs.cityselect.adjustDefaultValue();
|
|
//设置city_id
|
|
var pIdx = that.getIndex(that.address.province, that.district);
|
|
if (pIdx > -1) {
|
|
var citys = that.district[pIdx].c;
|
|
var cIdx = that.getIndex(that.address.city, citys);
|
|
if (cIdx > -1) {
|
|
var city = citys[cIdx];
|
|
that.address.city_id = city.v;
|
|
console.log('city_id:' + city.v);
|
|
}
|
|
}
|
|
|
|
|
|
},
|
|
fail: function (res) {
|
|
console.log('地址解析失败');
|
|
uni.hideLoading();
|
|
},
|
|
complete: function () {
|
|
|
|
}
|
|
});
|
|
// #endif
|
|
},
|
|
fail: function (res) {
|
|
uni.hideLoading();
|
|
|
|
// uni.showToast({
|
|
// title:'城市定位失败:'+JSON.stringify(res),
|
|
// icon:'none',
|
|
// duration:2000
|
|
// })
|
|
}
|
|
});
|
|
|
|
},
|
|
getCityList: function () {
|
|
let that = this;
|
|
getCity()
|
|
.then(res => {
|
|
that.district = res.data;
|
|
that.ready = true;
|
|
|
|
if (this.id) {
|
|
//设置city_id
|
|
var pIdx = that.getIndex(that.address.province, that.district);
|
|
if (pIdx > -1) {
|
|
var citys = that.district[pIdx].c;
|
|
var cIdx = that.getIndex(that.address.city, citys);
|
|
if (cIdx > -1) {
|
|
var city = citys[cIdx];
|
|
that.address.city_id = city.v;
|
|
console.log('city_id:' + city.v);
|
|
}
|
|
}
|
|
}
|
|
|
|
})
|
|
.catch(err => {
|
|
that.$dialog.error(err.msg);
|
|
});
|
|
},
|
|
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.address {
|
|
text {
|
|
width: 100%;
|
|
display: block;
|
|
}
|
|
}
|
|
.address-analysis-wrap {
|
|
padding: 20rpx;
|
|
margin: 20rpx;
|
|
border-radius: 15rpx;
|
|
background-color: #fff;
|
|
font-size: 24rpx;
|
|
.inp-textarea {
|
|
width: 100%;
|
|
height: 120rpx;
|
|
}
|
|
.btn-wrap {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
align-items: center;
|
|
margin: 20rpx 0 0 0;
|
|
.txt {
|
|
color: #999;
|
|
}
|
|
.btn {
|
|
padding: 12rpx 25rpx;
|
|
margin: 0 0 0 20rpx;
|
|
border-radius: 8rpx;
|
|
color: #fff;
|
|
background-color: #FE5261;
|
|
&.disable {
|
|
opacity: 0.75;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.dialog-container {
|
|
width: 600rpx;
|
|
padding: 40rpx;
|
|
background-color: #fff;
|
|
border-radius: 16rpx;
|
|
}
|
|
|
|
.dialog-title {
|
|
font-size: 32rpx;
|
|
font-weight: bold;
|
|
text-align: center;
|
|
margin-bottom: 40rpx;
|
|
}
|
|
|
|
.dialog-footer {
|
|
margin-top: 40rpx;
|
|
}
|
|
</style>
|