This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
yoshop-wechat/pages/address/detail.js
2021-07-16 16:37:55 +08:00

129 lines
2.5 KiB
JavaScript

let App = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
disabled: false,
nav_select: false, // 快捷导航
region: '',
detail: {},
error: '',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
// 获取当前地址信息
this.getAddressDetail(options.address_id);
},
/**
* 获取当前地址信息
*/
getAddressDetail: function(address_id) {
let _this = this;
App._get('address/detail', {
address_id
}, function(result) {
_this.setData(result.data);
});
},
/**
* 表单提交
*/
saveData: function(e) {
let _this = this,
values = e.detail.value
values.region = this.data.region;
// 表单验证
if (!_this.validation(values)) {
App.showError(_this.data.error);
return false;
}
// 按钮禁用
_this.setData({
disabled: true
});
// 提交到后端
values.address_id = _this.data.detail.address_id;
App._post_form('address/edit', values, function(result) {
App.showSuccess(result.msg, function() {
wx.navigateBack();
});
}, false, function() {
// 解除禁用
_this.setData({
disabled: false
});
});
},
/**
* 表单验证
*/
validation: function(values) {
if (values.name === '') {
this.data.error = '收件人不能为空';
return false;
}
if (values.phone.length < 1) {
this.data.error = '手机号不能为空';
return false;
}
// if (values.phone.length !== 11) {
// this.data.error = '手机号长度有误';
// return false;
// }
let reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/;
if (!reg.test(values.phone)) {
this.data.error = '手机号不符合要求';
return false;
}
if (!this.data.region) {
this.data.error = '省市区不能空';
return false;
}
if (values.detail === '') {
this.data.error = '详细地址不能为空';
return false;
}
return true;
},
/**
* 修改地区
*/
bindRegionChange: function(e) {
this.setData({
region: e.detail.value
})
},
/**
* 获取微信地址
*/
chooseAddress: function() {
let _this = this;
wx.chooseAddress({
success: function(res) {
_this.setData({
'detail.name': res.userName,
'detail.phone': res.telNumber,
'detail.detail': res.detailInfo,
region: [res.provinceName, res.cityName, res.countyName],
});
}
})
},
})