index.js 2.4 KB

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