index.js 15 KB

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