index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import Api from '../../model/api'
  2. import User from '../../model/user'
  3. import {
  4. getDataSet,
  5. getEventParam,
  6. toast
  7. } from "../../utils/utils";
  8. import {
  9. htmlTypes,
  10. liveSources,
  11. videoTypes,
  12. liveStatus
  13. } from "../../model/enum";
  14. import Route from "../../model/route";
  15. import storage from '../../utils/storage';
  16. const app = getApp();
  17. app.Base({
  18. data: {
  19. id: "", //用来区分是否是通过卡片分享进行来的用户,当id存在值的时候是guest
  20. homeData: {
  21. notice: {
  22. noticeContent: ''
  23. },
  24. carouselList: [],
  25. classInfo: {
  26. opened: 0,
  27. notYetOpened: 0,
  28. history: 0,
  29. },
  30. dictList: [],
  31. },
  32. tabs: [{
  33. title: "直播课程",
  34. icon: '/images/tab_live.png'
  35. },
  36. {
  37. title: "教材阅读",
  38. icon: '/images/tab_book.png',
  39. url: "/pages/book/book"
  40. },
  41. {
  42. title: "精品课程",
  43. icon: '/images/tab_course.png',
  44. url: "/pages/course/course"
  45. },
  46. {
  47. title: "社区交流",
  48. icon: '/images/tab_exchange.png'
  49. }
  50. ],
  51. active: 0,
  52. dictType: '',
  53. dictValue: '',
  54. showLiveList: false,
  55. actions: [],
  56. //等待跳转直播页面的结果,加入该变量解决在本页面登录成功后跳转直播页面多(两)次问题
  57. waitToLive: false,
  58. // showLiveDot: false,
  59. liveCourseCount: 0,
  60. showNickNameDialog: false, //显示昵称填写弹窗
  61. userNickName: '', //用户设置的昵称
  62. wxOpenId: '', //微信openId
  63. handleBeforeClose: (action) => {
  64. return new Promise((resolve, reject) => {
  65. if (action === 'confirm') {
  66. resolve(false);
  67. } else {
  68. resolve(true)
  69. }
  70. })
  71. },
  72. },
  73. async onLoad(options) {
  74. const {
  75. id,
  76. guest
  77. } = options;
  78. if (guest == 'true') {
  79. this.id = id;
  80. }
  81. const res = await Api.getIndex();
  82. const dictType = res.data.dictList[0].dictType;
  83. const dictValue = res.data.dictList[0].dictValue;
  84. this.setData({
  85. homeData: res.data,
  86. dictType,
  87. dictValue
  88. })
  89. this.selectComponent('#tabs').resize();
  90. },
  91. async setUserLiveCourseCount() {
  92. let isLogin = User.isLogin();
  93. let liveCourseCount = 0;
  94. if (isLogin) {
  95. //若用户已经登录,查询所在班级正在直播的课程
  96. const res = await Api.getUserLiveId(true);
  97. // let showLiveDot = true;
  98. if (res.data && res.data.length > 0) {
  99. // showLiveDot = true;
  100. liveCourseCount = res.data.length;
  101. }
  102. }
  103. console.log(liveCourseCount);
  104. this.setData({
  105. // showLiveDot
  106. liveCourseCount
  107. });
  108. },
  109. async handleNickNameConfirm() {
  110. if (!this.data.userNickName || !this.data.userNickName.trim()) {
  111. toast('请填写昵称!');
  112. return;
  113. }
  114. //设置昵称(通过接口)
  115. let guestUser = {
  116. id: `${this.data.wxOpenId}`,
  117. name: this.data.userNickName,
  118. }
  119. // 将数据存入redis中
  120. // await Api.insertRedisGuest(guestUser);
  121. // 将数据存入小程序缓存中
  122. this.setGuestUserStorage(guestUser);
  123. this.setData({
  124. showNickNameDialog: false
  125. });
  126. // this.setGuestUserStorage(guestUser);
  127. wx.nextTick(() => {
  128. this.jumpToLive(guestUser);
  129. });
  130. },
  131. hideNicknameDialog() {
  132. this.setData({
  133. userNickName: '',
  134. showNickNameDialog: false
  135. });
  136. },
  137. onUnload() {
  138. this.hideNicknameDialog();
  139. },
  140. onHide() {
  141. this.hideNicknameDialog();
  142. },
  143. async checkLiveCourseStatus(id) {
  144. const {
  145. data: courseInfo
  146. } = await Api.getCourseInfo(id);
  147. if (!courseInfo) {
  148. toast('课程不存在!');
  149. // resolve(true);
  150. return false;
  151. }
  152. if (courseInfo.liveStatus == liveStatus.NOLIVE) {
  153. toast('课程还未开始直播');
  154. // resolve(true);
  155. return false;
  156. } else if (courseInfo.liveStatus == liveStatus.LIVEEND) {
  157. toast('直播已结束!');
  158. // resolve(true);
  159. return false;
  160. }
  161. return true;
  162. },
  163. async onShow() {
  164. // console.log(' trigger index page onShow ');
  165. // console.log('onShow -> before ==> this.id = ', this.id);
  166. this.setUserLiveCourseCount();
  167. if (!this.id) {
  168. //如果不是通过直播分享进来的,不继续执行以下逻辑
  169. return;
  170. }
  171. let checkCourseFlag = await this.checkLiveCourseStatus(this.id);
  172. if (!checkCourseFlag) {
  173. //如果课程不再直播过程中
  174. this.id = "";
  175. return;
  176. }
  177. const isLogin = User.isLogin();
  178. //如果直播还在继续
  179. if (isLogin) {
  180. //若用户已经登录
  181. await User.gotoLive(this.id);
  182. this.id = "";
  183. return;
  184. }
  185. //如果用户未登录
  186. const user = await User.getUserInfoByLocal();
  187. if (user && user.id && user.name) {
  188. //如果本地缓存中有用户的id和昵称信息,直接跳转直播页面
  189. this.jumpToLive(user);
  190. } else {
  191. // 如果缓存中没有用户信息,获取用户的openid作为进入直播间的用户id并让用户设置昵称
  192. let openId = this.data.wxOpenId;
  193. if (!openId) {
  194. const data = await wx.login();
  195. //获取openid
  196. let res = await Api.getWXOpenId(data.code, true);
  197. openId = res.data.openId;
  198. }
  199. //查询openid是否已经绑定过昵称,已填写用之前填写的昵称
  200. // let {
  201. // data: nickName
  202. // } = await Api.queryGuestName(openId);
  203. //从缓存中查询用户是否填写过昵称
  204. let nickName = user && user.name ? user.name : '';
  205. // let nickName = user.nickName;
  206. if (!nickName) {
  207. //如果未设置过昵称,弹出页面提示填写昵称
  208. this.setData({
  209. wxOpenId: openId,
  210. showNickNameDialog: true
  211. });
  212. return;
  213. }
  214. //如果用户设置过昵称,将用户信息记录在缓存中,并跳转到直播页
  215. let guestUser = {
  216. id: openId,
  217. name: nickName
  218. };
  219. this.setGuestUserStorage(guestUser);
  220. this.jumpToLive(guestUser);
  221. }
  222. // 原来直播需跳转登录页的逻辑 start
  223. // await User.checkLogin(); //游客如果在登录页点了返回,会一直触发该函数(返回就重新跳转到登录页) 这里需要注意,如果以后需要修改为只触发一次,把它移动到下面的if语句中
  224. // if (!this.waitToLive) {
  225. // this.waitToLive = true;
  226. // // console.log('executed checkLogin!!');
  227. // // await User.checkLogin(); //游客如果一直不登录,会一直触发该函数 这里需要注意,如果以后需要修改为只触发一次,去掉注释
  228. // let flag = await User.gotoLive(this.id).finally(() => {
  229. // this.waitToLive = false;
  230. // });
  231. // if (flag) {
  232. // this.id = "";
  233. // // console.log('onShow -> after ==> this.id = ', this.id);
  234. // }
  235. // }
  236. // 直播需登录 end
  237. // let status = await User.gotoLive(this.id);
  238. // if (status === 'NOT_LOGIN') {
  239. // //如果未登录
  240. // const user = await User.getUserInfoByLocal();
  241. // if (user && user.id && user.name) {
  242. // //如果本地缓存中有用户的id和昵称信息,直接跳转直播页面
  243. // this.jumpToLive(user);
  244. // } else {
  245. // // 如果缓存中没有用户信息,获取用户的openid并让用户设置昵称
  246. // let openId = this.data.wxOpenId;
  247. // if (!openId) {
  248. // const data = await wx.login();
  249. // //获取openid
  250. // let res = await Api.getWXOpenId(data.code, true);
  251. // openId = res.data.openId;
  252. // }
  253. // //查询openid是否已经绑定过昵称,已填写用之前填写的昵称
  254. // let {
  255. // data: nickName
  256. // } = await Api.queryGuestName(openId);
  257. // if (!nickName) {
  258. // //如果未设置过昵称,弹出页面提示填写昵称
  259. // this.setData({
  260. // wxOpenId: openId,
  261. // showNickNameDialog: true
  262. // });
  263. // return;
  264. // }
  265. // //如果用户设置过昵称,将用户信息记录在缓存中,并跳转到直播页
  266. // let guestUser = {
  267. // id: openId,
  268. // name: nickName
  269. // };
  270. // this.setGuestUserStorage(guestUser);
  271. // this.jumpToLive(guestUser);
  272. // }
  273. // } else {
  274. // //将分享id 置空, 阻止页面多次跳转直播
  275. // this.id = "";
  276. // }
  277. },
  278. setGuestUserStorage({
  279. id,
  280. name
  281. }) {
  282. storage.setItem("user", {
  283. id,
  284. name,
  285. isLiveGuest: true
  286. });
  287. },
  288. jumpToLive(user) {
  289. Route.toLive(liveSources.GUEST,
  290. "分享直播",
  291. this.id, user.id, user.name, "");
  292. this.id = "";
  293. },
  294. handleNickNameInput(e) {
  295. let {
  296. value
  297. } = e.detail;
  298. // console.log('value -> ', value);
  299. this.data.userNickName = value;
  300. },
  301. async toNotice(e) {
  302. await User.checkLogin();
  303. Route.toNotice()
  304. },
  305. toBannerDetail(e) {
  306. const item = getEventParam(e, "item");
  307. Route.toNews(htmlTypes.BANNER, item.id, item.articleTitle, "")
  308. },
  309. async toClickGrid(e) {
  310. const title = getDataSet(e, "title");
  311. if ('直播课程' == title) {
  312. await this.findLive();
  313. // Route.toLive(liveSources.GUEST,
  314. // "分享直播",
  315. // "3", "333", "user.name","")
  316. } else if ('社区交流' == title) {
  317. await User.checkLogin();
  318. Route.tocCommunity()
  319. }
  320. },
  321. toNewsDetail(e) {
  322. const item = getDataSet(e, "item");
  323. const title = getDataSet(e, "title");
  324. if (item.isNews) {
  325. Route.toNews(htmlTypes.NEWS, item.id, item.title, "")
  326. } else {
  327. Route.toVideo(item.id, videoTypes.COMMON, title)
  328. }
  329. },
  330. toSchoolIntrouce() {
  331. Route.toSchoolIntrouce();
  332. },
  333. async onChange(e) {
  334. const index = getEventParam(e, "index");
  335. const dictType = this.data.homeData.dictList[index].dictType;
  336. const dictValue = this.data.homeData.dictList[index].dictValue;
  337. this.setData({
  338. active: index,
  339. dictType,
  340. dictValue
  341. }, async () => {
  342. this.changeResetData(this);
  343. })
  344. },
  345. async requestData() {
  346. const res = await Api.getNewsList({
  347. dictType: this.data.dictType,
  348. dictValue: this.data.dictValue,
  349. pageNum: this.pageNum,
  350. pageSize: this.pageSize
  351. });
  352. res.data.rows = res.data.rows.map(item => {
  353. return {
  354. title: item.articleTitle || item.videoName,
  355. visitedCount: item.visitedCount,
  356. cover: item.articleImg || item.videoImg,
  357. id: item.id,
  358. isNews: !!item.articleTitle
  359. }
  360. });
  361. return res;
  362. },
  363. async onPullDownRefresh() {
  364. const res = await Api.getIndex(true);
  365. const dictType = res.data.dictList[0].dictType;
  366. const dictValue = res.data.dictList[0].dictValue;
  367. this.setData({
  368. homeData: res.data,
  369. dictType,
  370. dictValue,
  371. active: 0
  372. })
  373. this.selectComponent('#tabs').resize();
  374. this.setUserLiveCourseCount();
  375. },
  376. async findLive() {
  377. await User.checkLogin();
  378. const res = await Api.getUserLiveId(true);
  379. if (res.data && res.data.length > 0) {
  380. if (res.data.length == 1) {
  381. let item = res.data[0];
  382. let id = item.scheduleId;
  383. let eId = item.eStudentId;
  384. let eName = item.eduStuName;
  385. Route.toLive(liveSources.DEFAULT,
  386. item.courseCeremoneyName,
  387. id, eId, eName, item.courseThumbnailUrl)
  388. } else {
  389. this.setData({
  390. actions: res.data.map(item => {
  391. item.name = `${item.teamName}-${item.courseCeremoneyName}`;
  392. return item;
  393. }),
  394. showLiveList: true
  395. })
  396. }
  397. } else {
  398. toast("没有进行的直播")
  399. }
  400. },
  401. onClose() {
  402. this.setData({
  403. showLiveList: false
  404. });
  405. },
  406. async onSelect(e) {
  407. const scheduleId = getEventParam(e, "scheduleId");
  408. const eStudentId = getEventParam(e, "eStudentId");
  409. const eduStuName = getEventParam(e, "eduStuName");
  410. const courseCeremoneyName = getEventParam(e, "courseCeremoneyName");
  411. const courseThumbnailUrl = getEventParam(e, "courseThumbnailUrl");
  412. Route.toLive(liveSources.DEFAULT, courseCeremoneyName,
  413. scheduleId, eStudentId, eduStuName, courseThumbnailUrl);
  414. },
  415. // 页面被用户分享时执行
  416. onShareAppMessage() {}
  417. })