import { useState, useEffect } from 'react' import Taro from '@tarojs/taro' import { View, Text, Input, Image } from '@tarojs/components' import { storeLogin } from '@/api/shop/shopStore' import { saveStorageByLoginUser } from '@/utils/server' import './index.scss' const StoreLogin = () => { const [phone, setPhone] = useState('') const [password, setPassword] = useState('') const [loading, setLoading] = useState(false) const [showContent, setShowContent] = useState(false) useEffect(() => { setTimeout(() => setShowContent(true), 100) }, []) /** 解析 redirect 参数 */ const router = Taro.getCurrentInstance().router const redirectUrl = (() => { const raw = (router?.params as Record | undefined)?.redirect if (!raw) return '' try { const decoded = decodeURIComponent(raw) return decoded.startsWith('/') ? decoded : `/${decoded}` } catch { return raw.startsWith('/') ? raw : `/${raw}` } })() /** 登录成功后跳转 */ const navigateAfterLogin = async () => { if (!redirectUrl) { await Taro.reLaunch({ url: '/pages/index/index' }) return } await Taro.redirectTo({ url: redirectUrl }) } /** 账号密码登录 */ const handleLogin = async () => { if (!phone.trim()) { Taro.showToast({ title: '请输入手机号', icon: 'none' }) return } if (!password.trim()) { Taro.showToast({ title: '请输入密码', icon: 'none' }) return } if (loading) return try { setLoading(true) const res = await storeLogin({ phone: phone.trim(), password: password.trim() }) if (res?.access_token) { const token = res.access_token const user = res.user saveStorageByLoginUser(token, user) Taro.showToast({ title: '登录成功', icon: 'success' }) setTimeout(() => navigateAfterLogin(), 800) } else { Taro.showToast({ title: '登录失败', icon: 'none' }) } } catch (e: any) { console.error('门店登录失败:', e) Taro.showToast({ title: e?.message || '登录失败', icon: 'none' }) } finally { setLoading(false) } } return ( 门店管理登录 请输入门店账号和密码 📱 setPhone(e.detail.value)} /> 🔒 setPassword(e.detail.value)} /> {loading ? '登录中...' : '登录'} 门店账号由管理员创建,如需账号请联系管理员 ) } export default StoreLogin