uniapp中钉钉免登+鉴权开发过程

大纲

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)
						}
					});
				});

			}
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值