permission.js 2.3 KB

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