userAction.js 1.92 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 => {
zhanghaozhe committed
18
    return http.post(`${API['passport-api']}/quick_login`, user)
zhanghaozhe committed
19 20 21 22 23 24 25 26 27
        .then(res => {
            return storeUser(res, dispatch)
        })
}

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

44 45 46 47 48 49
}


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


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


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

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


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