index.js 7.4 KB

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