cmy 4 years ago
parent
commit
f968a3ec7c
3 changed files with 141 additions and 51 deletions
  1. 8 1
      api/set.js
  2. 6 1
      pages/set/address.vue
  3. 127 49
      pages/set/addressManage.vue

+ 8 - 1
api/set.js

@@ -61,4 +61,11 @@ export function getAddressCity(data,id) {
 		method: 'get',
 		data
 	});
-}
+}
+export function getAddressDetail(data,id) {
+	return request({
+		url: '/api/address/detail/'+id,
+		method: 'get',
+		data
+	});
+}

+ 6 - 1
pages/set/address.vue

@@ -90,8 +90,13 @@ export default {
 		},
 		// 添加地址
 		addAddress(type, item) {
+			let url=`/pages/set/addressManage?type=${type}`
+			if(item&&item.id){
+				url += `&id=${item.id}`;
+			}
+			
 			uni.navigateTo({
-				url: `/pages/set/addressManage?type=${type}&data=${JSON.stringify(item)}`
+				url
 			});
 		},
 		//添加或修改成功之后回调

+ 127 - 49
pages/set/addressManage.vue

@@ -46,7 +46,7 @@ import uniList from '@/components/uni-list/uni-list.vue';
 import uniListItem from '@/components/uni-list-item/uni-list-item.vue';
 import uniPopup from '@/components/uni-popup/uni-popup.vue';
 import { addressEdit } from '@/api/user.js';
