index.js 2.5 KB

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