123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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
|