hot.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. [unReadMessageContent]: ''
  204. })
  205. return;
  206. }
  207. recommendRes.hotReader.forEach(item => {
  208. const temp = {};
  209. temp.title = item.userRead ? item.userRead.title : '';
  210. temp.summary = item.userRead.summary;
  211. temp.img = item.userRead.iconImg;
  212. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  213. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  214. temp.classId = item.userRead.id;
  215. temp.time = formatDate(item.userRead.gmtCreated, 3);
  216. temp.avatar = item.user ? item.user.avatar : '';
  217. temp.profession = item.user ? item.user.profession : '';
  218. temp.uid = item.user ? item.user.uid : '';
  219. // temp.avatar = item.user.avatar;
  220. temp.nickName = item.user ? item.user.nickName : '';
  221. // recommendWorks.push(temp);
  222. that.data.hotData.hotWorks.push(temp);
  223. });
  224. const hotStr = 'hotData.hotWorks'
  225. const myCourseStr = 'hotData.myCourse'
  226. const isMessageNormal = 'hotData.isMessageNormal'
  227. const unReadMessageContent = 'hotData.unReadMessageContent'
  228. that.setData({
  229. [hotStr]: that.data.hotData.hotWorks,
  230. [myCourseStr]: recommendRes.myCourse,
  231. [isMessageNormal]: recommendRes.unread,
  232. // [unReadMessageContent]: recommendRes.unread ? recommendRes.unreadMsg.title : recommendRes.annunciateMsg.title,
  233. [unReadMessageContent]: '',
  234. unfinishedCount: recommendRes.unfinishedCount
  235. })
  236. })
  237. }
  238. that.getHotRecommendSecond = (uid, pageNo, pageSize) => {
  239. httpRequestApi.getHotRecommendSecond(uid, pageNo, pageSize).success(res => {
  240. console.log(res)
  241. const recommendRes = res.data.data.list;
  242. if (recommendRes.length === 0) return;
  243. // const recommendWorks = [];
  244. recommendRes.forEach(item => {
  245. const temp = {};
  246. temp.title = item.userRead.title;
  247. temp.summary = item.userRead.summary;
  248. temp.img = item.userRead.iconImg;
  249. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  250. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  251. temp.classId = item.userRead.id;
  252. temp.time = formatDate(item.userRead.gmtCreated, 3);
  253. temp.avatar = item.user.avatar;
  254. temp.profession = item.user.profession;
  255. temp.uid = item.user.uid;
  256. // temp.avatar = item.user.avatar;
  257. temp.nickName = item.user.nickName;
  258. // recommendWorks.push(temp);
  259. that.data.hotData.hotWorks.push(temp);
  260. });
  261. const hotStr = 'hotData.hotWorks'
  262. that.setData({
  263. [hotStr]: that.data.hotData.hotWorks,
  264. recommendTotalNo: res.data.data.totalNo
  265. })
  266. })
  267. }
  268. // 去全部课本
  269. that.goToBooks = () => {
  270. wx.navigateTo({
  271. url: `../main/books/books`
  272. })
  273. }
  274. that.bannerTap = (e) => {
  275. const type = e.currentTarget.dataset.type;
  276. const id = e.currentTarget.dataset.id;
  277. switch (type) {
  278. case 'LESSON':
  279. that.openClass('tap', id);
  280. break;
  281. }
  282. },
  283. that.goToUsers = (e) => {
  284. console.log(e)
  285. let uid = e.currentTarget.dataset.uid;
  286. wx.navigateTo({
  287. url: `../../pages/user/myworks/myworks?uid=${uid}`
  288. });
  289. }
  290. // 下拉加载
  291. that.scrollUpdate = (e) => {
  292. console.log(e)
  293. }
  294. console.log(that.data.hotData.recommend)
  295. if (wx.getStorageSync('uid')) {
  296. that.getIndex();
  297. that.getHotRecommend(wx.getStorageSync('uid'));
  298. }
  299. // that.getIndex();
  300. // 获取全部课本
  301. // that.getBookList();
  302. // that.swiperChange = (e) => {
  303. // const str = 'hotData.swiperCurrent'
  304. // that.setData({
  305. // [str]: e.detail.current
  306. // });
  307. // }
  308. //跳转到课程详情
  309. that.goToBook = function(e) {
  310. console.log(e.currentTarget.dataset)
  311. let id = e.currentTarget.dataset.id;
  312. let title = e.currentTarget.dataset.title;
  313. console.log(id)
  314. wx.navigateTo({
  315. url: `/pages/groupPage/grade-details/grade-details?productId=${id}&title=${title}`
  316. })
  317. }
  318. that.swiperChange = (e) => {
  319. const str = 'hotData.swiperCurrent'
  320. that.setData({
  321. [str]: e.detail.current
  322. });
  323. }
  324. }