107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
|
|
import Taro from '@tarojs/taro'
|
|
import {
|
|
SET_USERINFO,
|
|
SET_ADMINUSERINFO,
|
|
SET_TOKEN,
|
|
SET_SESSIONKEY,
|
|
SET_SORTCODE,
|
|
CLEAR_TOKEN,
|
|
CLEAR_USERINFO,
|
|
CLEAR_SESSIONKEY,
|
|
CLEAR_ADMINUSERINFO,
|
|
CLEAR_SORTCODE
|
|
} from '../constants/userInfo'
|
|
|
|
export type UserParam = {
|
|
nickName?:string,
|
|
phone?:string,
|
|
avatarUrl?:string,
|
|
city?: string,
|
|
country?: string,
|
|
province?: string,
|
|
gender?: number,
|
|
language?: string,
|
|
timestamp?: number,
|
|
physical_warehouse?: number,
|
|
physical_warehouse_name?: string,
|
|
}
|
|
|
|
export type UserAdminParam = {
|
|
avatar_url?: string,
|
|
open_id?: string,
|
|
physical_warehouse?: number,
|
|
physical_warehouse_name?: string,
|
|
union_id?: string,
|
|
user_code?: string,
|
|
user_id?: number,
|
|
user_name?: string,
|
|
wechat_user_open_id?: number
|
|
is_authorize_name?: false|true,
|
|
is_authorize_phone?: false|true,
|
|
}
|
|
|
|
export type SortCodeParam = {
|
|
shareShortDetail?: {title:string, code: string, img: string}, //详情分享页面短码
|
|
shareShortPage?: {title:string, code: string, img: string}, //右上角分享页面短码
|
|
}
|
|
|
|
export type DataParam = {
|
|
token?: string
|
|
session_key?: string,
|
|
userInfo: UserParam,
|
|
adminUserInfo: UserAdminParam,
|
|
sort_code: SortCodeParam
|
|
}
|
|
|
|
type Action = {
|
|
type?: string,
|
|
data?: DataParam
|
|
}
|
|
|
|
|
|
const INIT_USER = {
|
|
userInfo: Taro.getStorageSync('userInfo')?JSON.parse(Taro.getStorageSync('userInfo')):null,
|
|
adminUserInfo: Taro.getStorageSync('adminUserInfo')?JSON.parse(Taro.getStorageSync('adminUserInfo')):null,
|
|
token: Taro.getStorageSync('token')||'',
|
|
session_key: Taro.getStorageSync('session_key')||'',
|
|
sort_code: Taro.getStorageSync('sort_code')?JSON.parse(Taro.getStorageSync('sort_code')):null,
|
|
}
|
|
|
|
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_ADMINUSERINFO:
|
|
Taro.setStorageSync('adminUserInfo',JSON.stringify(data?.adminUserInfo))
|
|
return {...state,...data}
|
|
case SET_TOKEN:
|
|
Taro.setStorageSync('token',data?.token)
|
|
return {...state,...data}
|
|
case SET_SESSIONKEY:
|
|
Taro.setStorageSync('session_key',data?.session_key)
|
|
return {...state,...data}
|
|
case SET_SORTCODE:
|
|
Taro.setStorageSync('sort_code',JSON.stringify(data?.sort_code))
|
|
return {...state,...data}
|
|
case CLEAR_TOKEN:
|
|
Taro.removeStorageSync('token')
|
|
return {...state, token:''}
|
|
case CLEAR_SESSIONKEY:
|
|
Taro.removeStorageSync('session_key')
|
|
return {...state, session_key:''}
|
|
case CLEAR_USERINFO:
|
|
Taro.removeStorageSync('userInfo')
|
|
return {...state, userInfo: null}
|
|
case CLEAR_ADMINUSERINFO:
|
|
Taro.removeStorageSync('adminUserInfo')
|
|
return {...state, adminUserInfo: null}
|
|
case CLEAR_SORTCODE:
|
|
Taro.removeStorageSync('sort_code')
|
|
return {...state, sortCode: null}
|
|
default:
|
|
return state
|
|
}
|
|
} |