index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. var host = app.globalData.host;
  5. Page({
  6. data: {
  7. zimu_list: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
  8. scroll: '', //滚动到指定 id值的子元素
  9. touchmove: 0,//给字母导航添加背景色 1 添加 0不添加
  10. nav_height: '',//字母导航单个元素高度
  11. nav_top:'',
  12. hiddenn: true,//hint_box 提示框 展示隐藏
  13. nav_text: '',//hint_box 提示框里面的文本
  14. lb:[],
  15. act:3,
  16. journals:[],
  17. organizers:[],
  18. subjects:[],
  19. sub_list:[],
  20. show:1,
  21. uid:'',
  22. page_org:1,
  23. page_sub:1,
  24. total_org:0,
  25. total_sub:0,
  26. cycleId: 'day',
  27. value: ['日榜','周榜','月榜'],
  28. select:1,
  29. day:'',
  30. week:'',
  31. month: '',
  32. m_value:0,
  33. show_month:0,
  34. sub_value:1,
  35. week_h:0,
  36. casci_list:[],
  37. page_csi:1
  38. },
  39. onLoad: function () {
  40. var y = new Date().getFullYear() ,m=(new Date().getMonth() + 1) ,d=new Date().getDate();
  41. m = m > 9 ? m : '0' + m;
  42. d = d > 9 ? d : '0' + d;
  43. this.setData({
  44. day:y+'-'+m+'-'+d,
  45. month: y + '-' + m
  46. })
  47. this.getTimer()
  48. if (this.data.uid){
  49. this.setData({
  50. organizers: [],
  51. subjects: [],
  52. page_org: 1,
  53. page_sub: 1,
  54. total_org: 0,
  55. total_sub: 0
  56. })
  57. this.getData();
  58. }else{
  59. this.setData({
  60. openid: app.globalData.openid
  61. })
  62. wx.getStorage({
  63. key: 'uid',
  64. success: res => {
  65. if (res.data) {
  66. this.setData({ show: 0, uid: res.data })
  67. }
  68. this.getData();
  69. },
  70. })
  71. }
  72. },
  73. /**
  74. * 周期切换点击
  75. * @param {*} e
  76. */
  77. onTabItemClick: function onTabItemClick(e) {
  78. var id = e.currentTarget.dataset.tabs;
  79. this.setData({
  80. select:id
  81. })
  82. },
  83. //周榜
  84. changeMonth:function(e){
  85. var id = e.currentTarget.dataset.id;
  86. this.setData({
  87. m_value: id
  88. })
  89. },
  90. monthSelect: function () {
  91. var week=this.data.columns[this.data.m_value]
  92. this.setData({
  93. week: week,
  94. show_month:0
  95. })
  96. },
  97. open: function () {
  98. this.setData({
  99. show_month: 1,
  100. week_h: this.data.m_value*40
  101. })
  102. },
  103. close:function(){
  104. this.setData({
  105. show_month: 0
  106. })
  107. },
  108. // 日榜
  109. bindDateChange:function(e){
  110. this.setData({
  111. day: e.detail.value
  112. })
  113. },
  114. //领域期刊
  115. subChange:function(e){
  116. this.setData({
  117. sub_value: e.detail.value
  118. })
  119. },
  120. //月榜
  121. bindDateChange1: function (e) {
  122. this.setData({
  123. month: e.detail.value
  124. })
  125. },
  126. onGotUserInfo: function () {
  127. wx.getUserProfile({
  128. desc: '用于完善会员资料',
  129. success: res=> {
  130. //获取基本信息
  131. var data = {
  132. nickName: res.userInfo.nickName,
  133. avatarUrl: res.userInfo.avatarUrl
  134. }
  135. wx.showLoading({
  136. title: '正在登陆',
  137. })
  138. wx.setStorage({
  139. key: 'userInfo',
  140. data: data,
  141. })
  142. wx.request({
  143. url: host + '/api/auth',
  144. method: 'POST',
  145. data: {
  146. nickname: data.nickName,
  147. avatar: data.avatarUrl,
  148. openid: app.globalData.openid,
  149. },
  150. success: res => {
  151. wx.hideLoading()
  152. wx.setStorage({
  153. key: 'uid',
  154. data: res.data.data.uid,
  155. })
  156. this.setData({
  157. show: 0,
  158. uid: res.data.data.uid
  159. })
  160. this.getData()
  161. },
  162. fail:error=>{
  163. wx.hideLoading()
  164. wx.showLoading({
  165. title: '登陆失败',
  166. })
  167. }
  168. })
  169. }
  170. })
  171. },
  172. //主办单位下一页
  173. next_org:function(){
  174. if(this.data.organizers.length < this.data.total_org){
  175. const page = this.data.page_org+1
  176. this.setData({
  177. page_org: page
  178. })
  179. this.getorg()
  180. }
  181. },
  182. //领域期刊下一页
  183. next_sub:function(){
  184. if(this.data.subjects.length < this.data.total_sub){
  185. const page = this.data.page_sub+1
  186. this.setData({
  187. page_sub: page
  188. })
  189. this.getsub()
  190. }
  191. },
  192. //获取所有数据
  193. getData:function(flag){
  194. wx.showNavigationBarLoading()
  195. var that=this;
  196. if(!flag){
  197. wx.request({
  198. url: host+'/api/index/banners',
  199. method:'GET',
  200. success:function(res){
  201. that.setData({
  202. lb: res.data.data.banners
  203. })
  204. },
  205. fail: function () {
  206. wx.hideNavigationBarLoading()
  207. wx.showToast({
  208. title: '服务器开小差啦!',
  209. icon: 'none'
  210. })
  211. }
  212. })
  213. // 全刊
  214. wx.request({
  215. url: host + '/api/index/journals',
  216. method: 'GET',
  217. success: function (res) {
  218. wx.hideNavigationBarLoading()
  219. that.setData({
  220. journals: res.data.data.journals
  221. })
  222. }
  223. })
  224. }
  225. //全部数据
  226. wx.request({
  227. url: host + '/api/index/subjects',
  228. method: 'GET',
  229. data: {
  230. uid: this.data.uid
  231. },
  232. success: res => {
  233. if (res.data.code == 0) {
  234. const list = res.data.data.list;
  235. this.setData({
  236. sub_list: list
  237. })
  238. this.getcasci()
  239. }
  240. }
  241. })
  242. this.getorg()
  243. this.getsub()
  244. },
  245. //指数
  246. getcasci:function(){
  247. console.log(233)
  248. var subject_id=this.data.sub_list[this.data.sub_value].id,date='';
  249. if (this.data.select == 0){
  250. date = this.data.day
  251. }
  252. if (this.data.select == 1) {
  253. date = this.data.week.replace('至',',')
  254. }
  255. if (this.data.select == 2) {
  256. date = this.data.month
  257. }
  258. wx.request({
  259. url: host + '/api/casci/list',
  260. method: 'GET',
  261. data: {
  262. uid: this.data.uid,
  263. date: date,
  264. signup_id: subject_id,
  265. // page:this.data.page_csi,
  266. // page_size:20
  267. },
  268. success: res => {
  269. wx.hideNavigationBarLoading()
  270. if (res.data.code == 0) {
  271. const list = res.data.data[1];
  272. this.setData({
  273. casci_list: list
  274. })
  275. }
  276. }
  277. })
  278. },
  279. //主办单位
  280. getorg:function(){
  281. wx.showNavigationBarLoading()
  282. const organizers=this.data.organizers;
  283. wx.request({
  284. url: host + '/api/index/organizers',
  285. method: 'GET',
  286. data: {
  287. uid: this.data.uid,
  288. page: this.data.page_org
  289. },
  290. success: res=>{
  291. wx.hideNavigationBarLoading()
  292. if(res.data.code == 0){
  293. const list=res.data.data.list;
  294. for(let i=0;i<list.length;i++){
  295. organizers.push(list[i])
  296. }
  297. this.setData({
  298. organizers: organizers,
  299. total_org:res.data.data.total
  300. })
  301. }
  302. }
  303. })
  304. },
  305. //领域期刊
  306. getsub:function(){
  307. wx.showNavigationBarLoading()
  308. const subjects=this.data.subjects;
  309. wx.request({
  310. url: host + '/api/index/subjects',
  311. method: 'GET',
  312. data: {
  313. uid: this.data.uid,
  314. page: this.data.page_sub
  315. },
  316. success: res=>{
  317. wx.hideNavigationBarLoading()
  318. if(res.data.code == 0){
  319. const list=res.data.data.list;
  320. for(let i=0;i<list.length;i++){
  321. subjects.push(list[i])
  322. }
  323. this.setData({
  324. subjects: subjects,
  325. total_sub:res.data.data.total
  326. })
  327. }
  328. }
  329. })
  330. },
  331. search:function(){
  332. wx.navigateTo({
  333. url: '../searchList/searchList',
  334. })
  335. },
  336. //置顶
  337. top: function(e){
  338. var type = e.target.dataset.type;
  339. wx.request({
  340. url: host+'/api/index/totop',
  341. method:'PUT',
  342. data:{
  343. id:e.target.dataset.id,
  344. type: type,
  345. uid:this.data.uid,
  346. action:'do'
  347. },
  348. success: res=>{
  349. wx.showToast({
  350. title: '置顶成功',
  351. icon:'none'
  352. })
  353. if (type == 'organizer'){
  354. this.setData({
  355. organizers:[],
  356. page_org:1
  357. })
  358. this.getorg()
  359. }else{
  360. this.setData({
  361. subjects: [],
  362. page_sub: 1
  363. })
  364. this.getsub()
  365. }
  366. }
  367. })
  368. },
  369. cancel_top: function (e) {
  370. var type = e.target.dataset.type;
  371. wx.request({
  372. url: host + '/api/index/totop',
  373. method: 'PUT',
  374. data: {
  375. id: e.target.dataset.id,
  376. type: type,
  377. uid: this.data.uid,
  378. action:'cancel'
  379. },
  380. success: res=> {
  381. wx.showToast({
  382. title: '已取消',
  383. icon: 'none'
  384. })
  385. if (type == 'organizer') {
  386. this.setData({
  387. organizers: [],
  388. page_org: 1
  389. })
  390. this.getorg()
  391. } else {
  392. this.setData({
  393. subjects: [],
  394. page_sub: 1
  395. })
  396. this.getsub()
  397. }
  398. }
  399. })
  400. },
  401. tab:function(e){
  402. this.setData({
  403. act: e.target.dataset.id
  404. })
  405. },
  406. swchange:function(e){
  407. this.setData({
  408. act: e.detail.current
  409. })
  410. },
  411. onReady: function () {
  412. // this.get_height();
  413. },
  414. //事件处理函数
  415. touchstart: function (e) {
  416. this.set_scroll(e)
  417. },
  418. touchmove: function (e) {
  419. this.set_scroll(e)
  420. },
  421. touchend: function () {
  422. this.setData({
  423. touchmove: 0,
  424. hiddenn: true
  425. })
  426. },
  427. scroll(e){
  428. var that = this
  429. this.setData({
  430. scroll:e.target.dataset.id,
  431. })
  432. },
  433. set_scroll: function (e) {
  434. var page_y = e.changedTouches[0].pageY - this.data.nav_top;
  435. // console.log(page_y)
  436. var nav_height = +this.data.nav_height
  437. var idx = Math.floor(page_y / nav_height)
  438. var zimu = this.data.zimu_list[idx];
  439. this.setData({
  440. touchmove: 1,
  441. scroll: zimu,
  442. nav_text: zimu,
  443. hiddenn: false
  444. })
  445. },
  446. get_height: function () {
  447. var that = this
  448. var query = wx.createSelectorQuery();
  449. query.select('#nav_item').boundingClientRect()
  450. wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) {
  451. that.setData({
  452. nav_top: rect.top,
  453. })
  454. }).exec()
  455. query.exec(function (res) {
  456. that.setData({
  457. nav_height: res[0].height,
  458. })
  459. })
  460. },
  461. getUserInfo: function(e) {
  462. app.globalData.userInfo = e.detail.userInfo
  463. this.setData({
  464. userInfo: e.detail.userInfo,
  465. hasUserInfo: true
  466. })
  467. },
  468. toDetail:function(e){
  469. // console.log(e)
  470. },
  471. toBanner:function(e){
  472. var url = e.target.dataset.id
  473. wx.navigateTo({
  474. url: url,
  475. })
  476. },
  477. /**
  478. * 页面相关事件处理函数--监听用户下拉动作
  479. */
  480. onPullDownRefresh: function () {
  481. wx.startPullDownRefresh()
  482. },
  483. getTimer() {
  484. let time = new Date() // 获取当前时间
  485. let nowTime = time.getTime()
  486. let day = time.getDay()
  487. let oneDayTime = 24 * 60 * 60 * 1000; // 一周的时间
  488. let MondayTime = nowTime - (day - 1) * oneDayTime; //显示当前周一
  489. let SundayTime = nowTime + (7 - day) * oneDayTime; //显示当前周日
  490. let setlist = [] // 初始化一个空数组 准备装食物--你好骚啊
  491. for (let i = 0; i < 365*3; i++) {
  492. // 这块我不知道怎么回事一直会有重复的push进去,后面做了去重,可以放心食用,当然也可以优化一下
  493. setlist.push(this.setTime(MondayTime) + ' 至 ' + this.setTime(SundayTime)) // this.setTime()在下面 放心食用
  494. // 重点 push完,赶紧让它获取上一周的时间 oneDayTime上面有写
  495. time = new Date(time - oneDayTime)
  496. nowTime = time.getTime()
  497. day = time.getDay()
  498. MondayTime = nowTime - (day - 1) * oneDayTime; //显示当前周一
  499. SundayTime = nowTime + (7 - day) * oneDayTime; //显示当前周日
  500. }
  501. let list = [...new Set(setlist)] // 简单去重一下
  502. // console.log(list);
  503. this.setData({
  504. columns: list,
  505. week: list[0]
  506. })
  507. },
  508. setTime(time) {
  509. // 将time时间戳 先格式化一下
  510. let date = new Date(time)
  511. let yy = date.getFullYear()
  512. let m = date.getMonth() + 1
  513. let day = date.getDate()
  514. let str = yy + '-' + (m < 10 ? '0' + m : m) + '-' + (day < 10 ? '0' + day : day)
  515. return str
  516. }
  517. })