search.js 5.64 KB
// pages/reserve/search.js\
const map_key='SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ'
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key:map_key // 必填
});
import util from '../../utils/util';
import App from '../../utils/api';
const {regeneratorRuntime} = global
const search_history_cache_key='SEARCH_HISTORY_CACHE_KEY';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history:[],
    suggestion:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHistory();
    this.getPos()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getHistory(){
    let ca=util.cache(search_history_cache_key);
    if(ca){
      this.setData({
        history:ca
      })
    }
  },
  setHistory(item){
    let newca=[item];
    let ca=this.data.history;
    let maxL=20;
    for(let i=0;i<ca.length;i++){
      if(ca[i].id!=item.id){
        newca.push(ca[i]);
      }
      if(newca.length>maxL) break;
    }
    util.cache(search_history_cache_key,newca);
    this.setData({
      history:newca,
    })
  },
  //在Page({})中使用下列代码
  //数据回填方法
  backfill: function (e) {
    var id = e.currentTarget.id;
    for (var i = 0; i < this.data.suggestion.length; i++) {
      if (i == id) {
        console.log('backfill,this.data.suggestion[i]:',this.data.suggestion[i])
        this.setHistory(this.data.suggestion[i])
        let pages = getCurrentPages();
        let p = pages[pages.length - 2];
        p.setSuggestion(this.data.suggestion[i])
        //util.cache('suggestion',this.data.suggestion[i]);
        this.setData({
          backfill: this.data.suggestion[i].title
        });
        wx.navigateBack();
      }
    }
  },
  backHistoryFill: function (e) {
    var i = util.attr(e,'index');
    let item=this.data.history[i];
    console.log(item)
    this.setHistory(item)
    this.setData({
      backfill: item.title
    });

    let pages = getCurrentPages();
    let p = pages[pages.length - 2];
    p.setSuggestion(item)
    wx.navigateBack();
  },
  inputVal:'',
  //触发关键词输入提示事件
  getsuggest: function (e) {
    var _this = this;
    //调用关键词提示接口
    this.inputVal=e.detail.value;
    if(e.detail.value.length<1){
      return this.setData({
        suggestion: []
      })
    }
    App.Customer.KeywordTips({
      keyword:this.inputVal,
      region:this.location.city||'',
      region_fix:0,
      location:this.location.latitude+','+this.location.longitude,
      page_size:20,
      page_index:1,
      key:map_key,
    }).then(res=>{
      res.data=res.data.data
      var sug = [];
      for (var i = 0; i < res.data.length; i++) {
        sug.push({ // 获取返回结果,放到sug数组中
          title: res.data[i].title,
          id: res.data[i].id,
          addr: res.data[i].address,
          city: res.data[i].city,
          district: res.data[i].district,
          latitude: res.data[i].location.lat,
          longitude: res.data[i].location.lng,
          type:res.data[i].type,
        });
      }
      _this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
        suggestion: sug
      });
    })
  },
  nearby_search: function () {
    App.Customer.SearchMap({
      boundary:'region('+this.location.city+',1)',
      //boundary:'nearby('+this.location.latitude+','+this.location.longitude+',1000,1)',
      location:this.location.latitude+','+this.location.longitude,
      keyword:this.inputVal,
      page_size:20,
      page_index:1,
      orderby:'_distance',
      key:map_key,
    }).then(res=>{
      res.data=res.data.data
      var mks = []
      for (var i = 0; i < res.data.length; i++) {
        mks.push({ // 获取返回结果,放到mks数组中
          title: res.data[i].title,
          id: res.data[i].id,
          addr: res.data[i].address,
          city: res.data[i].city,
          district: res.data[i].district,
          latitude: res.data[i].location.lat,
          longitude: res.data[i].location.lng,
          type:res.data[i].type,
        })
      }
      this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
        suggestion:mks,
      });
    })
  },
  location:'',
  getPos(){
    this._getPos().then(res => {
      console.log('wx.getLocation.then', res);
      this.location=res
      this.getCity()
    }).catch((e) => {
      console.log('wx.getLocation.catch', e);
      if ('getLocation:fail auth deny' == e.errMsg || 'getLocation:fail:auth denied' == e.errMsg) {
        this.selectComponent('#showModal').options({
          header: '定位失败',
          body: '需要地理位置授权'
        }).show();
      } else {
        setTimeout(() => {
          this.getPos()
        }, 500)
      }

    })
  },
  _getPos(){
    return new Promise((success, fail) => {
      wx.getLocation({
        type: 'gcj02',
        success: (res) => {
          success(res)
        },
        fail: (res) => {
          fail(res)
        },
        complete: (res) => {
          console.log('wx.getLocation', res)
        }
      });
    })
  },
  getCity(){
    qqmapsdk.reverseGeocoder({
      location:this.location,
      success:(res)=>{
        this.location['city']='';
        if(res.status==0){
          this.location['city']=res.result.ad_info.city
        }
      },
    })
  }
})