permission.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import store from './store'
  2. import { getAccessToken } from '@/utils/auth'
  3. import { authLogin } from "@/api/login";
  4. import { setToken, setTenantId } from '@/utils/auth'
  5. import { openAuth } from "dingtalk-design-libs/biz/openAuth";
  6. import * as dd from "dingtalk-jsapi";
  7. function getQueryString() {
  8. let url = window.location.href; //获取url中"?"符后的字串
  9. console.log(url)
  10. if (url.indexOf("?") != -1) {
  11. let str = url.substr(1);
  12. let strs = str.split("=");
  13. let index = strs[1].indexOf("#")
  14. let corpId = strs[1].substring(0, index);
  15. console.log(corpId)
  16. return corpId;
  17. }
  18. };
  19. // 登录页面
  20. const loginPage = "/pages/login"
  21. // 页面白名单
  22. const whiteList = [
  23. '/pages/login', '/pages/common/webview/index'
  24. ]
  25. // 检查地址白名单
  26. function checkWhite(url) {
  27. const path = url.split('?')[0]
  28. return whiteList.indexOf(path) !== -1
  29. }
  30. // 页面跳转验证拦截器
  31. let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
  32. list.forEach(item => {
  33. uni.addInterceptor(item, {
  34. invoke(to) {
  35. debugger
  36. let corpId = getQueryString();
  37. console.log(corpId);
  38. if (getAccessToken()) {
  39. if (to.path === loginPage) {
  40. uni.reLaunch({ url: "/" })
  41. }
  42. return true
  43. } else {
  44. if (dd.env.platform !== "notInDingTalk") {
  45. debugger
  46. return new Promise((resolve, reject) => {
  47. dd.ready(function () {
  48. // 唤起授权--统一授权套件SDK
  49. openAuth({
  50. clientId: "suite7tssbigaaqsejgth", // 应用ID(唯一标识)
  51. corpId: corpId, // 当前组织的corpId
  52. rpcScope: "Contact.User.Read", //通讯录
  53. fieldScope: "Contact.User.mobile", //手机号
  54. type: 0, // 0 标识授权个人信息;1 标识授权组织信息
  55. }).then((res) => {
  56. // 处理返回数据
  57. console.log("统一授权套件SDK==res=", res);
  58. console.log("corpId", corpId);
  59. authLogin({ code: res.result.authCode, corpId: corpId })
  60. .then((res) => {
  61. console.log("===登录授权=", res);
  62. setToken(res.data);
  63. setTenantId(res.msg)
  64. // 设置用户信息
  65. store.dispatch('GetInfo').then(res => {
  66. uni.reLaunch({ url: "/" })
  67. })
  68. resolve();
  69. })
  70. .catch((err) => {
  71. reject();
  72. });
  73. });
  74. });
  75. });
  76. } else {
  77. if (checkWhite(to.url)) {
  78. return true
  79. }
  80. uni.reLaunch({ url: loginPage })
  81. return false
  82. }
  83. }
  84. },
  85. fail(err) {
  86. console.log(err)
  87. }
  88. })
  89. })