App.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <script>
  2. import config from './config'
  3. import { getAccessToken } from '@/utils/auth'
  4. import { authLogin } from "@/api/login";
  5. import { setToken, setTenantId } from '@/utils/auth'
  6. import { openAuth } from "dingtalk-design-libs/biz/openAuth";
  7. import * as dd from "dingtalk-jsapi";
  8. function getQueryString() {
  9. let url = window.location.href; //获取url中"?"符后的字串
  10. console.log(url)
  11. if (url.indexOf("?") != -1) {
  12. let str = url.substr(1);
  13. let strs = str.split("=");
  14. let index = strs[1].indexOf("#")
  15. let corpId = strs[1].substring(0, index);
  16. console.log(corpId)
  17. return corpId;
  18. }
  19. };
  20. export default {
  21. onLaunch: function() {
  22. this.initApp()
  23. },
  24. methods: {
  25. // 初始化应用
  26. initApp() {
  27. // 初始化应用配置
  28. this.initConfig()
  29. // 检查用户登录状态
  30. //#ifdef H5
  31. this.checkLogin()
  32. //#endif
  33. },
  34. initConfig() {
  35. this.globalData.config = config
  36. },
  37. checkLogin() {
  38. let that = this;
  39. debugger
  40. let corpId = getQueryString();
  41. console.log(corpId);
  42. if (!getAccessToken()) {
  43. if (dd.env.platform !== "notInDingTalk") {
  44. debugger
  45. return new Promise((resolve, reject) => {
  46. dd.ready(function () {
  47. // 唤起授权--统一授权套件SDK
  48. openAuth({
  49. clientId: "suite7tssbigaaqsejgth", // 应用ID(唯一标识)
  50. corpId: corpId, // 当前组织的corpId
  51. rpcScope: "Contact.User.Read", //通讯录
  52. fieldScope: "Contact.User.mobile", //手机号
  53. type: 0, // 0 标识授权个人信息;1 标识授权组织信息
  54. }).then((res) => {
  55. // 处理返回数据
  56. console.log("统一授权套件SDK==res=", res);
  57. console.log("corpId", corpId);
  58. authLogin({ code: res.result.authCode, corpId: corpId })
  59. .then((res) => {
  60. console.log("===登录授权=", res);
  61. setToken(res.data);
  62. setTenantId(res.msg)
  63. // 设置用户信息
  64. that.$store.dispatch('GetInfo').then(res => {
  65. that.$tab.reLaunch('/pages/index')
  66. })
  67. resolve();
  68. })
  69. .catch((err) => {
  70. reject();
  71. });
  72. });
  73. });
  74. });
  75. }else{
  76. this.$tab.reLaunch('/pages/login')
  77. }
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. @import '@/static/scss/index.scss'
  85. </style>