userAction.js 1.93 KB
Newer Older
zhanghaozhe committed
1 2
import { http } from '@/utils';
import { encrypt } from '@/components/passport/encryption';
3 4 5 6
import jsCookie from 'js-cookie'


const accountLogin = user => dispatch => {
zhanghaozhe committed
7
    return http.post(`${API['passport-api']}/user_login`, {
8 9
        user_name: user.username,
        password: encrypt(user.password),
10
        is_encrypt: 1,
11
        redirect: encodeURIComponent(user.redirect)
12
    }).then(res => {
zhanghaozhe committed
13
        return storeUser(res, dispatch)
14 15 16
    })
}

zhanghaozhe committed
17
const quickLogin = user => dispatch => {
FE committed
18 19 20 21 22 23
    return http.post(`${API['passport-api']}/quick_login`, {
        ...user,
        plat: 5
    }).then(res => {
        return storeUser(res, dispatch)
    })
zhanghaozhe committed
24 25 26 27 28 29
}

const storeUser = (res, dispatch) => {
    const data = res.data
    let payload
    if (data.errno === 0) {
xuzhenghua committed
30
        const {user_name: username, avatar_file: avatar,is_vip: isVIP, ...rest} = data.data.user_info
zhanghaozhe committed
31 32 33
        payload = {
            hasError: false,
            msg: data.msg,
xuzhenghua committed
34
            data: {username, avatar,isVIP, ...rest}
zhanghaozhe committed
35 36 37 38
        }
    } else {
        payload = {
            hasError: true,
zhanghaozhe committed
39 40
            msg: data.msg,
            data: {}
zhanghaozhe committed
41 42 43 44 45
        }
    }
    dispatch(setCurrentUser(payload))
    return payload

46 47 48 49 50 51
}


const SET_CURRENT_USER = 'SET_CURRENT_USER'
const setCurrentUser = payload => ({
    type: SET_CURRENT_USER,
zhanghaozhe committed
52
    payload: {...payload, isFetching: false}
53 54 55 56 57
})


const LOGOUT = 'LOGOUT'
const logout = () => dispatch => {
zhanghaozhe committed
58 59
    jsCookie.remove('token', {path: '/', domain: '.julyedu.com'})
    jsCookie.remove('uid', {path: '/', domain: '.julyedu.com'})
zhanghaozhe committed
60
    dispatch(setCurrentUser({hasError: true}))
61 62 63
}


64 65 66 67 68 69
const UPDATE_USER = 'UPDATE_USER'
const updateUser = payload => ({
    type: UPDATE_USER,
    payload
})

zhanghaozhe committed
70 71 72 73 74 75
const START_FETCH_USER = 'START_FETCH_USER'
const startFetchUser = () => ({
    type: START_FETCH_USER
})


76 77 78
export {
    accountLogin,
    SET_CURRENT_USER,
zhanghaozhe committed
79
    setCurrentUser,
80 81 82
    quickLogin,
    logout,
    UPDATE_USER,
zhanghaozhe committed
83 84 85
    updateUser,
    START_FETCH_USER,
    startFetchUser,
86
}