Commit 6c08913b by HoMeTown

feat: login登录

parent 67fc813e
import http from '@/utils/request'
/**
* 登录
* @returns
*/
export function fetchLoginByUid(uid: string) {
return http.post('/user/api/login/mobile/v1/guest/login', {
userId: uid,
})
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import React, { createContext, useContext, useState } from 'react' import React, { createContext, useContext, useState } from 'react'
import { useLocalStorageState } from 'ahooks' import { useLocalStorageState } from 'ahooks'
import { fetchLoginByUid } from '@/api/common'
interface AuthContextType { interface AuthContextType {
isLoggedIn: boolean isLoggedIn: boolean
...@@ -19,7 +20,7 @@ interface AuthProviderProps { ...@@ -19,7 +20,7 @@ interface AuthProviderProps {
} }
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => { export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const [token] = useLocalStorageState<string | undefined>( const [token, setToken] = useLocalStorageState<string | undefined>(
'__TOKEN__', '__TOKEN__',
{ {
defaultValue: '', defaultValue: '',
...@@ -29,9 +30,13 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => { ...@@ -29,9 +30,13 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const [showLoginModal, setShowLoginModal] = useState(false) const [showLoginModal, setShowLoginModal] = useState(false)
const [showLoginTip, setShowLoginTip] = useState(!token) const [showLoginTip, setShowLoginTip] = useState(!token)
const login = () => { const login = async () => {
setIsLoggedIn(true) const res = await fetchLoginByUid('123')
setShowLoginModal(false) if (res.data) {
setToken(res.data.token)
setIsLoggedIn(true)
setShowLoginModal(false)
}
} }
const logout = () => { const logout = () => {
......
import React from 'react' import React from 'react'
import { Button, Link, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader } from '@nextui-org/react' import { Button, Link, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader } from '@nextui-org/react'
import { useAuth } from '@/auth/AuthContext'
interface LoginModalProps { interface LoginModalProps {
isOpen: boolean isOpen: boolean
...@@ -7,6 +8,11 @@ interface LoginModalProps { ...@@ -7,6 +8,11 @@ interface LoginModalProps {
} }
export const LoginModal: React.FC<LoginModalProps> = ({ isOpen, onClose }) => { export const LoginModal: React.FC<LoginModalProps> = ({ isOpen, onClose }) => {
const { login } = useAuth()
const handleLogin = () => {
login()
}
return ( return (
<Modal backdrop="blur" isOpen={isOpen} onClose={onClose}> <Modal backdrop="blur" isOpen={isOpen} onClose={onClose}>
<ModalContent> <ModalContent>
...@@ -31,7 +37,7 @@ export const LoginModal: React.FC<LoginModalProps> = ({ isOpen, onClose }) => { ...@@ -31,7 +37,7 @@ export const LoginModal: React.FC<LoginModalProps> = ({ isOpen, onClose }) => {
<Button onPress={onClose}> <Button onPress={onClose}>
再想想 再想想
</Button> </Button>
<Button color="primary" onPress={onClose}> <Button color="primary" onPress={handleLogin}>
同意 同意
</Button> </Button>
</ModalFooter> </ModalFooter>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment