home.js 13 KB

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