userAction.js 2.78 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 => {
FE committed
7
    return http.post(`${API['passport-api']}/m/login/accountLogin`, {
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
    })
}

FE committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
const studentLogin = params => dispatch => {
    return http.post(`${API['passport-api']}/phone_reg`, {
        phone: params.tel,
        password: params.password,
        code: params.code,
        area_code: params.num,
        school_name:  params.school,
        college_name: params.college,
        student_id: params.uid,
        student_name: params.name,
        redirect: encodeURIComponent(params.redirect),
        type: 1,
    }).then(res => {
        const { errno, data } = res.data;
        let result = {};
        if(errno === 0) {
            result = {
                data: {
                    errno: 200,
                    data,
                }
            };
        }else {
            result = res;
        }
        return storeUser(result, dispatch);
    });
}

zhanghaozhe committed
46
const quickLogin = user => dispatch => {
FE committed
47
    return http.post(`${API['passport-api']}/m/login/quickLogin`, {
FE committed
48 49 50 51 52
        ...user,
        plat: 5
    }).then(res => {
        return storeUser(res, dispatch)
    })
zhanghaozhe committed
53 54 55 56 57
}

const storeUser = (res, dispatch) => {
    const data = res.data
    let payload
FE committed
58
    if (data.errno === 200) {
xuzhenghua committed
59
        const {user_name: username, avatar_file: avatar,is_vip: isVIP, ...rest} = data.data.user_info
zhanghaozhe committed
60 61 62
        payload = {
            hasError: false,
            msg: data.msg,
xuzhenghua committed
63
            data: {username, avatar,isVIP, ...rest}
zhanghaozhe committed
64 65 66 67
        }
    } else {
        payload = {
            hasError: true,
zhanghaozhe committed
68
            msg: data.msg,
zhanghaozhe committed
69 70
            data: {},
            code: data.errno
zhanghaozhe committed
71 72 73 74 75
        }
    }
    dispatch(setCurrentUser(payload))
    return payload

76 77 78 79 80 81
}


const SET_CURRENT_USER = 'SET_CURRENT_USER'
const setCurrentUser = payload => ({
    type: SET_CURRENT_USER,
zhanghaozhe committed
82
    payload: {...payload, isFetching: false}
83 84 85 86 87
})


const LOGOUT = 'LOGOUT'
const logout = () => dispatch => {
zhanghaozhe committed
88 89
    jsCookie.remove('token', {path: '/', domain: '.julyedu.com'})
    jsCookie.remove('uid', {path: '/', domain: '.julyedu.com'})
zhanghaozhe committed
90
    dispatch(setCurrentUser({hasError: true}))
91 92 93
}


94 95 96 97 98 99
const UPDATE_USER = 'UPDATE_USER'
const updateUser = payload => ({
    type: UPDATE_USER,
    payload
})

zhanghaozhe committed
100 101 102 103 104 105
const START_FETCH_USER = 'START_FETCH_USER'
const startFetchUser = () => ({
    type: START_FETCH_USER
})


106 107
export {
    accountLogin,
FE committed
108
    studentLogin,
109
    SET_CURRENT_USER,
zhanghaozhe committed
110
    setCurrentUser,
111 112 113
    quickLogin,
    logout,
    UPDATE_USER,
zhanghaozhe committed
114 115 116
    updateUser,
    START_FETCH_USER,
    startFetchUser,
117
}