home.js 12 KB

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