Component({ props: { componentData: {}, addressData: null, btnBgColor: '#1677FF', onComplete: null, maxLen: 20, isShowDefault: true, btnMarginTop: '300rpx', regionType: 'map' }, data: { addressInfo: { name: '', mobile: '', region: [], regionCode: [], address: '', isDefault: false, latitude: '', longitude: '', addressNumber: '' } // 收货地址信息 }, didMount() { if (!this.isEmptyObject(this.props.addressData)) { this.setData({ addressInfo: { ...this.props.addressData } }); } }, methods: { isEmptyObject(obj) { return this.isObject(obj) && !Object.keys(obj).length; }, isObject(val) { return Object.prototype.toString.call(val) === '[object Object]'; }, defaultChange(e) { this.setData({ 'addressInfo.isDefault': e.detail.value }); }, chooseContact() { my.choosePhoneContact({ success: res => { const mobile = res.mobile.replace(/-|\s/g, ''); this.setData({ 'addressInfo.name': res.name, 'addressInfo.mobile': mobile }); } }); }, chooseRegion() { const { regionType = '' } = this.props; // 选择区域为地图 if (regionType === 'map') { my.chooseLocation({ success: res => { const { adName = '', address = '', cityName = '', latitude = 0, longitude = 0, provinceName = '', name = '', adCode = '' } = res || {}; let resAddress = address; [provinceName, cityName, adName].forEach(v => { resAddress = resAddress.replace(v, ''); }); const tempSet = new Set([provinceName, cityName, adName]); this.setData({ 'addressInfo.region': Array.from(tempSet), 'addressInfo.address': `${resAddress}${name}`, 'addressInfo.latitude': latitude, 'addressInfo.longitude': longitude, 'addressInfo.regionCode': ['', '', adCode] }); }, fail: () => {} }); } else { // 选择区域为regionPicker this.selRegion(); } }, selRegion() { if (my.regionPicker) { my.regionPicker({ selectedItem: this.data.addressInfo.region || [], success: res => { this.setData({ 'addressInfo.region': res.data, 'addressInfo.regionCode': res.code }); } }); } else { // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样提示 my.alert({ title: '提示', content: '当前支付宝版本过低,无法使用此功能,请升级最新版本支付宝' }); } }, checkMobile(mobile) { const reg = new RegExp(/^1\d{10}$/); return reg.test(mobile); }, saveAddress(e) { const obj = e.detail.value; console.log(obj, 'obj'); if (!obj.name) { return my.showToast({ content: '请输入收货人' }); } if (!obj.mobile) { return my.showToast({ content: '请输入手机号码' }); } if (!this.checkMobile(obj.mobile)) { return my.showToast({ content: '手机号码格式不正确' }); } if (this.props.regionType === 'map') { // 采用map 校验 const { latitude = '', longitude = '' } = this.data.addressInfo; if (!latitude || !longitude) { return my.showToast({ content: '请选择所在地区' }); } } else { // 采用region picker 校验 if (Object.prototype.toString.call(this.data.addressInfo.regionCode) === '[object Array]' && this.data.addressInfo.regionCode.length !== 3) { return my.showToast({ content: '请选择所在地区' }); } if (Object.prototype.toString.call(this.data.addressInfo.region) === '[object Array]' && this.data.addressInfo.region.length !== 3) { return my.showToast({ content: '请选择所在地区' }); } } if (!obj.addressNumber) { return my.showToast({ content: '请输入详细地址' }); } const region = { ...obj, region: this.data.addressInfo.region, regionCode: this.data.addressInfo.regionCode, latitude: this.data.addressInfo.latitude, longitude: this.data.addressInfo.longitude, address: this.data.addressInfo.address }; if (Object.prototype.toString.call(this.props.onComplete) === '[object Function]') { this.props.onComplete(region); } } } });