index.js 4.4 KB

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