// pages/list/food/index.js import commonApi from "../../../utils/https/common" import util from "../../../utils/util" Page({ /** * 页面的初始数据 */ data: { types:["街巷美食","美食地图"], typeIndex:0, list:[], total:1, sort:"weight", keywords:"", lat: '', lon: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.getLocation({ type: 'gcj02', success:(res)=>{ this.setData({ lat:res.latitude, lon:res.longitude }) this.getList() }, fail:()=>{ wx.showToast({ title: '请打开位置授权', icon:'none' }) } }) }, setSort:function(){ this.setData({ sort:this.data.sort=='weight'?'distance':'weight', list:[], total:1 }) this.getList(); }, changeType:function(e){ if(e.detail.value==1){ wx.reLaunch({ url: '/pages/map/index?type=4', }) return; } this.setData({ typeIndex:e.detail.value, list:[], total:1 }) this.getList() }, gotoDetail:function(e){ let item = e.currentTarget.dataset.item; if(this.data.typeIndex==0){ wx.navigateTo({ url: '/pages/info/foodInfo/index?id='+item.id, }) return; } util.gotoDetail(item); }, search:function(e){ this.setData(e.detail); this.setData({ list:[], total:1 }) this.getList(); }, getList:function(){ let service="product/get_product_by_tag",list=this.data.list,data = { title:this.data.keywords, tag_id:"14", offset:list.length, limit:1000, sort:this.data.sort, order:this.data.sort=='weight'?'desc':'asc', },that = this; if(list.length>=this.data.total) return; if(this.data.typeIndex==0){ // 景点 service = "scene/get_scene_by_tag"; data.tag_id = "12" data.lat = this.data.lat data.lon = this.data.lon } if(this.data.sort=='distance' && !this.data.latitude){ wx.getLocation({ type: 'gcj02', success: function (res) { that.setData({ latitude:res.latitude, longitude:res.longitude }) data.lon = res.longitude; data.lat = res.latitude; that.getRealList(service,data) }, fail:function(){ that.getRealList(service,data) } }) } else if(this.data.sort=='distance'){ data.lon = this.data.longitude; data.lat = this.data.latitude; this.getRealList(service,data) } else { this.getRealList(service,data) } }, getRealList:function(service,data){ let list=this.data.list; commonApi._post(service,data).then(res=>{ res.data.list.map(item=>{ item.display_tags = (item.display_tags?item.display_tags.split(","):[]).splice(0,2); if(item.distance){ item.distance = item.distance>=1000?(item.distance/1000+'km'):(item.distance+'m'); } }) this.setData({ list:list.concat(res.data.list), total:this.data.type==0?res.data.total:res.data.count }) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { // this.getList() }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })