index.js 15 KB

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