123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import storage from "../utils/storage";
- import Api from "./api";
- import Route from "./route";
- import {liveSources} from "./enum";
- import {showLoading} 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');
- if (!user) {
- // 如果没有user信息,将token也删除
- storage.clearItem('token');
- }
- if(user && user.isLiveGuest) {
- //如果是直播游客(未实际登录的)
- return false;
- }
- 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;
- if (user.wechatOpenId && !user.msWechatOpenId) {
- // 如果绑定了微信,但是发送消息的openid是空的,执行绑定微信方法 -- 对应在app中完成微信绑定的情况
- this.bindWechat();
- }
- }
- return storage.setItem('user', user);
- }
- static async gotoLive(id) {
- console.log(`分享进来的id${id}`)
- return new Promise(async (resolve, reject) => {
- // const {data: courseInfo} = await Api.getCourseInfo(id);
- // if (!courseInfo) {
- // toast('课程不存在!');
- // resolve(true);
- // return;
- // }
- // if (courseInfo.liveStatus == liveStatus.NOLIVE) {
- // toast('课程还未开始直播');
- // resolve(true);
- // return;
- // } else if (courseInfo.liveStatus == liveStatus.LIVEEND) {
- // toast('直播已结束!');
- // resolve(true);
- // return;
- // }
- // const isLogin = await this.isLogin();
- // // console.log('isLogin ->', isLogin);
- // if(!isLogin) {
- // resolve('NOT_LOGIN');
- // return;
- // }
- //判断学员是否为在直播课班级内的
- const res = await Api.getUserLiveId(true);
- if (res.data && res.data.length > 0) {
- const schedule = res.data.find(item => {
- return item.scheduleId == id;
- });
- // console.log('schedule --> ', schedule);
- if (schedule) {
- Route.toLive(liveSources.DEFAULT,
- schedule.courseCeremoneyName,
- id, schedule.eStudentId, schedule.eduStuName,schedule.courseThumbnailUrl)
- resolve(true);
- return;
- }
- }
- const user = await User.getUserInfoByLocal();
- // console.log('user -> ', user);
- Route.toLive(liveSources.GUEST,
- "分享直播",
- id, user.id, user.name,"")
- resolve(true);
- })
- }
- }
- export default User
|