index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. wx.showNavigationBarLoading()
  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. wx.showNavigationBarLoading()
  181. const subjects=this.data.subjects;
  182. wx.request({
  183. url: host + '/api/index/subjects',
  184. method: 'GET',
  185. data: {
  186. uid: this.data.uid,
  187. page: this.data.page_sub
  188. },
  189. success: res=>{
  190. wx.hideNavigationBarLoading()
  191. if(res.data.code == 0){
  192. const list=res.data.data.list;
  193. for(let i=0;i<list.length;i++){
  194. subjects.push(list[i])
  195. }
  196. this.setData({
  197. subjects: subjects,
  198. total_sub:res.data.data.total
  199. })
  200. }
  201. }
  202. })
  203. },
  204. search:function(){
  205. wx.navigateTo({
  206. url: '../searchList/searchList',
  207. })
  208. },
  209. //置顶
  210. top: function(e){
  211. var type = e.target.dataset.type;
  212. wx.request({
  213. url: host+'/api/index/totop',
  214. method:'PUT',
  215. data:{
  216. id:e.target.dataset.id,
  217. type: type,
  218. uid:this.data.uid,
  219. action:'do'
  220. },
  221. success: res=>{
  222. wx.showToast({
  223. title: '置顶成功',
  224. icon:'none'
  225. })
  226. if (type == 'organizer'){
  227. this.setData({
  228. organizers:[],
  229. page_org:1
  230. })
  231. this.getorg()
  232. }else{
  233. this.setData({
  234. subjects: [],
  235. page_sub: 1
  236. })
  237. this.getsub()
  238. }
  239. }
  240. })
  241. },
  242. cancel_top: function (e) {
  243. var type = e.target.dataset.type;
  244. wx.request({
  245. url: host + '/api/index/totop',
  246. method: 'PUT',
  247. data: {
  248. id: e.target.dataset.id,
  249. type: type,
  250. uid: this.data.uid,
  251. action:'cancel'
  252. },
  253. success: res=> {
  254. wx.showToast({
  255. title: '已取消',
  256. icon: 'none'
  257. })
  258. if (type == 'organizer') {
  259. this.setData({
  260. organizers: [],
  261. page_org: 1
  262. })
  263. this.getorg()
  264. } else {
  265. this.setData({
  266. subjects: [],
  267. page_sub: 1
  268. })
  269. this.getsub()
  270. }
  271. }
  272. })
  273. },
  274. tab:function(e){
  275. this.setData({
  276. act: e.target.dataset.id
  277. })
  278. },
  279. swchange:function(e){
  280. this.setData({
  281. act: e.detail.current
  282. })
  283. },
  284. onReady: function () {
  285. this.get_height();
  286. },
  287. //事件处理函数
  288. touchstart: function (e) {
  289. this.set_scroll(e)
  290. },
  291. touchmove: function (e) {
  292. this.set_scroll(e)
  293. },
  294. touchend: function () {
  295. this.setData({
  296. touchmove: 0,
  297. hiddenn: true
  298. })
  299. },
  300. scroll(e){
  301. var that = this
  302. this.setData({
  303. scroll:e.target.dataset.id,
  304. })
  305. },
  306. set_scroll: function (e) {
  307. var page_y = e.changedTouches[0].pageY - this.data.nav_top;
  308. // console.log(page_y)
  309. var nav_height = +this.data.nav_height
  310. var idx = Math.floor(page_y / nav_height)
  311. var zimu = this.data.zimu_list[idx];
  312. this.setData({
  313. touchmove: 1,
  314. scroll: zimu,
  315. nav_text: zimu,
  316. hiddenn: false
  317. })
  318. },
  319. get_height: function () {
  320. var that = this
  321. var query = wx.createSelectorQuery();
  322. query.select('#nav_item').boundingClientRect()
  323. wx.createSelectorQuery().select('#nav').boundingClientRect(function (rect) {
  324. that.setData({
  325. nav_top: rect.top,
  326. })
  327. }).exec()
  328. query.exec(function (res) {
  329. that.setData({
  330. nav_height: res[0].height,
  331. })
  332. })
  333. },
  334. getUserInfo: function(e) {
  335. console.log(e)
  336. app.globalData.userInfo = e.detail.userInfo
  337. this.setData({
  338. userInfo: e.detail.userInfo,
  339. hasUserInfo: true
  340. })
  341. },
  342. toDetail:function(e){
  343. console.log(e)
  344. },
  345. /**
  346. * 页面相关事件处理函数--监听用户下拉动作
  347. */
  348. onPullDownRefresh: function () {
  349. wx.startPullDownRefresh()
  350. },
  351. })