// pages/device/index.js const api = require('../../api/index.js'); Page({ /** * 页面的初始数据 */ data: { devicedata:[], latitude:'', longitude:'', accuracy:'', array:['南山','临平','其他'], index:0 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { this.getdevice() }, getdevice(){ let data = { } api.request(`/device/selectdevice`, 'POST',data) .then((res) => { console.log(res); if (res.code==200) { for (let index = 0; index < res.data.length; index++) { if (res.data[index].devicesLatitudelongitude) { res.data[index].devicesLatitudelongitude=res.data[index].devicesLatitudelongitude.split(",") } } this.setData({ devicedata : res.data }) } }) .catch((err) => { console.error('请求失败:', err); // 在这里处理请求失败的情况 }); }, // 设置当前经纬度 // 提取获取位置的具体逻辑 getLocation(e) { console.log(e.target.dataset.id); const that = this; wx.getLocation({ type: 'wgs84', isHighAccuracy:true, success: (res) => { console.log('位置获取成功:', res); that.setData({ latitude: res.latitude, longitude: res.longitude, accuracy:res.accuracy }); console.log(this.data.latitude); console.log(this.data.longitude); that.getpositioning(res.longitude,res.latitude,e.target.dataset.id); }, fail: (err) => { console.error('获取位置失败:', err); wx.showToast({ title: '获取位置失败,请重试', icon: 'none' }); } })}, getpositioning(latitude,longitude,id){ let data = { deviceId:id, latitudelongitude:latitude+','+longitude } api.request(`/device/updatelatit`, 'POST',data) .then((res) => { console.log(res); if (res.code==200) { wx.showToast({ title: '位置获取成功', icon: 'none' }); this.getdevice() } }) .catch((err) => { console.error('请求失败:', err); // 在这里处理请求失败的情况 }); }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })