index.js 2.1 KB

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