import store from './store' import { getAccessToken } from '@/utils/auth' import { authLogin } from "@/api/login"; import { setToken, setTenantId } from '@/utils/auth' import { openAuth } from "dingtalk-design-libs/biz/openAuth"; import * as dd from "dingtalk-jsapi"; function getQueryString() { let url = window.location.href; //获取url中"?"符后的字串 console.log(url) if (url.indexOf("?") != -1) { let str = url.substr(1); let strs = str.split("="); let index = strs[1].indexOf("#") let corpId = strs[1].substring(0, index); console.log(corpId) return corpId; } }; // 登录页面 const loginPage = "/pages/login" // 页面白名单 const whiteList = [ '/pages/login', '/pages/common/webview/index' ] // 检查地址白名单 function checkWhite(url) { const path = url.split('?')[0] return whiteList.indexOf(path) !== -1 } // 页面跳转验证拦截器 let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"] list.forEach(item => { uni.addInterceptor(item, { invoke(to) { let corpId = getQueryString(); console.log(corpId); if (getAccessToken()) { if (to.path === loginPage) { uni.reLaunch({ url: "/" }) } return true } else { if (dd.env.platform !== "notInDingTalk") { return new Promise((resolve, reject) => { dd.ready(function () { // 唤起授权--统一授权套件SDK openAuth({ clientId: "suite7tssbigaaqsejgth", // 应用ID(唯一标识) corpId: corpId, // 当前组织的corpId rpcScope: "Contact.User.Read", //通讯录 fieldScope: "Contact.User.mobile", //手机号 type: 0, // 0 标识授权个人信息;1 标识授权组织信息 }).then((res) => { // 处理返回数据 console.log("统一授权套件SDK==res=", res); console.log("corpId", corpId); authLogin({ code: res.result.authCode, corpId: corpId }) .then((res) => { console.log("===登录授权=", res); setToken(res.data); setTenantId(res.msg) // 设置用户信息 store.dispatch('GetInfo').then(res => { uni.reLaunch({ url: "/" }) }) resolve(); }) .catch((err) => { reject(); }); }); }); }); } else { if (checkWhite(to.url)) { return true } uni.reLaunch({ url: loginPage }) return false } } }, fail(err) { console.log(err) } }) })