home.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. import {
  2. ilogin,
  3. ihome,
  4. iactivity,
  5. icommunity,
  6. imine
  7. } from '../../api/api.js'
  8. const app = getApp()
  9. Page({
  10. loadMyinformation() {
  11. let id = wx.getStorageSync('id')
  12. imine.loadMyinformation({reqdata: {id}},r => {
  13. console.log(r);
  14. this.setData({
  15. user: Object.assign(this.data.user, r.object)
  16. })
  17. wx.setStorageSync('user', this.data.user)
  18. this.data.linkState && this.toLinkR()
  19. })
  20. },
  21. loadMyActivities() {
  22. imine.loadMyActivities(this.data.mine.pagination, r => {
  23. this.setData({
  24. ['mine.curNav']: 3,
  25. ['mine.list']: this.data.mine.list.concat(r.list),
  26. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  27. })
  28. })
  29. },
  30. loadMyComments() {
  31. imine.loadMyComments(this.data.mine.pagination, r => {
  32. for (let c of r.list) {
  33. c.showtime = new Date(c.comment_addtime).getDateName('hh:mm')
  34. }
  35. this.setData({
  36. ['mine.curNav']: 2,
  37. ['mine.list']: this.data.mine.list.concat(r.list),
  38. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  39. })
  40. })
  41. },
  42. loadMyPubarticles() {
  43. imine.loadMyPubarticles(this.data.mine.pagination, r => {
  44. this.setData({
  45. ['mine.curNav']: 0,
  46. ['mine.list']: this.data.mine.list.concat(r.list),
  47. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  48. })
  49. })
  50. },
  51. loadMyCollectedarticles() {
  52. imine.loadMyCollectedarticles(this.data.mine.pagination, r => {
  53. this.setData({
  54. ['mine.curNav']: 1,
  55. ['mine.list']: this.data.mine.list.concat(r.list).map(item => {
  56. item.status_attend = Date.now() < new Date(item.applyStartTime).valueOf() ? '未开始' : Date.now() > new Date(item.applyEndTime).valueOf() ? '已结束' :''
  57. return item
  58. }),
  59. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  60. })
  61. })
  62. },
  63. data: {
  64. showAuthPanel: false,
  65. curNav: 0,
  66. user: {},
  67. imgUrl: app.globalData.imgUrl,
  68. navs: [{
  69. key: 'home',
  70. name: '首页'
  71. }, {
  72. key: 'activity',
  73. name: '活动动态'
  74. }, {
  75. key: 'community',
  76. name: '社区'
  77. }, {
  78. key: 'mine',
  79. name: '个人中心'
  80. }],
  81. searchOn: false,
  82. home: {},
  83. activity: {
  84. list: [],
  85. pagination: {
  86. page: 1,
  87. rows: 10,
  88. reqdata: {
  89. keyword: ''
  90. }
  91. }
  92. },
  93. community: {
  94. showAddtidings: false,
  95. acts: [{
  96. favorite: true
  97. }],
  98. list: [],
  99. pagination: {
  100. page: 1,
  101. rows: 10,
  102. reqdata: {
  103. keyword: ''
  104. }
  105. }
  106. },
  107. mine: {
  108. list: [],
  109. curNav: 0,
  110. pagination: {
  111. page: 1,
  112. rows: 10
  113. }
  114. },
  115. linkState: false //判断执行 toLinkR 的条件
  116. // isRegister: wx.getStorageSync('user')?wx.getStorageSync('user').isRegister:null
  117. },
  118. loadArticles() {
  119. icommunity.loadArticles(this.data.community.pagination, r => {
  120. this.setData({
  121. ['community.list']: this.data.community.list.concat(r.list),
  122. ['community.pagination.page']: r.list.length > 0 ? this.data.community.pagination.page : --this.data.community.pagination.page
  123. })
  124. wx.stopPullDownRefresh()
  125. })
  126. },
  127. loadActivities() {
  128. iactivity.loadActivities(this.data.activity.pagination, r => {
  129. this.setData({
  130. ['activity.list']: this.data.activity.list.concat(r.list),
  131. ['activity.pagination.page']: r.list.length > 0 ? this.data.activity.pagination.page : --this.data.activity.pagination.page,
  132. })
  133. wx.stopPullDownRefresh()
  134. })
  135. },
  136. loadHome() {
  137. let count = 3
  138. ihome.loadBanners({},r => {
  139. console.log(r);
  140. this.setData({
  141. ['home.banners']: r.list
  142. })
  143. if (count == 1) {
  144. wx.stopPullDownRefresh()
  145. } else {
  146. count--
  147. }
  148. })
  149. ihome.loadPartners({},r => {
  150. this.setData({
  151. ['home.partners']: r.list
  152. })
  153. if (count == 1) {
  154. wx.stopPullDownRefresh()
  155. } else {
  156. count--
  157. }
  158. })
  159. ihome.loadHotnews({},r => {
  160. this.setData({
  161. ['home.hotnews']: r.list
  162. })
  163. if (count == 1) {
  164. wx.stopPullDownRefresh()
  165. } else {
  166. count--
  167. }
  168. })
  169. },
  170. onLoad(args) {
  171. this.loadHome()
  172. // this.loadConfig()
  173. },
  174. clickRemoveArticle(e) {
  175. console.log(e)
  176. wx.showModal({
  177. title: '确定删除?',
  178. content: '是否删除此动态?',
  179. confirmColor: '#f64f5f',
  180. success: r => {
  181. let {id,minenav,i} = e.currentTarget.dataset
  182. if (r.confirm) {
  183. icommunity[minenav==0?'loadDeleteArticle':'loadDeleteCollect']({
  184. reqdata:{id}
  185. }, r => {
  186. wx.showToast({
  187. title: '删除成功'
  188. })
  189. this.data.mine.list.splice(i, 1)
  190. this.setData({
  191. mine: this.data.mine
  192. })
  193. })
  194. }
  195. }
  196. })
  197. },
  198. clickFavorite(e) {
  199. let {id,isCollect} = e.currentTarget.dataset
  200. let scope = this.data.curNav == 2 ? 'community' : 'mine'
  201. let item = this.data[scope].list[e.currentTarget.dataset.i]
  202. icommunity[isCollect?'loadDeleteCollect':'loadAddArticle']({
  203. reqdata:{
  204. [isCollect?'id':'collectProjectId']:id,
  205. ['collectType']: isCollect?null:1
  206. }
  207. }, r => {
  208. item.isCollect = !item.isCollect
  209. item.collectNumber =item.isCollect? parseInt(item.collectNumber) + 1: parseInt(item.collectNumber) - 1
  210. this.setData({
  211. [scope + '.list[' + e.currentTarget.dataset.i + ']']: item
  212. })
  213. })
  214. },
  215. clickActivity(e) {
  216. if (!this.data.user || !this.data.user.id) {
  217. return this.setData({
  218. showAuthPanel: true
  219. })
  220. }
  221. app.data.nav.activityDetails = this.data.activity.list[e.currentTarget.dataset.i]
  222. wx.navigateTo({
  223. url: '../activities/activityDetails/activityDetails?id=' + e.currentTarget.dataset.id
  224. })
  225. },
  226. clickArticle(e) {
  227. console.log(e)
  228. app.data.nav.newsDetails = this.data.home.hotnews[e.currentTarget.dataset.i]
  229. wx.navigateTo({
  230. url: '../hotnews/newsDetails/newsDetails?id=' + e.currentTarget.dataset.id
  231. })
  232. },
  233. clickHotnewsMore() {
  234. wx.navigateTo({
  235. url: '../hotnews/hotnews/hotnews'
  236. })
  237. },
  238. clickMyinfo() {
  239. wx.navigateTo({
  240. url: '../mine/myinfo/edit/edit'
  241. })
  242. },
  243. clickCommunityArticle(e) {
  244. app.data.nav.communityArticle = e.currentTarget.dataset.item
  245. wx.navigateTo({
  246. url: e.currentTarget.dataset.item.fileType == 2 ? '../community/videoDetails/videoDetails' : '../community/articleDetails/articleDetails'
  247. })
  248. },
  249. clickUploadpics() {
  250. wx.navigateTo({
  251. url: '../community/uploadPics/uploadPics'
  252. })
  253. this.setData({
  254. ['community.showAddtidings']: false
  255. })
  256. },
  257. clickUploadvideos() {
  258. wx.navigateTo({
  259. url: '../community/uploadVideos/uploadVideos'
  260. })
  261. this.setData({
  262. ['community.showAddtidings']: false
  263. })
  264. },
  265. clickClosetidings() {
  266. this.setData({
  267. ['community.showAddtidings']: false
  268. })
  269. },
  270. clickAddtidings() {
  271. if (!this.data.user || !this.data.user.id) {
  272. return this.setData({
  273. showAuthPanel: true
  274. })
  275. }
  276. this.setData({
  277. ['community.showAddtidings']: true
  278. })
  279. },
  280. inputSearch(e) {
  281. if (this.data.curNav == 1) {
  282. this.setData({
  283. ['activity.pagination.reqdata.keyword']: e.detail.value,
  284. ['activity.pagination.page']: 1,
  285. ['activity.list']: [],
  286. })
  287. this.loadActivities()
  288. } else {
  289. this.setData({
  290. ['community.pagination.reqdata.keyword']: e.detail.value,
  291. ['community.pagination.page']: 1,
  292. ['community.list']: []
  293. })
  294. this.loadArticles()
  295. }
  296. },
  297. clickSearch() {
  298. this.setData({
  299. searchOn: true
  300. })
  301. },
  302. blurSearch(e) {
  303. if (!e.detail.value) {
  304. this.setData({
  305. searchOn: false
  306. })
  307. }
  308. },
  309. clickMinenav(e) {
  310. this.data.mine.pagination.page = 1
  311. this.data.mine.list = []
  312. switch (e.currentTarget.dataset.cur) {
  313. case '1':
  314. {
  315. this.loadMyCollectedarticles()
  316. };
  317. break;
  318. case '2':
  319. {
  320. this.loadMyComments()
  321. };
  322. break;
  323. case '3':
  324. {
  325. this.loadMyActivities()
  326. };
  327. break;
  328. default:
  329. {
  330. this.loadMyPubarticles()
  331. }
  332. }
  333. },
  334. clickNav(e) {
  335. switch (e.currentTarget.dataset.cur) {
  336. case '1':
  337. this.data.activity.pagination.page = 1
  338. this.data.activity.list = []
  339. this.loadActivities()
  340. break;
  341. case '2':
  342. this.data.community.pagination.page = 1
  343. this.data.community.list = []
  344. this.loadArticles()
  345. break;
  346. case '3':
  347. if (!this.data.user || !this.data.user.id) {
  348. return this.setData({
  349. showAuthPanel: true
  350. })
  351. }
  352. this.data.mine.pagination.page = 1
  353. this.data.mine.list = []
  354. this.loadMyinformation()
  355. // if (this.data.config.open_space == 1) {
  356. // this.loadMyPubarticles()
  357. // } else {
  358. // this.loadMyCollectedarticles()
  359. // }
  360. // this.loadMyCollectedarticles()
  361. this.setData({
  362. ['mine.curNav']: 0
  363. },()=>{
  364. this.loadMyPubarticles()
  365. })
  366. break;
  367. }
  368. this.setData({
  369. curNav: e.currentTarget.dataset.cur
  370. })
  371. wx.setNavigationBarTitle({
  372. title: this.data.navs[e.currentTarget.dataset.cur].name
  373. })
  374. },
  375. clickHideAuthPanel() {
  376. this.setData({
  377. showAuthPanel: false
  378. })
  379. },
  380. toLink (e) {
  381. if (!this.data.user || !this.data.user.id) {
  382. return this.setData({
  383. showAuthPanel: true
  384. })
  385. }
  386. wx.navigateTo({
  387. url: e.currentTarget.dataset.url,
  388. })
  389. },
  390. toLinkR() {
  391. if (!this.data.user || !this.data.user.id) {
  392. return this.setData({
  393. showAuthPanel: true,
  394. linkState: true
  395. })
  396. }
  397. console.log(111)
  398. this.setData({
  399. linkState: false
  400. })
  401. // user.isRegister?'/pages/contest/answer/answer':'/pages/contest/info/info'
  402. this.data.user.isRegister
  403. ? wx.showModal({
  404. title: '答题须知:',
  405. content: '1.每人每天最多答题三次\n\n 2.排行榜以得分情况计算',
  406. confirmColor: '#ff0000',
  407. cancelText: '返回',
  408. success (res) {
  409. if (res.confirm) {
  410. wx.navigateTo({
  411. url: '/pages/contest/answer/answer'
  412. })
  413. }
  414. }
  415. })
  416. : wx.navigateTo({
  417. url: '/pages/contest/info/info'
  418. })
  419. },
  420. clickStudy () {
  421. wx.showToast({
  422. title: '精彩视频,即将呈现~',
  423. icon: 'none'
  424. })
  425. },
  426. getUserInfo (e) {
  427. wx.login({
  428. success: r => {
  429. wx.getUserInfo({
  430. success: e => {
  431. let {encryptedData,iv} = e
  432. ilogin.loadAT({
  433. reqdata: {
  434. code: r.code,
  435. encryptedData,
  436. iv
  437. }
  438. }, r => {
  439. wx.setStorageSync('id', r.object.id)
  440. wx.setStorageSync('openId', r.object.openId)
  441. this.loadMyinformation()
  442. this.clickHideAuthPanel()
  443. })
  444. },
  445. })
  446. // ilogin.loadAT({
  447. // reqdata: {
  448. // code: r.code,
  449. // encryptedData,
  450. // iv
  451. // }
  452. // }, r => {
  453. // console.log(r)
  454. // })
  455. }
  456. })
  457. },
  458. onShow() {
  459. wx.getStorage({
  460. key: 'user',
  461. complete: r => {
  462. if (r.data) {
  463. this.setData({
  464. user: r.data
  465. })
  466. }
  467. }
  468. })
  469. },
  470. onPullDownRefresh() {
  471. switch (this.data.curNav) {
  472. case 1:
  473. case '1':
  474. this.data.activity.pagination.page = 1
  475. this.data.activity.list = []
  476. this.loadActivities()
  477. break;
  478. case 2:
  479. case '2':
  480. this.data.community.pagination.page = 1
  481. this.data.community.list = []
  482. this.loadArticles()
  483. break;
  484. case 3:
  485. case '3':
  486. wx.stopPullDownRefresh()
  487. break;
  488. default:
  489. this.loadHome()
  490. }
  491. },
  492. onReachBottom() {
  493. if (this.data.curNav == 1) {
  494. this.data.activity.pagination.page += 1
  495. this.loadActivities()
  496. }
  497. if (this.data.curNav == 2) {
  498. this.data.community.pagination.page += 1
  499. this.loadArticles()
  500. }
  501. if (this.data.curNav == 3) {
  502. this.data.mine.pagination.page += 1
  503. switch (this.data.mine.curNav) {
  504. case 1:
  505. case '1':
  506. this.loadMyCollectedarticles();
  507. break;
  508. case 2:
  509. case '2':
  510. this.loadMyComments()
  511. break;
  512. case 3:
  513. case '3':
  514. this.loadMyActivities()
  515. break;
  516. default:
  517. this.loadMyPubarticles()
  518. }
  519. }
  520. },
  521. loadConfig() {
  522. ilogin.loadConfig({},r => {
  523. app.config = r
  524. this.setData({
  525. config: r
  526. })
  527. })
  528. },
  529. onShareAppMessage() {
  530. return {
  531. title: '冰雪小程序',
  532. path: '/pages/home/home',
  533. imageUrl: '../../imgs/share2.jpg',
  534. success: function(res) {},
  535. fail: function(res) {
  536. console.log(res, '失败')    
  537. }
  538. }
  539. }
  540. })