index.js 23 KB

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