index.js 3.1 KB

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