uniapp页面传递对象 地址修改,添加,修改

346 阅读1分钟

uni-app 地址增加及修改 页面对象传递

<script>
	export default {
		data() {
			return {
				user:{}
			}
		},
		onShow() {
			// 我的用户信息
			this.userInfo()
		},
		methods: {
			// 我的用户信息
			async userInfo() {
				let res = await this.$api.user()
				if (res.code == 1) {
					this.user = res.data.addresses
				}
			},
			async edit(item){
				console.log('item123:'+item)
				uni.navigateTo({
					url:'./addAdress/addAdress?editItem='+encodeURIComponent(JSON.stringify(item))
				})
			},
			async dele(id){
				let res = await this.$api.address_remove({
					"id": id //地址id
				})
				if (res.code == 1) {
					// 我的用户信息
					this.userInfo()
					this.$u.toast('修改成功')
				} else {
					this.$u.toast('修改失败')
				}
			}
		}
	}
</script>

#另一个页面接受值
<script>
	export default {
		data() {
			return {
				user: {},
				isEdit:false,
				show: false
			}
		},

		onLoad(option) {
			
			let editItem = JSON.parse(decodeURIComponent(option.editItem));
			let user = {
				"id": editItem['id'], //地址id
				"name": editItem['name'], //姓名
				"mobile": editItem['mobile'], //手机号码
				"detail": editItem['detail'], //地址详情
				"area": editItem['area'] //6位数字的区域码
				}
			this.user = user
			this.isEdit = true
		},

		methods: {
			changeAddress(e) {
				console.log('label:'+e.province.label + e.city.label + e.area.label)
				this.user.area = e.province.label + e.city.label + e.area.label
			},
			async addRes() {
				if (!this.isEdit) { //新增
					let res = await this.$api.address_add({
						"name": this.user.name, //姓名
						"mobile": this.user.mobile, //手机号码
						"detail": this.user.detail, //地址详情
						"area": this.user.area //6位数字的区域码
					})
					if (res.code == 1) {
						// 我的用户信息
						this.$u.toast('新增成功')
						uni.navigateBack({
							delta:1
						})
					} else {
						this.$u.toast('新增失败')
					}
				} else { //修改
					let res = await this.$api.address_update({
						"id": this.user.id, //地址id
						"name": this.user.name, //姓名
						"mobile": this.user.mobile, //手机号码
						"detail": this.user.detail, //地址详情
						"area": this.user.area //6位数字的区域码
					})
					if (res.code == 1) {
						// 我的用户信息
						this.$u.toast('修改成功')
						uni.navigateBack({
							delta:1
						})
					} else {
						this.$u.toast('修改失败')
					}
				}

			}
		}
	}
</script>