// pages/index/index.js import { getProjectList, getPatrolRecord } from "../../utils/getApiUrl"; import { Request } from "../../common"; const moment = require("../../utils/moment"); Page({ /** * 页面的初始数据 */ data: { project: [], todayRecord: [], // 今日巡检记录 markers: [], isShowCallout: false, }, setMarkers(project, todayRecord) { const markers = project.map(p => { let todayCount = 0; todayRecord.forEach(r => { if (r.projectId === p.id) { todayCount += 1 } }) return { id: p.id, latitude: p.latitude, longitude: p.longitude, name: p.name, iconPath: '/images/gis.png', width: 29, height: 44, callout: { content: `${p.name}\n今日巡检:${todayCount}次`, padding: 10, display: this.data.isShowCallout ? 'ALWAYS' : 'BYCLICK', borderColor: '#1684FF', borderWidth: 1, }, } }) this.setData({ markers }) }, // 获取结构物和今日巡检次数 getData() { const that = this; wx.showLoading({ title: '加载中' }) const promiseArr = []; promiseArr.push(Request.get(getProjectList(), {})); promiseArr.push(Request.get(getPatrolRecord('all', moment().format('YYYY-MM-DD') + ' 00:00:00', moment().format('YYYY-MM-DD') + ' 23:59:59', 'null', 'null',{home:false}))); Promise.all(promiseArr).then(res => { wx.hideLoading() that.setData({ project: res[0].rows, todayRecord: res[1] }) that.setMarkers(res[0].rows, res[1]); // 缩放视野展示所有 markers const mapCtx = wx.createMapContext('mapDom') mapCtx.includePoints({ points: res[0].rows, padding: [40, 40, 40, 40] }) }) }, showCallout() { this.setData( { isShowCallout: !this.data.isShowCallout }, () => { this.setMarkers(this.data.markers, this.data.todayRecord); } ); }, onRefresh() { this.getData(); }, onMarkerTap(e) { const describe = this.data.project.find(p => p.id === e.detail.markerId).describe || ''; wx.navigateTo({ url: `/package/pointsStatus/pointsStatus?projectId=${e.detail.markerId}&describe=${describe}` }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const userInfo = wx.getStorageSync('userInfo'); if (!userInfo || !userInfo.id) { wx.reLaunch({ url: '/pages/login/login' }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { const userInfo = wx.getStorageSync('userInfo'); if (!userInfo || !userInfo.id) { wx.reLaunch({ url: '/pages/login/login' }); return } const userRole = wx.getStorageSync('userRole'); if (userRole && userRole.includes('管理')) { wx.reLaunch({ url: '/pages/home/home' }); return } this.getData(); if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 0 }) } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })