index.js 5.2 KB

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