钉钉开放平台:客户端SDK - 钉钉开放平台
1.使用npm安装。
npm install dingtalk-jsapi --save
2.加载 dingtalk-jsapi (在登录页引入)
import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载,也可按需进行加载
3.路由index.ts写好登录页路由并在main.ts引入使用,APP.vue写路由出口
(路由安装npm install vue-router@4)
main.ts:
import { createApp } from 'vue';
import App from './App.vue';
// 引入路由
import router from './router';
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.use(ElementPlus)
app.use(router)
app.mount('#app');
index.ts登录页路由:
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
const routes: Array<RouteRecordRaw> = [
// {s
// path: '/',
// name: 'index',
// component: () => import('../views/index.vue')
// },
{
path: '/login',
name: '/login',
component: () => import('../views/login.vue')
},
// 添加更多路由...
];
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach(async (to, from, next) => {
// 判断是否存在token
let token = localStorage.getItem('token')
if (!token && to.path == '/login') {
next()
} else {
if (token) {
next()
} else {
next('/login');
}
}
console.log(to, from, next)
})
export default router;
4.登录页
<template>
<div>
登录页
</div>
</template>
<script setup lang="ts">
import { defineAsyncComponent, onMounted } from "vue";
import * as dd from "dingtalk-jsapi";
// 页面加载
onMounted(() => {
// 判断是否是钉钉端登录
if (dd.env.platform != "notInDingTalk") {
console.log("钉钉环境");
}
});
</script>
<style scoped>
</style>
文件结构