Explorar el Código

feat: login 接口done

jihuaqiang hace 1 semana
padre
commit
c9323bdbf4
Se han modificado 5 ficheros con 55 adiciones y 105 borrados
  1. 4 3
      src/components/layout/headerbar.tsx
  2. 3 12
      src/http/api.ts
  3. 5 3
      src/http/index.ts
  4. 42 86
      src/http/sso.ts
  5. 1 1
      types/http/index.d.ts

+ 4 - 3
src/components/layout/headerbar.tsx

@@ -3,14 +3,15 @@ import { Button, Dropdown, Layout, message, Switch} from 'antd';
 import useConfigStore from '../../store/config';
 import useConfigStore from '../../store/config';
 import LogoIcon from "@src/assets/images/login/logo.svg?react";
 import LogoIcon from "@src/assets/images/login/logo.svg?react";
 import Icon, { UserOutlined } from "@ant-design/icons";
 import Icon, { UserOutlined } from "@ant-design/icons";
-import { logout } from "../../http/sso";
+import sso, { logout } from "../../http/sso";
 import { useNavigate } from 'react-router-dom';
 import { useNavigate } from 'react-router-dom';
 
 
 const { Header } = Layout;
 const { Header } = Layout;
 
 
 const Headerbar = (props: { colorBgContainer: string }) => {
 const Headerbar = (props: { colorBgContainer: string }) => {
 	const navigate = useNavigate()
 	const navigate = useNavigate()
-  const setAlgorithm = useConfigStore(state => state.setAlgorithm)
+	const setAlgorithm = useConfigStore(state => state.setAlgorithm)
+	const userInfo = sso.getUserInfo()
 
 
 	const logoutHandle = async () => {
 	const logoutHandle = async () => {
 		await logout()
 		await logout()
@@ -30,7 +31,7 @@ const Headerbar = (props: { colorBgContainer: string }) => {
           <Switch checkedChildren="Light" unCheckedChildren="Dark" defaultChecked onChange={(checked) => setAlgorithm(checked ? 'default' : 'dark')} />
           <Switch checkedChildren="Light" unCheckedChildren="Dark" defaultChecked onChange={(checked) => setAlgorithm(checked ? 'default' : 'dark')} />
 					<Dropdown trigger={['hover']} menu={{items: [{label: (<Button type="primary" onClick={logoutHandle}>退出账号</Button>), key: 'logout'}]}}>
 					<Dropdown trigger={['hover']} menu={{items: [{label: (<Button type="primary" onClick={logoutHandle}>退出账号</Button>), key: 'logout'}]}}>
 						<div className='flex items-center gap-[10px] text-xl'>
 						<div className='flex items-center gap-[10px] text-xl'>
-							<div>Yujian Xue</div>
+							<div>{userInfo.contactName || userInfo.name}</div>
 							<UserOutlined />
 							<UserOutlined />
 						</div>
 						</div>
 					</Dropdown>
 					</Dropdown>

+ 3 - 12
src/http/api.ts

@@ -4,22 +4,13 @@
 export const userLogin = `${import.meta.env.VITE_API_URL}/contentPlatform/account/login`
 export const userLogin = `${import.meta.env.VITE_API_URL}/contentPlatform/account/login`
 
 
 // 获取验证码
 // 获取验证码
-export const sendVerificationCode = `${import.meta.env.VITE_API_URL}/ad/platform/user/sendVerificationCode`
+export const sendVerificationCode = `${import.meta.env.VITE_API_URL}/contentPlatform/account/sendMessageVerifyCode`
 
 
 // 手机号登录
 // 手机号登录
-export const loginPhone = `${import.meta.env.VITE_API_URL}/ad/platform/user/login/phone`
-
-// 忘记密码
-export const forgotPassword = `${import.meta.env.VITE_API_URL}/ad/platform/user/forgotPassword`
-
-// 登出
-export const userLogout = `${import.meta.env.VITE_API_URL}/ad/platform/user/logout`
-
-// 修改密码
-export const changePassword = `${import.meta.env.VITE_API_URL}/ad/platform/user/changePassword`
+export const loginPhone = `${import.meta.env.VITE_API_URL}/contentPlatform/account/login`
 
 
 // 获取用户信息
 // 获取用户信息
-export const getAgencyInfo = `${import.meta.env.VITE_API_URL}/ad/platform/user/getAgencyInfo`
+// export const getAgencyInfo = `${import.meta.env.VITE_API_URL}/ad/platform/user/getAgencyInfo`
 
 
 
 
 
 

+ 5 - 3
src/http/index.ts

@@ -17,8 +17,10 @@ class Request {
     this.instance = axios.create(config)
     this.instance = axios.create(config)
 
 
     this.instance.interceptors.request.use(function(config) {
     this.instance.interceptors.request.use(function(config) {
-      if (!config.headers.token)
-        config.headers.token = sso.getAccessToken()
+			if (!config.headers.token) {
+				const userInfo = sso.getUserInfo()
+				config.headers.token = userInfo.token
+			}
 
 
       return config
       return config
     }, function(error) {
     }, function(error) {
@@ -31,7 +33,7 @@ class Request {
 
 
       // 登陆过期 || 未登陆
       // 登陆过期 || 未登陆
       if (code === invalidCode) {
       if (code === invalidCode) {
-        sso.clearAccessToken()
+        sso.clearUserInfo()
 
 
         const {pathname,search} = location
         const {pathname,search} = location
         if (pathname!=='/login') {
         if (pathname!=='/login') {

+ 42 - 86
src/http/sso.ts

@@ -3,36 +3,44 @@ import router from '../router/index.tsx'
 import http from './index'
 import http from './index'
 import {
 import {
   userLogin, sendVerificationCode, loginPhone,
   userLogin, sendVerificationCode, loginPhone,
-  userLogout, changePassword, forgotPassword
 } from './api.ts'
 } from './api.ts'
 
 
-export function getAccessToken(): string {
-  const accessToken = localStorage.getItem('token')
-  return accessToken || ''
+type UserInfo = { 
+	channel: string,
+	contactName: string,
+	createTimestamp: number,
+	id: number,
+	identity: number,
+	name: string,
+	telNum: string,
+	token: string
 }
 }
 
 
-export function setAccessToken(token: string): void {
-  localStorage.removeItem('token')
-  localStorage.setItem('token', token)
+export function getUserInfo(): UserInfo {
+  const activeUser = localStorage.getItem('userInfo')
+  return activeUser ? JSON.parse(activeUser) : {}
 }
 }
 
 
-export function clearAccessToken() {
-  localStorage.removeItem('token')
+export function setUserInfo(userInfo: UserInfo): void {
+  localStorage.removeItem('userInfo')
+  localStorage.setItem('userInfo', JSON.stringify(userInfo))
 }
 }
 
 
-type loginType = { token: string, isAdmin: boolean, menus: string[] }
+export function clearUserInfo() {
+  localStorage.removeItem('userInfo')
+}
+
+
 
 
 export function login(account: string, password: string) {
 export function login(account: string, password: string) {
-  return http.post(userLogin, {
+  return http.post<UserInfo>(userLogin, {
     telNum: account,
     telNum: account,
     password: window.btoa(password)
     password: window.btoa(password)
-  }).then(res => {
-    const { code, msg, data = {} } = res
-    const { token, } = data as loginType
-
+  }).then((res: ApiResponse<UserInfo>) => {
+    const { code, msg, data } = res
     if (code === 0) {
     if (code === 0) {
       message.success('登录成功')
       message.success('登录成功')
-      setAccessToken(token)
+      setUserInfo(data)
 			location.href = '/cooperationAccount/gzh'
 			location.href = '/cooperationAccount/gzh'
       return true
       return true
     }
     }
@@ -45,7 +53,7 @@ export function login(account: string, password: string) {
 
 
 export function sendCode(phone: string) {
 export function sendCode(phone: string) {
   return http.post(sendVerificationCode, {
   return http.post(sendVerificationCode, {
-    phone
+    telNum: phone
   }).then(res => {
   }).then(res => {
     const { code, msg = '发送失败' } = res
     const { code, msg = '发送失败' } = res
 
 
@@ -60,92 +68,40 @@ export function sendCode(phone: string) {
   }).catch(() => {})
   }).catch(() => {})
 }
 }
 
 
-export function loginBySendCode(phone: string, verificationCode: string) {
+export function loginBySendCode(telNum: string, verifyCode: string) {
   return http.post(loginPhone, {
   return http.post(loginPhone, {
-    phone,
-    verificationCode
+    telNum,
+    verifyCode
   }).then(res => {
   }).then(res => {
-    const { code, msg, data = {} } = res
-    const { token } = data as loginType
-
-    if (code === 0) {
+		const { code, msg, data } = res as { code: number, msg: string, data: UserInfo }
+    if (code === 0 && data) {
       message.success('登录成功')
       message.success('登录成功')
-      setAccessToken(token)
+      setUserInfo(data)
+			router.navigate('/cooperationAccount/gzh')
       return true
       return true
     }
     }
 
 
     message.error(msg)
     message.error(msg)
     return false
     return false
 
 
-  }).catch(() => {})
+	}).catch((err) => {
+		console.log(err)
+	})
 }
 }
 
 
 export function logout() {
 export function logout() {
-  return http.post(userLogout).then(res => {
-    const { code, msg } = res
-
-    if (code === 0) {
-      message.success('退出登录成功')
-      clearAccessToken()
-      router.navigate('/login')
-      return true
-    }
-
-    message.error(msg)
-    return false
-
-  }).catch(() => {})
-}
-
-export function changeLoginPassword(oldPassword: string, newPassword: string) {
-  return http.post(changePassword, {
-    oldPassword,
-    newPassword
-  }).then(res => {
-    const { code, msg } = res
-
-    if (code === 0) {
-      message.success('修改密码成功')
-      clearAccessToken()
-      router.navigate('/login')
-      return true
-    }
-
-    message.error(msg)
-    return false
-
-  }).catch(() => {})
+	message.success('已退出登录')
+	clearUserInfo()
+	router.navigate('/login')
+	return true
 }
 }
 
 
-export function forgotLoginPassword(phone: string, verificationCode: string, newPassword: string) {
-  return http.post(forgotPassword, {
-    phone,
-    verificationCode,
-    newPassword
-  }).then(res => {
-    const { code, msg } = res
-
-    if (code === 0) {
-      message.success('密码编辑成功')
-      clearAccessToken()
-      return true
-    }
-
-    message.error(msg)
-    return false
-
-  }).catch(() => {})
-}
-
-
 export default {
 export default {
-  getAccessToken,
-  setAccessToken,
-  clearAccessToken,
+  getUserInfo,
+  setUserInfo,
+  clearUserInfo,
   login,
   login,
   sendCode,
   sendCode,
   loginBySendCode,
   loginBySendCode,
   logout,
   logout,
-  changeLoginPassword,
-  forgotLoginPassword,
 }
 }

+ 1 - 1
types/http/index.d.ts

@@ -1,6 +1,6 @@
 interface ApiResponse<T> {
 interface ApiResponse<T> {
   msg:string, 
   msg:string, 
-  data?:T
+  data:T
   code: number
   code: number
   success: boolean
   success: boolean
 }
 }