import { fetchLoginPW, fetchUserInfo, fetchUserBaseInfo, } from '../services/usercenter/fetchUser'; import { loadConfig } from '../behaviors/loadConfig'; import dayjs from 'dayjs'; const app = getApp(); export const useLogin = Behavior({ methods: { loginPw(accessTokenPW) { wx.showLoading({ title: '登录中', mask: true, }); setTimeout(() => { wx.hideLoading(); }, 10000); var that = this; wx.login({ success: res => { accessTokenPW.code = res.code; fetchLoginPW(accessTokenPW).then(data => { wx.hideLoading(); if (data.success) { wx.getStorage({ key: 'accessToken', success(res) { app.globalData.accessToken = res.data; app.globalData.accessToken.userId = data.userId; app.globalData.accessToken.suserId = data.suserId; app.globalData.accessToken.openId = data.openId; if (accessTokenPW.userName) { app.globalData.accessToken.userName = accessTokenPW.userName; } if (accessTokenPW.password) { app.globalData.accessToken.password = accessTokenPW.password; } wx.setStorage({ key: 'accessToken', data: app.globalData.accessToken, }); that.gotoHomePage(); }, fail(e) { app.globalData.accessToken = { userName: accessTokenPW.userName, password: accessTokenPW.password, userId: data.userId, suserId: data.suserId, openId: data.openId, }; wx.setStorage({ key: 'accessToken', data: app.globalData.accessToken, }); that.gotoHomePage(); }, }); } else { that._onLoginFail(); } }); }, }); }, gotoHomePage() { fetchUserBaseInfo(app.globalData.accessToken.userId).then(res => { app.globalData.baseInfo = res.data; }); fetchUserInfo(app.globalData.accessToken.userId).then(data => { if (!this.isAdmin(data.usertypeid)) { wx.showToast({ title: '账户权限不足', duration: 1000, icon: 'none', }); this.setData({ loading: false, }); return; } if (data.uiCreateTime) { const registerTime = dayjs(data.uiCreateTime); const now = dayjs(); app.globalData.newUser = now.diff(registerTime, 'months') <= 1; } app.globalData.userInfo = data; app.globalData.userInfo.name = data.realname; app.globalData.userInfo.district = data.extension1; app.globalData.isLogin = true; wx.setStorage({ key: 'userInfo', data: data, success: result => { loadConfig().then(res => { wx.switchTab({ url: '/pages/home/index', }); }); }, }); }); }, isAdmin(userTypeId) { return userTypeId <= 2; }, }, });