浙政钉鉴权,以及添加权限,获取定位等

1,270 阅读1分钟

1-获取定位

export const getGeolocation = () => {
  return new Promise((resolve) => {
    dd.ready(function () {
      dd.getGeolocation({
        targetAccuracy: 100,
        coordinate: 0,
        withReGeocode: true,
        useCache: false, //默认是true,如果需要频繁获取地理位置,请设置false
      })
        .then((res) => {
          console.log("获取定位信息成功", res);
          const { longitude, latitude } = res;
          resolve(`X:${longitude} Y:${latitude}`);
        })
        .catch(() => {
          console.log("获取定位信息失败");
          resolve("获取定位信息失败");
        });
    });
  });
};

2-代码中添加已经后台配置过的权限编号

export const init= () => {
  dd.ready(function () {//准备dd
    dd.getPhoneInfo()//获取信息
      .then(() => {
        // 在浙政钉环境下面两个可以在配置后台拿到
        let corpId="111111";
        let zzdAppCode="aaaa";
        dd.ready(function () {
          dd.getAuthCode({
            corpId,
          })
            .then((info) => {
              // 通过该免登授权码code可以获取用户身份
              console.log("code:" + info.code);
              axios({
                method: "get",
                url: `/auth/auth/auu/authCode?authCode=${info.code}&type=token&zzdAppCode=${zzdAppCode}`,
                data: {},
                contentType: "application/json",
                dataType: "json",
              })
                .then(function (res) {
                  let RObj = res.data;
                  if (RObj.success) {
                    return RObj.data.user_name;
                  }else{
                    this.dialog("用户不存在请联系管理员!");
                  }
                })
                .catch((err) => {
                  console.log("获取用户信息失败", err);
                });
            })
            .catch((err) => {
              console.log(err);
            });
        });
      })
      .catch(() => {
        // 不在浙政钉环境,使用默认的用户信息,token通过登录接口获取
        const { username } = window.CONFIG;
        return username;
      });
  });
};

3-鉴权的其他权限配置

getJSApiToken() {
//下面是获取定位,拍照以及上传权限,还可以添加其他权限
      let jsApiList = ["getGeolocation","chooseVideo","uploadLocalFile"];
      axios({
        headers: {
          "X-Gisq-Token": "bearer " + localStorage.getItem("access_token"),
        },//token,必要
        method: "get",
        url: "/aaa/bbb/ding/getJSApiToken",//路径
        contentType: "application/json",
        dataType: "json",
      }).then(function (res) {
        let ticket = res.data.data.accessToken;
        dd.ready(() => {
          dd.authConfig({
            ticket: ticket,
            jsApiList: jsApiList,
          })
            .then((res) => {
              console.log('鉴权结果',res);
            })
            .catch((err) => {
              console.log("authConfig错误");
              console.log(err);
            });
        });
      });
    },