index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. import $ from '../../utils/Tool';
  2. import router from '../../utils/router';
  3. import {getSetting,getLocation,checkHasLogined,authlogin} from '../../utils/auth';
  4. const AUTH = require('../../utils/auth');
  5. import icons from "../../utils/icon";
  6. import Toast from '../../vant-weapp/dist/toast/toast';
  7. import {
  8. getopenId,
  9. userCheck,
  10. getSpaceAdjustList,
  11. spaceTop,
  12. getWeather,
  13. deleteSpaceAdjust,
  14. queryUseranswer,
  15. getCurrentSeason,
  16. powerCheck,
  17. openSpace,
  18. } from "../../requests/api";
  19. var refreshTimer=null;
  20. Page({
  21. data: {
  22. surveyShow:false, //是否显示调查问卷
  23. lottieShow:true, //是否显示lottie组件
  24. meauList: [{
  25. id: 1,
  26. title: '扫码调节',
  27. des: '扫描空间二维码',
  28. imgSrc: "../../static/images/qrcode.png"
  29. },
  30. {
  31. id: 2,
  32. title: '空间列表',
  33. des: '查找更多空间',
  34. imgSrc: "../../static/images/adjust.png"
  35. }
  36. ],
  37. envNamelist:$.store.get("envNamelist"),
  38. imgbaseUrl:$.store.get("imgbaseUrl"),
  39. picInitUrl:$.store.get("picInitUrl"),
  40. latitude:null,
  41. longitude:null,
  42. noDate:true,
  43. statusList: icons.statusList,
  44. spacestatusList:icons.spacestatusList ,
  45. cardList:[],
  46. cardCurrent:null,
  47. tenantName: $.store.get('tenantName'),
  48. tenants: $.store.get('tenants'),
  49. logined:$.storage.get('logined'),
  50. havePower:{result: "fail",message: "未定位到您的位置"},
  51. temperatureNum:'--',
  52. guideUser:false,
  53. CustomBar:$.store.get('CustomBar')
  54. },
  55. toDetail(e){
  56. if(!e.currentTarget.dataset.istemp){
  57. router.push("detail",e.currentTarget.dataset)
  58. }else{
  59. router.push("detail",{name:"温度",
  60. localname:"温度",
  61. param:"temperature",
  62. funcid:"Tdb",
  63. spaceid:e.currentTarget.dataset.spaceid,
  64. projectid:e.currentTarget.dataset.projectid})
  65. }
  66. },
  67. // 关闭问卷
  68. surveyClose(value){
  69. this.setData({surveyShow:false,lottieShow:true});
  70. if(value.detail.good){
  71. wx.showToast({
  72. title:"感谢您的反馈",
  73. image:"../../static/images/bixin.png",
  74. duration: 2000
  75. })
  76. }
  77. },
  78. // 检查是否需要填写问卷
  79. async userAnswer(){
  80. let day = new Date();
  81. let year = day.getFullYear();
  82. let month = day.getMonth() + 1;
  83. month=month<10?('0'+month):month;
  84. let today = day.getDate();
  85. today=today<10?('0'+today):today;
  86. let seasonType="";
  87. const dataSeason={
  88. projectId:$.store.get('projectId')||$.storage.get('projectId'),
  89. date:`${year}${month}${today}`
  90. }
  91. await getCurrentSeason(dataSeason).then(res=>{
  92. if(res.result=="success"){
  93. seasonType= res.data;
  94. }
  95. })
  96. let projectId= $.store.get('projectId');
  97. let tenantId= $.store.get('tenantId');
  98. let data={
  99. "criteria":{
  100. "userId": $.store.get('userId'),
  101. "projectId": projectId,
  102. "tenantId": tenantId,
  103. "startTime": {
  104. "$ge": `${year}${month}${today}`
  105. },
  106. "endTime": {
  107. "$le":`${year}${month}${today}`
  108. },
  109. "seasonType": seasonType
  110. }
  111. }
  112. queryUseranswer(data).then(res=>{
  113. (!res.count&&!res.content)&&this.setData({surveyShow:true,lottieShow:false})
  114. })
  115. },
  116. // 检查是否过引导
  117. checkGuide(value){
  118. if(!this.data.tenantName){
  119. return
  120. }
  121. (!$.storage.get('guideUser')&&!value&&!$.storage.get('nextRemind'))&&this.userAnswer();
  122. !$.storage.get('guideUser')&&this.getPageheight();
  123. this.setData({'guideUser':!$.storage.get('guideUser')})
  124. },
  125. // 步骤引导函数
  126. nextStep(e){
  127. $.storage.set('guideUser',true);
  128. this.setData({'guideUser':false});
  129. },
  130. // 获取容器高度
  131. getPageheight(){
  132. let that =this;
  133. wx.createSelectorQuery().select('#j_page').boundingClientRect(function(rect){
  134. that.setData({'pageHight':rect.height})
  135. }).exec()
  136. },
  137. swipeClick(e){
  138. let that = this;
  139. let {spaceid} = e.currentTarget.dataset;
  140. wx.showModal({
  141. title: '删除',
  142. content: '是否删除该空间',
  143. success (res) {
  144. if (res.confirm) {
  145. let data={
  146. "projectId": $.store.get('projectId'),
  147. "userId":$.store.get('userId'),
  148. spaceId:spaceid
  149. }
  150. deleteSpaceAdjust(data).then(res=>{
  151. that.getData()
  152. })
  153. }
  154. }
  155. })
  156. },
  157. // 置顶操作
  158. tapItem(e){
  159. let {id,top,projectId} = e.currentTarget.dataset.spaceid;
  160. let spaceIndex = e.currentTarget.dataset.index;
  161. let data = {
  162. "projectId":projectId,
  163. "spaceId":id,
  164. "userId":this.data.userId,
  165. "top":top?0:1
  166. }
  167. spaceTop(data).then(async res=>{
  168. if(res.result=="success"){
  169. await this.getData();
  170. wx.pageScrollTo({
  171. scrollTop:0,
  172. duration:300
  173. })
  174. }
  175. })
  176. },
  177. settingLocation(){
  178. return new Promise((relove,reject)=>{
  179. wx.showModal({
  180. title: '是否授权当前位置',
  181. content: '需要获取您的地理位置,请确认授权',
  182. confirmColor: '#f16765',
  183. success: res => {
  184. relove(res)
  185. },
  186. fail:err=>{
  187. reject(err)
  188. }
  189. })
  190. })
  191. },
  192. async isGetSetting(value) {
  193. let {authSetting} = await getSetting();
  194. if(authSetting['scope.userLocation']){
  195. await this.getUserLocation();
  196. }else{
  197. await this.settingLocation().then(res=>{
  198. if (res.confirm) {
  199. wx.openSetting({
  200. success: async data => {
  201. await this.getUserLocation();
  202. value&&this.remoteCheck();
  203. },
  204. })
  205. } else {
  206. this.setData({havePower: {result: "fail",message: "未定位到您的位置"}})
  207. }
  208. })
  209. }
  210. },
  211. // 获取位置信息
  212. async getUserLocation(cb) {
  213. var that = this;
  214. let {latitude,longitude} = await getLocation();
  215. this.setData({latitude,longitude});
  216. },
  217. // 检查是否注册 是否远程调节
  218. async remoteCheck() {
  219. // if($.storage.get('wxqcode')){
  220. // return
  221. // }
  222. var that=this;
  223. await this.isGetSetting('cb').then(async res=>{
  224. if(this.data.longitude){
  225. await userCheck({longitude:that.data.longitude,latitude:that.data.latitude}).then(async res=>{
  226. if (res.result === "fail") { // && !that.data.formAuth
  227. $.storage.set("logined",false);
  228. this.setData({logined:false});
  229. router.push('auth');
  230. } else {
  231. await that.getTenant(res);
  232. // 第一次进入获取数据
  233. console.log($.storage.get('wxqcode'),"$.storage.get('wxqcode')")
  234. !$.storage.get('wxqcode')&&that.getData();
  235. !$.storage.get('wxqcode')&&that.autoRefresh(this.getData);
  236. if($.storage.get('wxqcode')&&$.storage.get('logined')){
  237. this.checkCode($.storage.get('wxqcode'));
  238. }
  239. }
  240. });
  241. }
  242. });
  243. },
  244. getTenant(res) {
  245. // 存入全局
  246. $.store.set('userInfo',res.content);
  247. return new Promise(async (resolve,reject)=>{
  248. !$.store.get('openId')&&$.store.set("openId",res.content.openId);
  249. !$.store.get('userId')&&$.store.set("userId",res.content.id);
  250. $.store.set("tenants",res.content.tenants);
  251. // 检查定位哪个租户 优先定位 再根据最近使用
  252. let currentTenant = await this.checkTenant(res.content.tenants);
  253. $.store.set("projectId",currentTenant.projectId);
  254. $.store.set("tenantId",currentTenant.tenantId);
  255. $.store.set("tenantName",currentTenant.tenantName);
  256. // 存入Storage
  257. !$.store.get('logined')&&$.storage.set('logined', true);
  258. this.setData({logined:true});
  259. !$.storage.get('openId')&&$.storage.set('openId', res.content.openId);
  260. !$.storage.get('openId')&&$.storage.set('userId', res.content.id);
  261. $.storage.set('projectId', currentTenant.projectId);
  262. $.storage.set('tenantId', currentTenant.tenantId);
  263. $.storage.set('tenantName',currentTenant.tenantName);
  264. this.setData({
  265. projectId: $.store.get('projectId'),
  266. tenantId: $.store.get('tenantId'),
  267. tenantName: $.store.get('tenantName'),
  268. tenants: $.store.get('tenants'),
  269. userId: $.store.get('userId')
  270. });
  271. // if (this.data.wxqcode) {
  272. // this.checkCode(this.data.wxqcode);
  273. // this.setData({
  274. // formWxcode: false
  275. // });
  276. // }
  277. resolve();
  278. })
  279. },
  280. // 查询定位租户
  281. checkTenant(value = []) {
  282. // 首先默认选中的是定位的租户,如果小程序定位的位置就是要调节的租户对应的空间(定位距离租户200m以内),直接调节;
  283. return new Promise((resolve, reject)=>{
  284. const currntProjectId=$.storage.get('projectId');
  285. const currntProjecttenantId=$.storage.get('tenantId');
  286. const currntProjecttenantName=$.storage.get('tenantName');
  287. let current = {};
  288. if($.store.get('changeTenantId')){
  289. value.length&&value.forEach(item=>{
  290. if(item.tenantId === currntProjecttenantId){
  291. current.projectId = item.projectId;
  292. current.tenantId = item.tenantId;
  293. current.tenantName = item.tenantName;
  294. }
  295. })
  296. $.store.set('changeTenantId',false)
  297. }else if($.store.get('goHome')){
  298. $.store.set('goHome',false)
  299. current.projectId =$.store.get("projectId");
  300. current.tenantId = $.store.get("tenantId");
  301. current.tenantName =$.store.get("tenantName");
  302. }else if(value.length){
  303. // 定位到的租户id
  304. var currentTens=[];
  305. // 定位到的租户详情 可能多个
  306. var currentTensArr=[];
  307. value.forEach(item => {
  308. if(item.current){
  309. currentTens.push(item.tenantId);
  310. currentTensArr.push(item);
  311. }
  312. })
  313. if(currentTens.length){
  314. // 最近使用不包含定位 且没有手动切换项目
  315. if(!currentTens.includes(currntProjecttenantId)&&!this.data.formList){
  316. //定位改变 但不是通过调节改变 切换改变
  317. current = currentTensArr[0];
  318. }else if(currentTens.includes(currntProjecttenantId)){
  319. currentTensArr.forEach(item=>{
  320. if(item.tenantId===currntProjecttenantId){
  321. current.projectId = item.projectId;
  322. current.tenantId = item.tenantId;
  323. current.tenantName = item.tenantName;
  324. }
  325. })
  326. }
  327. }else{
  328. if(currntProjecttenantId!=''){
  329. current.projectId = currntProjectId;
  330. current.tenantId = currntProjecttenantId;
  331. current.tenantName = currntProjecttenantName;
  332. }else{
  333. current = value[0];
  334. }
  335. }
  336. }else{
  337. current.projectId = currntProjectId;
  338. current.tenantId = currntProjecttenantId;
  339. current.tenantName = currntProjecttenantName;
  340. }
  341. resolve(current)
  342. })
  343. },
  344. // 个人中心
  345. gotoUser(){
  346. router.push('usercenter')
  347. },
  348. // 自动刷新
  349. autoRefresh(fn){
  350. if(refreshTimer){
  351. clearInterval(refreshTimer);
  352. refreshTimer = null;
  353. }
  354. let refreshTime=$.store.get("autoRefreshTime");
  355. refreshTimer = setTimeout(()=>{
  356. fn();
  357. this.autoRefresh(fn);
  358. },refreshTime)
  359. },
  360. /**
  361. * 获取页面服务端数据
  362. */
  363. async getData() {
  364. // $.loading()
  365. let userId = $.store.get('userId')||$.storage.get('userId');
  366. if(!userId){
  367. return;
  368. }
  369. // wx.showLoading({
  370. // title:"加载中"
  371. // })
  372. const data = {
  373. "criteria": {
  374. "userId": userId,
  375. "projectId": $.store.get('projectId')||$.storage.get('projectId'),
  376. "tenantId": $.store.get('tenantId')||$.storage.get('tenantId')
  377. }
  378. }
  379. // let res = await queryRotation(data);
  380. let res = await getSpaceAdjustList(data);
  381. if(res.count){
  382. res.content.forEach(item=>{
  383. (item.humidity||item.humidity==0)&&(item.humiditylevel = this.checkLevel(item.humidity,"humidity"));
  384. (item.co2||item.co2==0)&&(item.co2level = this.checkLevel(item.co2,"co2"));
  385. (item.pm25||item.pm25==0)&&(item.pm25level = this.checkLevel(item.pm25,"pm25"));
  386. (item.hcho||item.hcho==0)&&(item.hcholevel = this.checkLevel(item.hcho,"hcho"));
  387. if(typeof item.isPassengerPass==="undefined"){
  388. item.isPassengerPassShow=false;
  389. }else{
  390. item.isPassengerPassShow=true;
  391. item.isPassengerPass=item.isPassengerPass?'有人':'无人'
  392. }
  393. })
  394. this.setData({cardList:res.content})
  395. }else{
  396. this.setData({noDate:true,cardList:[]})
  397. }
  398. // wx.hideLoading()
  399. // $.hideLoading()
  400. },
  401. checkLevel(value,name){
  402. let objList={
  403. humidity:{
  404. range:[30,70],
  405. text:["干燥","适宜","湿润"]
  406. },
  407. co2:{
  408. range:[800,1500],
  409. text:["适宜","偏高","超标"]
  410. },
  411. pm25:{
  412. range:[35,75],
  413. text:["优","良","差"]
  414. },
  415. hcho:{
  416. range:[0.08,0.1],
  417. text:["适宜","偏高","超标"]
  418. }
  419. };
  420. let sortArr=[value,...objList[name].range].sort((a,b)=>{return a-b});
  421. let level=sortArr.findIndex(item=>item===value);
  422. let levelTxt = objList[name].text[level];
  423. return {level,levelTxt}
  424. },
  425. /**
  426. * 去立即调节页面
  427. */
  428. goToadjust(e){
  429. let index = e.target.dataset.index;
  430. let data =this.data.cardList[index];
  431. data.outLine&&(data.outLine="");
  432. router.push("adjust",data);
  433. },
  434. meauClick(e){
  435. if(e.currentTarget.dataset.index===1){
  436. this.getScancode();
  437. }else{
  438. this.gotoSpacelist()
  439. }
  440. },
  441. // 获取扫码结果
  442. getScancode: function () {
  443. // if (!$.storage.get('logined')) {
  444. // router.push('auth');
  445. // return
  446. // }
  447. // 只允许从相机扫码
  448. wx.scanCode({
  449. onlyFromCamera: true,
  450. complete: (res) => {
  451. if (res.errMsg === '"scanCode:fail cancel"') {
  452. Toast.fail('已取消扫描');
  453. }
  454. },
  455. success: (res) => {
  456. this.checkCode(res.result);
  457. },
  458. fail: (res) => {
  459. Toast.fail('未扫描到结果');
  460. }
  461. })
  462. },
  463. gotoSpacelist(){
  464. router.push('spacelist')
  465. },
  466. // 检查二维码
  467. async checkCode(value) {
  468. console.log(value,381)
  469. let scanArr = value.split("?");
  470. let domain = scanArr[0];
  471. let qualifiedUrl=["http://meos.sagacloud.cn/scan",'https://duoduoenv.sagacloud.cn/scan']
  472. if (qualifiedUrl.includes(domain)) {
  473. let md = scanArr[1].split('=') || [];
  474. let md1 = md[1];
  475. router.push('adjust',{md1})
  476. } else {
  477. Toast.fail('无效的二维码');
  478. }
  479. },
  480. async userLogin(){
  481. let that =this;
  482. let isLogined = await checkHasLogined();
  483. if (!isLogined) {
  484. await authlogin().then(async result => {
  485. await getopenId(result.code).then(async res => {
  486. $.storage.set('openId',res.openId);
  487. $.store.set('openId',res.openId);
  488. await this.remoteCheck();
  489. });
  490. });
  491. } else {
  492. await this.remoteCheck();
  493. }
  494. },
  495. chooseTenant(e){
  496. if($.store.get('tenants').length<=1){
  497. return
  498. }
  499. router.push('projectlist')
  500. },
  501. async inItUserdate(){
  502. // 用户登录
  503. await this.userLogin();
  504. },
  505. /**
  506. * 生命周期函数--监听页面加载
  507. */
  508. async onLoad(options) {
  509. // 是否从微信直接进来
  510. if (options.q !== undefined) {
  511. let q = decodeURIComponent(options.q);
  512. // let q="https://duoduoenv.sagacloud.cn/scan?key=Sp110108025988e09ed4cd8c45b5a496f18622ab81ca";
  513. $.storage.set('wxqcode', q);
  514. // console.log($.storage.get('wxqcode'),"123")
  515. }
  516. $.storage.set('nextRemind',false);
  517. await this.inItUserdate();
  518. console.log($.storage.get('wxqcode'),$.storage.get('logined'),"449")
  519. // if($.storage.get('wxqcode')&&$.storage.get('logined')){
  520. // this.checkCode($.storage.get('wxqcode'));
  521. // }
  522. this.getWeatherNum();
  523. this.checkGuide(options.q);
  524. // 加载数据
  525. // this.getData();
  526. },
  527. getWeatherNum(){
  528. let data={
  529. projectId:$.store.get('projectId')
  530. }
  531. getWeather(data).then(res=>{
  532. if(res.content){
  533. this.setData({temperatureNum:res.content.temperature})
  534. }
  535. })
  536. },
  537. /**
  538. * 生命周期函数--监听页面显示
  539. */
  540. async onShow() {
  541. // await this.inItUserdate();
  542. if($.storage.get("tenantId")&&!$.store.get('changeTenantId')&&!this.data.formList){
  543. this.getData();
  544. this.autoRefresh(this.getData);
  545. }
  546. if(this.data.formList){
  547. this.setData({
  548. tenantName:$.store.get('tenantName'),
  549. tenantId:$.store.get('tenantId'),
  550. projectId:$.store.get('projectId'),
  551. formList:false
  552. },()=>{
  553. this.getData();
  554. this.getWeatherNum();
  555. this.userAnswer();
  556. })
  557. }
  558. if($.store.get('changeTenantId')){
  559. await this.remoteCheck();
  560. this.getWeatherNum();
  561. this.checkGuide();
  562. }
  563. },
  564. onHide(){
  565. clearInterval(refreshTimer);
  566. refreshTimer=null;
  567. },
  568. /**
  569. * 页面相关事件处理函数--监听用户下拉动作
  570. */
  571. async onPullDownRefresh() {
  572. $.store.set('goHome',true);
  573. await this.remoteCheck()
  574. wx.stopPullDownRefresh()
  575. },
  576. /**
  577. * 页面上拉触底事件的处理函数
  578. */
  579. onReachBottom: function () {
  580. },
  581. /**
  582. * 用户点击右上角分享
  583. */
  584. onShareAppMessage: function () {
  585. }
  586. })