H5应用集成到钉钉,完成免登录

钉钉创建H5应用--->访问H5主页--->获取钉钉用户信息(唯一ID)--->绑定H5应用用户--->H5模拟登录

1.页面获取authcode,用于后台获取钉钉用户信息;

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head lang="zh">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8" />
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="yes" name="apple-touch-fullscreen"/>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <title>钉钉授权CRM登录</title>
    <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/3.0.12/dingtalk.open.js"></script>

    <script>
        var corpId = '[[${corpId}]]';//钉钉后台可查到
        console.log(corpId);
        dd.runtime.permission.requestAuthCode({

            corpId: corpId,
            onSuccess : function(res) {
                document.getElementById("authCode").value= res.code;
               document.form1.submit();
                console.log(res.code)
            },
            onFail : function(err) {
                console.log("fail"+res);
            }
        });
    </script>
    <style>

        .myButton{
            background: #3296fb;
            color: #fff;
            border:#3296fb;
            font-size:16px;
            border-radius:5px;
            line-height: 20px;
            letter-spacing:5px;
            padding:10px 5px;
        }

    </style>
</head>
<body>
<form action="checkUser" method="post" name="form1" id="form1" style="margin-top: 30px;text-align: center;">
    <div style="line-height:50px;vertical-align:middle;padding-top:20px;">
        <img src="../dingding.png" style="width:100px;"/>
        <img src="../sq.png" style="width:50px;"/>
        <img src="../cloudcc.png" style="width:100px;"/>
    </div>

    <input type="hidden" name="authCode" id="authCode" />
    <div style="width:100%;clor:#999;font-size:20px;line-height:50px;padding:10px 5px;">您正在操作钉钉授权登录CRM系统会获取您的钉钉用户信息</div>
    <div>
        <input type="submit" value="&nbsp;确定授权" class="myButton"/>
    </div>
</form>
</body>
</html>

2.根据authcode获取钉钉用户id

 public String getUserId(String code) throws Exception{

        // URL_GET_USER_INFO   https://oapi.dingtalk.com//user/getuserinfo
        log.info(urlConstant.URL_GET_USER_INFO+"?access_token="+accessToken);
        //此处crmapi工具类,只使用了其中的get方法。可以自己写
        String getUserId = crmapi.sendGetRequest(urlConstant.URL_GET_USER_INFO,
                "access_token="+accessToken+"&code="+code);
        log.info("使用缓存中的token,userJson:"+getUserId);
        JSONObject json  = JSONObject.fromObject(getUserId);

        if(json.getString("errcode").equals("40014")){
            log.info("===请求新token");
            getUserId = crmapi.sendGetRequest(urlConstant.URL_GET_USER_INFO,
                    "access_token="+this.getToken()+"&code="+code);
            json  = JSONObject.fromObject(getUserId);
        }
        String userId =  json.getString("userid");

        return userId;
    }

3.H5应用内部逻辑

a.用户信息绑定钉钉用户id;

b.模拟登录;

4.下次访问直接通过钉钉用户id在H5系统里查询用户信息,访问系统。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jarry.liu

如果对您有帮助,鼓励下博主吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值