home.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. isRegister: wx.getStorageSync('user')?wx.getStorageSync('user').isRegister:null
  118. },
  119. loadArticles() {
  120. icommunity.loadArticles(this.data.community.pagination, r => {
  121. this.setData({
  122. ['community.list']: this.data.community.list.concat(r.list),
  123. ['community.pagination.page']: r.list.length > 0 ? this.data.community.pagination.page : --this.data.community.pagination.page
  124. })
  125. wx.stopPullDownRefresh()
  126. })
  127. },
  128. loadActivities() {
  129. iactivity.loadActivities(this.data.activity.pagination, r => {
  130. this.setData({
  131. ['activity.list']: this.data.activity.list.concat(r.list),
  132. ['activity.pagination.page']: r.list.length > 0 ? this.data.activity.pagination.page : --this.data.activity.pagination.page,
  133. })
  134. wx.stopPullDownRefresh()
  135. })
  136. },
  137. loadHome() {
  138. let count = 3
  139. ihome.loadBanners({},r => {
  140. console.log(r);
  141. this.setData({
  142. ['home.banners']: r.list
  143. })
  144. if (count == 1) {
  145. wx.stopPullDownRefresh()
  146. } else {
  147. count--
  148. }
  149. })
  150. ihome.loadPartners({},r => {
  151. this.setData({
  152. ['home.partners']: r.list
  153. })
  154. if (count == 1) {
  155. wx.stopPullDownRefresh()
  156. } else {
  157. count--
  158. }
  159. })
  160. ihome.loadHotnews({},r => {
  161. this.setData({
  162. ['home.hotnews']: r.list
  163. })
  164. if (count == 1) {
  165. wx.stopPullDownRefresh()
  166. } else {
  167. count--
  168. }
  169. })
  170. },
  171. onLoad(args) {
  172. this.loadHome()
  173. // this.loadConfig()
  174. },
  175. clickRemoveArticle(e) {
  176. console.log(e)
  177. wx.showModal({
  178. title: '确定删除?',
  179. content: '是否删除此动态?',
  180. confirmColor: '#f64f5f',
  181. success: r => {
  182. let {id,minenav,i} = e.currentTarget.dataset
  183. if (r.confirm) {
  184. icommunity[minenav==0?'loadDeleteArticle':'loadDeleteCollect']({
  185. reqdata:{id}
  186. }, r => {
  187. wx.showToast({
  188. title: '删除成功'
  189. })
  190. this.data.mine.list.splice(i, 1)
  191. this.setData({
  192. mine: this.data.mine
  193. })
  194. })
  195. }
  196. }
  197. })
  198. },
  199. clickFavorite(e) {
  200. let {id,isCollect} = e.currentTarget.dataset
  201. let scope = this.data.curNav == 2 ? 'community' : 'mine'
  202. let item = this.data[scope].list[e.currentTarget.dataset.i]
  203. icommunity[isCollect?'loadDeleteCollect':'loadAddArticle']({
  204. reqdata:{
  205. [isCollect?'id':'collectProjectId']:id,
  206. ['collectType']: isCollect?null:1
  207. }
  208. }, r => {
  209. item.isCollect = !item.isCollect
  210. item.collectNumber =item.isCollect? parseInt(item.collectNumber) + 1: parseInt(item.collectNumber) - 1
  211. this.setData({
  212. [scope + '.list[' + e.currentTarget.dataset.i + ']']: item
  213. })
  214. })
  215. },
  216. clickActivity(e) {
  217. app.data.nav.activityDetails = this.data.activity.list[e.currentTarget.dataset.i]
  218. wx.navigateTo({
  219. url: '../activities/activityDetails/activityDetails?id=' + e.currentTarget.dataset.id
  220. })
  221. },
  222. clickArticle(e) {
  223. console.log(e)
  224. app.data.nav.newsDetails = this.data.home.hotnews[e.currentTarget.dataset.i]
  225. wx.navigateTo({
  226. url: '../hotnews/newsDetails/newsDetails?id=' + e.currentTarget.dataset.id
  227. })
  228. },
  229. clickHotnewsMore() {
  230. wx.navigateTo({
  231. url: '../hotnews/hotnews/hotnews'
  232. })
  233. },
  234. clickMyinfo() {
  235. wx.navigateTo({
  236. url: '../mine/myinfo/edit/edit'
  237. })
  238. },
  239. clickCommunityArticle(e) {
  240. app.data.nav.communityArticle = e.currentTarget.dataset.item
  241. wx.navigateTo({
  242. url: e.currentTarget.dataset.item.fileType == 2 ? '../community/videoDetails/videoDetails' : '../community/articleDetails/articleDetails'
  243. })
  244. },
  245. clickUploadpics() {
  246. wx.navigateTo({
  247. url: '../community/uploadPics/uploadPics'
  248. })
  249. this.setData({
  250. ['community.showAddtidings']: false
  251. })
  252. },
  253. clickUploadvideos() {
  254. wx.navigateTo({
  255. url: '../community/uploadVideos/uploadVideos'
  256. })
  257. this.setData({
  258. ['community.showAddtidings']: false
  259. })
  260. },
  261. clickClosetidings() {
  262. this.setData({
  263. ['community.showAddtidings']: false
  264. })
  265. },
  266. clickAddtidings() {
  267. if (!this.data.user) {
  268. return this.setData({
  269. showAuthPanel: true
  270. })
  271. }
  272. this.setData({
  273. ['community.showAddtidings']: true
  274. })
  275. },
  276. inputSearch(e) {
  277. if (this.data.curNav == 1) {
  278. this.setData({
  279. ['activity.pagination.reqdata.keyword']: e.detail.value,
  280. ['activity.pagination.page']: 1,
  281. ['activity.list']: [],
  282. })
  283. this.loadActivities()
  284. } else {
  285. this.setData({
  286. ['community.pagination.reqdata.keyword']: e.detail.value,
  287. ['community.pagination.page']: 1,
  288. ['community.list']: []
  289. })
  290. this.loadArticles()
  291. }
  292. },
  293. clickSearch() {
  294. this.setData({
  295. searchOn: true
  296. })
  297. },
  298. blurSearch(e) {
  299. if (!e.detail.value) {
  300. this.setData({
  301. searchOn: false
  302. })
  303. }
  304. },
  305. clickMinenav(e) {
  306. this.data.mine.pagination.page = 1
  307. this.data.mine.list = []
  308. switch (e.currentTarget.dataset.cur) {
  309. case '1':
  310. {
  311. this.loadMyCollectedarticles()
  312. };
  313. break;
  314. case '2':
  315. {
  316. this.loadMyComments()
  317. };
  318. break;
  319. case '3':
  320. {
  321. this.loadMyActivities()
  322. };
  323. break;
  324. default:
  325. {
  326. this.loadMyPubarticles()
  327. }
  328. }
  329. },
  330. clickNav(e) {
  331. switch (e.currentTarget.dataset.cur) {
  332. case '1':
  333. this.data.activity.pagination.page = 1
  334. this.data.activity.list = []
  335. this.loadActivities()
  336. break;
  337. case '2':
  338. this.data.community.pagination.page = 1
  339. this.data.community.list = []
  340. this.loadArticles()
  341. break;
  342. case '3':
  343. if (!this.data.user || !this.data.user.id) {
  344. return this.setData({
  345. showAuthPanel: true
  346. })
  347. }
  348. this.data.mine.pagination.page = 1
  349. this.data.mine.list = []
  350. this.loadMyinformation()
  351. // if (this.data.config.open_space == 1) {
  352. // this.loadMyPubarticles()
  353. // } else {
  354. // this.loadMyCollectedarticles()
  355. // }
  356. // this.loadMyCollectedarticles()
  357. this.setData({
  358. ['mine.curNav']: 0
  359. },()=>{
  360. this.loadMyPubarticles()
  361. })
  362. break;
  363. }
  364. this.setData({
  365. curNav: e.currentTarget.dataset.cur
  366. })
  367. wx.setNavigationBarTitle({
  368. title: this.data.navs[e.currentTarget.dataset.cur].name
  369. })
  370. },
  371. clickHideAuthPanel() {
  372. this.setData({
  373. showAuthPanel: false
  374. })
  375. },
  376. toLink (e) {
  377. if (!this.data.user || !this.data.user.id) {
  378. return this.setData({
  379. showAuthPanel: true
  380. })
  381. }
  382. wx.navigateTo({
  383. url: e.currentTarget.dataset.url,
  384. })
  385. },
  386. getUserInfo (e) {
  387. wx.login({
  388. success: r => {
  389. wx.getUserInfo({
  390. success: e => {
  391. let {encryptedData,iv} = e
  392. ilogin.loadAT({
  393. reqdata: {
  394. code: r.code,
  395. encryptedData,
  396. iv
  397. }
  398. }, r => {
  399. wx.setStorageSync('id', r.object.id)
  400. wx.setStorageSync('openId', r.object.openId)
  401. this.loadMyinformation()
  402. this.clickHideAuthPanel()
  403. this.setData({
  404. isRegister: r.object.isRegister
  405. })
  406. })
  407. },
  408. })
  409. // ilogin.loadAT({
  410. // reqdata: {
  411. // code: r.code,
  412. // encryptedData,
  413. // iv
  414. // }
  415. // }, r => {
  416. // console.log(r)
  417. // })
  418. }
  419. })
  420. },
  421. onShow() {
  422. wx.getStorage({
  423. key: 'user',
  424. complete: r => {
  425. if (r.data) {
  426. this.setData({
  427. user: r.data
  428. })
  429. }
  430. }
  431. })
  432. },
  433. onPullDownRefresh() {
  434. switch (this.data.curNav) {
  435. case 1:
  436. case '1':
  437. this.data.activity.pagination.page = 1
  438. this.data.activity.list = []
  439. this.loadActivities()
  440. break;
  441. case 2:
  442. case '2':
  443. this.data.community.pagination.page = 1
  444. this.data.community.list = []
  445. this.loadArticles()
  446. break;
  447. case 3:
  448. case '3':
  449. wx.stopPullDownRefresh()
  450. break;
  451. default:
  452. this.loadHome()
  453. }
  454. },
  455. onReachBottom() {
  456. if (this.data.curNav == 1) {
  457. this.data.activity.pagination.page += 1
  458. this.loadActivities()
  459. }
  460. if (this.data.curNav == 2) {
  461. this.data.community.pagination.page += 1
  462. this.loadArticles()
  463. }
  464. if (this.data.curNav == 3) {
  465. this.data.mine.pagination.page += 1
  466. switch (this.data.mine.curNav) {
  467. case 1:
  468. case '1':
  469. this.loadMyCollectedarticles();
  470. break;
  471. case 2:
  472. case '2':
  473. this.loadMyComments()
  474. break;
  475. case 3:
  476. case '3':
  477. this.loadMyActivities()
  478. break;
  479. default:
  480. this.loadMyPubarticles()
  481. }
  482. }
  483. },
  484. loadConfig() {
  485. ilogin.loadConfig({},r => {
  486. app.config = r
  487. this.setData({
  488. config: r
  489. })
  490. })
  491. },
  492. onShareAppMessage() {
  493. return {
  494. title: '冰雪小程序',
  495. path: '/pages/home/home',
  496. imageUrl: '../../imgs/share2.jpg',
  497. success: function(res) {},
  498. fail: function(res) {
  499. console.log(res, '失败')    
  500. }
  501. }
  502. }
  503. })