123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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);
- }
- }
- }
- });
|