hot.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import httpRequestApi from '../../utils/APIClient';
  2. import {
  3. formatDate
  4. } from '../../utils/util';
  5. export const hotInit = (that) => {
  6. if (!that.data.hotData) {
  7. that.setData({
  8. hotData: {
  9. title: '热门',
  10. autoplay: false,
  11. swiperCurrent: 0,
  12. interval: 5000,
  13. duration: 700,
  14. circular: true,
  15. hotWareCardFirst: 'hotWareCardFirst',
  16. hotWareCard: 'hotWareCard',
  17. hotSearch: '',
  18. banner: [],
  19. recommend: [],
  20. hotWorks: [],
  21. winH: that.data.winH,
  22. searchIcon: true,
  23. unReadMessageNum: 0,
  24. myCourse: [],
  25. inputFocus: false,
  26. inputValue: '',
  27. baiduFlag: false
  28. // circular: true
  29. }
  30. })
  31. }
  32. // 搜索方法
  33. that.searchHandler = () => {
  34. if (that.data.hotInput.lenght !== 0) {
  35. wx.navigateTo({
  36. url: `../main/searchResult/searchResult?keyWords=${that.data.hotInput}`
  37. })
  38. }
  39. const str = 'hotData.inputValue';
  40. that.setData({
  41. [str]: ''
  42. })
  43. };
  44. // 输入框获取焦点 放大镜消失
  45. that.searchIconDisappear = () => {
  46. const str = 'hotData.searchIcon'
  47. that.setData({
  48. [str]: false
  49. })
  50. };
  51. that.searchIconCome = () => {
  52. const str = 'hotData.searchIcon'
  53. that.setData({
  54. [str]: true
  55. })
  56. };
  57. // 百度弹窗弹出
  58. that.clickBaidu = () => {
  59. const str = 'hotData.baiduFlag'
  60. that.setData({
  61. [str]: true
  62. });
  63. };
  64. // 百度弹窗关闭
  65. that.xiaoduClick = () => {
  66. const str = 'hotData.baiduFlag'
  67. that.setData({
  68. [str]: false
  69. });
  70. };
  71. // 百度保存图片
  72. that.PreservationXiaodu = () => {
  73. wx.getImageInfo({
  74. src: 'https://reader.image.lingjiao.cn/reader/resource/海报.png',
  75. success (res) {
  76. wx.saveImageToPhotosAlbum({
  77. filePath: res.path,
  78. success: (res) => {
  79. wx.showModal({
  80. title: '海报已保存至相册',
  81. content: '快去分享给小伙伴吧',
  82. confirmText: '我知道了',
  83. showCancel: false,
  84. success(res) {
  85. console.log('用户点击确定')
  86. }
  87. })
  88. const str = 'hotData.baiduFlag'
  89. that.setData({
  90. [str]: false
  91. });
  92. },
  93. fail: res => {
  94. console.log(res)
  95. wx.getSetting({
  96. success(res) {
  97. if (!res.authSetting['scope.writePhotosAlbum']) {
  98. wx.showModal({
  99. title: '无法保存分享图片到相册',
  100. content: '点击右上角浮点按钮->设置,进行授权',
  101. confirmText: '我知道了',
  102. showCancel: false,
  103. success(res) {
  104. console.log('用户点击确定')
  105. }
  106. })
  107. }
  108. }
  109. })
  110. }
  111. })
  112. }
  113. })
  114. };
  115. // 输入框输入
  116. that.inputHandler = (e) => {
  117. that.setData({
  118. hotInput: e.detail.value
  119. });
  120. };
  121. // 输入框聚焦
  122. that.inputFocus = () => {
  123. const str = 'hotData.inputFocus'
  124. that.setData({
  125. [str]: true
  126. });
  127. console.log(that.data.hotData.inputFocus)
  128. };
  129. // 打开全部课本
  130. that.openBooks = (e) => {
  131. wx.navigateTo({
  132. url: `../main/books/books`
  133. })
  134. }
  135. // 打开更多页面
  136. that.openMore = () => {
  137. wx.navigateTo({
  138. url: `../main/week/week`
  139. })
  140. }
  141. // 打开课程页面
  142. that.openClass = (e, lessonId) => {
  143. if (lessonId) {
  144. wx.navigateTo({
  145. url: `../main/class/class?id=${lessonId}`
  146. })
  147. } else {
  148. let id = e.currentTarget.dataset.classid;
  149. let title = e.currentTarget.dataset.title;
  150. wx.navigateTo({
  151. url: `../main/class/class?id=${id}&title=${title}`
  152. })
  153. }
  154. }
  155. // 打开用户作品页面
  156. that.openWorks = (e) => {
  157. let readId = e.currentTarget.dataset.readid;
  158. let title = e.currentTarget.dataset.title;
  159. wx.navigateTo({
  160. url: `../social/works/works?id=${readId}&title=${title}`
  161. })
  162. }
  163. that.getIndex = () => {
  164. const uid = wx.getStorageSync('uid');
  165. httpRequestApi.getIndex(
  166. uid
  167. ).success((res) => {
  168. const recommend = res.data.data.recommendLesson;
  169. const recommendTemp = [];
  170. recommend.forEach(item => {
  171. const oTemp = {};
  172. oTemp.img = item.boothContent;
  173. oTemp.classId = item.operationContent;
  174. oTemp.title = item.title;
  175. oTemp.summary = item.summary;
  176. recommendTemp.push(oTemp);
  177. });
  178. let recommendIndex = 'hotData.recommend';
  179. that.setData({
  180. [recommendIndex]: recommendTemp
  181. });
  182. httpRequestApi.userIntoPage('pages/index/index', '首页推荐').success((res) => {
  183. })
  184. })
  185. }
  186. that.getHotRecommend = (uid) => {
  187. httpRequestApi.getHotRecommend(
  188. uid
  189. ).success((res) => {
  190. // 点击切换按钮时 只刷新我的课程和未读消息 官方推荐和热门不加载
  191. const recommendRes = res.data.data;
  192. console.log(that.data.hotData.hotWorks.length)
  193. if (that.data.hotData.hotWorks.length > 0) {
  194. const myCourseStr = 'hotData.myCourse'
  195. const isMessageNormal = 'hotData.isMessageNormal'
  196. const unReadMessageContent = 'hotData.unReadMessageContent'
  197. // unread为true时显示 未读消息,unread为false时显示通知消息,并且通知消息不可读
  198. that.setData({
  199. [myCourseStr]: recommendRes.myCourse,
  200. [isMessageNormal]: recommendRes.unread,
  201. // [unReadMessageContent]: recommendRes.unread ? recommendRes.unreadMsg.title : '',
  202. [unReadMessageContent]: recommendRes.unread ? recommendRes.unreadMsg.title : recommendRes.annunciateMsg.title
  203. })
  204. return;
  205. }
  206. recommendRes.hotReader.forEach(item => {
  207. const temp = {};
  208. temp.title = item.userRead.title;
  209. temp.summary = item.userRead.summary;
  210. temp.img = item.userRead.iconImg;
  211. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  212. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  213. temp.classId = item.userRead.id;
  214. temp.time = formatDate(item.userRead.gmtCreated, 3);
  215. temp.avatar = item.user ? item.user.avatar : '';
  216. temp.profession = item.user ? item.user.profession : '';
  217. temp.uid = item.user ? item.user.uid : '';
  218. // temp.avatar = item.user.avatar;
  219. temp.nickName = item.user ? item.user.wechatName : '';
  220. // recommendWorks.push(temp);
  221. that.data.hotData.hotWorks.push(temp);
  222. });
  223. const hotStr = 'hotData.hotWorks'
  224. const myCourseStr = 'hotData.myCourse'
  225. const isMessageNormal = 'hotData.isMessageNormal'
  226. const unReadMessageContent = 'hotData.unReadMessageContent'
  227. that.setData({
  228. [hotStr]: that.data.hotData.hotWorks,
  229. [myCourseStr]: recommendRes.myCourse,
  230. [isMessageNormal]: recommendRes.unread,
  231. [unReadMessageContent]: recommendRes.unread ? recommendRes.unreadMsg.title : recommendRes.annunciateMsg.title,
  232. unfinishedCount: recommendRes.unfinishedCount
  233. })
  234. })
  235. }
  236. that.getHotRecommendSecond = (uid, pageNo, pageSize) => {
  237. httpRequestApi.getHotRecommendSecond(uid, pageNo, pageSize).success(res => {
  238. console.log(res)
  239. const recommendRes = res.data.data.list;
  240. if (recommendRes.length === 0) return;
  241. // const recommendWorks = [];
  242. recommendRes.forEach(item => {
  243. const temp = {};
  244. temp.title = item.userRead.title;
  245. temp.summary = item.userRead.summary;
  246. temp.img = item.userRead.iconImg;
  247. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  248. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  249. temp.classId = item.userRead.id;
  250. temp.time = formatDate(item.userRead.gmtCreated, 3);
  251. temp.avatar = item.user.avatar;
  252. temp.profession = item.user.profession;
  253. temp.uid = item.user.uid;
  254. // temp.avatar = item.user.avatar;
  255. temp.nickName = item.user.wechatName;
  256. // recommendWorks.push(temp);
  257. that.data.hotData.hotWorks.push(temp);
  258. });
  259. const hotStr = 'hotData.hotWorks'
  260. that.setData({
  261. [hotStr]: that.data.hotData.hotWorks,
  262. recommendTotalNo: res.data.data.totalNo
  263. })
  264. })
  265. }
  266. // 去全部课本
  267. that.goToBooks = () => {
  268. wx.navigateTo({
  269. url: `../main/books/books`
  270. })
  271. }
  272. that.bannerTap = (e) => {
  273. const type = e.currentTarget.dataset.type;
  274. const id = e.currentTarget.dataset.id;
  275. switch (type) {
  276. case 'LESSON':
  277. that.openClass('tap', id);
  278. break;
  279. }
  280. },
  281. that.goToUsers = (e) => {
  282. console.log(e)
  283. let uid = e.currentTarget.dataset.uid;
  284. wx.navigateTo({
  285. url: `../../pages/user/myworks/myworks?uid=${uid}`
  286. });
  287. }
  288. // 下拉加载
  289. that.scrollUpdate = (e) => {
  290. console.log(e)
  291. }
  292. console.log(that.data.hotData.recommend)
  293. if (wx.getStorageSync('uid')) {
  294. that.getIndex();
  295. that.getHotRecommend(wx.getStorageSync('uid'));
  296. }
  297. // that.getIndex();
  298. // 获取全部课本
  299. // that.getBookList();
  300. // that.swiperChange = (e) => {
  301. // const str = 'hotData.swiperCurrent'
  302. // that.setData({
  303. // [str]: e.detail.current
  304. // });
  305. // }
  306. //跳转到课程详情
  307. that.goToBook = function (e) {
  308. console.log(e.currentTarget.dataset)
  309. let id = e.currentTarget.dataset.id;
  310. let title = e.currentTarget.dataset.title;
  311. console.log(id)
  312. wx.navigateTo({
  313. url: `/pages/groupPage/grade-details/grade-details?productId=${id}&title=${title}`
  314. })
  315. }
  316. that.swiperChange = (e) => {
  317. const str = 'hotData.swiperCurrent'
  318. that.setData({
  319. [str]: e.detail.current
  320. });
  321. }
  322. }