You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
5.5 KiB
JavaScript

const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
type: 0,
id:"",
name: "",
phone: "",
latitude: "",
longitude: "",
address: "",
door: "",
label: 1,
checkboxItems: [{
name: '设为默认地址',
checked: false,
value: ""
}]
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
// console.log(options.address);
var tempAddress = null;
if (options.address) {
wx.setNavigationBarTitle({
title: '编辑收货地址',
})
tempAddress = JSON.parse(options.address);
if (tempAddress.defaultFlag==1){
var defaultAddBtn = this.data.checkboxItems;
defaultAddBtn[0].checked=true;
this.setData({ checkboxItems: defaultAddBtn})
}
this.setData({
type: 1,
id:tempAddress.id,
name: tempAddress.receiveName,
phone: tempAddress.mobile,
address: tempAddress.receiveAddress,
door: tempAddress.receiveDoor,
label: 0,
latitude: tempAddress.latitude,
longitude: tempAddress.longitude
});
}
this.setData({color:app.globalData.color})
},
checkboxChange: function (e) {
var that = this;
let checkboxValues = null;
let checkboxItems = this.data.checkboxItems,
values = e.detail.value
for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
if (checkboxItems[i].value == values[values.length - 1]) {
checkboxItems[i].checked = true;
checkboxValues = checkboxItems[i].value;
} else {
checkboxItems[i].checked = false;
}
}
if (e.detail.value.length == 0) {
that.setData({ isDefault: false })
} else {
that.setData({ isDefault: true })
}
that.setData({
checkboxItems,
checkboxValues
})
},
/**
* 失去焦点
*/
cy_bindblur: function(e) {
console.log(e);
var index = e.currentTarget.dataset.index;
if (index == 1) {
this.data.name = e.detail.value;
}
if (index == 2) {
this.data.phone = e.detail.value;
}
if (index == 3) {
this.data.address = e.detail.value;
}
if (index == 4) {
this.data.door = e.detail.value;
}
},
/**
* 选择收货地址
*/
cy_tapSelectAddress: function(e) {
var that = this;
wx.chooseLocation({
type: "wgs84",
success: res => {
console.log(res);
if (res.errMsg == "chooseLocation:ok") {
that.setData({
latitude: res.latitude,
longitude: res.longitude,
address: res.address
});
}
},
fail: res => {
console.log(res);
},
complete: res => {
console.log(res);
}
})
},
/**
* 选择标签
*/
cy_tapLabel: function(e) {
var index = e.currentTarget.dataset.index;
this.setData({
label: index
})
},
/**
* 重置
*/
cy_onceReset: function() {
this.setData({
name: "",
phone: "",
latitude: "",
longitude: "",
address: "",
door: "",
label: "",
});
},
/**
* 保存收货地址
*/
cy_comfirmTime: function() {
var that = this;
var name = that.data.name;
if (!name) {
wx.showToast({
title: '请填写收货人',
icon: "none"
})
return;
}
var phone = that.data.phone;
if (!phone || phone.length != 11) {
var title = "请填写联系方式";
if (phone.length > 0 && phone.length != 11) {
title = "手机号输入错误";
}
wx.showToast({
title: title,
icon: "none"
})
return;
}
var address = that.data.address;
var latitude = that.data.latitude;
var longitude = that.data.longitude;
if (!address) {
wx.showToast({
title: '请选择收货地址',
icon: "none"
})
return;
}
var receiveDoor = that.data.door;
var label = that.data.label;
var method = "program.useraddress.create";
if (that.data.type == 1) {
method = "program.useraddress.update";
}
var params = {
method: method,
programId: app.globalData.programId,
openId: app.openId,
mobile: phone,
receiveName: name,
receiveMobile: phone,
receiveAddress: address,
receiveDoor: receiveDoor,
addressType: label,
latitude: latitude,
longitude: longitude
}
if (that.data.checkboxItems[0].checked){
params.defaultFlag=1
}
if(that.data.type == 1){
params.id = that.data.id;
}
var ignores = ["openId", "mobile", "receiveName", "receiveMobile", "receiveAddress", "receiveDoor", "addressType", "latitude", "longitude","defaultFlag"];
console.log(params);
app.jsapi.memberApi(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function(json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
var title = "添加成功";
if(that.data.type == 1){
title = "修改成功";
}
wx.showToast({
title: title,
icon: "none"
})
wx.navigateBack({
})
} else {
wx.showToast({
title: data.errMsg,
icon:"none"
})
}
}, function(err) {
wx.showToast({
title: '网络异常,请检查',
icon:"none",
})
});
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})