index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.setData({
  25. list: []
  26. })
  27. this.getList();
  28. api.getByName(this, 'getUserInfo', 'userInfo', {}, function(res) {
  29. app.updateUserInfo(res);
  30. });
  31. },
  32. onLoad: function () {
  33. if (app.globalData.userInfo) {
  34. this.setData({
  35. userInfo: app.globalData.userInfo,
  36. hasUserInfo: true
  37. })
  38. } else if (this.data.canIUse){
  39. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  40. // 所以此处加入 callback 以防止这种情况
  41. app.userInfoReadyCallback = res => {
  42. this.setData({
  43. userInfo: res.userInfo,
  44. hasUserInfo: true
  45. })
  46. }
  47. } else {
  48. // 在没有 open-type=getUserInfo 版本的兼容处理
  49. wx.getUserInfo({
  50. success: res => {
  51. app.globalData.userInfo = res.userInfo
  52. this.setData({
  53. userInfo: res.userInfo,
  54. hasUserInfo: true
  55. })
  56. }
  57. })
  58. }
  59. },
  60. navigate: function(e) {
  61. wx.navigateTo({
  62. url: e.currentTarget.dataset.url,
  63. })
  64. },
  65. updateInput: function(e) {
  66. var name = e.currentTarget.dataset.name
  67. this.setData({
  68. [name]: e.detail.value
  69. })
  70. },
  71. search: function() {
  72. this.setData({
  73. list: [],
  74. page: 1
  75. })
  76. this.getList()
  77. },
  78. getList: function() {
  79. if(this.data.touchBottom) return false;
  80. var that = this
  81. http({
  82. url: 'projects/get',
  83. data: {
  84. page: this.data.page,
  85. name: this.data.keyword
  86. },
  87. success: function(res) {
  88. if(res.code == 0) {
  89. var list = that.data.list.concat(res.data)
  90. that.setData({
  91. touchBottom: res.data.length == 0,
  92. list: list
  93. })
  94. }
  95. }
  96. })
  97. },
  98. getUserInfo: function(e) {
  99. console.log(e)
  100. app.globalData.userInfo = e.detail.userInfo
  101. this.setData({
  102. userInfo: e.detail.userInfo,
  103. hasUserInfo: true
  104. })
  105. },
  106. onReachBottom: function() {
  107. if(!this.data.touchBottom) {
  108. this.setData({
  109. page: this.data.page + 1
  110. })
  111. this.getList()
  112. } else {
  113. wx.showToast({
  114. icon: 'none',
  115. title: '没有更多了',
  116. })
  117. }
  118. }
  119. })