Initial commit
This commit is contained in:
158
miniprogram/pages/user/components/t-location/index.js
Normal file
158
miniprogram/pages/user/components/t-location/index.js
Normal file
@@ -0,0 +1,158 @@
|
||||
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);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
8
miniprogram/pages/user/components/t-location/index.json
Normal file
8
miniprogram/pages/user/components/t-location/index.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {
|
||||
"t-cell": "tdesign-miniprogram/cell/cell",
|
||||
"t-icon": "tdesign-miniprogram/icon/icon",
|
||||
"t-toast": "tdesign-miniprogram/toast/toast"
|
||||
}
|
||||
}
|
||||
16
miniprogram/pages/user/components/t-location/index.wxml
Normal file
16
miniprogram/pages/user/components/t-location/index.wxml
Normal file
@@ -0,0 +1,16 @@
|
||||
<view class="wx-address t-class" bind:tap="getWxLocation">
|
||||
<block wx:if="{{isCustomStyle}}">
|
||||
<view class="wx-address-custom">
|
||||
<t-icon prefix="wr" t-class="weixin" color="#0ABF5B" name="wechat" size="48rpx" />
|
||||
<text>{{title}}</text>
|
||||
</view>
|
||||
<slot />
|
||||
</block>
|
||||
<block wx:else>
|
||||
<t-cell title="{{title}}" title-class="cell__title" wr-class="cell" border="{{false}}">
|
||||
<t-icon t-class="weixin" slot="icon" color="#0ABF5B" name="logo-windows" size="48rpx" />
|
||||
<t-icon slot="right-icon" name="chevron-right" class="custom-icon" color="#bbb" />
|
||||
</t-cell>
|
||||
</block>
|
||||
</view>
|
||||
<t-toast id="t-toast" />
|
||||
19
miniprogram/pages/user/components/t-location/index.wxss
Normal file
19
miniprogram/pages/user/components/t-location/index.wxss
Normal file
@@ -0,0 +1,19 @@
|
||||
.wx-address .weixin {
|
||||
display: inline-block;
|
||||
font-size: 48rpx !important;
|
||||
margin-right: 20rpx;
|
||||
font-weight: normal;
|
||||
}
|
||||
.wx-address .cell {
|
||||
padding: 32rpx 30rpx;
|
||||
border-radius: 8rpx;
|
||||
}
|
||||
.wx-address .cell__title {
|
||||
font-size: 30rpx;
|
||||
color: #333333;
|
||||
}
|
||||
.wx-address-custom {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 32rpx;
|
||||
}
|
||||
Reference in New Issue
Block a user