//index.js //获取应用实例 import { getopenId, userCheck, powerCheck, queryRotation, queryChart, getfeedBack, getSpacedetail, followRoom, openSpace, getEquipmentAction } from "../../requests/api"; import utils from "../../utils/util" import icons from "../../utils/icon" import Toast from '../../vant-weapp/dist/toast/toast'; var chartbox = null; const AUTH = require('../../utils/auth') const wxCharts = require('../../utils/wxcharts.js'); const app = getApp(); Page({ data: { hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), swiperCurrent: 0, //首页轮播图 popswiperCurrent: 0, //弹窗轮播图按钮 statusList: icons.statusList, spacestatusList:icons.spacestatusList , popstatusList: [{ id: 0, nowind: [{}], wind: [] }], popShow: false, //下弹框显示问题 meauList: [{ id: 1, title: '扫码调节', des: '调节二维码对应空间的环境', imgSrc: "../../static/images/qrcode.png" }, { id: 2, title: '收藏的空间', des: '对已收藏的空间进行调节', imgSrc: "../../static/images/adjust.png" }, { id: 3, title: '关闭空调', des: '下班了让环境节能控制', imgSrc: "../../static/images/lamp.png" }, ], envList: [{ name: "温度", value: "--", unit:"°C" }, { name: "湿度", value: "--", unit:"%" }, { name: "CO₂", value: "--", unit:"ppm", }, { name: "PM2.5", value: "--", unit:"μg/m³", }, ], tempType: [{ id: 2, value: "太冷了", checked: false, imgSrc: "../../static/images/severityCold.png" }, { id: 1, value: "有点冷", checked: false, imgSrc: "../../static/images/cold.png" }, { id: 11, value: "赞", checked: false, imgSrc: "../../static/images/good.png" }, { id: 3, value: "有点热", checked: false, imgSrc: "../../static/images/hot.png" }, { id: 4, value: "太热了", checked: false, imgSrc: "../../static/images/severityHot.png" }, ], tempTypeValue: -1, envType: [{ id: 5, value: '风太大', checked: false, imgSrc: "../../static/images/severityWind.png" }, ], envTypeValue: -1, imageWidth: 0, feedBack: true, projectId: "", tenantName: "", tenantId: "", userId: "", imgbaseUrl: app.globalData.imgbaseUrl, swiperDate: [{ "localName": "", id: "init", initPic:"../../static/images/room.png", // "pic": "../../static/images/room.png", isInit: true }], popswiperDate: {}, chartDate: [], chartShow: false, spaceStatus: "", notice: "", equipmentStatus: "", equipmentStatustext: [], speedValue: 60, addFollow: '1', latitude: '', longitude: '', havePower: '', spaceStatusId: null, monthDay: '', tenants: [], fromFollowpage: false, secondpopValue: false, secondSpaceStatus: '', secondNotice: '', secondIcon: '', isEdit: false, wxqcode: '', formWxcode: false, formAuth: false, hideFlag: false, changeTenant:false, //是否通过调节改变项目 formList:false, //是否改变项目 imgShow:false, isRefresh:false }, //事件处理函数 bindViewTap: function () {}, // 获取扫码结果 getScancode: function () { if (!wx.getStorageSync('logined')) { wx.navigateTo({ url: `../auth/index`, }) return } // 只允许从相机扫码 wx.scanCode({ onlyFromCamera: true, complete: (res) => { if (res.errMsg === '"scanCode:fail cancel"') { Toast.fail('已取消扫描'); } }, success: (res) => { // console.log(res) this.checkCode(res.result); }, fail: (res) => { Toast.fail('未扫描到结果'); } }) }, // 检查是否位置授权 isGetSetting() { wx.getSetting({ success: async (res) => { var statu = res.authSetting; if (!statu['scope.userLocation']) { wx.showModal({ title: '是否授权当前位置', content: '需要获取您的地理位置,请确认授权', confirmColor: '#f16765', success: res => { if (res.confirm) { wx.openSetting({ success: async data => { // if (data.authSetting["scope.userLocation"]) { // await this.getUserLocation(); // } else { await this.getUserLocation(); // } }, }) } else { this.setData({ havePower: { result: "fail", message: "未定位到您的位置" } }) } } }) } else { await this.getUserLocation(); } } }) }, // 检查二维码 async checkCode(value) { let scanArr = value.split("?"); let domain = scanArr[0]; let qualifiedUrl=["http://meos.sagacloud.cn/scan",'https://duoduoenv.sagacloud.cn/scan'] if (qualifiedUrl.includes(domain)) { let md = scanArr[1].split('='); let md1 = md[1]; let data = { "criteria": { "spaceId": md1, "userId": this.data.userId || wx.getStorageSync('userId'), "projectId": 'Pj' + md1.substring(2, 12) } } this.setData({ "popswiperDate.id": md1, "popswiperDate.projectId": 'Pj' + md1.substring(2, 12) }); // this.setData({ // projectId: 'Pj' + md1.substring(2, 12) // }) let changeProjectid = wx.getStorageSync('projectId') if (changeProjectid && changeProjectid !== ('Pj' + md1.substring(2, 12))) { // this.setData() } let res = await getSpacedetail(data); this.setData({ wxqcode: '' }) wx.setStorageSync("wxqcode",''); this.setData({ popswiperDate: res.content[0] || {} }); this.initOpenTime(); this.changePop() } else { Toast.fail('不支持的二维码'); } }, // 检查是否注册 async check(data={}) { var that=this; async function cb(){ data.latitude=''; if(data.latitude==''){ data.longitude=that.data.longitude || ''; data.latitude=that.data.latitude || ''; } let res = await userCheck(data); if (res.result === "fail" && !that.data.formAuth) { wx.setStorageSync('logined', false); wx.navigateTo({ url: `../auth/index`, }) } else { that.getTenant(res); } } if(wx.getStorageSync('logined') || !that.data.longitude){ await this.getUserLocation(cb); }else{ await cb(); } }, imageLoad(e){ var index = e.currentTarget.dataset.index; // var item = this.data.swiperDate[index]; var param = {}; var str = "swiperDate["+index+"].imgShow"; //index为下标 param[str] = true; // item.imgShow = true; // var str=`swiperDate[${index}]`; // console.log(param,"str"); this.setData(param); }, changeImgshow(){ setTimeout(function (){ this.data.dataset },1000) }, getTenant(res) { app.globalData.openId = res.content.openId || ''; app.globalData.userId = res.content.id || ''; app.globalData.tenants = res.content.tenants || []; // 检查定位哪个租户 优先定位再根据最近使用 let currentTenant = this.checkTenant(app.globalData.tenants); app.globalData.projectId =currentTenant.projectId ||wx.getStorageSync('projectId') || ''; app.globalData.projectName =currentTenant.projectName||wx.getStorageSync('projectName')|| ''; app.globalData.tenantId = currentTenant.tenantId||wx.getStorageSync('tenantId')|| ''; app.globalData.tenantName =currentTenant.tenantName|| wx.getStorageSync('tenantName') || ''; wx.setStorageSync('logined', true); wx.setStorageSync('openId', res.content.openId); wx.setStorageSync('userId', res.content.id); wx.setStorageSync('projectId', app.globalData.projectId); wx.setStorageSync('projectName', app.globalData.projectName); wx.setStorageSync('tenantId', app.globalData.tenantId); wx.setStorageSync('tenantName', app.globalData.tenantName); this.setData({ projectId: app.globalData.projectId, tenantId: app.globalData.tenantId, tenantName: app.globalData.tenantName, tenants: app.globalData.tenants, userId: app.globalData.userId }); if (this.data.wxqcode) { this.checkCode(this.data.wxqcode); this.setData({ formWxcode: false }); } this.getswiperDate(); }, // 查询定位租户 checkTenant(value = []) { var currntProjectId=wx.getStorageSync('projectId') || ''; var currntProjectName=wx.getStorageSync('projectName') || ''; var currntProjecttenantId=wx.getStorageSync('tenantId') || ''; var currntProjecttenantName=wx.getStorageSync('tenantName') || ''; var current = {}; if (value.length) { // 定位到的租户id var currentTens=[]; // 定位到的租户详情 var currentTensArr=[]; value.forEach(item => { if(item.current){ currentTens.push(item.tenantId); currentTensArr.push(item); } }) if(currentTens.length){ if(!currentTens.includes(currntProjecttenantId)&&!this.data.changeTenant&&!this.data.formList){ // 定位改变 但不是通过调节改变 切换改变 current = currentTensArr[0]; }else if(this.data.changeTenant){ current.projectId = currntProjectId current.projectName = currntProjectName current.tenantId = currntProjecttenantId current.tenantName = currntProjecttenantName } }else{ if(currntProjecttenantId!=''&&currntProjecttenantName!=''){ current.projectId = currntProjectId current.projectName = currntProjectName current.tenantId = currntProjecttenantId current.tenantName = currntProjecttenantName }else{ current = value[0] } } return current }else{ current.projectId = currntProjectId current.projectName = currntProjectName current.tenantId = currntProjecttenantId current.tenantName = currntProjecttenantName return current } }, // 查询租户名称 changeTenantname(value){ let tenarr=app.globalData.tenants; tenarr.forEach(item=>{ if(item.tenantId == value){ this.setData({changeTenant:true,tenantName:item.tenantName}); wx.setStorageSync('tenantName', item.tenantName) } }) }, onHide: function () { this.setData({ formAuth: false, hideFlag: true, formList: false }) }, onLoad: async function (options) { // 是否从微信直接进来 if (options.q !== undefined) { let q = decodeURIComponent(options.q); console.log(q); this.setData({ wxqcode: q }); wx.setStorageSync('wxqcode', q); //截取参数 options.q ='域名地址?bedcode=12345678'; // this.checkCode(q,true); } this.setData({ imageWidth: wx.getSystemInfoSync().windowWidth }); var isLogined = await AUTH.checkHasLogined(); if (!isLogined) { await AUTH.login().then(async result => { await getopenId(result.code).then(async res => { app.globalData.openId = res.openId; wx.setStorage({ data: app.globalData.openId, key: 'openId', }) await this.check({ openid: res.openId }); }); }); } else { await this.check(); } }, onShow: async function () { if (this.data.fromFollowpage) { this.setData({ fromFollowpage: false }); // this.setData({ // popswiperDate: JSON.parse(options.data) // }); this.initOpenTime(); setTimeout(() => { this.changePop(); }, 300) } if(this.data.formList || this.data.formWxcode){ await this.check(); } }, // 阻止滚动穿透 preventTouchMove: function () { return }, swiperChange(e) { this.setData({ swiperCurrent: e.detail.current }) }, popswiperChange(e) { this.setData({ popswiperCurrent: e.detail.current }) }, // 图表数据 async drawChart() { var windowW = this.data.imageWidth / 375; let chartValue = this.data.chartDate; this.setData({ chartShow: false }) if (!chartValue.length) { return; } let dataX = []; let dataY = []; chartValue.forEach(item => { if (item[1] === -9999) { dataY.push(null) } else { this.setData({ chartShow: true }) // 只显示9点后的 dataY.push(item[1].toFixed(1)) } dataX.push(utils.formatDate(item[0])) }) if (!this.data.chartShow) { return } chartbox = null; chartbox = new wxCharts({ canvasId: 'areaCanvas', type: 'line', // line area categories: dataX, dataLabel: false, dataPointShape: false, animation: false, extra: { lineStyle: 'curve' //线条的形状(弧形) }, series: [{ name: '当日温度', data: dataY, }], yAxis: { min: 20, max: 30, //max+max*0.2 format: function (val) { return val; }, }, xAxis: { disableGrid: true, }, width: (windowW * 330), //(windowW * 300) height: (windowW * 115) }); }, touchHandler: function (e) { console.log(chartbox.getCurrentDataIndex(e)); chartbox.showToolTip(e, { // background: '#7cb5ec', format: function (item, category) { return category + '温度:' + item.data } }); }, getUserInfo() { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, // 获取位置信息 async getUserLocation(cb) { var that = this; await wx.getLocation({ altitude: 'false', type: 'wgs84', complete: (res) => {}, success: async res => { that.setData({ latitude: res.latitude }) that.setData({ longitude: res.longitude }) if(cb){ await cb() }else{ this.checkPower(); } }, fail() { if(cb){ // console.log(cb); cb() } Toast.fail('未定位到您所在位置'); // that.checkPower(); that.setData({ havePower: { result: "fail", message: "未定位到您的位置" } }) } }) }, // 是否有操作权限 async checkPower() { const data = { "criteria": { "projectId":this.data.popswiperDate.projectId, "spaceId": this.data.popswiperDate.id, "userId": app.globalData.userId, "tenantId": app.globalData.tenantId, "longitude": this.data.longitude, "latitude": this.data.latitude } }; await powerCheck(data).then(res => { this.setData({ havePower: res }) }) }, // 获取轮播图数据 async getswiperDate() { const data = { "criteria": { "userId": app.globalData.userId || wx.getStorageSync('userId'), "projectId": app.globalData.projectId || wx.getStorageSync('projectId'), "tenantId": app.globalData.tenantId || wx.getStorageSync('tenantId') } } console.log(data, "getswiperDate") let res = await queryRotation(data); console.log(res, "res.count") if (res.count) { this.setData({ swiperCurrent:0, swiperDate: [{ "localName": "", id: "init", // "pic": "../../static/images/room.png", isInit: true, imgShow:true, initPic:"../../static/images/room.png", }] }) let list = res.content; if (res.count > 5) { list = list.slice(0, 5); } list.map(item=>{ item.imgShow=false; item.initPic=utils.picInit(item.roomFuncType); }) this.setData({ swiperDate: list }) }else{ this.setData({ swiperDate: [{ "localName": "", id: "init", // "pic": "../../static/images/room.png", isInit: true, imgShow:true, initPic:"../../static/images/room.png", }] }) } }, // 获取温度曲线 async getChart() { // if(this.popswiperDate.isClose){ // this.setData({ // 'popShow': true // }); // return // } let data = { projectId:this.data.popswiperDate.projectId, spaceId: this.data.popswiperDate.id } let res = await queryChart(data) || []; if (res.result == "success") { let date = res.data.slice(1); this.setData({ chartDate: date }); this.drawChart(); } this.setData({ 'popShow': true }); }, // 按钮点击 itemClick: function (value) { var btnType = value.currentTarget.id; if (btnType === '2') { wx.navigateTo({ url: '../search/index', }) } else if (btnType === '3') { wx.navigateTo({ url: '../efficient/index', }) } else { this.getScancode(); } }, async changePop(e) { if (!wx.getStorageSync('logined') || (e && this.data.swiperDate[e.currentTarget.dataset.index].id == "init")) { return; } if (!this.data.formWxcode) { this.isGetSetting(); } this.setData({ addFollow: '1', isEdit: false, feedBack: true, tempTypeValue: -1, envTypeValue: -1 }); if (e) { let index = e.currentTarget.dataset.index; this.setData({ popswiperDate: this.data.swiperDate[index] }); this.initOpenTime(); } this.setData({ "envList[0].value": this.data.popswiperDate.temperature || '--', "envList[1].value": this.data.popswiperDate.humidity || '--', "envList[2].value": this.data.popswiperDate.co2 || '--', "envList[3].value": this.data.popswiperDate.pm25 || '--' }); this.getChart(); }, // 是否加入关注列表 addonChange(e) { if (this.data.addFollow == "0") { this.setData({ addFollow: '1' }) } else { this.setData({ addFollow: '0' }) } }, async colseFeedback() { // this.setData({isEdit:true}); if (!this.data.popswiperDate.subscribe && this.data.addFollow == "1" && !this.data.feedBack) { let data = { "userId": app.globalData.userId || wx.getStorageSync('userId'), //用户id "projectId":this.data.popswiperDate.projectId , //项目id "spaceId": this.data.popswiperDate.id, //空间id flag: 1 } // 关注空间 await followRoom(data); await this.check(); } else { this.getswiperDate(); } this.setData({ popShow: false }) // this.setData({ // feedBack: true // }) }, // 弹窗的显示隐藏 popClose(e) { if (this.data.isEdit) { this.colseFeedback(); } // this.colseFeedback(); this.setData({ 'popShow': false }); // chartbox = null; // this.setData({popswiperCurrent:0}) }, // 提交反馈 submitFeedback() { if (this.data.tempTypeValue != -1 || this.data.envTypeValue != -1) { this.setData({ isEdit: true, notice: [], equipmentStatustext: [] }); let data = { "projectId": this.data.popswiperDate.projectId, //项目id "objectId": this.data.popswiperDate.id, //空间id "valueType": 1, //固定为1 "itemIds": [] } this.data.tempTypeValue != -1 ? data.itemIds.push(this.data.tempTypeValue) : ''; this.data.envTypeValue != -1 ? data.itemIds.push(this.data.envTypeValue) : ''; getEquipmentAction(data).then(res => { this.setData({ equipmentStatus: res.equipmentStatus }); let textMsg = ""; let textMsgarr=[]; this.data.equipmentStatus.forEach(item => { if (item.actions) { item.actions.forEach(items => { let textMsgobj={}; textMsgobj.name=item.localName; textMsgobj.actions=items; textMsgarr.push(textMsgobj); textMsg = textMsg + item.localName + '--' + items + ' '; }) } }) this.setData({ equipmentStatustext: textMsgarr }); }) getfeedBack(data).then(res => { this.setData({ spaceStatus: res.spaceStatus, spaceStatusId: res.icon - 1 }); let notice = res.notice ? res.notice.split('。') : []; this.setData({ notice: notice }); let currentTenantId=wx.getStorageSync('tenantId'); if(currentTenantId!==res.tenantId){ app.globalData.projectId = res.projectId; app.globalData.tenantId = res.tenantId; wx.setStorageSync('projectId', res.projectId); wx.setStorageSync('tenantId', res.tenantId); this.changeTenantname(res.tenantId); } }) this.setData({ feedBack: false, popswiperCurrent: 0 }); } }, // 反馈数据 changeType(e) { if (this.data.havePower.result === 'fail' && wx.getStorageSync('logined')) { Toast.fail(this.data.havePower.message); return } var currentIndex = e.currentTarget.dataset.index; if (e.currentTarget.dataset.type === "temp") { currentIndex = currentIndex == this.data.tempTypeValue ? -1 : currentIndex; this.setData({ "tempTypeValue": currentIndex }); } else { currentIndex = currentIndex == this.data.envTypeValue ? -1 : currentIndex; this.setData({ "envTypeValue": currentIndex }); } }, async openEnv() { // await this.checkPower(); if (this.data.havePower.result === 'fail' && wx.getStorageSync('logined')) { Toast.fail(this.data.havePower.message); return } let data = { "projectId":this.data.popswiperDate.projectId, //项目id "objectId": this.data.popswiperDate.id //空间id } openSpace(data).then(res => { this.getswiperDate(); this.setData({ secondSpaceStatus: res.spaceStatus, secondNotice: res.notice, secondIcon: res.icon, popShow: false, secondpopValue: true }); }) }, // 选择项目 goProject(e) { wx.navigateTo({ url: '../projectlist/index', }) }, fillZore(value) { if (value < 10) { value = 0 + value } return value }, formatTimeall(value) { let stringValue; const date = new Date(); var nowMonth = date.getMonth() + 1; let nowDay = date.getDate(); let torrowDay = new Date(date); torrowDay.setDate(date.getDate() + 1); let torrowMonth = torrowDay.getMonth() + 1; nowDay = this.fillZore(nowDay); nowMonth = this.fillZore(nowMonth); torrowMonth = this.fillZore(torrowMonth); if (nowMonth == value.substring(4, 6) || torrowMonth == value.substring(4, 6)) { if (value.substring(6, 8) == nowDay) { stringValue = "今日" } let torrowVlue = torrowDay.getDate(); torrowVlue = this.fillZore(torrowVlue); if (value.substring(6, 8) == torrowVlue) { stringValue = "明日" } } if (stringValue) { stringValue = stringValue + `${value.substring(8,10)}:${value.substring(10,12)}` } else { stringValue = `${value.substring(4,6)}月${value.substring(6,8)}日${value.substring(8,10)}:${value.substring(10,12)}` } return stringValue }, initOpenTime() { // 是否为关闭状态 if (this.data.popswiperDate.isClose) { this.setData({ popswiperCurrent: 0 }) this.setData({ monthDay: this.formatTimeall(this.data.popswiperDate.nextOpenTime) }); } }, commitClose() { this.setData({ secondpopValue: false }) } })