登录封装完成
This commit is contained in:
+44
-16
@@ -1,23 +1,51 @@
|
||||
|
||||
import { GETUSER, SETUSER } from '../constants/userInfo'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { SET_USERINFO, SET_TOKEN, SET_SESSIONKEY, CLEAR_TOKEN, CLEAR_USERINFO, CLEAR_SESSIONKEY} from '../constants/userInfo'
|
||||
|
||||
const INIT_USER = {
|
||||
name: '张三',
|
||||
phone: '110',
|
||||
avatarUrl: '',
|
||||
token: '',
|
||||
export type UserParam = {
|
||||
name?:string,
|
||||
phone?:string,
|
||||
avatarUrl?:string,
|
||||
}
|
||||
|
||||
export default function counter (state = INIT_USER, action) {
|
||||
switch (action.type) {
|
||||
case GETUSER:
|
||||
return {
|
||||
...state,
|
||||
}
|
||||
case SETUSER:
|
||||
return {
|
||||
...state,
|
||||
}
|
||||
type Action = {
|
||||
type?: string,
|
||||
data?: DataParam
|
||||
}
|
||||
|
||||
export type DataParam = {
|
||||
token?: string
|
||||
sessionkey?: string,
|
||||
userInfo: UserParam
|
||||
}
|
||||
|
||||
const INIT_USER = {
|
||||
userInfo: Taro.getStorageSync('userInfo')?JSON.parse(Taro.getStorageSync('userInfo')):null,
|
||||
token: Taro.getStorageSync('token')||'',
|
||||
session_key: Taro.getStorageSync('sessionkey')||'',
|
||||
}
|
||||
|
||||
export default function counter (state = INIT_USER, action: Action) {
|
||||
const {type, data} = action
|
||||
switch (type) {
|
||||
case SET_USERINFO:
|
||||
Taro.setStorageSync('userInfo',JSON.stringify(data?.userInfo))
|
||||
return {...state,...data}
|
||||
case SET_TOKEN:
|
||||
Taro.setStorageSync('token',JSON.stringify(data?.token))
|
||||
return {...state,...data}
|
||||
case SET_SESSIONKEY:
|
||||
Taro.setStorageSync('sessionkey',JSON.stringify(data?.sessionkey))
|
||||
return {...state,...data}
|
||||
case CLEAR_TOKEN:
|
||||
Taro.removeStorageSync('token')
|
||||
return {...state, token:''}
|
||||
case CLEAR_SESSIONKEY:
|
||||
Taro.removeStorageSync('sessionkey')
|
||||
return {...state, session_key:''}
|
||||
case CLEAR_USERINFO:
|
||||
Taro.removeStorageSync('userInfo')
|
||||
return {...state, userInfo: null}
|
||||
default:
|
||||
return state
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user