index.js 14 KB

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