userAction.js 2.64 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 8 9 10 11 12 13 14
  return http.post(`${API['passport-api']}/m/login/accountLogin`, {
    user_name: user.username,
    password: encrypt(user.password),
    is_encrypt: 1,
    redirect: encodeURIComponent(user.redirect),
  }).then(res => {
    return storeUser(res, dispatch)
  })
15 16
}

FE committed
17
const studentLogin = params => dispatch => {
zhanghaozhe committed
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
  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);
  });
FE committed
44 45
}

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

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

76 77 78 79 80
}


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


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


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

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

const END_FETCH_USER = 'END_FETCH_USER'
const endFetchUser = () => ({
  type: END_FETCH_USER,
zhanghaozhe committed
108 109 110
})


111
export {
zhanghaozhe committed
112 113 114 115 116 117 118 119 120 121 122 123
  accountLogin,
  studentLogin,
  SET_CURRENT_USER,
  setCurrentUser,
  quickLogin,
  logout,
  UPDATE_USER,
  updateUser,
  START_FETCH_USER,
  startFetchUser,
  END_FETCH_USER,
  endFetchUser,
124
}