优化细节
This commit is contained in:
@@ -1,90 +1,65 @@
|
||||
import {useEffect, useState} from "react";
|
||||
import {Button, Cell, CellGroup, Space, Empty, ConfigProvider} from '@nutui/nutui-react-taro'
|
||||
import {Dongdong} from '@nutui/icons-react-taro'
|
||||
import {useEffect} from "react";
|
||||
import Taro from '@tarojs/taro'
|
||||
import {ShopUserAddress} from "@/api/shop/shopUserAddress/model";
|
||||
import {listShopUserAddress} from "@/api/shop/shopUserAddress";
|
||||
import {addShopUserAddress} from "@/api/shop/shopUserAddress";
|
||||
|
||||
const Address = () => {
|
||||
const [list, setList] = useState<ShopUserAddress[]>([{
|
||||
id: 1,
|
||||
name: '张三',
|
||||
phone: '13800138000',
|
||||
country: '中国',
|
||||
province: '广东省',
|
||||
city: '广州市',
|
||||
region: '天河区',
|
||||
address: '黄埔大道西10号',
|
||||
fullAddress: '广东省广州市天河区黄埔大道西10号',
|
||||
lat: '23.129163',
|
||||
lng: '113.382391',
|
||||
gender: 1,
|
||||
type: 'home',
|
||||
isDefault: true,
|
||||
userId: 1,
|
||||
tenantId: 1,
|
||||
createTime: '2021-09-01 10:10:10',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: '李四',
|
||||
phone: '13800138000',
|
||||
country: '中国',
|
||||
province: '广西壮族自治区',
|
||||
city: '南宁市',
|
||||
region: '青秀区',
|
||||
address: '青秀区民族大道100号',
|
||||
fullAddress: '广西壮族自治区南宁市青秀区民族大道100号',
|
||||
lat: '23.129163',
|
||||
lng: '113.382391',
|
||||
gender: 1,
|
||||
type: 'home',
|
||||
isDefault: true,
|
||||
userId: 1,
|
||||
tenantId: 1,
|
||||
createTime: '2021-09-01 10:10:10',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: '张三',
|
||||
phone: '13800138000',
|
||||
country: '中国',
|
||||
province: '广西',
|
||||
city: '南宁市',
|
||||
region: '青秀区',
|
||||
address: '青秀区民族大道100号',
|
||||
fullAddress: '广西壮族自治区南宁市青秀区民族大道100号',
|
||||
lat: '23.129163',
|
||||
lng: '113.382391',
|
||||
gender: 1,
|
||||
type: 'home',
|
||||
isDefault: true,
|
||||
userId: 1,
|
||||
tenantId: 1,
|
||||
createTime: '2021-09-01 10:10:10',
|
||||
}])
|
||||
|
||||
const reload = () => {
|
||||
listShopUserAddress({userId: Taro.getStorageSync('UserId')}).then(res => {
|
||||
// setList(res)
|
||||
}).catch(error => {
|
||||
console.error("Failed to fetch goods detail:", error);
|
||||
})
|
||||
/**
|
||||
* 从微信API获取用户收货地址
|
||||
* 调用微信原生地址选择界面,获取成功后保存到服务器并刷新列表
|
||||
*/
|
||||
const getWeChatAddress = () => {
|
||||
Taro.chooseAddress()
|
||||
.then(res => {
|
||||
// 格式化微信返回的地址数据为后端所需格式
|
||||
const addressData = {
|
||||
name: res.userName,
|
||||
phone: res.telNumber,
|
||||
country: res.nationalCode || '中国',
|
||||
province: res.provinceName,
|
||||
city: res.cityName,
|
||||
region: res.countyName,
|
||||
address: res.detailInfo,
|
||||
postalCode: res.postalCode,
|
||||
isDefault: false
|
||||
}
|
||||
console.log(res, 'addrs..')
|
||||
// 调用保存地址的API(假设存在该接口)
|
||||
addShopUserAddress(addressData)
|
||||
.then((msg) => {
|
||||
console.log(msg)
|
||||
Taro.showToast({
|
||||
title: `${msg}`,
|
||||
icon: 'none'
|
||||
})
|
||||
setTimeout(() => {
|
||||
// 保存成功后返回
|
||||
Taro.navigateBack()
|
||||
}, 1000)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('保存地址失败:', error)
|
||||
Taro.showToast({title: '保存地址失败', icon: 'error'})
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('获取微信地址失败:', err)
|
||||
// 处理用户拒绝授权的情况
|
||||
if (err.errMsg.includes('auth deny')) {
|
||||
Taro.showModal({
|
||||
title: '授权失败',
|
||||
content: '请在设置中允许获取地址权限',
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
reload()
|
||||
getWeChatAddress()
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
{list.map((item) => (
|
||||
<div className={'flex flex-col bg-white my-3 py-1 px-4'}>
|
||||
<div className={'py-1'}>{item.province}{item.city}{item.region}</div>
|
||||
<div className={'py-1'}>{item.address}</div>
|
||||
<div className={'text-gray-500 py-1'}>{item.name} {item.phone}</div>
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user