Java钉钉开发_02_免登授权(身份验证)

Java钉钉开发_02_免登授权(身份验证)

  • 商品编号:
    #77675645_776
    • 原价:
      免费
    • 会员价:
      免费
  • 分类:
    • V1.0.0
  • 数量:

购物车中已存在此商品,请在购物车中操作单击跳转购物车

  • 开发者:shirayner
  • 开发语言:Java
  • 开发环境:IDEA
  • 数据库:无
  • 商品架构:B/S
  • 代码管理工具:github
  • 编码格式:utf-8
  • 是否开源:是
  • 开源协议:BSD

一、本节要点

1.免登授权的流程

(1)签名校验

(2)获取code,并传到后台

(3)根据code获取userid

(4)根据userid获取用户信息,(此处可进行相应业务处理)

(5)将用户信息传到前端,前端拿到用户信息,并做相应处理

\"748623-20170930112052481-721842382\"

2.计算签名信息(signature)

2.1 待签名参数

ticketjsapi_ticket
nonceStr       随机字符串,随机生成
timeStamp时间戳
url当前网页的URL,不包含#及其后面部分

2.2签名流程

(1)字典序

将所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式 (即 key1=value1&key2=value2…)拼接成字符串string1

如:String string1= \"jsapi_ticket=\" + jsTicket + \"&noncestr=\" + nonceStr + \"&timestamp=\" + timeStamp + \"&url=\" + url;

