index.js 2.5 KB

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