index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //index.js
  2. //获取应用实例
  3. import http from '../../utils/http'
  4. import api from '../../utils/api'
  5. const app = getApp()
  6. Page({
  7. data: {
  8. motto: 'Hello World',
  9. userInfo: {},
  10. hasUserInfo: false,
  11. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  12. keyword: '',
  13. page: 1,
  14. list: [],
  15. touchBottom: false
  16. },
  17. //事件处理函数
  18. bindViewTap: function() {
  19. wx.navigateTo({
  20. url: '../logs/logs'
  21. })
  22. },
  23. onShow: function() {
  24. this.getTabBar().init();
  25. this.setData({
  26. list: []
  27. })
  28. this.getList();
  29. // api.getByName(this, 'getUserInfo', 'userInfo', {}, function(res) {
  30. // app.updateUserInfo(res);
  31. // });
  32. },
  33. onLoad: function () {
  34. if (app.globalData.userInfo) {
  35. this.setData({
  36. userInfo: app.globalData.userInfo,
  37. hasUserInfo: true
  38. })
  39. } else if (this.data.canIUse){
  40. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  41. // 所以此处加入 callback 以防止这种情况
  42. app.userInfoReadyCallback = res => {
  43. this.setData({
  44. userInfo: res.userInfo,
  45. hasUserInfo: true
  46. })
  47. }
  48. } else {
  49. // 在没有 open-type=getUserInfo 版本的兼容处理
  50. wx.getUserInfo({
  51. success: res => {
  52. app.globalData.userInfo = res.userInfo
  53. this.setData({
  54. userInfo: res.userInfo,
  55. hasUserInfo: true
  56. })
  57. }
  58. })
  59. }
  60. },
  61. navigate: function(e) {
  62. wx.navigateTo({
  63. url: e.currentTarget.dataset.url,
  64. })
  65. },
  66. updateInput: function(e) {
  67. var name = e.currentTarget.dataset.name
  68. this.setData({
  69. [name]: e.detail.value
  70. })
  71. },
  72. search: function() {
  73. this.setData({
  74. list: [],
  75. page: 1
  76. })
  77. this.getList()
  78. },
  79. getList: function() {
  80. if(this.data.touchBottom) return false;
  81. var that = this
  82. http({
  83. url: 'projects/get',
  84. data: {
  85. page: this.data.page,
  86. name: this.data.keyword
  87. },
  88. success: function(res) {
  89. if(res.code == 0) {
  90. var list = that.data.list.concat(res.data)
  91. that.setData({
  92. touchBottom: res.data.length == 0,
  93. list: list
  94. })
  95. }
  96. }
  97. })
  98. },
  99. getUserInfo: function(e) {
  100. console.log(e)
  101. app.globalData.userInfo = e.detail.userInfo
  102. this.setData({
  103. userInfo: e.detail.userInfo,
  104. hasUserInfo: true
  105. })
  106. },
  107. onReachBottom: function() {
  108. if(!this.data.touchBottom) {
  109. this.setData({
  110. page: this.data.page + 1
  111. })
  112. this.getList()
  113. } else {
  114. wx.showToast({
  115. icon: 'none',
  116. title: '没有更多了',
  117. })
  118. }
  119. }
  120. })