大纲
1、集成钉钉jspai
2、免登
3、鉴权
4、页面主动刷新后,鉴权失效如何配置
5、Gitee示例代码地址
集成钉钉jspai
1、在index.html页面中增加钉钉的jsApi.js:
<script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.10.3/dingtalk.open.js"></script>
2、在页面中可以使用window.dd来操作了
免登
1、免登操作,此处我是结合鉴权来使用的,所以corpId使用后台返回的数据,不需鉴权的话,可以直接用钉钉开发者后台对应的corpId代替即可。
window.dd.runtime.permission.requestAuthCode({
corpId: _config.corpId,
onSuccess: function(result) {
// that.toast('登录中,请稍后...');
console.log(result.code);
that.$store.dispatch('DDLogin', result.code).then(() => {
that.$store.dispatch('GetInfo').then(res => {
uni.reLaunch({
url: '/pages/index/index'
});
});
})
.catch((res) => {
console.log(res)
that.toast('登录异常,请联系管理员排查系统账号和钉钉账号对应关系');
});
},
onFail: function(err) {
alert('err:')
alert(err)
}
});
2、如果需要鉴权后免登的,可以在登录页login.vue使用如下方式
<template>
<view>
<u-loading-page loading-text="loading..." bg-color="#e8e8e8" loading-color="#000000"></u-loading-page>
</view>
</template>
<script>
import {
getSignatureInfo
} from '@/api/login';
export default {
data() {
return {
};
},
onLoad() {
// 设置全局鉴权的url,方便页面主动刷新导致的鉴权失败问题
uni.setStorageSync('permission_url', location.href);
// 免登
this.ddLogin();
},
methods: {
// 钉钉登录
ddLogin() {
let that = this;
// 获取后台鉴权信息
getSignatureInfo(location.href).then(res => {
const _config = res.data;
var endStamp=Date.now()+2*60*60*1000;
// 默认鉴权有效期是2小时,鉴权数据存本地,避免每次都去服务器申请
uni.setStorageSync('endStamp', endStamp);
uni.setStorageSync('_config.agentId', _config.agentId);
uni.setStorageSync('_config.corpId', _config.corpId);
uni.setStorageSync('_config.timeStamp', _config.timeStamp);
uni.setStorageSync('_config.nonceStr', _config.nonceStr);
uni.setStorageSync('_config.signature', _config.signature);
//console.log(res.data)
var jsapilist = [
'device.notification.alert', //警告框
'device.notification.confirm', //确认框
'biz.navigation.setTitle', //设置导航栏标题
'biz.navigation.quit', //关闭页面
'biz.util.openModal', //打开弹窗(模态窗)
'biz.contact.complexPicker', //选择人员
'device.geolocation.get', //获取当前地理位置信息
'biz.map.locate', //地图定位
'biz.map.search', //地图页面支持搜索
'biz.map.view' //展示位置
] //需要使用的jsapi列表
//1、鉴权
window.dd.config({
agentId: _config.agentId,
corpId: _config.corpId, //必填,企业ID
timeStamp: _config.timeStamp, // 必填,生成签名的时间戳
nonceStr: _config.nonceStr, // 必填,生成签名的随机串
signature: _config.signature, // 必填,签名
jsApiList: jsapilist // 必填,需要使用的jsapi列表,注意:不要带dd。
});
window.dd.error(function(err) {
//验证失败
console.log("进入到error中");
console.log('dd error: ' + JSON.stringify(err));
})
// 免登
window.dd.runtime.permission.requestAuthCode({
corpId: _config.corpId,
onSuccess: function(result) {
// that.toast('登录中,请稍后...');
console.log(result.code);
that.$store.dispatch('DDLogin', result.code).then(() => {
that.$store.dispatch('GetInfo').then(res => {
uni.reLaunch({
url: '/pages/index/index'
});
});
})
.catch((res) => {
console.log(res)
that.toast('登录异常,请联系管理员排查系统账号和钉钉账号对应关系');
});
},
onFail: function(err) {
alert('err:')
alert(err)
}
});
});
}