index.js 4.3 KB

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