index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. 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. uid: res.data.data.uid
  85. })
  86. this.getData()
  87. },
  88. fail:error=>{
  89. wx.hideLoading()
  90. wx.showLoading({
  91. title: '登陆失败',
  92. })
  93. }
  94. })
  95. }
  96. })
  97. },
  98. //主办单位下一页
  99. next_org:function(){
  100. if(this.data.organizers.length < this.data.total_org){
  101. const page = this.data.page_org+1
  102. this.setData({
  103. page_org: page
  104. })
  105. this.getorg()
  106. }
  107. },
  108. //领域期刊下一页
  109. next_sub:function(){
  110. if(this.data.subjects.length < this.data.total_sub){
  111. const page = this.data.page_sub+1
  112. this.setData({
  113. page_sub: page
  114. })
  115. this.getsub()
  116. }
  117. },
  118. //获取所有数据
  119. getData:function(flag){
  120. wx.showNavigationBarLoading()
  121. var that=this;
  122. if(!flag){
  123. wx.request({
  124. url: host+'/api/index/banners',
  125. method:'GET',
  126. success:function(res){
  127. that.setData({
  128. lb: res.data.data.banners
  129. })
  130. },
  131. fail: function () {
  132. wx.hideNavigationBarLoading()
  133. wx.showToast({
  134. title: '服务器开小差啦!',
  135. icon: 'none'
  136. })
  137. }
  138. })
  139. // 全刊
  140. wx.request({
  141. url: host + '/api/index/journals',
  142. method: 'GET',
  143. success: function (res) {
  144. wx.hideNavigationBarLoading()
  145. that.setData({
  146. journals: res.data.data.journals
  147. })
  148. }
  149. })
  150. }
  151. this.getorg()
  152. this.getsub()
  153. },
  154. //主办单位
  155. getorg:function(){
  156. wx.showNavigationBarLoading()
  157. const organizers=this.data.organizers;
  158. wx.request({
  159. url: host + '/api/index/organizers',
  160. method: 'GET',
  161. data: {
  162. uid: this.data.uid,
  163. page: this.data.page_org
  164. },
  165. success: res=>{
  166. wx.hideNavigationBarLoading()
  167. if(res.data.code == 0){
  168. const list=res.data.data.list;
  169. for(let i=0;i<list.length;i++){
  170. organizers.push(list[i])
  171. }
  172. this.setData({
  173. organizers: organizers,
  174. total_org:res.data.data.total
  175. })
  176. }
  177. }
  178. })
  179. },
  180. //领域期刊
  181. getsub:function(){
  182. wx.showNavigationBarLoading()
  183. const subjects=this.data.subjects;
  184. wx.request({
  185. url: host + '/api/index/subjects',
  186. method: 'GET',
  187. data: {
  188. uid: this.data.uid,
  189. page: this.data.page_sub
  190. },
  191. success: res=>{
  192. wx.hideNavigationBarLoading()
  193. if(res.data.code == 0){
  194. const list=res.data.data.list;
  195. for(let i=0;i<list.length;i++){
  196. subjects.push(list[i])
  197. }
  198. this.setData({
  199. subjects: subjects,
  200. total_sub:res.data.data.total
  201. })
  202. }
  203. }
  204. })
  205. },
  206. search:function(){
  207. wx.navigateTo({
  208. url: '../searchList/searchList',
  209. })
  210. },
  211. //置顶
  212. top: function(e){
  213. var type = e.target.dataset.type;
  214. wx.request({
  215. url: host+'/api/index/totop',
  216. method:'PUT',
  217. data:{
  218. id:e.target.dataset.id,
  219. type: type,
  220. uid:this.data.uid,
  221. action:'do'
  222. },
  223. success: res=>{
  224. wx.showToast({
  225. title: '置顶成功',
  226. icon:'none'
  227. })
  228. if (type == 'organizer'){
  229. this.setData({
  230. organizers:[],
  231. page_org:1
  232. })
  233. this.getorg()
  234. }else{
  235. this.setData({
  236. subjects: [],
  237. page_sub: 1
  238. })
  239. this.getsub()
  240. }
  241. }
  242. })
  243. },
  244. cancel_top: function (e) {
  245. var type = e.target.dataset.type;
  246. wx.request({
  247. url: host + '/api/index/totop',
  248. method: 'PUT',
  249. data: {
  250. id: e.target.dataset.id,
  251. type: type,
  252. uid: this.data.uid,
  253. action:'cancel'
  254. },
  255. success: res=> {
  256. wx.showToast({
  257. title: '已取消',
  258. icon: 'none'
  259. })
  260. if (type == 'organizer') {
  261. this.setData({
  262. organizers: [],
  263. page_org: 1
  264. })
  265. this.getorg()
  266. } else {
  267. this.setData({
  268. subjects: [],
  269. page_sub: 1
  270. })
  271. this.getsub()
  272. }
  273. }
  274. })
  275. },
  276. tab:function(e){
  277. this.setData({
  278. act: e.target.dataset.id
  279. })
  280. },
  281. swchange:function(e){
  282. this.setData({
  283. act: e.detail.current
  284. })
  285. },
  286. onReady: function () {
  287. this.get_height();
  288. },
  289. //事件处理函数
  290. touchstart: function (e) {
  291. this.set_scroll(e)
  292. },
  293. touchmove: function (e) {
  294. this.set_scroll(e)
  295. },
  296. touchend: function () {
  297. this.setData({
  298. touchmove: 0,
  299. hiddenn: true
  300. })
  301. },
  302. scroll(e){
  303. var that = this
  304. this.setData({
  305. scroll:e.target.dataset.id,
  306. })
  307. },
  308. set_scroll: function (e) {
  309. var page_y = e.changedTouches[0].pageY - this.data.nav_top;
  310. // console.log(page_y)
  311. var nav_height = +this.data.nav_height
  312. var idx = Math.floor(page_y / nav_height)
  313. var zimu = this.data.zimu_list[idx];
  314. this.setData({
  315. touchmove: 1,
  316. scroll: zimu,
  317. nav_text: zimu,
  318. hiddenn: false
  319. })
  320. },
  321. get_height: function () {
  322. var that = this
  323. var query = wx.createSelectorQuery();
  324. query.select('#nav_item').boundingClientRect()
  325. wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) {
  326. that.setData({
  327. nav_top: rect.top,
  328. })
  329. }).exec()
  330. query.exec(function (res) {
  331. that.setData({
  332. nav_height: res[0].height,
  333. })
  334. })
  335. },
  336. getUserInfo: function(e) {
  337. app.globalData.userInfo = e.detail.userInfo
  338. this.setData({
  339. userInfo: e.detail.userInfo,
  340. hasUserInfo: true
  341. })
  342. },
  343. toDetail:function(e){
  344. // console.log(e)
  345. },
  346. /**
  347. * 页面相关事件处理函数--监听用户下拉动作
  348. */
  349. onPullDownRefresh: function () {
  350. wx.startPullDownRefresh()
  351. },
  352. })