riku
2024-07-26 c827d70bd16535e103f4bcdd9f3ed53a5fd324e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// pages/search/index.js
import { fetchEnterprise } from '../../../services/enterprise/fetchEnterprise';
import { useLoading } from '../../../behaviors/loading';
 
Page({
  behaviors: [useLoading],
  data: {
    placeholder: '输入关键词搜索企业',
    searchValue: '',
    isSearch: false,
    searchResult: [],
    historyWords: [],
    dialog: {
      title: '确认删除当前历史记录',
      showCancelButton: true,
      message: '',
    },
    dialogShow: false,
  },
 
  deleteType: 0,
  deleteIndex: '',
 
  onLoad(options) {},
 
  onShow() {
    this.queryHistory();
  },
 
  onReachBottom() {
    this._loadMore();
  },
 
  queryHistory() {
    wx.getStorage({
      key: 'historyWords',
      success: res => {
        this.setData({
          historyWords: res.data,
        });
      },
    });
  },
 
  //删除某个历史记录
  deleteCurr(e) {
    const { index } = e.currentTarget.dataset;
    const { dialog } = this.data;
    this.deleteIndex = index;
    this.setData({
      dialog: {
        ...dialog,
        message: '确认删除当前历史记录',
        deleteType: 0,
      },
      dialogShow: true,
    });
  },
 
  //清空历史记录
  handleClearHistory() {
    const { dialog } = this.data;
    this.deleteType = 1;
    this.setData({
      dialog: {
        ...dialog,
        message: '确认删除所有历史记录',
      },
      dialogShow: true,
    });
  },
 
  confirm() {
    const { historyWords } = this.data;
    const { deleteType, deleteIndex } = this;
    if (deleteType === 0) {
      historyWords.splice(deleteIndex, 1);
      this.setData({
        historyWords,
        dialogShow: false,
      });
    } else {
      this.setData({ historyWords: [], dialogShow: false });
    }
    wx.setStorage({
      key: 'historyWords',
      data: [],
    });
  },
 
  close() {
    this.setData({ dialogShow: false });
  },
 
  //点击历史记录
  handleHistoryTap(e) {
    const { historyWords } = this.data;
    const { dataset } = e.currentTarget;
    const _searchValue = historyWords[dataset.index || 0] || '';
    if (_searchValue) {
      this.setData({ searchValue: _searchValue });
      this._startLoad();
    }
  },
 
  //点击搜索
  handleSubmit() {
    const { historyWords, searchValue } = this.data;
    if (historyWords.indexOf(searchValue) == -1) {
      historyWords.push(searchValue);
      this.setData({ historyWords });
    }
    wx.setStorage({
      key: 'historyWords',
      data: historyWords,
    });
    this._startLoad();
  },
 
  //清除搜索
  handleClear() {
    this.setData({ searchValue: '', searchResult: [], isSearch: false });
  },
 
  handleChange() {
    if (this.data.searchValue == '') {
      this.setData({ searchResult: [], isSearch: false });
    }
  },
 
  _fetchData(page) {
    const { searchValue } = this.data;
    if (searchValue.length === 0) return;
    this.setData({ isSearch: true });
    return fetchEnterprise({
      page,
      data: { searchText: searchValue },
    }).then(res => {
      this.setData({
        searchResult:
          page == 1 ? res.data : this.data.searchResult.concat(res.data),
      });
      return res.head;
    });
  },
 
  handCellClick(e) {
    console.log(e);
    let item = e.detail;
    // let item = this.data.searchResult[index];
    wx.navigateTo({
      url: '/pages/enterprise/detail/index',
      success: result => {
        result.eventChannel.emit('acceptEnterpriseData', { enterprise: item });
      },
    });
  },
});