소스 검색

优化页面授权次数,同一个url只需要授权一次

zhujindu 1 년 전
부모
커밋
7820f37272
2개의 변경된 파일95개의 추가작업 그리고 61개의 파일을 삭제
  1. 78 47
      src/utils/getPosition.js
  2. 17 14
      src/views/storeManagement/storeDetail.vue

+ 78 - 47
src/utils/getPosition.js

@@ -8,11 +8,13 @@ const wx = Vue.prototype.wx;
 // 腾讯位置服务 key
 const TxMapKey = "WLCBZ-HRM6L-YOMPV-ME62B-AQOG6-JUBW6";
 
-// 获取当前定位
+/**
+ * 获取当前定位 调用之前确保当前页面已经授权(getTicketFun)
+ * */
 export function getPosition() {
   return new Promise((resolve, reject) => {
     toastLoading(0, "定位中...", true);
-    // 本地开发 test 环境时跳过获取定位功能 模拟定位
+    // 本地开发 test 环境时跳过获取定位功能 模拟定位
     if (process.env.NODE_ENV === "test") {
       let resData = {
         latitude: 34.615684509277344,
@@ -25,57 +27,86 @@ export function getPosition() {
       toastLoading().clear();
       resolve({ TXisBD, resData });
     } else {
-      let url = window.location.href;
-      //  获取签名
-      getTicket({ url: url }).then((response) => {
-        console.log(response);
-        toastLoading().clear();
-        if (response.code == 200) {
-          let qiyeData = response.data;
-          wx.config({
-            beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
-            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
-            appId: qiyeData.appId, // 必填,企业微信的corpID
-            timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
-            nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
-            signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
-            jsApiList: ["ready", "getLocation"], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
-          });
-          wx.ready(() => {
-            wx.getLocation({
-              type: "gcj02",
-              success: (resData) => {
-                toastLoading().clear();
-                console.log("处理前");
-                console.log(resData.latitude, resData.longitude);
-                // 定位坐标转换 腾讯转百度
-                let TXisBD = CJ02BD(resData.latitude, resData.longitude);
-                console.log("处理后");
-                console.log(TXisBD);
-                localStorage.setItem("lat", resData.latitude);
-                localStorage.setItem("lon", resData.longitude);
-                resolve({ TXisBD, resData });
-              },
-              fail: () => {
-                toastLoading().clear();
-                reject("GPS未开启");
-              },
-            });
-          });
-          wx.error((err) => {
+      // let url = window.location.href;
+      // //  获取签名
+      // getTicket({ url: url }).then((response) => {
+      //   console.log(response);
+      //   toastLoading().clear();
+      //   if (response.code == 200) {
+      //     let qiyeData = response.data;
+      //     wx.config({
+      //       beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
+      //       debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
+      //       appId: qiyeData.appId, // 必填,企业微信的corpID
+      //       timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
+      //       nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
+      //       signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
+      //       jsApiList: ["ready", "getLocation"], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
+      //     });
+      wx.ready(() => {
+        wx.getLocation({
+          type: "gcj02",
+          success: (resData) => {
+            toastLoading().clear();
+            console.log("处理前");
+            console.log(resData.latitude, resData.longitude);
+            // 定位坐标转换 腾讯转百度
+            let TXisBD = CJ02BD(resData.latitude, resData.longitude);
+            console.log("处理后");
+            console.log(TXisBD);
+            localStorage.setItem("lat", resData.latitude);
+            localStorage.setItem("lon", resData.longitude);
+            resolve({ TXisBD, resData });
+          },
+          fail: () => {
             toastLoading().clear();
-            console.log(err);
-            reject("定位失败,请开启企微定位权限");
-          });
-        } else {
-          toastLoading().clear();
-          reject("获取签名失败");
-        }
+            reject("GPS未开启");
+          },
+        });
+      });
+      wx.error((err) => {
+        toastLoading().clear();
+        console.log(err);
+        reject("定位失败,请开启企微定位权限");
       });
+      // } else {
+      //   toastLoading().clear();
+      //   reject("获取签名失败");
+      // }
+      // });
     }
   });
 }
 
+/**
+ *当前页面授权 一个url只需要授权一次
+ */
+export function getTicketFun() {
+  return new Promise((resolve, reject) => {
+    // 当前页面
+    let url = window.location.href;
+    //  获取签名
+    getTicket({ url: url }).then((response) => {
+      console.log(response);
+      toastLoading().clear();
+      if (response.code == 200) {
+        let qiyeData = response.data;
+        wx.config({
+          beta: true, // 必须这么写,否则wx.invoke调用形式的jsapi会有问题
+          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
+          appId: qiyeData.appId, // 必填,企业微信的corpID
+          timestamp: qiyeData.timestamp, // 必填,生成签名的时间戳
+          nonceStr: qiyeData.nonceStr, // 必填,生成签名的随机串
+          signature: qiyeData.signature, // 必填,签名,见 附录-JS-SDK使用权限签名算法
+          jsApiList: ["ready", "getLocation"], // 必填,需要使用的JS接口列表,凡是要调用的接口都需要传进来
+        });
+        resolve("获取签名成功");
+      } else {
+        reject("获取签名失败");
+      }
+    });
+  });
+}
 /**
  * 地点搜索 获取500米范围poi点
  * @param {*object} location //当前位置

+ 17 - 14
src/views/storeManagement/storeDetail.vue

@@ -396,7 +396,7 @@ import axios from "axios";
 import { updateStoreAddress, valid } from "@/api/visitstore";
 import uploadImg from "@/components/viewaddreddUploadImg.vue";
 import helpPage from "./helpPage.vue";
-import { getPosition, getMapPoi, getkeywordPoi } from "@/utils/getPosition";
+import { getPosition, getMapPoi, getkeywordPoi, getTicketFun } from "@/utils/getPosition";
 export default {
   components: { uploadImg, mapmarker, viewUploadImg, helpPage },
   data() {
@@ -1194,19 +1194,22 @@ export default {
       ImagePreview([val]);
     },
     getStoreTypeList() {
-      if (localStorage.getItem("storeType") == "2") {
-        this.DIYshow = true;
-        getStoreyslTypeList({}).then((res) => {
-          this.storeTypeList = res.data;
-          this.getDetail();
-        });
-      } else {
-        this.DIYshow = false;
-        getStoreTypeList({}).then((res) => {
-          this.storeTypeList = res.data;
-          this.getDetail();
-        });
-      }
+      // 先授权
+      getTicketFun().then(() => {
+        if (localStorage.getItem("storeType") == "2") {
+          this.DIYshow = true;
+          getStoreyslTypeList({}).then((res) => {
+            this.storeTypeList = res.data;
+            this.getDetail();
+          });
+        } else {
+          this.DIYshow = false;
+          getStoreTypeList({}).then((res) => {
+            this.storeTypeList = res.data;
+            this.getDetail();
+          });
+        }
+      });
     },
     getDetail() {
       this.pageShow = false;