1234567891011121314151617181920212223242526272829303132333435 |
- import { login } from '../api/user'
- import { FETCH_AND_FORMAT_USER_INFO } from './util'
- // 检查是否登录
- export function isLoggedIn() {
- const token = wx.getStorageSync('token')
- const expires = wx.getStorageSync('expires_in')
- return token && expires && expires > Date.now()
- }
- // 调用登录
- export async function doLogin({ code, phone, nickname, avatar }) {
- wx.showLoading({
- title: '登录中...',
- mask: true
- });
- try {
- const res = await login({
- code,
- phone,
- nickname,
- avatar
- })
- wx.setStorageSync('token', res.data.token_type + ' ' + res.data.access_token)
- wx.setStorageSync('expires_in', Date.now() + ((res.data.expires_in - 10) * 1000))
- FETCH_AND_FORMAT_USER_INFO()
- } catch (error) {
- wx.showToast({
- title: '登录失败',
- icon: 'none',
- duration: 2000
- });
- } finally{
- wx.hideLoading();
- }
- }
|