userAction.js 1.84 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 10 11
        user_name: user.username,
        password: encrypt(user.password),
        is_encrypt: 1
    }).then(res => {
zhanghaozhe committed
12
        return storeUser(res, dispatch)
13 14 15
    })
}

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

const storeUser = (res, dispatch) => {
    const data = res.data
    let payload
    if (data.errno === 0) {
        const {user_name: username, avatar_file: avatar, ...rest} = data.data.user_info
        payload = {
            hasError: false,
            msg: data.msg,
            data: {username, avatar, ...rest}
        }
    } else {
        payload = {
            hasError: true,
zhanghaozhe committed
36 37
            msg: data.msg,
            data: {}
zhanghaozhe committed
38 39 40 41 42
        }
    }
    dispatch(setCurrentUser(payload))
    return payload

43 44 45 46 47 48
}


const SET_CURRENT_USER = 'SET_CURRENT_USER'
const setCurrentUser = payload => ({
    type: SET_CURRENT_USER,
zhanghaozhe committed
49
    payload: {...payload, isFetching: false}
50 51 52 53 54
})


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


61 62 63 64 65 66
const UPDATE_USER = 'UPDATE_USER'
const updateUser = payload => ({
    type: UPDATE_USER,
    payload
})

zhanghaozhe committed
67 68 69 70 71 72
const START_FETCH_USER = 'START_FETCH_USER'
const startFetchUser = () => ({
    type: START_FETCH_USER
})


73 74 75
export {
    accountLogin,
    SET_CURRENT_USER,
zhanghaozhe committed
76
    setCurrentUser,
77 78 79
    quickLogin,
    logout,
    UPDATE_USER,
zhanghaozhe committed
80 81 82
    updateUser,
    START_FETCH_USER,
    startFetchUser,
83
}