zoujiandong 4948f35fb3 1.22
2024-01-22 08:42:14 +08:00

456 lines
9.9 KiB
JavaScript

// pages/editAddress/editAddress.js
const app = getApp();
import {editAddress,addAddress,getAddressDetail} from "../../../api/address"
import {getProvince,getCity,getCountry} from "../../../api/consultExpert"
let provinceArr=[];
let cityArr=[];
import {throttle} from "../../../utils/util"
Page({
data: {
show: false,
navName:"添加地址",
address_id:'',
isEdit:false,
consignee_name:'',
hideConsignee_name:false,
consignee_tel:'',
hideConsignee_tel:false,
consignee_zip_code:'',
order_prescription_id:'',
area:"",
is_default:0,
address:'',
hideAddress:false,
province_id:'',
province_name:'',
city_id:'',
city_name:'',
county_id:'',
county_name:'',
tag:"",
img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
columns: [
{
values:[],
className: 'column1',
},
{
values: [],
className: 'column2',
defaultIndex: 1,
},
{
values: [],
className: 'column3',
defaultIndex: 1,
}
]
},
/**
* 页面的初始数据
*/
goBack(){
wx.navigateBack({
delta: 1,
})
},
selectTag(event){
let index=event.currentTarget.dataset.tag;
if(index==this.data.tag){
this.setData({
tag:''
})
}else{
this.setData({
tag:index
})
}
},
onConfirm(event) {
const { picker, value, index } = event.detail;
let currentPosition=''
for (let index = 0; index < value.length; index++) {
currentPosition+=value[index].area_name
if(index===0){
this.setData({
province_id:value[index].area_id,
province_name:value[index].area_name
})
}else if(index==1){
this.setData({
city_id:value[index].area_id,
city_name:value[index].area_name
})
}else{
this.setData({
county_id:value[index].area_id,
county_name:value[index].area_name,
consignee_zip_code:value[index].zip
})
}
}
this.setData({
show:false,
area:currentPosition
})
},
onChangePicker(event) {
const { picker, value, index } = event.detail;
if(index===0){
let currentIndex=picker.getIndexes()[0]
this.cityList(provinceArr[currentIndex].area_id);
}else if(index===1){
let currentIndex=picker.getIndexes()[1]
this.countryList(cityArr[currentIndex].area_id);
}
},
onCancel() {
this.setData({
show:false
})
},
provinceList(){
getProvince().then((res)=>{
provinceArr= res;
let province=[];
for (let i = 0; i < res.length; i++) {
province.push(res[i]);
};
this.setData({
'columns[0].values':province,
})
this.cityList(res[0].area_id);
})
},
cityList(area_id){
getCity({
area_id:area_id
}).then((res)=>{
this.isLoding=true;
cityArr=res;
let city=[];
for (let i = 0; i < res.length; i++) {
city.push(res[i]);
};
this.setData({
'columns[1].values':city
})
this.countryList(res[0].area_id);
})
},
countryList(area_id){
getCountry({
area_id:area_id
}).then((res)=>{
let area=[];
for (let i = 0; i < res.length; i++) {
area.push(res[i]);
};
this.setData({
'columns[2].values':area
})
})
},
handleAddress:throttle(function(){
this.data.isEdit?this.handleEditAddress():this.handleAddAddress()
}),
validate(){
if (!/^[\u4E00-\u9FA5A-Za-z0-9_]{2,10}$/.test(this.data.consignee_name)) {
wx.showToast({
title: `姓名要求在2-10个字符`,
icon: 'none',
});
return false;
};
if(!/^1[3456789]\d{9}$/.test(this.data.consignee_tel)){
wx.showToast({
title: `请输入有效的手机号码!`,
icon: 'none',
});
return false;
}
if(!this.data.area){
wx.showToast({
title: `请选择地区`,
icon: 'none',
});
return false;
}
if(!this.data.address){
wx.showToast({
title: `请输入详细地址`,
icon: 'none',
});
return false;
}
return true
},
handleGetAddressDetail(id){
getAddressDetail(id).then(data=>{
for (const key in this.data){
if(data[key]!=undefined){
this.setData({
[key]:data[key]
})
}
};
this.setData({
area:data.province+data.city+data.county,
province_name:data.province,
city_name:data.city,
county_name:data.county
});
if(data.consignee_name){
this.setData({
"hideConsignee_name":true
})
};
if(data.consignee_tel){
this.setData({
"hideConsignee_tel":true
})
};
if(data.address){
this.setData({
"hideAddress":true
})
}
})
},
handleEditAddress(){
let {consignee_name,consignee_tel,consignee_zip_code,is_default,address,province_id,city_id,county_id,tag,address_id,order_prescription_id,province_name,city_name,county_name}=this.data;
let dataObj={};
if(tag){
dataObj={
consignee_name,
consignee_tel,
province_id,
city_id,
county_id,
tag,
consignee_zip_code,
is_default,
address:address
}
}else{
dataObj={
consignee_name,
consignee_tel,
province_id,
city_id,
county_id,
consignee_zip_code,
is_default,
address:address
}
};
if(this.validate()){
editAddress(address_id,dataObj).then(data=>{
wx.showToast({
title:"地址修改成功",
icon:"none"
});
if(order_prescription_id){
let pages = getCurrentPages();
let prevPage = pages[pages.length - 3]; //上上一页
prevPage.setData({
prevData:JSON.stringify({
name:consignee_name,
phone:consignee_tel,
province:province_name,
city:city_name,
country:county_name,
address,
address_id
})
});
wx.navigateBack({
delta: 2,
})
}else{
wx.navigateBack({
delta: 1,
})
}
})
}
},
inputChange(e){
let value=e.currentTarget.dataset.value;
console.log(value)
if(!e.detail.value){
this.setData({
[value]:false,
})
}else{
this.setData({
[value]:true,
})
}
this.setData({
[e.target.dataset.id]: e.detail.value
})
},
handleAddAddress(){
let {consignee_name,consignee_tel,consignee_zip_code,is_default,address,area,province_id,city_id,county_id,tag,order_prescription_id,province_name,city_name,county_name}=this.data;
let dataObj={};
if(tag){
dataObj={
consignee_name,
consignee_tel,
province_id,
city_id,
county_id,
tag,
consignee_zip_code,
is_default,
address:address
}
}else{
dataObj={
consignee_name,
consignee_tel,
province_id,
city_id,
county_id,
consignee_zip_code,
is_default,
address:address
}
};
if(this.validate()){
addAddress(dataObj).then(data=>{
wx.showToast({
title:"地址添加成功",
icon:"none"
});
if(order_prescription_id){
let pages = getCurrentPages();
let prevPage = pages[pages.length - 3]; //上上一页
prevPage.setData({
prevData:JSON.stringify({
name:consignee_name,
phone:consignee_tel,
province:province_name,
city:city_name,
country:county_name,
address,
address_id:data.address_id
})
});
wx.navigateBack({
delta: 2,
})
}else{
wx.navigateBack({
delta: 1,
})
}
})
}
},
onChange({detail}) {
// 需要手动对 checked 状态进行更新
this.setData({
is_default: detail?1:0
});
},
handleClose() {
this.setData({
show: false
})
},
showArea() {
this.setData({
show: true
})
},
handleConfirm(event) {
let arr = event.detail.values;
let add = '';
for (let i = 0; i < arr.length; i++) {
add += arr[i].name;
};
this.setData({
show: false,
area:add
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
let address_id=options.address_id;
if(options.order_prescription_id){
this.setData({
order_prescription_id:options.order_prescription_id
})
}
if(address_id){
wx.setNavigationBarTitle({
title: "编辑地址",
});
this.setData({
isEdit:true,
navName:"编辑地址",
address_id
})
this.handleGetAddressDetail(address_id)
}
this.provinceList();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.setData({
img_host:app.hostConfig().imghost
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
})