index.js 17 KB

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