123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { message } from 'antd'
- import router from '../router/index.tsx'
- import http from './index'
- import {
- userLogin, sendVerificationCode, loginPhone,
- } from './api.ts'
- type UserInfo = {
- channel: string,
- contactName: string,
- createTimestamp: number,
- id: number,
- identity: number,
- name: string,
- telNum: string,
- token: string
- }
- export function getUserInfo(): UserInfo {
- const activeUser = localStorage.getItem('userInfo')
- return activeUser ? JSON.parse(activeUser) : {}
- }
- export function getAccessToken(): string {
- const userInfo = getUserInfo()
- return userInfo.token
- }
- export function setUserInfo(userInfo: UserInfo): void {
- localStorage.removeItem('userInfo')
- localStorage.setItem('userInfo', JSON.stringify(userInfo))
- }
- export function clearUserInfo() {
- localStorage.removeItem('userInfo')
- }
- export function login(account: string, password: string) {
- return http.post<UserInfo>(userLogin, {
- telNum: account,
- password: window.btoa(password)
- }).then((res: ApiResponse<UserInfo>) => {
- const { code, msg, data } = res
- if (code === 0) {
- message.success('登录成功')
- setUserInfo(data)
- location.href = '/cooperationAccount/gzh'
- return true
- }
- message.error(msg)
- return false
- }).catch(() => {})
- }
- export function sendCode(phone: string) {
- return http.post(sendVerificationCode, {
- telNum: phone
- }).then(res => {
- const { code, msg = '发送失败' } = res
- if (code === 0) {
- message.success('短信已发送,请注意查收')
- return true
- }
- message.error(msg)
- return false
- }).catch(() => {})
- }
- export function loginBySendCode(telNum: string, verifyCode: string) {
- return http.post(loginPhone, {
- telNum,
- verifyCode
- }).then(res => {
- const { code, msg, data } = res as { code: number, msg: string, data: UserInfo }
- if (code === 0 && data) {
- message.success('登录成功')
- setUserInfo(data)
- router.navigate('/cooperationAccount/gzh')
- return true
- }
- message.error(msg)
- return false
- }).catch((err) => {
- console.log(err)
- })
- }
- export function logout() {
- message.success('已退出登录')
- clearUserInfo()
- router.navigate('/login')
- return true
- }
- export default {
- getUserInfo,
- setUserInfo,
- clearUserInfo,
- login,
- sendCode,
- loginBySendCode,
- logout,
- }
|