home.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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.setStorage({
  18. key: 'user',
  19. data: this.data.user
  20. })
  21. })
  22. },
  23. loadMyActivities() {
  24. imine.loadMyActivities(this.data.mine.pagination, r => {
  25. this.setData({
  26. ['mine.curNav']: 3,
  27. ['mine.list']: this.data.mine.list.concat(r.list),
  28. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  29. })
  30. })
  31. },
  32. loadMyComments() {
  33. imine.loadMyComments(this.data.mine.pagination, r => {
  34. for (let c of r.list) {
  35. c.showtime = new Date(c.comment_addtime).getDateName('hh:mm')
  36. }
  37. this.setData({
  38. ['mine.curNav']: 2,
  39. ['mine.list']: this.data.mine.list.concat(r.list),
  40. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  41. })
  42. })
  43. },
  44. loadMyPubarticles() {
  45. imine.loadMyPubarticles(this.data.mine.pagination, r => {
  46. this.setData({
  47. ['mine.curNav']: 0,
  48. ['mine.list']: this.data.mine.list.concat(r.list),
  49. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  50. })
  51. })
  52. },
  53. loadMyCollectedarticles() {
  54. imine.loadMyCollectedarticles(this.data.mine.pagination, r => {
  55. this.setData({
  56. ['mine.curNav']: 1,
  57. ['mine.list']: this.data.mine.list.concat(r.list).map(item => {
  58. item.status_attend = Date.now() < new Date(item.applyStartTime).valueOf() ? '未开始' : Date.now() > new Date(item.applyEndTime).valueOf() ? '已结束' :''
  59. return item
  60. }),
  61. ['mine.pagination.page']: r.list.length > 0 ? this.data.mine.pagination.page : --this.data.mine.pagination.page
  62. })
  63. })
  64. },
  65. data: {
  66. showAuthPanel: false,
  67. curNav: 0,
  68. user: {},
  69. imgUrl: app.globalData.imgUrl,
  70. navs: [{
  71. key: 'home',
  72. name: '首页'
  73. }, {
  74. key: 'activity',
  75. name: '活动动态'
  76. }, {
  77. key: 'community',
  78. name: '社区'
  79. }, {
  80. key: 'mine',
  81. name: '个人中心'
  82. }],
  83. searchOn: false,
  84. home: {},
  85. activity: {
  86. list: [],
  87. pagination: {
  88. page: 1,
  89. rows: 10,
  90. reqdata: {
  91. keyword: ''
  92. }
  93. }
  94. },
  95. community: {
  96. showAddtidings: false,
  97. acts: [{
  98. favorite: true
  99. }],
  100. list: [],
  101. pagination: {
  102. page: 1,
  103. rows: 10,
  104. reqdata: {
  105. keyword: ''
  106. }
  107. }
  108. },
  109. mine: {
  110. list: [],
  111. curNav: 0,
  112. pagination: {
  113. page: 1,
  114. rows: 10
  115. }
  116. }
  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. app.data.nav.activityDetails = this.data.activity.list[e.currentTarget.dataset.i]
  217. wx.navigateTo({
  218. url: '../activities/activityDetails/activityDetails?id=' + e.currentTarget.dataset.id
  219. })
  220. },
  221. clickArticle(e) {
  222. console.log(e)
  223. app.data.nav.newsDetails = this.data.home.hotnews[e.currentTarget.dataset.i]
  224. wx.navigateTo({
  225. url: '../hotnews/newsDetails/newsDetails?id=' + e.currentTarget.dataset.id
  226. })
  227. },
  228. clickHotnewsMore() {
  229. wx.navigateTo({
  230. url: '../hotnews/hotnews/hotnews'
  231. })
  232. },
  233. clickMyinfo() {
  234. wx.navigateTo({
  235. url: '../mine/myinfo/edit/edit'
  236. })
  237. },
  238. clickCommunityArticle(e) {
  239. app.data.nav.communityArticle = e.currentTarget.dataset.item
  240. wx.navigateTo({
  241. url: e.currentTarget.dataset.item.fileType == 2 ? '../community/videoDetails/videoDetails' : '../community/articleDetails/articleDetails'
  242. })
  243. },
  244. clickUploadpics() {
  245. wx.navigateTo({
  246. url: '../community/uploadPics/uploadPics'
  247. })
  248. this.setData({
  249. ['community.showAddtidings']: false
  250. })
  251. },
  252. clickUploadvideos() {
  253. wx.navigateTo({
  254. url: '../community/uploadVideos/uploadVideos'
  255. })
  256. this.setData({
  257. ['community.showAddtidings']: false
  258. })
  259. },
  260. clickClosetidings() {
  261. this.setData({
  262. ['community.showAddtidings']: false
  263. })
  264. },
  265. clickAddtidings() {
  266. if (!this.data.user) {
  267. return this.setData({
  268. showAuthPanel: true
  269. })
  270. }
  271. this.setData({
  272. ['community.showAddtidings']: true
  273. })
  274. },
  275. inputSearch(e) {
  276. if (this.data.curNav == 1) {
  277. this.setData({
  278. ['activity.pagination.reqdata.keyword']: e.detail.value,
  279. ['activity.pagination.page']: 1,
  280. ['activity.list']: [],
  281. })
  282. this.loadActivities()
  283. } else {
  284. this.setData({
  285. ['community.pagination.reqdata.keyword']: e.detail.value,
  286. ['community.pagination.page']: 1,
  287. ['community.list']: []
  288. })
  289. this.loadArticles()
  290. }
  291. },
  292. clickSearch() {
  293. this.setData({
  294. searchOn: true
  295. })
  296. },
  297. blurSearch(e) {
  298. if (!e.detail.value) {
  299. this.setData({
  300. searchOn: false
  301. })
  302. }
  303. },
  304. clickMinenav(e) {
  305. this.data.mine.pagination.page = 1
  306. this.data.mine.list = []
  307. switch (e.currentTarget.dataset.cur) {
  308. case '1':
  309. {
  310. this.loadMyCollectedarticles()
  311. };
  312. break;
  313. case '2':
  314. {
  315. this.loadMyComments()
  316. };
  317. break;
  318. case '3':
  319. {
  320. this.loadMyActivities()
  321. };
  322. break;
  323. default:
  324. {
  325. this.loadMyPubarticles()
  326. }
  327. }
  328. },
  329. clickNav(e) {
  330. switch (e.currentTarget.dataset.cur) {
  331. case '1':
  332. this.data.activity.pagination.page = 1
  333. this.data.activity.list = []
  334. this.loadActivities()
  335. break;
  336. case '2':
  337. this.data.community.pagination.page = 1
  338. this.data.community.list = []
  339. this.loadArticles()
  340. break;
  341. case '3':
  342. if (!this.data.user || !this.data.user.id) {
  343. return this.setData({
  344. showAuthPanel: true
  345. })
  346. }
  347. this.data.mine.pagination.page = 1
  348. this.data.mine.list = []
  349. this.loadMyinformation()
  350. // if (this.data.config.open_space == 1) {
  351. // this.loadMyPubarticles()
  352. // } else {
  353. // this.loadMyCollectedarticles()
  354. // }
  355. // this.loadMyCollectedarticles()
  356. this.setData({
  357. ['mine.curNav']: 0
  358. },()=>{
  359. this.loadMyPubarticles()
  360. })
  361. break;
  362. }
  363. this.setData({
  364. curNav: e.currentTarget.dataset.cur
  365. })
  366. wx.setNavigationBarTitle({
  367. title: this.data.navs[e.currentTarget.dataset.cur].name
  368. })
  369. },
  370. clickHideAuthPanel() {
  371. this.setData({
  372. showAuthPanel: false
  373. })
  374. },
  375. toLink (e) {
  376. wx.navigateTo({
  377. url: e.currentTarget.dataset.url,
  378. })
  379. },
  380. // getUserinfo(e) {
  381. // let {encryptedData,iv} = e.detail
  382. // console.log(e);
  383. // wx.login({
  384. // success: r => {
  385. // ilogin.loadAT({
  386. // reqdata: {
  387. // code: r.code,
  388. // encryptedData,
  389. // iv
  390. // }
  391. // }, r => {
  392. // console.log(r)
  393. // })
  394. // }
  395. // })
  396. // },
  397. getUserInfo (e) {
  398. wx.login({
  399. success: r => {
  400. wx.getUserInfo({
  401. success: e => {
  402. let {encryptedData,iv} = e
  403. ilogin.loadAT({
  404. reqdata: {
  405. code: r.code,
  406. encryptedData,
  407. iv
  408. }
  409. }, r => {
  410. wx.setStorageSync('id', r.object.id)
  411. wx.setStorageSync('openId', r.object.openId)
  412. this.loadMyinformation()
  413. this.clickHideAuthPanel()
  414. })
  415. },
  416. })
  417. // ilogin.loadAT({
  418. // reqdata: {
  419. // code: r.code,
  420. // encryptedData,
  421. // iv
  422. // }
  423. // }, r => {
  424. // console.log(r)
  425. // })
  426. }
  427. })
  428. },
  429. onShow() {
  430. wx.getStorage({
  431. key: 'user',
  432. complete: r => {
  433. if (r.data) {
  434. this.setData({
  435. user: r.data
  436. })
  437. }
  438. }
  439. })
  440. },
  441. onPullDownRefresh() {
  442. switch (this.data.curNav) {
  443. case 1:
  444. case '1':
  445. this.data.activity.pagination.page = 1
  446. this.data.activity.list = []
  447. this.loadActivities()
  448. break;
  449. case 2:
  450. case '2':
  451. this.data.community.pagination.page = 1
  452. this.data.community.list = []
  453. this.loadArticles()
  454. break;
  455. case 3:
  456. case '3':
  457. wx.stopPullDownRefresh()
  458. break;
  459. default:
  460. this.loadHome()
  461. }
  462. },
  463. onReachBottom() {
  464. if (this.data.curNav == 1) {
  465. this.data.activity.pagination.page += 1
  466. this.loadActivities()
  467. }
  468. if (this.data.curNav == 2) {
  469. this.data.community.pagination.page += 1
  470. this.loadArticles()
  471. }
  472. if (this.data.curNav == 3) {
  473. this.data.mine.pagination.page += 1
  474. switch (this.data.mine.curNav) {
  475. case 1:
  476. case '1':
  477. this.loadMyCollectedarticles();
  478. break;
  479. case 2:
  480. case '2':
  481. this.loadMyComments()
  482. break;
  483. case 3:
  484. case '3':
  485. this.loadMyActivities()
  486. break;
  487. default:
  488. this.loadMyPubarticles()
  489. }
  490. }
  491. },
  492. loadConfig() {
  493. ilogin.loadConfig({},r => {
  494. app.config = r
  495. this.setData({
  496. config: r
  497. })
  498. })
  499. },
  500. onShareAppMessage() {
  501. return {
  502. title: '冰雪小程序',
  503. path: '/pages/home/home',
  504. imageUrl: '../../imgs/share2.jpg',
  505. success: function(res) {},
  506. fail: function(res) {
  507. console.log(res, '失败')    
  508. }
  509. }
  510. }
  511. })