index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. show:1
  20. },
  21. onLoad: function () {
  22. this.setData({
  23. openid: app.globalData.openid
  24. })
  25. wx.getStorage({
  26. key: 'userInfo',
  27. success: res => {
  28. if (res.data) {
  29. this.setData({ show: 0 })
  30. }
  31. },
  32. })
  33. this.getData();
  34. },
  35. onGotUserInfo: function (e) {
  36. wx.setStorage({
  37. key: 'userInfo',
  38. data: e.detail.userInfo,
  39. })
  40. wx.request({
  41. url: host + '/api/auth',
  42. method: 'POST',
  43. data: {
  44. nickname: e.detail.userInfo.nickName,
  45. avatar: e.detail.userInfo.avatarUrl,
  46. openid: app.globalData.openid,
  47. },
  48. success: res => {
  49. console.log(res)
  50. app.globalData.uid=res.data.data.uid
  51. this.setData({
  52. show: 0
  53. })
  54. }
  55. })
  56. },
  57. //获取所有数据
  58. getData:function(){
  59. wx.showLoading({
  60. title: '加载中',
  61. })
  62. var that=this;
  63. wx.request({
  64. url: host+'/api/index/banners',
  65. method:'GET',
  66. success:function(res){
  67. that.setData({
  68. lb: res.data.data.banners
  69. })
  70. },
  71. fail: function () {
  72. wx.hideLoading();
  73. wx.showToast({
  74. title: '服务器开小差啦!',
  75. icon: 'none'
  76. })
  77. }
  78. })
  79. // 全刊
  80. wx.request({
  81. url: host + '/api/index/journals',
  82. method: 'GET',
  83. success: function (res) {
  84. wx.hideLoading()
  85. that.setData({
  86. journals: res.data.data.journals
  87. })
  88. }
  89. })
  90. // 主办单位
  91. wx.request({
  92. url: host + '/api/index/organizers',
  93. method: 'GET',
  94. success: function (res) {
  95. that.setData({
  96. organizers: res.data.data.list
  97. })
  98. }
  99. })
  100. // 领域期刊
  101. wx.request({
  102. url: host + '/api/index/subjects',
  103. method: 'GET',
  104. success: function (res) {
  105. that.setData({
  106. subjects: res.data.data.list
  107. })
  108. }
  109. })
  110. },
  111. //置顶
  112. top: function(e){
  113. wx.request({
  114. url: host+'/api/index/totop',
  115. method:'PUT',
  116. data:{
  117. id:e.target.dataset.id,
  118. type:'subject'
  119. },
  120. success:function(res){
  121. console.log(res.data)
  122. }
  123. })
  124. },
  125. tab:function(e){
  126. this.setData({
  127. act: e.target.dataset.id
  128. })
  129. },
  130. swchange:function(e){
  131. this.setData({
  132. act: e.detail.current
  133. })
  134. },
  135. onReady: function () {
  136. this.get_height();
  137. },
  138. //事件处理函数
  139. touchstart: function (e) {
  140. this.set_scroll(e)
  141. },
  142. touchmove: function (e) {
  143. this.set_scroll(e)
  144. },
  145. touchend: function () {
  146. this.setData({
  147. touchmove: 0,
  148. hiddenn: true
  149. })
  150. },
  151. scroll(e){
  152. var that = this
  153. this.setData({
  154. scroll:e.target.dataset.id,
  155. })
  156. },
  157. set_scroll: function (e) {
  158. var page_y = e.changedTouches[0].pageY - this.data.nav_top;
  159. // console.log(page_y)
  160. var nav_height = +this.data.nav_height
  161. var idx = Math.floor(page_y / nav_height)
  162. var zimu = this.data.zimu_list[idx];
  163. this.setData({
  164. touchmove: 1,
  165. scroll: zimu,
  166. nav_text: zimu,
  167. hiddenn: false
  168. })
  169. },
  170. get_height: function () {
  171. var that = this
  172. var query = wx.createSelectorQuery();
  173. query.select('#nav_item').boundingClientRect()
  174. wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) {
  175. that.setData({
  176. nav_top: rect.top,
  177. })
  178. }).exec()
  179. query.exec(function (res) {
  180. that.setData({
  181. nav_height: res[0].height,
  182. })
  183. })
  184. },
  185. getUserInfo: function(e) {
  186. console.log(e)
  187. app.globalData.userInfo = e.detail.userInfo
  188. this.setData({
  189. userInfo: e.detail.userInfo,
  190. hasUserInfo: true
  191. })
  192. },
  193. toDetail:function(e){
  194. console.log(e)
  195. }
  196. })