index.js 12 KB

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