-import { getAddressArea, getAddressCity } from '@/api/set.js';
+import { getAddressArea, getAddressCity, getAddressDetail } from '@/api/set.js';
 import { mapState, mapMutations } from 'vuex';
 export default {
 	components: {
@@ -63,14 +63,12 @@ export default {
 			addressData: {
 				name: '',
 				mobile: '',
-				address: {
-					province: '',
-					city: '',
-					district: ''
-				},
 				area: '',
-				default: false
-			}
+				default: false,
+				id: ''
+			},
+			type: 'add', //判断当前加载类型
+			loadCityp: false //判断是否已经初次加载过数据
 		};
 	},
 	computed: {
@@ -79,6 +77,7 @@ export default {
 		areaJs() {
 			if (this.area.length > 0) {
 				const index1 = this.addressIndex[0];
+				console.log(this.addressIndex, this.area[index1], this.area[index1].child[this.addressIndex[1]]);
 				return [this.area, this.area[index1].child, this.area[index1].child[this.addressIndex[1]].child];
 			}
 		},
@@ -92,29 +91,21 @@ export default {
 	},
 	onLoad(option) {
 		let title = '新增收货地址';
-		if (option.type === 'edit') {
+		this.type = option.type;
+		if (this.type === 'edit') {
 			title = '编辑收货地址';
-			let data = JSON.parse(option.data);
-			console.log(data);
-			this.addressData = {
-				name: data.real_name,
-				mobile: data.phone,
-				address: {
-					province: data.province,
-					city: data.city,
-					district: data.district
-				},
-				area: data.detail,
-				default: data.is_default == 1,
-				id: data.id
-			};
-			this.addressDetail = data.province + data.city + data.district;
+			this.getAddressDetail(option.id);
+			// this.addressDetail = data.province + data.city + data.district;
+		}
+		if (this.type === 'add') {
+			console.log('');
+			this.loadCityp = true;
+			this.init();
 		}
 		this.manageType = option.type;
 		uni.setNavigationBarTitle({
 			title
 		});
-		this.init();
 	},
 	methods: {
 		...mapMutations('address', ['setArea', 'setCity']),
@@ -123,13 +114,59 @@ export default {
 			if (this.area.length <= 0) {
 				// 获取省市区信息
 				this.getAddressArea();
+			} else {
+				if (this.type == 'edit' && !this.loadCityp) {
+					this.changeArea();
+				}
 			}
 		},
+		// 获取地址详情
+		getAddressDetail(id) {
+			getAddressDetail({}, id)
+				.then(e => {
+					let address = this.addressData;
+					let data = e.data;
+					address.name = data.real_name; //保存用户姓名
+					address.mobile = data.phone; //保存用户手机号
+					address.area = data.detail; //保存用户详细地址
+					address.default = data.is_default == 1 ? true : false; //保存是否默认地址
+					address.id = data.id;
+					// 开始加载村镇信息
+					let arr = e.data.address_arr.split(',');
+					let ar1 = [];
+					let ar2 = [];
+					for (var i = 0; i < 5; i++) {
+						if (i < 3) {
+							ar1.push(arr[i]);
+						}
+						if (i >= 3) {
+							// 判断是否有值
+							if (arr[i]) {
+								ar2.push(arr[i]);
+							} else {
+								ar2.push(0);
+							}
+						}
+					}
+					this.addressIndex = ar1;
+					this.addressIndexCity = ar2;
+					// 开始初始化
+					this.init();
+				})
+				.catch(e => {
+					console.log(e);
+				});
+		},
 		// 获取省市区信息
 		getAddressArea() {
 			getAddressArea().then(e => {
 				this.setArea(e.data);
 				this.getAddressCity(e.data[0].child[0].child[0].city_id);
+				// 判断是否为修改
+				if (this.type == 'edit' && !this.loadCityp) {
+					console.log('初始化数据');
+					this.changeArea();
+				}
 			});
 		},
 		// 获取村镇信息
@@ -142,21 +179,29 @@ export default {
 				.then(e => {
 					uni.hideLoading();
 					this.setCity(e.data);
+					if (!this.loadCityp) {
+						this.loadCityp = true;
+						this.changeCity();
+					}
 				})
 				.catch(e => {
+					console.log(e);
 					uni.hideLoading();
-					uni.showToast({
-						title: ''
-					});
 				});
 		},
 		//省市区确认后村镇数据更新
-		changeArea(e) {
+		changeArea() {
 			if (this.area.length > 0) {
 				const index0 = this.addressIndex[0];
 				const index1 = this.addressIndex[1];
 				const index2 = this.addressIndex[2];
 				this.addressDetail = this.areaJs[0][index0].name + this.areaJs[1][index1].name + this.areaJs[2][index2].name;
+				// 判断是否已经加载过修改
+				if (this.loadCityp) {
+					// 初始化选中的村镇
+					this.addressIndexCity = [0, 0];
+					this.addressDetailCity = '';
+				}
 				this.getAddressCity(this.areaJs[2][index2].city_id);
 			} else {
 				this.addressDetail = '';
@@ -164,7 +209,7 @@ export default {
 		},
 		// 村镇切换
 		changeCity(e) {
-			if (this.area.length > 0) {
+			if (this.city.length > 0) {
 				const index0 = this.addressIndexCity[0];
 				const index1 = this.addressIndexCity[1];
 				this.addressDetailCity = this.cityJs[0][index0].name + this.cityJs[1][index1].name;
@@ -202,7 +247,6 @@ export default {
 		chooseLocation() {
 			uni.chooseLocation({
 				success: data => {
-					console.log(data);
 					this.addressData.addressName = data.name;
 					this.addressData.address = data.name;
 				}
@@ -224,38 +268,72 @@ export default {
 				this.$api.msg('请输入正确的手机号码');
 				return;
 			}
-			if (!data.address) {
-				this.$api.msg('请在地图选择所在位置');
-				return;
-			}
 			if (!data.area) {
 				this.$api.msg('请填写门牌号信息');
 				return;
 			}
-
+			if (!this.addressDetail) {
+				this.$api.msg('请选择省市区');
+				return;
+			}
+			// 数组
+			let address = [];
+			let arrObj = [];
+			// 保存省市区信息
+			this.areaJs.forEach((e, ind) => {
+				arrObj.push(this.addressIndex[ind]);
+				address.push({
+					city_id: e[this.addressIndex[ind]].city_id,
+					name: e[this.addressIndex[ind]].name
+				});
+			});
+			// 保存村镇信息
+			if (this.cityJs && this.cityJs.length > 0) {
+				this.cityJs.forEach((e, ind) => {
+					arrObj.push(this.addressIndexCity[ind]);
+					address.push({
+						city_id: e[this.addressIndexCity[ind]].city_id,
+						name: e[this.addressIndexCity[ind]].name
+					});
+				});
+			}
+			console.log(address);
+			uni.showLoading({
+				title: '提交中....',
+				mask: true
+			});
 			//this.$api.prePage()获取上一页实例,可直接调用上页所有数据和方法,在App.vue定义
 			addressEdit({
 				real_name: data.name,
 				phone: data.mobile,
 				address: {
-					province: data.address.province,
-					city: data.address.city,
-					district: data.address.district
+					province: address[0].name,
+					city: address[1].name,
+					district: address[2].name,
+					street: address[3] ? address[3].name : '', //街道
+					village: address[4] ? address[4].name : '', //村
+					city_id: address[address.length - 1].city_id //保存id
 				},
+				address_arr: arrObj,
 				detail: data.area,
 				is_default: data.default,
 				id: data.id || '',
-				type: 1
-			}).then(function(e) {
-				obj.$api.prePage().refreshList();
-				uni.showToast({
-					title: '提交成功',
-					duration: 2000
+				type: 0
+			})
+				.then(function(e) {
+					uni.hideLoading();
+					uni.showToast({
+						title: '提交成功',
+						duration: 2000
+					});
+					obj.$api.prePage().refreshList();
+					setTimeout(function() {
+						uni.navigateBack();
+					}, 800);
+				})
+				.catch(e => {
+					uni.hideLoading();
 				});
-				setTimeout(function() {
-					uni.navigateBack();
-				}, 800);
-			});
 		}
 	}
 };