|
@@ -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,
|
|
|
|
}
|
|
}
|