index.js 3.1 KB

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