// pages/list/hotel/index.js import commonApi from "../../../utils/https/common" import util from "../../../utils/util" Page({ /** * 页面的初始数据 */ data: { type:5, total:1, list:[], keywords:"", area_id:null, location:null, isDistanceSort:false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(options.area_id){ this.setData({ area_id:options.area_id }) } this.initLocation().then(res=>{ this.getList() }).catch(err=>{ this.getList() }) }, changeSort:function(){ this.setData({ isDistanceSort:!this.data.isDistanceSort, list:[], total:1 }) if(this.data.isDistanceSort && !this.data.location){ // 如果是距离排序并且没有开启定位的话 this.initLocation().then(res=>{ this.getList() }).catch(err=>{ this.getList() }) } else { this.getList() } }, initLocation:function(){ let that = this; if(this.data.location){ return new Promise((resolve,reject)=>{ resolve() }) } return new Promise((resolve,reject)=>{ wx.getLocation({ type:"glc02", success:function(e){ that.setData({ location:{ lon:e.longitude, lat:e.latitude }, isDistanceSort:true }) resolve() }, fail:function(err){ // wx.showToast({ // title: '请开启定位后重试', // icon:'none' // }) reject() } }) }) }, search:function(e){ this.setData(e.detail); this.setData({ list:[], total:1 }) this.getList(); }, gotoDetail:function(e){ let item = e.currentTarget.dataset.item; util.gotoDetail(item); }, getList:function(){ let list = this.data.list; if(list.length>=this.data.total) return; commonApi._post('scene/get_scene_by_tag',{ offset:list.length, limit:10, sort:"weight", tag_id:this.data.type, title:this.data.keywords, area_id:this.data.area_id, lon:this.data.location?this.data.location.lon:null, lat:this.data.location?this.data.location.lat:null, sort:this.data.isDistanceSort?'distance':null, order:this.data.isDistanceSort?'asc':null }).then(res=>{ res.data.list.map(item=>{ if(item.distance>=1000){ item.distance = (item.distance/1000).toFixed(2)+"km"; } else if(item.distance){ item.distance = item.distance+'m'; } item.display_tags = (item.display_tags?item.display_tags.split(","):[]).splice(0,2); }) this.setData({ total:res.data.count, list:list.concat(res.data.list) }) console.log(res) }) }, changeType:function(e){ this.setData({ type:e.currentTarget.dataset.type, list:[], total:1 }) this.getList() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { this.getList() }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })