123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- 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() {}
- })
|