index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. //index.js
  2. //获取应用实例
  3. // 获取授权登录
  4. import {
  5. getOpenidSessionKey
  6. } from '../../utils/httpUtil';
  7. // 不用获取授权登录
  8. import {
  9. getOpenidNoLogin
  10. } from '../../utils/httpUtilNoLogin';
  11. import {
  12. formatDate
  13. } from '../../utils/util';
  14. const app = getApp()
  15. import {
  16. hotInit
  17. } from '../../component/hot/hot';
  18. import {
  19. groupInit
  20. } from '../../component/group/group';
  21. import {
  22. myInit
  23. } from '../../component/my/my';
  24. import httpRequestApi from '../../utils/APIClient';
  25. import httputil from '../../utils/httpUtil';
  26. import {
  27. GetQueryString
  28. } from '../../utils/util';
  29. Page({
  30. data: {
  31. tab: [{
  32. name: '推荐',
  33. templates: 'recommend',
  34. },
  35. {
  36. name: '关注',
  37. templates: 'follow',
  38. },
  39. {
  40. name: '资源',
  41. templates: 'courses',
  42. },
  43. {
  44. name: '我的',
  45. templates: 'my',
  46. }
  47. ],
  48. winH: 568,
  49. myIndex: 0,
  50. followData: [],
  51. recommendPageNo: 0,
  52. recommendTotalNo: 1,
  53. myData: {},
  54. templates: '',
  55. title: 'index中的title',
  56. jurisdictionFlag: true,
  57. hotInput: '12345',
  58. mineSettingInfo: '528',
  59. hide: true,
  60. isIOS: app.globalData.isIOS,
  61. ifHaveMore: true,
  62. indexSignDialog: false,
  63. indexMissionDialog: false,
  64. unfinishedCount: 0,
  65. videoList: [],
  66. nextMargin: '400rpx', // 视频下边距
  67. commentShow: false,
  68. commentList: [],
  69. commentNum: 0,
  70. followPageNo: 1,
  71. followPageSize: 6,
  72. coursesData: []
  73. },
  74. jurisdiction: function () {
  75. //隐藏弹框
  76. this.setData({
  77. hide: !this.data.hide
  78. })
  79. //登录页信息
  80. this.updateData(0)
  81. },
  82. //tab点击
  83. switcher: function ({
  84. currentTarget
  85. }) {
  86. if (currentTarget.dataset.index === this.data.myIndex) return;
  87. this.updateData(currentTarget.dataset.index);
  88. },
  89. // 根据index 更新template
  90. updateData: function (index) {
  91. let myIndex = index;
  92. this.setData({
  93. myIndex,
  94. });
  95. // 获取推荐列表
  96. if (myIndex == 0) {
  97. this.setData({
  98. videoList: [],
  99. isSwiper: true
  100. }, () => {
  101. this.getHotRecommend(this.uid);
  102. })
  103. return;
  104. }
  105. /* 关注和我的需要登陆后查看 */
  106. if (!this.data.isLogin) {
  107. wx.navigateTo({
  108. url: `../../pages/login/login`
  109. });
  110. return;
  111. }
  112. console.log('继续')
  113. // 刷新关注列表
  114. if (myIndex == 1) {
  115. this.setData({
  116. videoList: [],
  117. isSwiper: true,
  118. followPageNo: 1
  119. }, () => {
  120. this.getFollowData()
  121. })
  122. return;
  123. }
  124. // 刷新资源
  125. if (myIndex == 2) {
  126. this.setData({
  127. videoList: [],
  128. templates: 'courses'
  129. }, () => {
  130. this.getCoursesList();
  131. })
  132. return;
  133. }
  134. // 刷新我的
  135. if (myIndex == 3) {
  136. this.setData({
  137. videoList: [],
  138. templates: 'my',
  139. isSwiper: false
  140. }, () => {
  141. myInit(this);
  142. })
  143. return;
  144. }
  145. },
  146. showPage: function () {
  147. let options = this.data.options;
  148. if (options && options.index) {
  149. this.updateData(options.index)
  150. } else {
  151. this.updateData(0)
  152. }
  153. setTimeout(() => {
  154. if (wx.getStorageSync('uid')) {
  155. httpRequestApi.userLoginRecord().success(res => {
  156. if (res.data.data && res.data.data.length !== 0) {
  157. // 有数据弹签到
  158. if (this.data.unfinishedCount > 0) {
  159. this.setData({
  160. indexSignDialog: true
  161. })
  162. }
  163. } else {
  164. // 没数据弹任务
  165. let oldDay = wx.getStorageSync('oldDay');
  166. let newDate = new Date();
  167. let day = newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate()
  168. let timeStep = (newDate.getMonth() + 1).toString() + day
  169. if (oldDay) {
  170. let temp = parseInt(timeStep) - parseInt(oldDay)
  171. if (temp > 0) {
  172. wx.setStorageSync('oldDay', timeStep);
  173. this.setData({
  174. indexMissionDialog: true
  175. })
  176. }
  177. } else {
  178. wx.setStorageSync('oldDay', timeStep);
  179. this.setData({
  180. indexMissionDialog: true
  181. })
  182. }
  183. }
  184. })
  185. }
  186. }, 2800)
  187. },
  188. onLoad: function (options) {
  189. this.uid = wx.getStorageSync('uid');
  190. let grade = wx.getStorageSync('grade');
  191. let user = wx.getStorageSync('user');
  192. if (!this.uid || !grade) {
  193. this.setData({
  194. isGradeShow: true,
  195. options
  196. })
  197. console.log('没有uid 也不是游客,需要去授权')
  198. return;
  199. }
  200. this.showPage()
  201. if (this.uid && !user) {
  202. console.log('游客状态')
  203. }
  204. if (this.uid && user) {
  205. console.log('登陆状态')
  206. }
  207. wx.getSystemInfo({
  208. success: (res) => {
  209. console.log('系统', res)
  210. let winH = res.windowHeight * res.pixelRatio;
  211. let minusNumber = (winH * 920) / 1206;
  212. let nextMargin = parseInt(winH - minusNumber);
  213. app.globalData.nextMargin = nextMargin;
  214. this.setData({
  215. winH: winH,
  216. devicePixelRatio: res.pixelRatio,
  217. nextMargin: nextMargin + 'rpx'
  218. });
  219. }
  220. });
  221. },
  222. onShow: function (e) {
  223. console.log('页面显示', e)
  224. this.getUserWorksInfo(1)
  225. wx.setNavigationBarTitle({
  226. title: '小学语文朗读配音'
  227. })
  228. const userInfo = wx.getStorageSync('user')
  229. if (userInfo.wechatName) {
  230. this.setData({
  231. isLogin: true
  232. })
  233. } else {
  234. this.setData({
  235. isLogin: false
  236. })
  237. }
  238. },
  239. onHide: function () {
  240. const str = 'hotData.inputFocus'
  241. this.setData({
  242. [str]: false
  243. });
  244. },
  245. /* 两个接口维护同一个数组,手动的结束后添加算法的 */
  246. // 推荐页信息 获取消息和手动推荐内容
  247. getHotRecommend: function (uid) {
  248. httpRequestApi.getHotRecommend(
  249. uid
  250. ).success((res) => {
  251. // 点击切换按钮时 只刷新我的课程和未读消息 官方推荐和热门不加载
  252. /* const recommendRes = res.data.data;
  253. console.log(res)
  254. recommendRes.hotReader.forEach(item => {
  255. const temp = {};
  256. temp.title = item.userRead ? item.userRead.title : '';
  257. temp.img = item.userRead.iconImg;
  258. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  259. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  260. temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 0;
  261. temp.classId = item.userRead.lessonId;
  262. temp.time = formatDate(item.userRead.gmtCreated, 3);
  263. temp.avatar = item.user ? item.user.avatar : '';
  264. temp.uid = item.user ? item.user.uid : '';
  265. temp.url = item.userRead.originVideo;
  266. temp.type = item.userRead.type;
  267. // temp.avatar = item.user.avatar;
  268. temp.nickName = item.user ? item.user.wechatName : '';
  269. temp.id = item.userRead.id;
  270. temp.isLike = item.isLike;
  271. temp.isFavorite = item.isFavorites;
  272. this.data.videoList.push(temp);
  273. });
  274. this.setData({
  275. videoList: this.data.videoList
  276. }) */
  277. this.getHotRecommendSecond(1, 5)
  278. })
  279. },
  280. // 获取热门作品 算法出来的
  281. getHotRecommendSecond: function (pageNo, pageSize) {
  282. let grade = wx.getStorageSync('grade')
  283. httpRequestApi.getHotRecommendSecond(grade, pageNo, pageSize).success(res => {
  284. console.log(res)
  285. const recommendRes = res.data.data.list;
  286. if (recommendRes.length === 0) return;
  287. // const recommendWorks = [];
  288. recommendRes.forEach(item => {
  289. const temp = {};
  290. temp.title = item.userRead.title;
  291. temp.summary = item.userRead.summary;
  292. temp.img = item.userRead.iconImg;
  293. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  294. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  295. temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 0;
  296. temp.classId = item.userRead.exampleId ? item.userRead.exampleId : 1605097720036046;
  297. temp.time = formatDate(item.userRead.gmtCreated, 3);
  298. temp.avatar = item.user.avatar;
  299. temp.profession = item.user.profession;
  300. temp.uid = item.user.uid;
  301. temp.url = item.userRead.videoPath ? item.userRead.videoPath : item.userRead.originVideo;
  302. temp.id = item.userRead.id;
  303. temp.type = item.userRead.type;
  304. // temp.avatar = item.user.avatar;
  305. temp.nickName = item.user.wechatName;
  306. temp.isLike = item.isLike;
  307. temp.isFavorite = item.isFavorites;
  308. // recommendWorks.push(temp);
  309. this.data.videoList.push(temp);
  310. });
  311. this.setData({
  312. videoList: this.data.videoList
  313. })
  314. })
  315. },
  316. // 获取用户信息
  317. getUserWorksInfo: function (flag) {
  318. if (flag) {
  319. httpRequestApi.getUserWorksInfo().success(res => {
  320. const userInfo = this.formatGrade(res.data.data.user);
  321. const str = 'myData.userInfo.nickName';
  322. const avatarStr = 'myData.userInfo.avatar';
  323. const gradeTextStr = 'myData.userInfo.gradeText';
  324. this.setData({
  325. [str]: userInfo.nickName,
  326. [avatarStr]: userInfo.avatar,
  327. [gradeTextStr]: userInfo.gradeText
  328. })
  329. })
  330. return;
  331. }
  332. httpRequestApi.getUserWorksInfo().success(res => {
  333. this.data.myData.user = res.data.data;
  334. httpRequestApi.userIntoPage('pages/index/index', '首页我的').success((res) => {})
  335. if (res.data.data.myRead) {
  336. res.data.data.myRead.gmtCreated = formatDate(this.data.myData.user.myRead.gmtCreated, 4)
  337. }
  338. res.data.data.user = this.formatGrade(res.data.data.user);
  339. this.setData({
  340. myData: res.data.data,
  341. ['myData.userInfo']: res.data.data.user
  342. }, () => {
  343. this.getMyRead()
  344. });
  345. }).fail(error => {
  346. console.log(error)
  347. })
  348. },
  349. formatGrade(userInfo) {
  350. switch (userInfo.grade) {
  351. case 'PRESCHOOL':
  352. userInfo.gradeText = '学前班'
  353. break;
  354. case 'PRIMARY_FIRST_GRADE':
  355. userInfo.gradeText = '小学一年级'
  356. break;
  357. case 'PRIMARY_SECOND_GRADE':
  358. userInfo.gradeText = '小学二年级'
  359. break;
  360. case 'PRIMARY_THREE_GRADE':
  361. userInfo.gradeText = '小学三年级'
  362. break;
  363. }
  364. return userInfo;
  365. },
  366. videoChange: function (e) {
  367. console.log('到了底部到了底部', e.detail.index)
  368. console.log('到了底部到了底部', e.detail._last)
  369. console.log('到了底部到了底部', e.detail.nextQueue)
  370. if (e.detail.nextQueue.length === 1) {
  371. if (this.data.myIndex === 1) {
  372. this.setData({
  373. followPageNo: this.data.followPageNo + 1
  374. }, () => {
  375. this.getFollowData()
  376. })
  377. }
  378. }
  379. },
  380. // 触底加载
  381. onReachBottom: function () {
  382. console.log('到了底部到了底部', this.data.myIndex)
  383. // if (this.data.myIndex === 0) {
  384. // this.setData({
  385. // followPageNo: this.data.followPageNo + 1
  386. // })
  387. // if (this.data.followPageNo <= this.data.followPageTotalNo) {
  388. // this.getFollowWorks(this.data.followPageNo, 3);
  389. // } else {
  390. // console.log('没有更多')
  391. // this.setData({
  392. // ifHaveMore: false
  393. // })
  394. // }
  395. // }
  396. // // 当前在推荐页面 加载推荐
  397. // if (this.data.myIndex === 1) {
  398. // console.log(this.data.recommendPageNo)
  399. // console.log(this.data.recommendTotalNo)
  400. // this.setData({
  401. // recommendPageNo: this.data.recommendPageNo + 1
  402. // })
  403. // if (this.data.recommendPageNo <= this.data.recommendTotalNo) {
  404. // this.getHotRecommendSecond(this.uid, this.data.recommendPageNo, 3);
  405. // } else {
  406. // console.log('没有更多')
  407. // }
  408. // }
  409. },
  410. onPullDownRefresh: function () {
  411. //当前在团购页下拉加载
  412. if (this.data.myIndex === 0) {
  413. // groupInit(this);
  414. }
  415. wx.showNavigationBarLoading() //在标题栏中显示加载
  416. //模拟加载
  417. setTimeout(function () {
  418. wx.hideNavigationBarLoading() //完成停止加载
  419. wx.stopPullDownRefresh() //停止下拉刷新
  420. }, 1500);
  421. },
  422. goToMessage: function () {
  423. wx.navigateTo({
  424. url: `../../pages/social/insideMessage/insideMessage`
  425. });
  426. const str = 'hotData.unReadMessageNum';
  427. this.setData({
  428. [str]: 0
  429. })
  430. },
  431. toMyCollage: function (e) {
  432. if (app.globalData.isIOS) {
  433. wx.navigateTo({
  434. url: `../../pages/groupPage/my-group/my-group?title=我的助力`
  435. });
  436. } else {
  437. wx.navigateTo({
  438. url: `../../pages/groupPage/my-group/my-group?title=我的拼团`
  439. });
  440. }
  441. },
  442. toMyCourse: function () {
  443. wx.navigateTo({
  444. url: `../../pages/user/mycourse/mycourse?title=我的课程`
  445. });
  446. },
  447. goToFlower: function () {
  448. wx.navigateTo({
  449. url: `../../pages/social/littleFlower/littleFlower`
  450. });
  451. },
  452. signInBtn: function (e) {
  453. this.setData({
  454. indexSignDialog: false
  455. })
  456. console.log(e.detail.formId)
  457. httpRequestApi.postFormId(e.detail.formId).success(res => {
  458. console.log(res)
  459. })
  460. this.goToFlower();
  461. },
  462. missionBtn: function () {
  463. this.setData({
  464. indexMissionDialog: false
  465. })
  466. this.goToFlower();
  467. },
  468. getUserAuth: function () {
  469. httpRequestApi.getUserAuth().success(res => {
  470. console.log(res)
  471. const str = 'myData.isVIP'
  472. if (res.data.data) {
  473. this.setData({
  474. [str]: true
  475. })
  476. } else {
  477. this.setData({
  478. [str]: false
  479. })
  480. }
  481. })
  482. },
  483. // 获取我的朗读
  484. getMyRead: function () {
  485. httpRequestApi.myRead().success(res => {
  486. console.log(123123, res)
  487. console.log('mydata', this.data.myData)
  488. const myList = res.data.data;
  489. if (myList.length === 0) return;
  490. // const recommendWorks = [];
  491. myList.forEach(item => {
  492. console.log('mydata', this.data.myData)
  493. console.log('mydata', this.data.myData.userInfo.user)
  494. const temp = {};
  495. temp.title = item.title;
  496. temp.summary = item.summary;
  497. temp.img = item.iconImg;
  498. temp.plays = item.playAmount;
  499. temp.likes = item.likeAmount;
  500. temp.classId = item.exampleId ? item.exampleId : 1605097720036046;
  501. temp.time = formatDate(item.gmtCreated, 3);
  502. temp.avatar = this.data.myData.userInfo.avatar;
  503. temp.uid = this.uid;
  504. temp.url = item.videoPath;
  505. temp.id = item.id;
  506. // temp.avatar = item.user.avatar;
  507. temp.nickName = this.data.myData.userInfo.wechatName;
  508. // recommendWorks.push(temp);
  509. this.data.videoList.push(temp);
  510. });
  511. this.setData({
  512. videoList: this.data.videoList
  513. })
  514. })
  515. },
  516. // 评论区点击
  517. commentTap: function (e) {
  518. console.log('点击评论区', e)
  519. if (e.target.dataset.type === 'blank') {
  520. this.setData({
  521. commentShow: false
  522. })
  523. }
  524. },
  525. // 打开评论
  526. openComment: function (e) {
  527. //
  528. console.log('id', e.detail.activeId)
  529. this.setData({
  530. commentShow: !this.data.commentShow,
  531. commentId: e.detail.activeId,
  532. // commentList: []
  533. });
  534. // this.getReply(e.detail.activeId);
  535. },
  536. // 获取评论信息
  537. getReply: function (columnId) {
  538. // let columnId = this.data.id;
  539. console.log(123123123, columnId)
  540. // let pageNo = this.data.pageNo;
  541. // let pageSize = this.data.pageSize;
  542. httpRequestApi.getReply(this.uid, columnId, 1, 10).success((res) => {
  543. console.log('reply', res)
  544. const commentList = res.data.data.list;
  545. const commentNum = res.data.data.totalSize;
  546. console.log('评论数量', commentNum)
  547. commentList.forEach((item) => {
  548. const temp = {};
  549. temp.nickName = item.user.wechatName;
  550. temp.avatar = item.user.avatar;
  551. temp.uid = item.user.uid;
  552. temp.text = item.detailDesc;
  553. temp.id = item.id;
  554. temp.replyCount = item.replyCount;
  555. temp.time = formatDate(item.gmtCreated, 3);
  556. temp.likes = item.postsAttributeInfo.favors || 0;
  557. temp.isLike = item.isLike;
  558. temp.replyList = item.replyVOList;
  559. this.data.commentList.push(temp);
  560. });
  561. this.setData({
  562. commentList: this.data.commentList,
  563. commentNum: commentNum
  564. })
  565. });
  566. },
  567. // 发布回复
  568. sendReply: function (e) {
  569. console.log('triger', e.detail.content);
  570. let data = {
  571. columnId: this.data.commentId,
  572. colunmNames: 'what',
  573. detailDesc: e.detail.content,
  574. // productId: this.data.productId
  575. }
  576. httpRequestApi.postReply(this.uid, data).success(res => {
  577. console.log(res)
  578. this.setData({
  579. pageNo: 1,
  580. commentList: []
  581. }, () => {
  582. this.getReply(this.data.commentId);
  583. })
  584. });
  585. },
  586. gradeTap: function () {
  587. // console.log("组件回调,返回上一页");
  588. this.setData({
  589. isGradeShow: true
  590. })
  591. },
  592. // 修改年级
  593. changeGrade: function (e) {
  594. const grade = e.target.dataset.code;
  595. wx.setStorageSync('grade', grade)
  596. this.setData({
  597. isGradeShow: false,
  598. })
  599. getOpenidNoLogin((res) => {
  600. console.log('getOpenidNoLogin', res)
  601. // 登录或注册完成 展示页面
  602. this.showPage()
  603. }, (error) => {
  604. console.log('获取失败')
  605. wx.setStorageSync('userSourseType', 'normal')
  606. this.setData({
  607. hide: !this.data.hide
  608. })
  609. }, grade);
  610. },
  611. getFollowData: function () {
  612. httpRequestApi.getFollowWorks(this.data.followPageNo, this.data.followPageSize).success(res => {
  613. if (res.data.data.totalSize === 0) {
  614. this.setData({
  615. videoList: []
  616. })
  617. console.log('没有关注人或关注的人没有发过作品')
  618. return
  619. }
  620. console.log('关注列表', res)
  621. const followData = res.data.data.list;
  622. const videoList = [];
  623. followData.forEach(item => {
  624. const temp = {};
  625. temp.title = item.userRead ? item.userRead.title : '';
  626. temp.img = item.userRead.iconImg;
  627. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  628. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  629. temp.classId = item.userRead.exampleId ? item.userRead.exampleId : 1605097720036046;
  630. temp.time = formatDate(item.userRead.gmtCreated, 3);
  631. temp.avatar = item.user ? item.user.avatar : '';
  632. temp.uid = item.user ? item.user.uid : '';
  633. temp.url = item.userRead.videoPath ? item.userRead.videoPath : item.userRead.originVideo;
  634. temp.nickName = item.user ? item.user.wechatName : '';
  635. temp.id = item.userRead.id;
  636. videoList.push(temp);
  637. });
  638. this.setData({
  639. videoList: videoList
  640. })
  641. });
  642. },
  643. // 点击用户头像区域
  644. headTapHandler: function (e) {
  645. console.log('点击头像', e)
  646. let tapId = e.detail.activeId;
  647. // 点击头像既关注 测试
  648. httpRequestApi.followUser(this.uid, tapId).success(res => {
  649. console.log(res)
  650. })
  651. },
  652. getCoursesList: function () {
  653. const grade = wx.getStorageSync('grade');
  654. const data = {
  655. grade,
  656. type: 'EXAMPLE'
  657. }
  658. httpRequestApi.getClassRead(data).success(res => {
  659. console.log('资源', res)
  660. this.setData({
  661. coursesData: res.data.data.list
  662. }, () => {
  663. console.log(this.data.coursesData)
  664. })
  665. })
  666. },
  667. goToReading: function (e) {
  668. console.log('去朗读', e)
  669. const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id;
  670. wx.navigateTo({
  671. url: `../../pages/main/reading/reading?id=${id}`
  672. });
  673. },
  674. })