index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {
  2. getOtherUser,
  3. setFans
  4. } from '~/api/user';
  5. import event from '~/mixins/event'
  6. import reachBottom from '~/mixins/reachBottom';
  7. Page({
  8. behaviors: [reachBottom, event],
  9. data: {
  10. text: '',
  11. placeholderText:'查找昵称/学号/手机号',
  12. historySearch: [],
  13. localUid: wx.getStorageSync('uid')
  14. },
  15. onShow() {
  16. // isEachOther 是否互相关注 isFans是否被关注 isFansOther是否关注别人
  17. this.setData({
  18. historySearch: wx.getStorageSync('userSearch')
  19. });
  20. this.resetData();
  21. },
  22. setSearch({
  23. detail
  24. }) {
  25. if (!detail.value) {
  26. this.setData({
  27. nullList: false,
  28. list: []
  29. });
  30. }
  31. this.setData({
  32. text: detail.value
  33. });
  34. },
  35. searchUser({
  36. currentTarget
  37. }) {
  38. if (currentTarget.dataset.text) {
  39. this.setData({
  40. text: currentTarget.dataset.text
  41. });
  42. }
  43. if (!this.data.text) {
  44. this.setData({
  45. list: []
  46. });
  47. return;
  48. }
  49. this.resetData();
  50. if (!this.data.historySearch.includes(this.data.text)) {
  51. this.setData({
  52. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
  53. });
  54. }
  55. wx.setStorageSync('userSearch', this.data.historySearch);
  56. },
  57. deleteHistory({
  58. currentTarget
  59. }) {
  60. let newList = this.data.historySearch.filter(item => {
  61. return item != currentTarget.dataset.text;
  62. });
  63. this.setData({
  64. historySearch: newList.slice(0, 20)
  65. });
  66. wx.setStorageSync('userSearch', this.data.historySearch);
  67. },
  68. clearHistory() {
  69. wx.showModal({
  70. title: '温馨提示',
  71. content: '历史记录清除后无法恢复,是否清除全部记录',
  72. success: res => {
  73. if (res.confirm) {
  74. this.setData({
  75. historySearch: []
  76. });
  77. wx.setStorageSync('search', this.data.historySearch);
  78. }
  79. }
  80. });
  81. },
  82. loadMore() {
  83. if (!this.data.text) {
  84. return;
  85. }
  86. this.getData(getOtherUser, {
  87. query: this.data.text
  88. });
  89. },
  90. jumpUserInfo({
  91. currentTarget
  92. }) {
  93. let uid = currentTarget.dataset.uid;
  94. wx.navigateTo({
  95. url: `/pages/personal/index?uid=${uid}`
  96. });
  97. },
  98. async setFans({
  99. currentTarget
  100. }) {
  101. if (!currentTarget.dataset.iseachother) {
  102. await setFans({
  103. uid: currentTarget.dataset.uid
  104. });
  105. let listCopy = JSON.parse(JSON.stringify(this.data.list));
  106. listCopy.forEach(item => {
  107. if (item.uid == currentTarget.dataset.uid) {
  108. item.isEachOther = true;
  109. }
  110. });
  111. this.setData({
  112. list: listCopy
  113. });
  114. wx.showToast({
  115. title: '已关注',
  116. icon: 'none'
  117. });
  118. }
  119. },
  120. sendMsg({
  121. currentTarget
  122. }) {
  123. let user = currentTarget.dataset.user
  124. let {
  125. nickName,
  126. eid,
  127. uid
  128. } = user
  129. if (this.data.localUid == uid) {
  130. return wx.showToast({
  131. title: '不可以给自己发私信哦~',
  132. icon: 'none'
  133. })
  134. }
  135. wx.navigateTo({
  136. url: `/pages/chat/index?title=${nickName||eid}&uid=${uid}`,
  137. })
  138. },
  139. clipboar({
  140. currentTarget
  141. }) {
  142. wx.setClipboardData({
  143. data: currentTarget.dataset.eid,
  144. success: function (res) { //成功回调函数
  145. wx.showToast({
  146. title: '已复制',
  147. icon: "none"
  148. })
  149. }
  150. })
  151. },
  152. cleanPlaceholder() {
  153. this.setData({
  154. placeholderText: ''
  155. })
  156. },
  157. setPlaceholder() {
  158. this.setData({
  159. placeholderText: '请输入搜索内容'
  160. })
  161. },
  162. onReachBottom() {
  163. this.loadMore();
  164. }
  165. });