import request from '../Utils/RequestUtils' import { addAsyncStorage, removeAsyncStorage } from '../Utils/AsyncStorageUtils' import submitPhone from '../Utils/FormUtils' import Toast from './Toast' const app = (now) => ({ id: null, userInfo: {}, getUser() { return request .get('/user/my') .then((res) => { now({ id: res.id, userInfo: res, }) }) .catch((e) => { now({ id: 0, }) return Promise.reject(e) }) }, loginByPsd(phone, password) { const { loading, warnning, success } = now(Toast) loading() return request .post('/auth/login', { data: { username: submitPhone(phone), password, }, requestType: 'form', }) .then((res) => { return addAsyncStorage('token', res) }) .then(() => { const { getUser } = now() return getUser() }) .then(() => { success('登录成功') }) .catch((e) => { warnning(e.error) }) }, logout() { const { loading, success } = now(Toast) loading() // 移除 token return removeAsyncStorage('token').then(() => { // 清除用户信息 now({ id: 0, userInfo: {} }) success('退出成功') }) }, }) export default app