index.js 22 KB

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