index.js 7.1 KB

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