permission.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. let corpId = getQueryString();
  36. console.log(corpId);
  37. if (getAccessToken()) {
  38. if (to.path === loginPage) {
  39. uni.reLaunch({ url: "/" })
  40. }
  41. return true
  42. } else {
  43. if (dd.env.platform !== "notInDingTalk") {
  44. return new Promise((resolve, reject) => {
  45. dd.ready(function () {
  46. // 唤起授权--统一授权套件SDK
  47. openAuth({
  48. clientId: "suite7tssbigaaqsejgth", // 应用ID(唯一标识)
  49. corpId: corpId, // 当前组织的corpId
  50. rpcScope: "Contact.User.Read", //通讯录
  51. fieldScope: "Contact.User.mobile", //手机号
  52. type: 0, // 0 标识授权个人信息;1 标识授权组织信息
  53. }).then((res) => {
  54. // 处理返回数据
  55. console.log("统一授权套件SDK==res=", res);
  56. console.log("corpId", corpId);
  57. authLogin({ code: res.result.authCode, corpId: corpId })
  58. .then((res) => {
  59. console.log("===登录授权=", res);
  60. setToken(res.data);
  61. setTenantId(res.msg)
  62. // 设置用户信息
  63. store.dispatch('GetInfo').then(res => {
  64. uni.reLaunch({ url: "/" })
  65. })
  66. resolve();
  67. })
  68. .catch((err) => {
  69. reject();
  70. });
  71. });
  72. });
  73. });
  74. } else {
  75. if (checkWhite(to.url)) {
  76. return true
  77. }
  78. uni.reLaunch({ url: loginPage })
  79. return false
  80. }
  81. }
  82. },
  83. fail(err) {
  84. console.log(err)
  85. }
  86. })
  87. })