riku
2024-08-14 f2a0ea849099f49a3d2a9c7e5c44d033df22468f
pages/inspection/scene/search/index.js
@@ -1,66 +1,158 @@
// pages/inspection/scene/search/index.js
import { fetchEnterprise } from '../../../../services/enterprise/fetchEnterprise';
import { searchScene } from '../../../../services/inspection/fetchScene';
import { useLoading } from '../../../../behaviors/loading';
Page({
  /**
   * 页面的初始数据
   */
  behaviors: [useLoading],
  data: {
    placeholder: '输入关键词搜索场景',
    searchValue: '',
    isSearch: false,
    searchResult: [],
    historyWords: [],
    dialog: {
      title: '确认删除当前历史记录',
      showCancelButton: true,
      message: '',
    },
    dialogShow: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  historyTag: 'historyScene',
  deleteType: 0,
  deleteIndex: '',
  },
  onLoad(options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryHistory();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this._loadMore();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  queryHistory() {
    wx.getStorage({
      key: this.historyTag,
      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: this.historyTag,
      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: this.historyTag,
      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 });
    const area = {
      sceneName: searchValue,
    };
    return searchScene(area, page).then(res => {
      this.setData({
        searchResult: page == 1 ? res.data : this.data.searchResult.concat(res.data),
      });
      return res.head;
    });
  },
  handCellClick(e) {
    const scene = e.detail;
    wx.navigateTo({
      url: '/pages/inspection/scene/info/index',
      success: result => {
        result.eventChannel.emit('acceptSceneData', {
          scene: scene,
        });
      },
    });
  },
});