//index.js //获取应用实例 const app = getApp() var host = app.globalData.host; Page({ data: { 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'], scroll: '', //滚动到指定 id值的子元素 touchmove: 0,//给字母导航添加背景色 1 添加 0不添加 nav_height: '',//字母导航单个元素高度 nav_top:'', hiddenn: true,//hint_box 提示框 展示隐藏 nav_text: '',//hint_box 提示框里面的文本 lb:[], act:0, journals:[], organizers:[], subjects:[], show:1, uid:'' }, onLoad: function () { this.setData({ openid: app.globalData.openid }) wx.getStorage({ key: 'uid', success: res => { if (res.data) { this.setData({ show: 0 ,uid:res.data}) } }, }) this.getData(); }, onGotUserInfo: function (e) { wx.setStorage({ key: 'userInfo', data: e.detail.userInfo, }) wx.request({ url: host + '/api/auth', method: 'POST', data: { nickname: e.detail.userInfo.nickName, avatar: e.detail.userInfo.avatarUrl, openid: app.globalData.openid, }, success: res => { console.log(res) wx.setStorage({ key: 'uid', data: res.data.data.uid, }) this.setData({ show: 0 }) } }) }, //获取所有数据 getData:function(flag){ wx.showNavigationBarLoading() var that=this; if(!flag){ wx.request({ url: host+'/api/index/banners', method:'GET', success:function(res){ that.setData({ lb: res.data.data.banners }) }, fail: function () { wx.hideNavigationBarLoading() wx.showToast({ title: '服务器开小差啦!', icon: 'none' }) } }) } // 全刊 wx.request({ url: host + '/api/index/journals', method: 'GET', success: function (res) { wx.hideNavigationBarLoading() that.setData({ journals: res.data.data.journals }) } }) // 主办单位 wx.request({ url: host + '/api/index/organizers', method: 'GET', data:{ uid:this.data.uid }, success: function (res) { that.setData({ organizers: res.data.data.list }) } }) // 领域期刊 wx.request({ url: host + '/api/index/subjects', method: 'GET', data: { uid: this.data.uid }, success: function (res) { that.setData({ subjects: res.data.data.list }) } }) }, search:function(){ wx.navigateTo({ url: '../search/search', }) }, //置顶 top: function(e){ var that=this; wx.request({ url: host+'/api/index/totop', method:'PUT', data:{ id:e.target.dataset.id, type: e.target.dataset.type, uid:this.data.uid, action:'do' }, success:function(res){ wx.showToast({ title: '置顶成功', icon:'none' }) that.getData(1) } }) }, cancel_top: function (e) { wx.request({ url: host + '/api/index/totop', method: 'PUT', data: { id: e.target.dataset.id, type: e.target.dataset.type, uid: this.data.uid, action:'cancel' }, success: function (res) { wx.showToast({ title: '已取消', icon: 'none' }) that.getData(1) } }) }, tab:function(e){ this.setData({ act: e.target.dataset.id }) }, swchange:function(e){ this.setData({ act: e.detail.current }) }, onReady: function () { this.get_height(); }, //事件处理函数 touchstart: function (e) { this.set_scroll(e) }, touchmove: function (e) { this.set_scroll(e) }, touchend: function () { this.setData({ touchmove: 0, hiddenn: true }) }, scroll(e){ var that = this this.setData({ scroll:e.target.dataset.id, }) }, set_scroll: function (e) { var page_y = e.changedTouches[0].pageY - this.data.nav_top; // console.log(page_y) var nav_height = +this.data.nav_height var idx = Math.floor(page_y / nav_height) var zimu = this.data.zimu_list[idx]; this.setData({ touchmove: 1, scroll: zimu, nav_text: zimu, hiddenn: false }) }, get_height: function () { var that = this var query = wx.createSelectorQuery(); query.select('#nav_item').boundingClientRect() wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) { that.setData({ nav_top: rect.top, }) }).exec() query.exec(function (res) { that.setData({ nav_height: res[0].height, }) }) }, getUserInfo: function(e) { console.log(e) app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, toDetail:function(e){ console.log(e) } })