import storage from "../utils/storage"; import Api from "./api"; import Route from "./route"; import {liveSources} from "./enum"; import {showLoading, toast} from "../utils/utils"; const app = getApp(); class User { static async getUserSign(userId) { const res = await Api.getSig({userId}); return res.data } static async loginByPhone({phone, verify}) { showLoading(); const res = await Api.login(phone, verify); app.globalData.token = res.data.access_token; storage.setItem("token", app.globalData.token) await this.updateUserInfo(); wx.hideLoading(); } static async loginByWechat() { showLoading(); const data = await wx.login(); const res = await Api.loginByWX(data.code); app.globalData.token = res.data.access_token; storage.setItem("token", app.globalData.token) await this.updateUserInfo(); wx.hideLoading(); } static async bindWechat() { const data = await wx.login(); await Api.binding(data.code); } static isLogin() { const user = storage.getItem('user'); return !!user; } static async checkLogin() { return new Promise((resolve, reject) => { if (this.isLogin()) { resolve(); } else { Route.toLogin({ login: ({isLogin}) => { if (isLogin) { resolve(); } else { reject(); } } }) } }); } static async logout() { storage.clearItem('token'); storage.clearItem('user'); app.globalData.token = ""; try { await Api.appLogout(); return true; } catch (e) { console.log(e) return false; } } static getUserInfoByLocal() { return storage.getItem('user'); } static async updateUserInfo(user) { if (!user) { const res = await Api.getUserInfo(); user = res.data.student; } return storage.setItem('user', user); } static async checkLive(id) { console.log(`分享进来的id${id}`) return new Promise(async (resolve, reject) => { if (this.isLogin()) { try { const res = await Api.getUserLiveId(true); if (res.data && res.data.length > 0) { const schedule = res.data.find(item => { return item.scheduleId == id; }) if (schedule) { await Route.toLive(liveSources.DEFAULT, schedule.courseCeremoneyName, id, schedule.eStudentId, schedule.eduStuName) return; } } } catch (e) { toast("分享异常") console.log("分享卡片进入获取直播接口有异常情况", e) } const user = User.getUserInfoByLocal(); await Route.toLive(liveSources.GUEST, "分享直播", id, user.id, user.name) resolve(true); } else { Route.toLogin({ login: ({isLogin}) => { resolve(isLogin); } }) } }) } } export default User