Files
2025-11-17 14:11:46 +08:00

159 lines
5.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { getPermission } from '../../../../utils/getPermission';
import Toast from 'tdesign-miniprogram/toast/index';
import { addressParse } from '../../../../utils/addressParse';
import { resolveAddress, rejectAddress } from '../../../../services/address/list';
Component({
externalClasses: ['t-class'],
properties: {
title: {
type: String,
},
navigateUrl: {
type: String,
},
navigateEvent: {
type: String,
},
isCustomStyle: {
type: Boolean,
value: false,
},
isDisabledBtn: {
type: Boolean,
value: false,
},
isOrderSure: {
type: Boolean,
value: false,
},
},
methods: {
getWxLocation() {
console.log('[微信地址导入] 开始获取微信地址');
console.log('[微信地址导入] 按钮状态检查:', {
isDisabledBtn: this.properties.isDisabledBtn,
isOrderSure: this.properties.isOrderSure,
navigateUrl: this.properties.navigateUrl
});
if (this.properties.isDisabledBtn) {
console.log('[微信地址导入] 按钮被禁用,退出');
return;
}
console.log('[微信地址导入] 开始获取通讯地址权限');
getPermission({ code: 'scope.address', name: '通讯地址' }).then(() => {
console.log('[微信地址导入] 权限获取成功调用wx.chooseAddress');
wx.chooseAddress({
success: async (options) => {
console.log('[微信地址导入] wx.chooseAddress成功回调');
console.log('[微信地址导入] 原始地址数据:', options);
const { provinceName, cityName, countyName, detailInfo, userName, telNumber } = options;
console.log('[微信地址导入] 解析后的地址字段:', {
provinceName,
cityName,
countyName,
detailInfo,
userName,
telNumber
});
// 移除手机号验证逻辑,允许导入任何格式的手机号
console.log('[微信地址导入] 跳过手机号验证,直接使用微信提供的手机号:', telNumber);
const target = {
name: userName,
phone: telNumber,
countryName: '中国',
countryCode: 'chn',
detailAddress: detailInfo,
provinceName: provinceName,
cityName: cityName,
districtName: countyName,
isDefault: false,
isOrderSure: this.properties.isOrderSure,
};
console.log('[微信地址导入] 构建目标地址对象:', target);
try {
console.log('[微信地址导入] 开始地址解析:', { provinceName, cityName, countyName });
const { provinceCode, cityCode, districtCode } = await addressParse(provinceName, cityName, countyName);
console.log('[微信地址导入] 地址解析成功:', { provinceCode, cityCode, districtCode });
const params = Object.assign(target, {
provinceCode,
cityCode,
districtCode,
});
console.log('[微信地址导入] 最终地址参数:', params);
if (this.properties.isOrderSure) {
console.log('[微信地址导入] 订单确认模式调用onHandleSubmit');
this.onHandleSubmit(params);
} else if (this.properties.navigateUrl != '') {
console.log('[微信地址导入] 导航模式,跳转到:', this.properties.navigateUrl);
const { navigateEvent } = this.properties;
this.triggerEvent('navigate');
wx.navigateTo({
url: this.properties.navigateUrl,
success: function (res) {
console.log('[微信地址导入] 页面跳转成功,发送事件:', navigateEvent);
res.eventChannel.emit(navigateEvent, params);
},
});
} else {
console.log('[微信地址导入] 触发change事件');
this.triggerEvent('change', params);
}
} catch (error) {
console.error('[微信地址导入] 地址解析失败:', error);
wx.showToast({ title: '地址解析出错,请稍后再试', icon: 'none' });
}
},
fail(err) {
console.warn('[微信地址导入] 用户取消选择或选择失败:', err);
},
});
}).catch((error) => {
console.error('[微信地址导入] 权限获取失败:', error);
});
},
async queryAddress(addressId) {
try {
const { data } = await apis.userInfo.queryAddress({ addressId });
return data.userAddressVO;
} catch (err) {
console.error('查询地址错误', err);
throw err;
}
},
findPage(pageRouteUrl) {
const currentRoutes = getCurrentPages().map((v) => v.route);
return currentRoutes.indexOf(pageRouteUrl);
},
async onHandleSubmit(params) {
try {
const orderPageDeltaNum = this.findPage('pages/order/order-confirm/index');
if (orderPageDeltaNum > -1) {
wx.navigateBack({ delta: 1 });
resolveAddress(params);
return;
}
} catch (err) {
rejectAddress(params);
console.error(err);
}
},
},
});