import Api from '../../model/api' import User from '../../model/user' import { getDataSet, getEventParam, toast } from "../../utils/utils"; import { htmlTypes, liveSources, videoTypes, liveStatus } from "../../model/enum"; import Route from "../../model/route"; import storage from '../../utils/storage'; const app = getApp(); app.Base({ data: { id: "", //用来区分是否是通过卡片分享进行来的用户,当id存在值的时候是guest homeData: { notice: { noticeContent: '' }, carouselList: [], classInfo: { opened: 0, notYetOpened: 0, history: 0, }, dictList: [], }, tabs: [{ title: "直播课程", icon: '/images/tab_live.png' }, { title: "教材阅读", icon: '/images/tab_book.png', url: "/pages/book/book" }, { title: "精品课程", icon: '/images/tab_course.png', url: "/pages/course/course" }, { title: "社区交流", icon: '/images/tab_exchange.png' } ], active: 0, dictType: '', dictValue: '', showLiveList: false, actions: [], //等待跳转直播页面的结果,加入该变量解决在本页面登录成功后跳转直播页面多(两)次问题 waitToLive: false, // showLiveDot: false, liveCourseCount: 0, showNickNameDialog: false, //显示昵称填写弹窗 userNickName: '', //用户设置的昵称 wxOpenId: '', //微信openId handleBeforeClose: (action) => { return new Promise((resolve, reject) => { if (action === 'confirm') { resolve(false); } else { resolve(true) } }) }, }, async onLoad(options) { const { id, guest } = options; if (guest == 'true') { this.id = id; } const res = await Api.getIndex(); const dictType = res.data.dictList[0].dictType; const dictValue = res.data.dictList[0].dictValue; this.setData({ homeData: res.data, dictType, dictValue }) this.selectComponent('#tabs').resize(); }, async setUserLiveCourseCount() { let isLogin = User.isLogin(); let liveCourseCount = 0; if (isLogin) { //若用户已经登录,查询所在班级正在直播的课程 const res = await Api.getUserLiveId(true); // let showLiveDot = true; if (res.data && res.data.length > 0) { // showLiveDot = true; liveCourseCount = res.data.length; } } console.log(liveCourseCount); this.setData({ // showLiveDot liveCourseCount }); }, async handleNickNameConfirm() { if (!this.data.userNickName || !this.data.userNickName.trim()) { toast('请填写昵称!'); return; } //设置昵称(通过接口) let guestUser = { id: `${this.data.wxOpenId}`, name: this.data.userNickName, } // 将数据存入redis中 // await Api.insertRedisGuest(guestUser); // 将数据存入小程序缓存中 this.setGuestUserStorage(guestUser); this.setData({ showNickNameDialog: false }); // this.setGuestUserStorage(guestUser); wx.nextTick(() => { this.jumpToLive(guestUser); }); }, hideNicknameDialog() { this.setData({ userNickName: '', showNickNameDialog: false }); }, onUnload() { this.hideNicknameDialog(); }, onHide() { this.hideNicknameDialog(); }, async checkLiveCourseStatus(id) { const { data: courseInfo } = await Api.getCourseInfo(id); if (!courseInfo) { toast('课程不存在!'); // resolve(true); return false; } if (courseInfo.liveStatus == liveStatus.NOLIVE) { toast('课程还未开始直播'); // resolve(true); return false; } else if (courseInfo.liveStatus == liveStatus.LIVEEND) { toast('直播已结束!'); // resolve(true); return false; } return true; }, async onShow() { // console.log(' trigger index page onShow '); // console.log('onShow -> before ==> this.id = ', this.id); this.setUserLiveCourseCount(); if (!this.id) { //如果不是通过直播分享进来的,不继续执行以下逻辑 return; } let checkCourseFlag = await this.checkLiveCourseStatus(this.id); if (!checkCourseFlag) { //如果课程不再直播过程中 this.id = ""; return; } const isLogin = User.isLogin(); //如果直播还在继续 if (isLogin) { //若用户已经登录 await User.gotoLive(this.id); this.id = ""; return; } //如果用户未登录 const user = await User.getUserInfoByLocal(); if (user && user.id && user.name) { //如果本地缓存中有用户的id和昵称信息,直接跳转直播页面 this.jumpToLive(user); } else { // 如果缓存中没有用户信息,获取用户的openid作为进入直播间的用户id并让用户设置昵称 let openId = this.data.wxOpenId; if (!openId) { const data = await wx.login(); //获取openid let res = await Api.getWXOpenId(data.code, true); openId = res.data.openId; } //查询openid是否已经绑定过昵称,已填写用之前填写的昵称 // let { // data: nickName // } = await Api.queryGuestName(openId); //从缓存中查询用户是否填写过昵称 let nickName = user && user.name ? user.name : ''; // let nickName = user.nickName; if (!nickName) { //如果未设置过昵称,弹出页面提示填写昵称 this.setData({ wxOpenId: openId, showNickNameDialog: true }); return; } //如果用户设置过昵称,将用户信息记录在缓存中,并跳转到直播页 let guestUser = { id: openId, name: nickName }; this.setGuestUserStorage(guestUser); this.jumpToLive(guestUser); } // 原来直播需跳转登录页的逻辑 start // await User.checkLogin(); //游客如果在登录页点了返回,会一直触发该函数(返回就重新跳转到登录页) 这里需要注意,如果以后需要修改为只触发一次,把它移动到下面的if语句中 // if (!this.waitToLive) { // this.waitToLive = true; // // console.log('executed checkLogin!!'); // // await User.checkLogin(); //游客如果一直不登录,会一直触发该函数 这里需要注意,如果以后需要修改为只触发一次,去掉注释 // let flag = await User.gotoLive(this.id).finally(() => { // this.waitToLive = false; // }); // if (flag) { // this.id = ""; // // console.log('onShow -> after ==> this.id = ', this.id); // } // } // 直播需登录 end // let status = await User.gotoLive(this.id); // if (status === 'NOT_LOGIN') { // //如果未登录 // const user = await User.getUserInfoByLocal(); // if (user && user.id && user.name) { // //如果本地缓存中有用户的id和昵称信息,直接跳转直播页面 // this.jumpToLive(user); // } else { // // 如果缓存中没有用户信息,获取用户的openid并让用户设置昵称 // let openId = this.data.wxOpenId; // if (!openId) { // const data = await wx.login(); // //获取openid // let res = await Api.getWXOpenId(data.code, true); // openId = res.data.openId; // } // //查询openid是否已经绑定过昵称,已填写用之前填写的昵称 // let { // data: nickName // } = await Api.queryGuestName(openId); // if (!nickName) { // //如果未设置过昵称,弹出页面提示填写昵称 // this.setData({ // wxOpenId: openId, // showNickNameDialog: true // }); // return; // } // //如果用户设置过昵称,将用户信息记录在缓存中,并跳转到直播页 // let guestUser = { // id: openId, // name: nickName // }; // this.setGuestUserStorage(guestUser); // this.jumpToLive(guestUser); // } // } else { // //将分享id 置空, 阻止页面多次跳转直播 // this.id = ""; // } }, setGuestUserStorage({ id, name }) { storage.setItem("user", { id, name, isLiveGuest: true }); }, jumpToLive(user) { Route.toLive(liveSources.GUEST, "分享直播", this.id, user.id, user.name, ""); this.id = ""; }, handleNickNameInput(e) { let { value } = e.detail; // console.log('value -> ', value); this.data.userNickName = value; }, async toNotice(e) { await User.checkLogin(); Route.toNotice() }, toBannerDetail(e) { const item = getEventParam(e, "item"); Route.toNews(htmlTypes.BANNER, item.id, item.articleTitle, "") }, async toClickGrid(e) { const title = getDataSet(e, "title"); if ('直播课程' == title) { await this.findLive(); // Route.toLive(liveSources.GUEST, // "分享直播", // "3", "333", "user.name","") } else if ('社区交流' == title) { await User.checkLogin(); Route.tocCommunity() } }, toNewsDetail(e) { const item = getDataSet(e, "item"); const title = getDataSet(e, "title"); if (item.isNews) { Route.toNews(htmlTypes.NEWS, item.id, item.title, "") } else { Route.toVideo(item.id, videoTypes.COMMON, title) } }, toSchoolIntrouce() { Route.toSchoolIntrouce(); }, async onChange(e) { const index = getEventParam(e, "index"); const dictType = this.data.homeData.dictList[index].dictType; const dictValue = this.data.homeData.dictList[index].dictValue; this.setData({ active: index, dictType, dictValue }, async () => { this.changeResetData(this); }) }, async requestData() { const res = await Api.getNewsList({ dictType: this.data.dictType, dictValue: this.data.dictValue, pageNum: this.pageNum, pageSize: this.pageSize }); res.data.rows = res.data.rows.map(item => { return { title: item.articleTitle || item.videoName, visitedCount: item.visitedCount, cover: item.articleImg || item.videoImg, id: item.id, isNews: !!item.articleTitle } }); return res; }, async onPullDownRefresh() { const res = await Api.getIndex(true); const dictType = res.data.dictList[0].dictType; const dictValue = res.data.dictList[0].dictValue; this.setData({ homeData: res.data, dictType, dictValue, active: 0 }) this.selectComponent('#tabs').resize(); this.setUserLiveCourseCount(); }, async findLive() { await User.checkLogin(); const res = await Api.getUserLiveId(true); if (res.data && res.data.length > 0) { if (res.data.length == 1) { let item = res.data[0]; let id = item.scheduleId; let eId = item.eStudentId; let eName = item.eduStuName; Route.toLive(liveSources.DEFAULT, item.courseCeremoneyName, id, eId, eName, item.courseThumbnailUrl) } else { this.setData({ actions: res.data.map(item => { item.name = `${item.teamName}-${item.courseCeremoneyName}`; return item; }), showLiveList: true }) } } else { toast("没有进行的直播") } }, onClose() { this.setData({ showLiveList: false }); }, async onSelect(e) { const scheduleId = getEventParam(e, "scheduleId"); const eStudentId = getEventParam(e, "eStudentId"); const eduStuName = getEventParam(e, "eduStuName"); const courseCeremoneyName = getEventParam(e, "courseCeremoneyName"); const courseThumbnailUrl = getEventParam(e, "courseThumbnailUrl"); Route.toLive(liveSources.DEFAULT, courseCeremoneyName, scheduleId, eStudentId, eduStuName, courseThumbnailUrl); }, // 页面被用户分享时执行 onShareAppMessage() {} })