(2)SHA-1签名,得到 signature

    /**\r\n     * @desc : 3.生成签名的函数 \r\n     *  \r\n     * @param ticket jsticket\r\n     * @param nonceStr 随机串,自己定义\r\n     * @param timeStamp 生成签名用的时间戳 \r\n     * @param url 需要进行免登鉴权的页面地址,也就是执行dd.config的页面地址 \r\n     * @return\r\n     * @throws Exception String\r\n     */\r\n    \r\n    public static String getSign(String jsTicket, String nonceStr, Long timeStamp, String url) throws Exception {  \r\n        String plainTex = \"jsapi_ticket=\" + jsTicket + \"&noncestr=\" + nonceStr + \"&timestamp=\" + timeStamp + \"&url=\" + url;\r\n        System.out.println(plainTex);\r\n        try {  \r\n            MessageDigest crypt = MessageDigest.getInstance(\"SHA-1\");\r\n            crypt.reset();\r\n            crypt.update(plainTex.getBytes(\"UTF-8\"));\r\n            return byteToHex(crypt.digest());\r\n        } catch (NoSuchAlgorithmException e) {  \r\n            throw new Exception(e.getMessage());  \r\n        } catch (UnsupportedEncodingException e) {  \r\n            throw new Exception(e.getMessage());  \r\n        }  \r\n    }  \r\n\r\n    //将bytes类型的数据转化为16进制类型  \r\n    private static String byteToHex(byte[] hash) {\r\n        Formatter formatter = new Formatter();\r\n        for (byte b : hash) {\r\n            formatter.format(\"%02x\", new Object[] { Byte.valueOf(b) });\r\n        }\r\n        String result = formatter.toString();\r\n        formatter.close();\r\n        return result;\r\n    }

3.签名校验的流程

3.1 后端准备好前端校验参数

 后台方法:getConfig(HttpServletRequest) 

 public static String getConfig(HttpServletRequest request){\r\n        //1.准备好参与签名的字段\r\n        /* \r\n         *以http://localhost/test.do?a=b&c=d为例 \r\n         *request.getRequestURL的结果是http://localhost/test.do \r\n         *request.getQueryString的返回值是a=b&c=d \r\n         */  \r\n        String urlString = request.getRequestURL().toString();\r\n        String queryString = request.getQueryString();\r\n\r\n        String queryStringEncode = null;\r\n        String url;\r\n        if (queryString != null) {\r\n            queryStringEncode = URLDecoder.decode(queryString);\r\n            url = urlString + \"?\" + queryStringEncode;\r\n        } else {\r\n            url = urlString;\r\n        }\r\n\r\n\r\n        String nonceStr=UUID.randomUUID().toString();      //随机数\r\n        long timeStamp = System.currentTimeMillis() / 1000;     //时间戳参数  \r\n\r\n        String signedUrl = url;\r\n        String accessToken = null;\r\n        String ticket = null;\r\n        String signature = null;       //签名\r\n\r\n        //2.进行签名,获取signature\r\n        try {  \r\n            accessToken=AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);  \r\n\r\n            ticket=AuthHelper.getJsapiTicket(accessToken);  \r\n            signature=getSign(ticket,nonceStr,timeStamp,signedUrl);  \r\n\r\n\r\n        } catch (Exception e) {  \r\n            // TODO Auto-generated catch block  \r\n            e.printStackTrace();  \r\n        }  \r\n\r\n        System.out.println(\"accessToken:\"+accessToken);\r\n        System.out.println(\"ticket:\"+ticket);\r\n        System.out.println(\"nonceStr:\"+nonceStr);\r\n        System.out.println(\"timeStamp:\"+timeStamp);\r\n        System.out.println(\"signedUrl:\"+signedUrl);\r\n        System.out.println(\"signature:\"+signature);\r\n        System.out.println(\"agentId:\"+Env.AGENTID);\r\n        System.out.println(\"corpId:\"+Env.CORP_ID);\r\n        \r\n        \r\n        \r\n           String configValue = \"{jsticket:\'\" + ticket + \"\',signature:\'\" + signature + \"\',nonceStr:\'\" + nonceStr + \"\',timeStamp:\'\"\r\n                    + timeStamp + \"\',corpId:\'\" + Env.CORP_ID + \"\',agentId:\'\" + Env.AGENTID + \"\'}\";\r\n            System.out.println(configValue);\r\n\r\n        return configValue;  \r\n    }

3.2 前端接收后台参数

    在前端调用后端方法,获取dd.config所需的校验参数:‘url’,‘nonceStr’,‘agentId’,‘timeStamp’,‘corpId’,‘signature’。

<script type=\"text/javascript\">\r\n    var _config =<%=com.ray.dingtalk.auth.AuthHelper.getConfig(request)%>;\r\n</script>

3.3 执行前端 dd.config ,进行签名校验

    dd.config 用接收到的 nonceStr、agentId、timeStamp、corpId这四个参数去钉钉官方后端计算出一个签名(signature ), 并将这个签名与我们后端所计算的signature来进行比对,若一致,则校验通过。若不一致,则是我们后端计算签名的时候出错了。此时可根据错误消息提示去进行调试。

dd.config({\r\n    agentId : _config.agentId,\r\n    corpId : _config.corpId,\r\n    timeStamp : _config.timeStamp,\r\n    nonceStr : _config.nonceStr,\r\n    signature : _config.signature,\r\n    jsApiList : [                           //需要调用的借口列表 \r\n        \'runtime.info\',\r\n        \'biz.contact.choose\',            //选择用户接口  \r\n        \'device.notification.confirm\', \r\n        \'device.notification.alert\',   //confirm,alert,prompt都是弹出小窗口的接口     \r\n        \'device.notification.prompt\', \r\n        \'biz.ding.post\',\r\n        \'biz.util.openLink\' ]\r\n});

3.4 异常:js加载顺序有误所引起的 前端什么信息都不提示

出现这个原因,可能是自己js出错了。我的原因是js加载顺序有误。

请注意这几个js的加载顺序: _config,jquery-3.2.1.min.js 必须在auth.js之前加载

<script src=\"js/jquery-3.2.1.min.js\"></script>\r\n<script type=\"text/javascript\" src=\"http://g.alicdn.com/dingding/open-develop/1.6.9/dingtalk.js\"></script>\r\n\r\n<script type=\"text/javascript\">\r\n    var _config =<%=com.ray.dingtalk.auth.AuthHelper.getConfig(request)%>;\r\n</script>\r\n<script type=\"text/javascript\" src=\"js/auth.js\"></script>

4. 将code送往后端:ajax

签名校验成功之后,即dd.config校验成功之后,会执行dd.ready函数,这时我们就可以使用钉钉的jsapi了。

签名校验成功后,我们就可以调用获取免登授权码(CODE)的jsapi,来获取code,然后通过ajax方式将这个code传到后台userInfoServlet

/**获取免登授权码 CODE\r\n     * \r\n     */\r\n    dd.runtime.permission.requestAuthCode({  \r\n        corpId : _config.corpId,  \r\n        onSuccess : function(info) {                                                   //成功获得code值,code值在info中  \r\n            alert(\'authcode: \' + info.code);  \r\n            /* \r\n             *$.ajax的是用来使得当前js页面和后台服务器交互的方法 \r\n             *参数url:是需要交互的后台服务器处理代码,userInfoServlet\r\n             *参数type:指定和后台交互的方法,因为后台servlet代码中处理Get和post的doGet和doPost \r\n             *data:负责传递请求参数\r\n             *其中success方法和error方法是回调函数,分别表示成功交互后和交互失败情况下处理的方法 \r\n             */  \r\n            $.ajax({  \r\n                type : \"POST\",\r\n                url : \"http://p65s3p.natappfree.cc/DingTalk_Demo/userInfoServlet\",\r\n                data : {\r\n                    code : info.code\r\n                },\r\n                success : function(data, status, xhr) {\r\n                    alert(data);\r\n                    var userInfo = JSON.parse(data);\r\n\r\n                    document.getElementById(\"userName\").innerHTML = userInfo.name;\r\n                    document.getElementById(\"userId\").innerHTML = userInfo.userid;\r\n\r\n                    // 图片\r\n                    if(info.avatar.length != 0){\r\n                        var img = document.getElementById(\"userImg\");\r\n                        img.src = info.avatar;\r\n                        img.height = \'200\';\r\n                        img.width = \'200\';\r\n                    }\r\n\r\n                },\r\n                error : function(xhr, errorType, error) {  \r\n                    logger.e(\"yinyien:\" + _config.corpId);  \r\n                    alert(errorType + \', \' + error);  \r\n                }  \r\n            });  \r\n\r\n        },  \r\n        onFail : function(err) {                                                       //获得code值失败  \r\n            alert(\'fail: \' + JSON.stringify(err));  \r\n        }  \r\n    });

5.根据code获取userid

private static final String GET_USERINFO_BYCODE_URL=\"https://oapi.dingtalk.com/user/getuserinfo?access_token=ACCESSTOKEN&code=CODE\";\r\n\r\n/** 5.根据免登授权码Code查询免登用户userId\r\n     * @desc :钉钉服务器返回的用户信息为:\r\n     * userid    员工在企业内的UserID\r\n     * deviceId    手机设备号,由钉钉在安装时随机产生\r\n     * is_sys    是否是管理员\r\n     * sys_level    级别,0:非管理员 1:超级管理员(主管理员) 2:普通管理员(子管理员) 100:老板\r\n     *  \r\n     * @param accessToken\r\n     * @param code\r\n     * @throws Exception void\r\n     */\r\n    public JSONObject getUserInfo(String accessToken,String code) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_USERINFO_BYCODE_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"CODE\", code);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n            //4.请求成功,则返回jsonObject\r\n            if (0==jsonObject.getInteger(\"errcode\")) {\r\n                return jsonObject;\r\n            }\r\n            //5.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n\r\n        return null;\r\n    }

6.根据userid获取用户信息

private static final String GET_USER_URL=\"https://oapi.dingtalk.com/user/get?access_token=ACCESSTOKEN&userid=USERID\";\r\n\r\n\r\n    /** 2.根据userid获取成员详情\r\n     * @desc :获取成员详情\r\n     *   参考文档: https://open-doc.dingtalk.com/docs/doc.htm?spm=0.0.0.0.jjSfQQ&treeId=371&articleId=106816&docType=1#s0\r\n     * @param accessToken\r\n     * @param userId void\r\n     * @throws Exception \r\n     */\r\n    public JSONObject getUser(String accessToken, String userId) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_USER_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"USERID\", userId);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n            //4.请求成功,则返回jsonObject\r\n            if (0==jsonObject.getInteger(\"errcode\")) {\r\n                return jsonObject;\r\n            }\r\n            //5.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n\r\n        return null;\r\n    }

7.将用户信息传到前端

注意:传输格式为json

//3.通过userid换取用户信息\r\nJSONObject jsonObject=us.getUser(accessToken, userId);\r\nresult=JSON.toJSON(jsonObject);\r\n\r\nPrintWriter out = response.getWriter(); \r\nout.print(result);  \r\nout.close();  \r\nout = null;

8.前端接收用户信息后做相应处理

jsp中代码:

<div align=\"center\">\r\n        <img id=\"userImg\" alt=\"头像\" src=\"\">\r\n    </div>\r\n\r\n    <div align=\"center\">\r\n        <span>UserName:</span>\r\n        <div id=\"userName\" style=\"display: inline-block\"></div>\r\n    </div>\r\n\r\n    <div align=\"center\">\r\n        <span>UserId:</span>\r\n        <div id=\"userId\" style=\"display: inline-block\"></div>\r\n    </div>

js中代码:发送code的ajax调用成功后

success : function(data, status, xhr) {\r\n

    alert(data);\r\n

    //接收后端发送过来的用户信息\r\n

    var userInfo = JSON.parse(data);\r\n

    //收到用户信息后所做的处理\r\n

    document.getElementById(\"userName\").innerHTML = userInfo.name;\r\n

    document.getElementById(\"userId\").innerHTML = userInfo.userid;\r\n

    // 图片\r\n

    if(info.avatar.length != 0){\r\n

        var img = document.getElementById(\"userImg\");\r\n

        img.src = info.avatar;\r\n

        img.height = \'200\';\r\n

        img.width = \'200\';\r\n

    }\r\n

},

二、代码实现

1.钉钉参数配置——Env.java

将Env.java中的配置修改成你自己的

package com.ray.dingtalk.config;\r\n\r\n/**@desc  : 企业应用接入时的常量定义\r\n * \r\n * @author: shirayner\r\n * @date  : 2017年9月27日 下午4:57:36\r\n */\r\n\r\npublic class Env {\r\n\r\n    /**\r\n     * 企业应用接入秘钥相关\r\n     */\r\n    public static final String CORP_ID = \"ding6d4828968696691535c2f4657eb6378f\";\r\n    public static final String CORP_SECRET = \"ZigmkCY4VcsGUhLIzmfxOmP0ElJbGI5uBhn-2mPelovnjPcA6e4LrjpYXQQw89Q4\";\r\n    public static final String SSO_Secret = \"YgIGtCHmcwAmOuKsAo_lgqJJiOwyez2G6vBvhCf1zwR6kZ5DGMJsxOcUgK5p1C\";\r\n    public static final String AGENTID = \"128838526\";\r\n    \r\n    \r\n    /**\r\n     * DING API地址\r\n     */\r\n    public static final String OAPI_HOST = \"https://oapi.dingtalk.com\";\r\n    /**\r\n     * 企业应用后台地址,用户管理后台免登使用\r\n     */\r\n    public static final String OA_BACKGROUND_URL = \"\";\r\n\r\n\r\n    /**\r\n     * 企业通讯回调加密Token,注册事件回调接口时需要传递给钉钉服务器\r\n     */\r\n    public static final String TOKEN = \"\";\r\n    public static final String ENCODING_AES_KEY = \"\";\r\n}

2.Http请求工具类——HttpHelper.java

主要包括发送GET请求和POST请求

package com.ray.dingtalk.util;\r\n\r\nimport java.io.IOException;\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.security.MessageDigest;\r\nimport java.security.NoSuchAlgorithmException;\r\nimport java.util.Formatter;\r\n\r\nimport javax.servlet.http.HttpServletRequest;\r\n\r\nimport org.apache.http.HttpEntity;\r\nimport org.apache.http.client.config.RequestConfig;\r\nimport org.apache.http.client.methods.CloseableHttpResponse;\r\nimport org.apache.http.client.methods.HttpGet;\r\nimport org.apache.http.client.methods.HttpPost;\r\nimport org.apache.http.entity.StringEntity;\r\nimport org.apache.http.impl.client.CloseableHttpClient;\r\nimport org.apache.http.impl.client.HttpClients;\r\nimport org.apache.http.protocol.BasicHttpContext;\r\nimport org.apache.http.util.EntityUtils;\r\n\r\nimport com.alibaba.fastjson.JSON;\r\nimport com.alibaba.fastjson.JSONObject;\r\nimport com.ray.dingtalk.auth.AuthHelper;\r\nimport com.ray.dingtalk.config.Env;\r\n\r\n/**\r\n * HTTP请求封装,建议直接使用sdk的API\r\n */\r\npublic class HttpHelper {\r\n\r\n    /**\r\n     * @desc :1.发起GET请求\r\n     *  \r\n     * @param url\r\n     * @return JSONObject\r\n     * @throws Exception \r\n     */\r\n    public static JSONObject httpGet(String url) throws Exception {\r\n        //1.创建httpClient \r\n        CloseableHttpClient httpClient = HttpClients.createDefault();\r\n        //2.生成一个请求\r\n        HttpGet httpGet = new HttpGet(url);\r\n        //3.配置请求的属性\r\n        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();\r\n        httpGet.setConfig(requestConfig);\r\n        \r\n        //4.发起请求,获取响应信息\r\n        CloseableHttpResponse response = null;\r\n        try {\r\n            response = httpClient.execute(httpGet, new BasicHttpContext());\r\n\r\n            //如果返回结果的code不等于200,说明出错了  \r\n            if (response.getStatusLine().getStatusCode() != 200) {\r\n\r\n                System.out.println(\"request url failed, http code=\" + response.getStatusLine().getStatusCode()\r\n                        + \", url=\" + url);\r\n                return null;\r\n            }\r\n            //5.解析请求结果\r\n            HttpEntity entity = response.getEntity();      //reponse返回的数据在entity中 \r\n            if (entity != null) {\r\n                String resultStr = EntityUtils.toString(entity, \"utf-8\");  //将数据转化为string格式  \r\n\r\n                JSONObject result = JSON.parseObject(resultStr);    //将String转换为 JSONObject\r\n                if (result.getInteger(\"errcode\") == 0) {\r\n                    return result;\r\n                } else {\r\n                    System.out.println(\"request url=\" + url + \",return value=\");\r\n                    System.out.println(resultStr);\r\n                    int errCode = result.getInteger(\"errcode\");\r\n                    String errMsg = result.getString(\"errmsg\");\r\n                    throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n                }\r\n            }\r\n        } catch (IOException e) {\r\n            System.out.println(\"request url=\" + url + \", exception, msg=\" + e.getMessage());\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (response != null) try {\r\n                response.close();\r\n            } catch (IOException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n\r\n        return null;\r\n    }\r\n\r\n\r\n    /** 2.发起POST请求\r\n     * @desc :\r\n     *  \r\n     * @param url\r\n     * @param data\r\n     * @return\r\n     * @throws Exception JSONObject\r\n     */\r\n    public static JSONObject httpPost(String url, Object data) throws Exception {\r\n        HttpPost httpPost = new HttpPost(url);\r\n        CloseableHttpResponse response = null;\r\n        CloseableHttpClient httpClient = HttpClients.createDefault();\r\n        RequestConfig requestConfig = RequestConfig.custom().\r\n                setSocketTimeout(2000).setConnectTimeout(2000).build();\r\n        httpPost.setConfig(requestConfig);\r\n        httpPost.addHeader(\"Content-Type\", \"application/json\");\r\n\r\n        try {\r\n            StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), \"utf-8\");\r\n            httpPost.setEntity(requestEntity);\r\n\r\n            response = httpClient.execute(httpPost, new BasicHttpContext());\r\n\r\n            if (response.getStatusLine().getStatusCode() != 200) {\r\n\r\n                System.out.println(\"request url failed, http code=\" + response.getStatusLine().getStatusCode()\r\n                        + \", url=\" + url);\r\n                return null;\r\n            }\r\n            HttpEntity entity = response.getEntity();\r\n            if (entity != null) {\r\n                String resultStr = EntityUtils.toString(entity, \"utf-8\");\r\n\r\n                JSONObject result = JSON.parseObject(resultStr);\r\n                if (result.getInteger(\"errcode\") == 0) {\r\n                    result.remove(\"errcode\");\r\n                    result.remove(\"errmsg\");\r\n                    return result;\r\n                } else {\r\n                    System.out.println(\"request url=\" + url + \",return value=\");\r\n                    System.out.println(resultStr);\r\n                    int errCode = result.getInteger(\"errcode\");\r\n                    String errMsg = result.getString(\"errmsg\");\r\n                    throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n                }\r\n            }\r\n        } catch (IOException e) {\r\n            System.out.println(\"request url=\" + url + \", exception, msg=\" + e.getMessage());\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (response != null) try {\r\n                response.close();\r\n            } catch (IOException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n\r\n        return null;\r\n    }\r\n}

3.钉钉相关接口权限的获取工具类——AuthHelper.java

主要包括:AccessToken、JsapiTicket、以及签名校验的工具类

package com.ray.dingtalk.auth;\r\n\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.net.URLDecoder;\r\nimport java.security.MessageDigest;\r\nimport java.security.NoSuchAlgorithmException;\r\nimport java.util.Formatter;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport java.util.UUID;\r\n\r\nimport javax.servlet.http.HttpServletRequest;\r\n\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\n\r\nimport com.alibaba.fastjson.JSONObject;\r\nimport com.ray.dingtalk.config.Env;\r\nimport com.ray.dingtalk.util.HttpHelper;\r\n\r\n\r\n\r\n/**\r\n * 钉钉相关配置参数的获取工具类\r\n * @desc  : AccessToken和jsticket的获取封装\r\n * \r\n * @author: shirayner\r\n * @date  : 2017年9月27日 下午5:00:25\r\n */\r\npublic class AuthHelper {\r\n    //private static Logger log = LoggerFactory.getLogger(AuthHelper.class);  \r\n    //获取access_token的接口地址,有效期为7200秒\r\n    private static final String GET_ACCESSTOKEN_URL=\"https://oapi.dingtalk.com/gettoken?corpid=CORPID&corpsecret=CORPSECRET\"; \r\n\r\n    //获取getJsapiTicket的接口地址,有效期为7200秒 \r\n    private static final String GET_JSAPITICKET_URL=\"https://oapi.dingtalk.com/get_jsapi_ticket?access_token=ACCESSTOKE\"; \r\n\r\n\r\n    /** 1.获取access_token \r\n     * @desc : \r\n     *  \r\n     * @param corpId\r\n     * @param corpSecret\r\n     * @return\r\n     * @throws Exception String\r\n     */\r\n    public static String getAccessToken(String corpId,String corpSecret) throws Exception {\r\n        //1.获取请求url\r\n        String url=GET_ACCESSTOKEN_URL.replace(\"CORPID\", corpId).replace(\"CORPSECRET\", corpSecret);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n\r\n        //3.解析结果,获取accessToken\r\n        String accessToken=\"\";  \r\n        if (null != jsonObject) {  \r\n            accessToken=jsonObject.getString(\"access_token\");\r\n\r\n            //4.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }  \r\n\r\n\r\n        return accessToken;\r\n    }\r\n\r\n    /**\r\n     * 2、获取JSTicket, 用于js的签名计算\r\n     * 正常的情况下,jsapi_ticket的有效期为7200秒,所以开发者需要在某个地方设计一个定时器,定期去更新jsapi_ticket\r\n     * @throws Exception \r\n     */\r\n    public static String getJsapiTicket(String accessToken) throws Exception  {\r\n        //1.获取请求url\r\n        String url=GET_JSAPITICKET_URL.replace(\"ACCESSTOKE\", accessToken);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n\r\n        //3.解析结果,获取ticket\r\n        String ticket=\"\";  \r\n        if (null != jsonObject) {  \r\n            ticket=jsonObject.getString(\"ticket\");\r\n\r\n            //4.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }  \r\n\r\n        return ticket;\r\n    }\r\n\r\n\r\n    /**\r\n     * @desc : 3.生成签名的函数 \r\n     *  \r\n     * @param ticket jsticket\r\n     * @param nonceStr 随机串,自己定义\r\n     * @param timeStamp 生成签名用的时间戳 \r\n     * @param url 需要进行免登鉴权的页面地址,也就是执行dd.config的页面地址 \r\n     * @return\r\n     * @throws Exception String\r\n     */\r\n    \r\n    public static String getSign(String jsTicket, String nonceStr, Long timeStamp, String url) throws Exception {  \r\n        String plainTex = \"jsapi_ticket=\" + jsTicket + \"&noncestr=\" + nonceStr + \"&timestamp=\" + timeStamp + \"&url=\" + url;\r\n        System.out.println(plainTex);\r\n        try {  \r\n            MessageDigest crypt = MessageDigest.getInstance(\"SHA-1\");\r\n            crypt.reset();\r\n            crypt.update(plainTex.getBytes(\"UTF-8\"));\r\n            return byteToHex(crypt.digest());\r\n        } catch (NoSuchAlgorithmException e) {  \r\n            throw new Exception(e.getMessage());  \r\n        } catch (UnsupportedEncodingException e) {  \r\n            throw new Exception(e.getMessage());  \r\n        }  \r\n    }  \r\n\r\n    //将bytes类型的数据转化为16进制类型  \r\n    private static String byteToHex(byte[] hash) {\r\n        Formatter formatter = new Formatter();\r\n        for (byte b : hash) {\r\n            formatter.format(\"%02x\", new Object[] { Byte.valueOf(b) });\r\n        }\r\n        String result = formatter.toString();\r\n        formatter.close();\r\n        return result;\r\n    }\r\n\r\n    \r\n    \r\n\r\n\r\n    /**\r\n     * @desc :获取前端jsapi需要的配置参数(已弃用,请用getConfig(HttpServletRequest))\r\n     *  \r\n     * @param request      request:在钉钉中点击微应用图标跳转的url地址 \r\n     * @return Map<String,Object>  将需要的参数存入map,并返回\r\n     */\r\n    public static Map<String, Object> getDDConfig(HttpServletRequest request){  \r\n\r\n        Map<String, Object> configMap = new HashMap<String, Object>();\r\n\r\n        //1.准备好参与签名的字段\r\n        /* \r\n         *以http://localhost/test.do?a=b&c=d为例 \r\n         *request.getRequestURL的结果是http://localhost/test.do \r\n         *request.getQueryString的返回值是a=b&c=d \r\n         */  \r\n        String urlString = request.getRequestURL().toString();\r\n        String queryString = request.getQueryString();\r\n\r\n        String queryStringEncode = null;\r\n        String url;\r\n        if (queryString != null) {\r\n            queryStringEncode = URLDecoder.decode(queryString);\r\n            url = urlString + \"?\" + queryStringEncode;\r\n        } else {\r\n            url = urlString;\r\n        }\r\n\r\n\r\n        String nonceStr=UUID.randomUUID().toString();      //随机数\r\n        long timeStamp = System.currentTimeMillis() / 1000;     //时间戳参数  \r\n\r\n        String signedUrl = url;\r\n        String accessToken = null;\r\n        String ticket = null;\r\n        String signature = null;       //签名\r\n\r\n        //2.进行签名,获取signature\r\n        try {  \r\n            accessToken=AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);  \r\n\r\n            ticket=AuthHelper.getJsapiTicket(accessToken);  \r\n            signature=getSign(ticket,nonceStr,timeStamp,signedUrl);  \r\n\r\n\r\n        } catch (Exception e) {  \r\n            // TODO Auto-generated catch block  \r\n            e.printStackTrace();  \r\n        }  \r\n\r\n        System.out.println(\"accessToken:\"+accessToken);\r\n        System.out.println(\"ticket:\"+ticket);\r\n        System.out.println(\"nonceStr:\"+nonceStr);\r\n        System.out.println(\"timeStamp:\"+timeStamp);\r\n        System.out.println(\"signedUrl:\"+signedUrl);\r\n        System.out.println(\"signature:\"+signature);\r\n        System.out.println(\"agentId:\"+Env.AGENTID);\r\n        System.out.println(\"corpId:\"+Env.CORP_ID);\r\n        \r\n        \r\n        \r\n        //3.将配置参数存入Map\r\n        configMap.put(\"agentId\", Env.AGENTID);\r\n        configMap.put(\"corpId\", Env.CORP_ID);\r\n        configMap.put(\"timeStamp\", timeStamp);\r\n        configMap.put(\"nonceStr\", nonceStr);\r\n        configMap.put(\"signature\", signature);\r\n\r\n        return configMap;  \r\n    }  \r\n\r\n    public static String getConfig(HttpServletRequest request){  \r\n\r\n        //1.准备好参与签名的字段\r\n        /* \r\n         *以http://localhost/test.do?a=b&c=d为例 \r\n         *request.getRequestURL的结果是http://localhost/test.do \r\n         *request.getQueryString的返回值是a=b&c=d \r\n         */  \r\n        String urlString = request.getRequestURL().toString();\r\n        String queryString = request.getQueryString();\r\n\r\n        String queryStringEncode = null;\r\n        String url;\r\n        if (queryString != null) {\r\n            queryStringEncode = URLDecoder.decode(queryString);\r\n            url = urlString + \"?\" + queryStringEncode;\r\n        } else {\r\n            url = urlString;\r\n        }\r\n\r\n\r\n        String nonceStr=UUID.randomUUID().toString();      //随机数\r\n        long timeStamp = System.currentTimeMillis() / 1000;     //时间戳参数  \r\n\r\n        String signedUrl = url;\r\n        String accessToken = null;\r\n        String ticket = null;\r\n        String signature = null;       //签名\r\n\r\n        //2.进行签名,获取signature\r\n        try {  \r\n            accessToken=AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);  \r\n\r\n            ticket=AuthHelper.getJsapiTicket(accessToken);  \r\n            signature=getSign(ticket,nonceStr,timeStamp,signedUrl);  \r\n\r\n\r\n        } catch (Exception e) {  \r\n            // TODO Auto-generated catch block  \r\n            e.printStackTrace();  \r\n        }  \r\n\r\n        System.out.println(\"accessToken:\"+accessToken);\r\n        System.out.println(\"ticket:\"+ticket);\r\n        System.out.println(\"nonceStr:\"+nonceStr);\r\n        System.out.println(\"timeStamp:\"+timeStamp);\r\n        System.out.println(\"signedUrl:\"+signedUrl);\r\n        System.out.println(\"signature:\"+signature);\r\n        System.out.println(\"agentId:\"+Env.AGENTID);\r\n        System.out.println(\"corpId:\"+Env.CORP_ID);\r\n        \r\n        \r\n        \r\n           String configValue = \"{jsticket:\'\" + ticket + \"\',signature:\'\" + signature + \"\',nonceStr:\'\" + nonceStr + \"\',timeStamp:\'\"\r\n                    + timeStamp + \"\',corpId:\'\" + Env.CORP_ID + \"\',agentId:\'\" + Env.AGENTID + \"\'}\";\r\n            System.out.println(configValue);\r\n\r\n        return configValue;  \r\n    }  \r\n    \r\n}

4.用户业务类——UserService.java

package com.ray.dingtalk.service.contact;\r\n\r\nimport com.alibaba.fastjson.JSON;\r\nimport com.alibaba.fastjson.JSONObject;\r\nimport com.ray.dingtalk.model.contact.User;\r\nimport com.ray.dingtalk.util.HttpHelper;\r\n\r\n/**@desc  : \r\n * \r\n * @author: shirayner\r\n * @date  : 2017年9月28日 上午9:53:51\r\n */\r\npublic class UserService {\r\n\r\n    private static final String CREATE_USER_URL=\"https://oapi.dingtalk.com/user/create?access_token=ACCESSTOKEN\";\r\n    private static final String GET_USER_URL=\"https://oapi.dingtalk.com/user/get?access_token=ACCESSTOKEN&userid=USERID\";\r\n    private static final String GET_DEPARTMENTUSER_URL=\"https://oapi.dingtalk.com/user/simplelist?access_token=ACCESSTOKEN&department_id=DEPARTMENTID\";\r\n    private static final String GET_DEPARTMENTUSERDETAIL_URL=\"https://oapi.dingtalk.com/user/list?access_token=ACCESSTOKEN&department_id=DEPARTMENTID\";\r\n    private static final String GET_USERINFO_BYCODE_URL=\"https://oapi.dingtalk.com/user/getuserinfo?access_token=ACCESSTOKEN&code=CODE\";\r\n\r\n    /**1.创建用户\r\n     * @desc :\r\n     *  \r\n     * @param accessToken\r\n     * @param user\r\n     * @return\r\n     * @throws Exception String\r\n     */\r\n    public String createUser(String accessToken,User user) throws Exception {\r\n        //1.准备POST请求参数\r\n        Object data=JSON.toJSON(user);\r\n        System.out.println(data);\r\n\r\n        //2.获取请求url\r\n        String url=CREATE_USER_URL.replace(\"ACCESSTOKEN\", accessToken);\r\n\r\n        //3.发起POST请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpPost(url, data);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n\r\n        //4.解析结果,获取UserId\r\n        String userId=\"\";\r\n        if (null != jsonObject) {  \r\n            userId=jsonObject.getString(\"userid\");\r\n            //5.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n\r\n        return userId;\r\n    }\r\n\r\n\r\n    /** 2.根据userid获取成员详情\r\n     * @desc :获取成员详情\r\n     *   参考文档: https://open-doc.dingtalk.com/docs/doc.htm?spm=0.0.0.0.jjSfQQ&treeId=371&articleId=106816&docType=1#s0\r\n     * @param accessToken\r\n     * @param userId void\r\n     * @throws Exception \r\n     */\r\n    public JSONObject getUser(String accessToken, String userId) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_USER_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"USERID\", userId);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n            //4.请求成功,则返回jsonObject\r\n            if (0==jsonObject.getInteger(\"errcode\")) {\r\n                return jsonObject;\r\n            }\r\n            //5.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n\r\n        return null;\r\n    }\r\n\r\n    /** 3.获取部门成员\r\n     * @desc :\r\n     *  \r\n     * @param accessToken\r\n     * @param departmentId\r\n     * @throws Exception void\r\n     */\r\n    public void getDepartmentUser(String accessToken, String departmentId) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_DEPARTMENTUSER_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"DEPARTMENTID\", departmentId);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n\r\n            //4.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n    }\r\n\r\n    /** 4.获取部门成员(详情)\r\n     * @desc :\r\n     *  \r\n     * @param accessToken\r\n     * @param departmentId\r\n     * @throws Exception void\r\n     */\r\n    public void getDepartmentUserDetail(String accessToken, String departmentId) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_DEPARTMENTUSERDETAIL_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"DEPARTMENTID\", departmentId);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n\r\n            //4.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n    }\r\n\r\n    /** 5.根据免登授权码Code查询免登用户userId\r\n     * @desc :钉钉服务器返回的用户信息为:\r\n     * userid    员工在企业内的UserID\r\n     * deviceId    手机设备号,由钉钉在安装时随机产生\r\n     * is_sys    是否是管理员\r\n     * sys_level    级别,0:非管理员 1:超级管理员(主管理员) 2:普通管理员(子管理员) 100:老板\r\n     *  \r\n     * @param accessToken\r\n     * @param code\r\n     * @throws Exception void\r\n     */\r\n    public JSONObject getUserInfo(String accessToken,String code) throws Exception {\r\n\r\n        //1.获取请求url\r\n        String url=GET_USERINFO_BYCODE_URL.replace(\"ACCESSTOKEN\", accessToken).replace(\"CODE\", code);\r\n\r\n        //2.发起GET请求,获取返回结果\r\n        JSONObject jsonObject=HttpHelper.httpGet(url);\r\n        System.out.println(\"jsonObject:\"+jsonObject.toString());\r\n\r\n        //3.解析结果,获取User\r\n        if (null != jsonObject) {  \r\n            //4.请求成功,则返回jsonObject\r\n            if (0==jsonObject.getInteger(\"errcode\")) {\r\n                return jsonObject;\r\n            }\r\n            //5.错误消息处理\r\n            if (0 != jsonObject.getInteger(\"errcode\")) {  \r\n                int errCode = jsonObject.getInteger(\"errcode\");\r\n                String errMsg = jsonObject.getString(\"errmsg\");\r\n                throw new Exception(\"error code:\"+errCode+\", error message:\"+errMsg); \r\n            }  \r\n        }   \r\n\r\n        return null;\r\n    }\r\n\r\n}

5.Servlet——UserInfoServlet

(1)UserInfoServlet.java

package com.ray.dingtalk.servlet;\r\n\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\n\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport com.alibaba.fastjson.JSON;\r\nimport com.alibaba.fastjson.JSONObject;\r\nimport com.ray.dingtalk.auth.AuthHelper;\r\nimport com.ray.dingtalk.config.Env;\r\nimport com.ray.dingtalk.service.contact.UserService;\r\n\r\n/**身份认证Servlet:免登\r\n * \r\n * \r\n * Servlet implementation class AuthServlet\r\n */\r\n@WebServlet(\"/UserInfoServlet\")\r\npublic class UserInfoServlet extends HttpServlet {\r\n    private static final long serialVersionUID = 1L;\r\n       \r\n  \r\n    public UserInfoServlet() {\r\n        super();\r\n        // TODO Auto-generated constructor stub\r\n    }\r\n\r\n    \r\n    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n        // TODO Auto-generated method stub\r\n        response.getWriter().append(\"Served at: \").append(request.getContextPath());\r\n    }\r\n\r\n    \r\n    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n        //1.将请求、响应的编码均设置为UTF-8(防止中文乱码)  \r\n        request.setCharacterEncoding(\"UTF-8\");  \r\n        response.setCharacterEncoding(\"UTF-8\"); \r\n        \r\n        //1.获取code\r\n        String code = request.getParameter(\"code\");\r\n        System.out.println(\"code:\"+code);\r\n        \r\n        Object result=null;\r\n        try {\r\n            //2.通过CODE换取身份userid\r\n            String accessToken = AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);\r\n            UserService us = new UserService();\r\n            String userId=us.getUserInfo(accessToken, code).getString(\"userid\");\r\n        \r\n            //3.通过userid换取用户信息\r\n            JSONObject jsonObject=us.getUser(accessToken, userId);\r\n            result=JSON.toJSON(jsonObject);\r\n            \r\n        } catch (Exception e) {\r\n            // TODO Auto-generated catch block\r\n            e.printStackTrace();\r\n        }\r\n        \r\n        \r\n        PrintWriter out = response.getWriter(); \r\n        out.print(result);  \r\n        out.close();  \r\n        out = null;  \r\n    }\r\n\r\n}

(2)web.xml

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:web=\"http://java.sun.com/xml/ns/javaee\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" version=\"2.5\">\r\n  <welcome-file-list>\r\n    <welcome-file>index.jsp</welcome-file>\r\n  </welcome-file-list>\r\n  \r\n  <servlet>\r\n    <servlet-name>userInfoServlet</servlet-name>\r\n    <servlet-class>  \r\n            com.ray.dingtalk.servlet.UserInfoServlet\r\n    </servlet-class>\r\n  </servlet>\r\n  <servlet-mapping>\r\n    <servlet-name>userInfoServlet</servlet-name>\r\n    <url-pattern>/userInfoServlet</url-pattern>\r\n  </servlet-mapping>\r\n</web-app>

6.前端代码

(1)IDAuthentication.jsp

<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\"\r\n    pageEncoding=\"UTF-8\"%>\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n<title>身份认证</title>\r\n<script src=\"js/jquery-3.2.1.min.js\"></script>\r\n<script type=\"text/javascript\" src=\"http://g.alicdn.com/dingding/open-develop/1.6.9/dingtalk.js\"></script>\r\n\r\n<script type=\"text/javascript\">\r\n    var _config =<%=com.ray.dingtalk.auth.AuthHelper.getConfig(request)%>;\r\n</script>\r\n<script type=\"text/javascript\" src=\"js/auth.js\"></script>\r\n\r\n\r\n</head>\r\n<body>\r\n\r\n\r\n    <div align=\"center\">\r\n        <img id=\"userImg\" alt=\"头像\" src=\"\">\r\n    </div>\r\n\r\n    <div align=\"center\">\r\n        <span>UserName:</span>\r\n        <div id=\"userName\" style=\"display: inline-block\"></div>\r\n    </div>\r\n\r\n    <div align=\"center\">\r\n        <span>UserId:</span>\r\n        <div id=\"userId\" style=\"display: inline-block\"></div>\r\n    </div>\r\n\r\n\r\n    <div align=\"center\">\r\n        <span class=\"desc\">是否验证成功</span>\r\n        <button class=\"btn btn_primary\" id=\"yanzheng\">ceshi</button>\r\n    </div>\r\n    <div align=\"center\">\r\n        <span class=\"desc\">测试按钮</span>\r\n        <button class=\"btn btn_primary\" id=\"ceshi\">ceshi</button>\r\n    </div>\r\n</body>\r\n</html>

(2)auth.js

dd.config({\r\n    agentId : _config.agentId,\r\n    corpId : _config.corpId,\r\n    timeStamp : _config.timeStamp,\r\n    nonceStr : _config.nonceStr,\r\n    signature : _config.signature,\r\n    jsApiList : [                           //需要调用的借口列表 \r\n        \'runtime.info\',\r\n        \'biz.contact.choose\',            //选择用户接口  \r\n        \'device.notification.confirm\', \r\n        \'device.notification.alert\',   //confirm,alert,prompt都是弹出小窗口的接口     \r\n        \'device.notification.prompt\', \r\n        \'biz.ding.post\',\r\n        \'biz.util.openLink\' ]\r\n});\r\n\r\ndd.ready(function() {  \r\n\r\n    document.getElementById(\"yanzheng\").innerHTML = \"验证成功\";  \r\n\r\n    document.querySelector(\'#ceshi\').onclick = function () {\r\n        alert(\"ceshiaaa\");\r\n    };\r\n\r\n    /* 1.获取容器信息\r\n     *获取容器信息,返回值为ability:版本号,也就是返回容器版本 \r\n     *用来表示这个版本的jsapi的能力,来决定是否使用jsapi \r\n     */  \r\n    dd.runtime.info({  \r\n        onSuccess : function(info) {  \r\n            logger.e(\'runtime info: \' + JSON.stringify(info));  \r\n        },  \r\n        onFail : function(err) {  \r\n            logger.e(\'fail: \' + JSON.stringify(err));  \r\n        }  \r\n    });      \r\n\r\n\r\n\r\n    /**获取免登授权码 CODE\r\n     * \r\n     */\r\n    dd.runtime.permission.requestAuthCode({  \r\n        corpId : _config.corpId,  \r\n        onSuccess : function(info) {                                                   //成功获得code值,code值在info中  \r\n            alert(\'authcode: \' + info.code);  \r\n            /* \r\n             *$.ajax的是用来使得当前js页面和后台服务器交互的方法 \r\n             *参数url:是需要交互的后台服务器处理代码,userInfoServlet\r\n             *参数type:指定和后台交互的方法,因为后台servlet代码中处理Get和post的doGet和doPost \r\n             *data:负责传递请求参数\r\n             *其中success方法和error方法是回调函数,分别表示成功交互后和交互失败情况下处理的方法 \r\n             */  \r\n            $.ajax({  \r\n                type : \"POST\",\r\n                url : \"http://p65s3p.natappfree.cc/DingTalk_Demo/userInfoServlet\",\r\n                data : {\r\n                    code : info.code\r\n                },\r\n                success : function(data, status, xhr) {\r\n                    alert(data);\r\n                    //接收后端发送过来的用户信息\r\n                    var userInfo = JSON.parse(data);\r\n\r\n                    //收到用户信息后所做的处理\r\n                    document.getElementById(\"userName\").innerHTML = userInfo.name;\r\n                    document.getElementById(\"userId\").innerHTML = userInfo.userid;\r\n\r\n                    // 图片\r\n                    if(info.avatar.length != 0){\r\n                        var img = document.getElementById(\"userImg\");\r\n                        img.src = info.avatar;\r\n                        img.height = \'200\';\r\n                        img.width = \'200\';\r\n                    }\r\n\r\n                },\r\n                error : function(xhr, errorType, error) {  \r\n                    logger.e(\"yinyien:\" + _config.corpId);  \r\n                    alert(errorType + \', \' + error);  \r\n                }  \r\n            });  \r\n\r\n        },  \r\n        onFail : function(err) {                                                       //获得code值失败  \r\n            alert(\'fail: \' + JSON.stringify(err));  \r\n        }  \r\n    });  \r\n\r\n\r\n\r\n\r\n\r\n});  \r\n\r\n\r\n//在dd.config函数验证失败时执行 dd.error\r\ndd.error(function(err) {                                             //验证失败  \r\n    alert(\"进入到error中\");  \r\n    document.getElementById(\"userName\").innerHTML = \"验证出错\";  \r\n    alert(\'dd error: \' + JSON.stringify(err));  \r\n});


权利声明:本站所有商品信息、客户评价等信息是初心商城重要的数据资源,未经许可,禁止非法转载使用。 注:本站商品信息均来自初心商城,其真实性、准确性和合法性由初心商城负责。

                  初心源说明:初心商城主要为程序员提供开发基础的代码源以及成熟项目,网站中所有的商品有提供收费版本的, 也有提供免费版本的,按照大家各自不同的需求进行购买。实实在在的让程序员只用专注于自己的业务实现你的小梦想, 如果您对我们的成果表示认同并且觉得对你有所帮助我们愿意接受来自各方面的支持^_^。

                  支持:用手机扫描二维码支付

                  支付宝支持我们 微信支持我们

                  您的支持将被用于:
                  1、持续深入的上传更多更好的源代码
                  2、建立更加完善的技术社区
                  3、完善现在系统出现各种问题
                  4、购买域名和租赁服务器

                  1、交易规则

                  2、发货方式

                  1、自动:在上方保障服务中标有自动发货的商品,拍下后,将会自动收到来自卖家的商品获取(下载)链接

                  2、手动:在上方保障服务中标有手动发货的商品,拍下后,卖家会收到邮件,也可通过QQ或订单中的电话联系对方。

                  3、退款说明

                  1、描述:源码描述(含标题)与实际源码不一致的(例:描述PHP实际为ASP、描述的功能实际缺少、版本不符等)

                  2、演示:有演示站时,与实际源码小于95%一致的(但描述中有"不保证完全一样、有变化的可能性"类似显著声明的除外)

                  3、发货:手动发货源码,在卖家未发货前,已申请退款的

                  4、服务:卖家不提供安装服务或需额外收费的(但描述中有显著声明的除外)

                  5、其它:如质量方面的硬性常规问题等

                  备注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。交易中的商品,卖家无法对描述进行修改!

                  4、注意事项

                  1、客户买完之后未确认收货,将不会收到下载地址和下载码,确认收货之后才能收到下载地址和下载码。

                  2、在未拍下前,双方在QQ上所商定的内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准);

                  3、在商品同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外);

                  4、在没有"无任何正当退款依据"的前提下,写有"一旦售出,概不支持退款"等类似的声明,视为无效声明;

                  5、虽然交易产生纠纷的几率很小,但请尽量保留如聊天记录这样的重要信息,以防产生纠纷时出现问题不明确的情况。

                  5、交易声明

                  1、本站作为直卖平台,依据交易合同(商品描述、交易前商定的内容)来保障交易的安全及买卖双方的权益;

                  2、非平台线上交易的商品,出现任何后果均与本站无关;无论卖家以何理由要求线下交易的,请联系管理举报。

                  初心Logo

                  初心商城| 初心系列| 初心博客| 初心公告| 系统反馈

                  © 2016-2018 山西米立信息技术有限公司 保留所有权利 京ICP备16055626号
                  违法和不良信息举报电话:186-2950-9347,本网站所列数据,除特殊说明,所有数据均出自我工作室
                  本网站兼容所有主流浏览器,不支持手机自适应