index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. followPageNo: 1,
  54. followPageTotalNo: 1,
  55. myData: {},
  56. templates: 'hot',
  57. title: 'index中的title',
  58. jurisdictionFlag: true,
  59. hotInput: '12345',
  60. mineSettingInfo: '528',
  61. hide: true,
  62. isIOS: app.globalData.isIOS,
  63. ifHaveMore: true,
  64. indexSignDialog: false,
  65. indexMissionDialog: false,
  66. unfinishedCount: 0,
  67. videoList: [],
  68. nextMargin: '400rpx' // 视频下边距
  69. },
  70. jurisdiction: function () {
  71. //隐藏弹框
  72. this.setData({
  73. hide: !this.data.hide
  74. })
  75. //登录页信息
  76. this.updateData(0)
  77. },
  78. //tab点击
  79. switcher: function ({
  80. currentTarget
  81. }) {
  82. if (currentTarget.dataset.index === this.data.myIndex) return;
  83. this.updateData(currentTarget.dataset.index);
  84. },
  85. // 根据index 更新template
  86. updateData: function (index) {
  87. let myIndex = index;
  88. let templates = this.data.tab[myIndex].templates;
  89. this.setData({
  90. myIndex,
  91. templates
  92. });
  93. if (myIndex == 0) {
  94. // groupInit(this);
  95. this.setData({
  96. followPageNo: 1,
  97. })
  98. getOpenidSessionKey((res) => {
  99. console.log(res)
  100. myInit(this);
  101. }, (error) => {
  102. console.log(error)
  103. wx.setStorageSync('userSourseType', 'normal')
  104. this.setData({
  105. hide: !this.data.hide
  106. })
  107. return;
  108. });
  109. }
  110. if (myIndex == 1) {
  111. // this.setData({
  112. // recommendPageNo: 0
  113. // })
  114. hotInit(this);
  115. }
  116. if (myIndex == 2) {
  117. groupInit(this);
  118. }
  119. },
  120. onLoad: function (options) {
  121. this.uid = wx.getStorageSync('uid');
  122. wx.getSystemInfo({
  123. success: (res) => {
  124. console.log('系统', res)
  125. this.setData({
  126. winH: res.windowHeight,
  127. devicePixelRatio: res.pixelRatio
  128. });
  129. }
  130. });
  131. if (options.index) {
  132. this.updateData(options.index)
  133. }
  134. setTimeout(() => {
  135. if (wx.getStorageSync('uid')) {
  136. httpRequestApi.userLoginRecord().success(res => {
  137. if (res.data.data && res.data.data.length !== 0) {
  138. // 有数据弹签到
  139. if (this.data.unfinishedCount > 0) {
  140. this.setData({
  141. indexSignDialog: true
  142. })
  143. }
  144. } else {
  145. // 没数据弹任务
  146. let oldDay = wx.getStorageSync('oldDay');
  147. let newDate = new Date();
  148. let day = newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate()
  149. let timeStep = (newDate.getMonth() + 1).toString() + day
  150. if (oldDay) {
  151. let temp = parseInt(timeStep) - parseInt(oldDay)
  152. if (temp > 0) {
  153. wx.setStorageSync('oldDay', timeStep);
  154. this.setData({
  155. indexMissionDialog: true
  156. })
  157. }
  158. } else {
  159. wx.setStorageSync('oldDay', timeStep);
  160. this.setData({
  161. indexMissionDialog: true
  162. })
  163. }
  164. }
  165. })
  166. }
  167. }, 2800)
  168. },
  169. onShow: function () {
  170. wx.setNavigationBarTitle({
  171. title: '小学语文朗读配音'
  172. })
  173. // this.init();
  174. getOpenidNoLogin((res) => {
  175. let winH = this.data.winH * 2;
  176. // let minusNumber = 860;
  177. let minusNumber = (winH * 860) / 1206;
  178. // if(winH < 510){
  179. // minusNumber = 400;
  180. // }
  181. // let nextMargin = parseInt(winH *(1 - (834 / winH)) );
  182. let nextMargin = parseInt(winH - minusNumber);
  183. console.log(123, winH)
  184. console.log(321, nextMargin)
  185. this.setData({
  186. nextMargin: nextMargin + 'rpx'
  187. })
  188. this.getHotRecommend(this.uid);
  189. // hotInit(this)
  190. }, (error) => {
  191. // console.log(error)
  192. wx.setStorageSync('userSourseType', 'normal')
  193. this.setData({
  194. hide: !this.data.hide
  195. })
  196. return;
  197. });
  198. if (this.data.myIndex === 0) {
  199. // 从修改信息页面退回
  200. // this.getUserWorksInfo(true);
  201. }
  202. },
  203. onHide: function () {
  204. const str = 'hotData.inputFocus'
  205. this.setData({
  206. [str]: false
  207. });
  208. },
  209. /* 两个接口维护同一个数组,手动的结束后添加算法的 */
  210. // 推荐页信息 获取消息和手动推荐内容
  211. getHotRecommend: function (uid) {
  212. httpRequestApi.getHotRecommend(
  213. uid
  214. ).success((res) => {
  215. // 点击切换按钮时 只刷新我的课程和未读消息 官方推荐和热门不加载
  216. const recommendRes = res.data.data;
  217. console.log(res)
  218. recommendRes.hotReader.forEach(item => {
  219. const temp = {};
  220. temp.title = item.userRead ? item.userRead.title : '';
  221. temp.img = item.userRead.iconImg;
  222. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  223. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  224. temp.classId = item.userRead.id;
  225. temp.time = formatDate(item.userRead.gmtCreated, 3);
  226. temp.avatar = item.user ? item.user.avatar : '';
  227. temp.uid = item.user ? item.user.uid : '';
  228. temp.url = item.userRead.originVideo;
  229. // temp.avatar = item.user.avatar;
  230. temp.nickName = item.user ? item.user.wechatName : '';
  231. temp.id = item.id;
  232. // recommendWorks.push(temp);
  233. // that.data.hotData.hotWorks.push(temp);
  234. this.data.videoList.push(temp);
  235. });
  236. this.setData({
  237. videoList: this.data.videoList
  238. })
  239. this.getHotRecommendSecond(this.uid, 1, 5)
  240. const hotStr = 'hotData.hotWorks'
  241. const myCourseStr = 'hotData.myCourse'
  242. const isMessageNormal = 'hotData.isMessageNormal'
  243. const unReadMessageContent = 'hotData.unReadMessageContent'
  244. // that.setData({
  245. // })
  246. })
  247. },
  248. // 获取热门作品 算法出来的
  249. getHotRecommendSecond: function (uid, pageNo, pageSize) {
  250. httpRequestApi.getHotRecommendSecond(uid, pageNo, pageSize).success(res => {
  251. console.log(res)
  252. const recommendRes = res.data.data.list;
  253. if (recommendRes.length === 0) return;
  254. // const recommendWorks = [];
  255. recommendRes.forEach(item => {
  256. const temp = {};
  257. temp.title = item.userRead.title;
  258. temp.summary = item.userRead.summary;
  259. temp.img = item.userRead.iconImg;
  260. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  261. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  262. temp.classId = item.userRead.id;
  263. temp.time = formatDate(item.userRead.gmtCreated, 3);
  264. temp.avatar = item.user.avatar;
  265. temp.profession = item.user.profession;
  266. temp.uid = item.user.uid;
  267. temp.url = item.userRead.originVideo;
  268. // temp.avatar = item.user.avatar;
  269. temp.nickName = item.user.wechatName;
  270. // recommendWorks.push(temp);
  271. this.data.videoList.push(temp);
  272. });
  273. this.setData({
  274. videoList: this.data.videoList
  275. })
  276. console.log('dangqian', this.data.videoList)
  277. })
  278. },
  279. // 获取用户信息
  280. getUserWorksInfo: function (flag) {
  281. console.log(flag)
  282. if (flag) {
  283. httpRequestApi.getUserWorksInfo().success(res => {
  284. // const str = 'myData.user.user.nickName';
  285. // const avatarStr = 'myData.user.user.avatar';
  286. // this.setData({
  287. // [str]: res.data.data.user.nickName,
  288. // [avatarStr]: res.data.data.user.avatar
  289. // })
  290. })
  291. return;
  292. }
  293. const userLocal = wx.getStorageSync('user')
  294. console.log(userLocal)
  295. const str = 'myData.user';
  296. this.setData({
  297. [str]: userLocal
  298. })
  299. httpRequestApi.getUserWorksInfo().success(res => {
  300. this.data.myData.user = res.data.data;
  301. httpRequestApi.userIntoPage('pages/index/index', '首页我的').success((res) => {})
  302. if (this.data.myData.user.myRead) {
  303. this.data.myData.user.myRead.gmtCreated = formatDate(this.data.myData.user.myRead.gmtCreated, 4)
  304. }
  305. this.setData({
  306. myData: this.data.myData,
  307. });
  308. }).fail(error => {
  309. console.log(error)
  310. })
  311. },
  312. // 触底加载
  313. onReachBottom: function () {
  314. console.log(this.data.myIndex)
  315. if (this.data.myIndex === 0) {
  316. this.setData({
  317. followPageNo: this.data.followPageNo + 1
  318. })
  319. if (this.data.followPageNo <= this.data.followPageTotalNo) {
  320. this.getFollowWorks(this.data.followPageNo, 3);
  321. } else {
  322. console.log('没有更多')
  323. this.setData({
  324. ifHaveMore: false
  325. })
  326. }
  327. }
  328. // 当前在推荐页面 加载推荐
  329. if (this.data.myIndex === 1) {
  330. console.log(this.data.recommendPageNo)
  331. console.log(this.data.recommendTotalNo)
  332. this.setData({
  333. recommendPageNo: this.data.recommendPageNo + 1
  334. })
  335. if (this.data.recommendPageNo <= this.data.recommendTotalNo) {
  336. this.getHotRecommendSecond(this.uid, this.data.recommendPageNo, 3);
  337. } else {
  338. console.log('没有更多')
  339. }
  340. }
  341. },
  342. onPullDownRefresh: function () {
  343. //当前在团购页下拉加载
  344. if (this.data.myIndex === 0) {
  345. // groupInit(this);
  346. }
  347. wx.showNavigationBarLoading() //在标题栏中显示加载
  348. //模拟加载
  349. setTimeout(function () {
  350. wx.hideNavigationBarLoading() //完成停止加载
  351. wx.stopPullDownRefresh() //停止下拉刷新
  352. }, 1500);
  353. },
  354. goToMessage: function () {
  355. wx.navigateTo({
  356. url: `../../pages/social/insideMessage/insideMessage`
  357. });
  358. const str = 'hotData.unReadMessageNum';
  359. this.setData({
  360. [str]: 0
  361. })
  362. },
  363. toMyCollage: function (e) {
  364. if (app.globalData.isIOS) {
  365. wx.navigateTo({
  366. url: `../../pages/groupPage/my-group/my-group?title=我的助力`
  367. });
  368. } else {
  369. wx.navigateTo({
  370. url: `../../pages/groupPage/my-group/my-group?title=我的拼团`
  371. });
  372. }
  373. },
  374. toMyCourse: function () {
  375. wx.navigateTo({
  376. url: `../../pages/user/mycourse/mycourse?title=我的课程`
  377. });
  378. },
  379. goToFlower: function () {
  380. wx.navigateTo({
  381. url: `../../pages/social/littleFlower/littleFlower`
  382. });
  383. },
  384. signInBtn: function (e) {
  385. this.setData({
  386. indexSignDialog: false
  387. })
  388. console.log(e.detail.formId)
  389. httpRequestApi.postFormId(e.detail.formId).success(res => {
  390. console.log(res)
  391. })
  392. this.goToFlower();
  393. },
  394. missionBtn: function () {
  395. this.setData({
  396. indexMissionDialog: false
  397. })
  398. this.goToFlower();
  399. },
  400. getUserAuth: function () {
  401. httpRequestApi.getUserAuth().success(res => {
  402. console.log(res)
  403. const str = 'myData.isVIP'
  404. if (res.data.data) {
  405. this.setData({
  406. [str]: true
  407. })
  408. } else {
  409. this.setData({
  410. [str]: false
  411. })
  412. }
  413. })
  414. }
  415. })