index.js 12 KB

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