index.js 5.8 KB

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