index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { getOtherUser, setFans } from '~/api/user';
  2. import reachBottom from '~/mixins/reachBottom';
  3. Page({
  4. behaviors: [reachBottom],
  5. data: {
  6. text: '',
  7. historySearch: [],
  8. localUid: wx.getStorageSync('uid')
  9. },
  10. onShow() {
  11. this.setData({
  12. historySearch: wx.getStorageSync('userSearch')
  13. });
  14. this.resetData();
  15. },
  16. setSearch({
  17. detail
  18. }) {
  19. if (!detail.value) {
  20. this.setData({
  21. nullList: false,
  22. list: []
  23. });
  24. }
  25. this.setData({
  26. text: detail.value
  27. });
  28. },
  29. searchUser({
  30. currentTarget
  31. }) {
  32. if (currentTarget.dataset.text) {
  33. this.setData({
  34. text: currentTarget.dataset.text
  35. });
  36. }
  37. if (!this.data.text) {
  38. this.setData({
  39. list: []
  40. });
  41. return;
  42. }
  43. this.resetData();
  44. if (!this.data.historySearch.includes(this.data.text)) {
  45. this.setData({
  46. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
  47. });
  48. }
  49. wx.setStorageSync('userSearch', this.data.historySearch);
  50. },
  51. deleteHistory({
  52. currentTarget
  53. }) {
  54. let newList = this.data.historySearch.filter(item => {
  55. return item != currentTarget.dataset.text;
  56. });
  57. this.setData({
  58. historySearch: newList.slice(0, 20)
  59. });
  60. wx.setStorageSync('userSearch', this.data.historySearch);
  61. },
  62. clearHistory() {
  63. wx.showModal({
  64. title: '温馨提示',
  65. content: '历史记录清除后无法恢复,是否清除全部记录',
  66. success: res => {
  67. if (res.confirm) {
  68. this.setData({
  69. historySearch: []
  70. });
  71. wx.setStorageSync('search', this.data.historySearch);
  72. }
  73. }
  74. });
  75. },
  76. loadMore() {
  77. if (!this.data.text) {
  78. return;
  79. }
  80. this.getData(getOtherUser, {
  81. query: this.data.text
  82. });
  83. },
  84. jumpUserInfo({
  85. currentTarget
  86. }) {
  87. let uid = currentTarget.dataset.uid;
  88. wx.navigateTo({
  89. url: `/pages/personal/index?uid=${uid}`
  90. });
  91. },
  92. async setFans({
  93. currentTarget
  94. }) {
  95. if (!currentTarget.dataset.iseachother) {
  96. await setFans({
  97. uid: currentTarget.dataset.uid
  98. });
  99. let listCopy = JSON.parse(JSON.stringify(this.data.list));
  100. listCopy.forEach(item => {
  101. if (item.uid == currentTarget.dataset.uid) {
  102. item.isEachOther = true;
  103. }
  104. });
  105. this.setData({
  106. list: listCopy
  107. });
  108. wx.showToast({
  109. title: '已关注',
  110. icon: 'none'
  111. });
  112. }
  113. },
  114. onReachBottom() {
  115. this.loadMore();
  116. }
  117. });