diff --git a/config/index.js b/config/index.js index 822db49..54f3b9d 100644 --- a/config/index.js +++ b/config/index.js @@ -32,7 +32,9 @@ const config = { alias: { '@/components': path.resolve(__dirname, '..', 'src/components'), '@/assets': path.resolve(__dirname, '..', 'src/assets'), - '@/images': path.resolve(__dirname, '..', 'src/assets/images') + '@/images': path.resolve(__dirname, '..', 'src/assets/images'), + '@/config': path.resolve(__dirname, '..', 'src/config'), + '@/utils': path.resolve(__dirname, '..', 'src/utils'), }, mini: { postcss: { diff --git a/src/app.config.js b/src/app.config.js index f8624b8..067c4e4 100644 --- a/src/app.config.js +++ b/src/app.config.js @@ -31,6 +31,7 @@ export default defineAppConfig({ 'pages/message/index', 'pages/message-list/index', 'pages/message-detail/index', + '/pages/login-quick/index' ], window: { backgroundTextStyle: 'light', @@ -39,11 +40,5 @@ export default defineAppConfig({ navigationBarTextStyle: 'black', navigationStyle: 'custom' }, - components: [ - 'pages/index/index', - 'pages/login/index', - 'pages/register/index', - 'pages/forgot/index', - 'pages/goods-detail/index', - ] + }) diff --git a/src/config/config.js b/src/config/config.js new file mode 100644 index 0000000..4cd750b --- /dev/null +++ b/src/config/config.js @@ -0,0 +1,5 @@ +export default { + api: "http://1.14.121.134:9101", + debugApi: "http://1.14.121.134:9101", + debug: false +} diff --git a/src/pages/address-create/index.jsx b/src/pages/address-create/index.jsx index c6959e4..d7682bb 100644 --- a/src/pages/address-create/index.jsx +++ b/src/pages/address-create/index.jsx @@ -11,6 +11,9 @@ import checked from '@/images/checked.png' import Taro from '@tarojs/taro'; import { useRouter } from '@tarojs/taro'; import { Button, Textarea, Address } from '@nutui/nutui-react-taro'; +import { cities, createAddress, getAddress, updateAddress } from '../../utils/api'; +import { useCallback } from 'react'; +import { backOrGo, closeLoading, errorNotice, loading, redirectTo } from '../../utils/utils'; @@ -19,7 +22,7 @@ function Index() { const param = useRouter().params const [id] = useState(param.id) const [home] = useState(param.home) - const [addrId, setAddrId] = useState(0) + const [addrId, setAddrId] = useState([]) const [isDefault, setIsDefault] = useState(false) const [user, setUser] = useState('') const [phone, setPhone] = useState('') @@ -28,13 +31,8 @@ function Index() { const [text, setText] = useState('请选择地址') const [normal, setNormal] = useState(false) - const [province, setProvince] = useState([ - { id: 1, name: '北京', title: 'B' }, - { id: 2, name: '广西', title: 'G' }, - { id: 3, name: '江西', title: 'J' }, - { id: 4, name: '四川', title: 'S' }, - { id: 5, name: '浙江', title: 'Z' }, - ]) + const [cityList, setCityList] = useState([]) + const [province, setProvince] = useState([]) const [city, setCity] = useState([]) @@ -46,27 +44,60 @@ function Index() { city, country, town, + addressIdStr: '0_0_0_0', }) - const onChange = (cal) => { + useEffect(() => { + cities().then(res => { + if (!res) return + setCityList(res.items) + setProvince(res.items?.filter(item => item.deep == 0)) + }) + }, []) + + useEffect(() => { + if (!id) return + getAddress().then(re => { + if (!re) return + const data = re.items.filter(item => item.id == id) + if (data.length < 1) return + const addr = data[0] + console.log(addr) + setAddrId([addr.province_id, addr.city_id, addr.county_id]) + setText(`${addr.province.name}${addr.city.name}${addr.county.name}`) + setAddrInfo(addr.address) + setUser(addr.recipient_name) + setPhone(addr.recipient_phone) + setAddress({ province: addr.province, city: addr.city, country: addr.county, town: [], addressIdStr: `${addr.province_id}_${addr.city_id}_${addr.county_id}_0` }) + if (addr.province_id && addr.city_id) { + cities(addr.province_id).then(r => { + setCity(r.items) + }) + } + if (addr.city_id && addr.county_id) { + cities(addr.city_id).then(r => { + console.log(r.items, addr, addrId) + setCountry(r.items) + }) + } + }) + }, [id]) + + const onChange = async (cal) => { + const re = await cities(cal.value.id) + const d = re.items + if (!d.length) { + setNormal(false) + return + } setTimeout(() => { switch (cal.next) { case 'city': - setCity([ - { id: 7, name: '朝阳区', title: 'C' }, - { id: 8, name: '崇文区', title: 'C' }, - { id: 9, name: '昌平区', title: 'C' }, - { id: 6, name: '石景山区', title: 'S' }, - { id: 3, name: '八里庄街道', title: 'B' }, - { id: 10, name: '北苑', title: 'B' }, - ]) + + setCity(d) break; case 'country': - setCountry([ - { id: 3, name: '八里庄街道', title: 'B' }, - { id: 9, name: '北苑', title: 'B' }, - { id: 4, name: '常营乡', title: 'C' }, - ]) + setCountry(d) break; default: setNormal(false) @@ -74,35 +105,61 @@ function Index() { }, 200) } const close = (val) => { - console.log(val, "Data") setNormal(false) - if (val.data && !!val.data.addressStr) { setText((val.data).addressStr) - setAddress({ province: val.data.province, city: val.data.city, country: val.data.country, town: val.data.town }) + setAddress({ province: val.data.province, city: val.data.city, country: val.data.country, town: val.data.town, addressIdStr: val.data.addressIdStr }) } } - - useEffect(() => { - - - }, [id]) - - - // 跳转 - const navDetailFn = (id) => { - Taro.navigateTo({ - url: `/pages/pay-success/index?id=${id}` - }) - } // 返回页面 const backFn = () => { - Taro.getCurrentPages().length > 0 && Taro.navigateBack() + Taro.getCurrentPages().length > 0 && Taro.navigateBack({ delta: 1 }) + } + + const submit = async () => { + const addressId = address.addressIdStr.split('_') + if (!phone || !user || !addrInfo || address.addressIdStr == '0_0_0_0') { + errorNotice('请填写地址完整信息') + return + } + + if (addrInfo.length < 6) { + errorNotice('详细地址信息错误') + return + } + + const data = { + zipcode: '000000', + recipient_phone: phone, + recipient_name: user, + address: addrInfo, + province_id: Number(addressId[0]), + city_id: Number(addressId[1]), + county_id: Number(addressId[2]), + is_default: isDefault + } + + if (id) { + loading('地址编辑中...') + const re = await updateAddress(id, data) + closeLoading() + if (re) { + backOrGo('/pages/address/index') + } + return + } + + loading('地址新增中...') + const re = await createAddress(data) + closeLoading() + if (re) { + backOrGo('/pages/address/index') + } } return ( - + { @@ -111,36 +168,36 @@ function Index() { - - - - 收货人 - { + + + + 收货人 + { setUser(e.detail.value) }} /> - - 电话 - { + + 电话 + { setPhone(e.detail.value) }} /> - - 所在地区 - { setNormal(true) }}> + + 所在地区 + { setNormal(true) }}> - - 详细地址 + + 详细地址