index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.getList();
  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. if(['/pages/create-project/index', '/pages/create-project-role/index'].indexOf(url) != -1) {
  77. var role = this.data.topRole
  78. if(!role || ['manager', 'admin'].indexOf(role.key) == -1) {
  79. util.error('没有权限');
  80. return false;
  81. }
  82. }
  83. wx.navigateTo({
  84. url: url,
  85. })
  86. },
  87. updateInput: function(e) {
  88. var name = e.currentTarget.dataset.name
  89. this.setData({
  90. [name]: e.detail.value
  91. })
  92. },
  93. search: function() {
  94. this.setData({
  95. list: [],
  96. page: 1
  97. })
  98. this.getList()
  99. },
  100. getList: function() {
  101. if(this.data.touchBottom) return false;
  102. var that = this
  103. http({
  104. url: 'projects/get',
  105. data: {
  106. page: this.data.page,
  107. name: this.data.keyword
  108. },
  109. success: function(res) {
  110. if(res.code == 0) {
  111. var list = that.data.list.concat(res.data)
  112. that.setData({
  113. touchBottom: res.data.length == 0,
  114. list: list
  115. })
  116. }
  117. }
  118. })
  119. },
  120. getUserInfo: function(e) {
  121. console.log(e)
  122. app.globalData.userInfo = e.detail.userInfo
  123. this.setData({
  124. userInfo: e.detail.userInfo,
  125. hasUserInfo: true
  126. })
  127. },
  128. onReachBottom: function() {
  129. if(!this.data.touchBottom) {
  130. this.setData({
  131. page: this.data.page + 1
  132. })
  133. this.getList()
  134. } else {
  135. wx.showToast({
  136. icon: 'none',
  137. title: '没有更多了',
  138. })
  139. }
  140. }
  141. })