钉钉PC端开发

  • Post author:
  • Post category:其他


第一步:建立一个微应用

第二步:获取配置信息


        /// <summary>
        /// sha1加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string Sha1Hex(string value)
        {
            SHA1 algorithm = SHA1.Create();
            byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
            string sh1 = "";
            for (int i = 0; i < data.Length; i++)
            {
                sh1 += data[i].ToString("x2").ToUpperInvariant();
            }
            return sh1;
        }

        /// <summary>  
        /// 获取签名包  
        /// </summary>
        /// <param name="url">当前请求地址</param>
        /// <returns></returns>  
        public SignPackageEntity FetchSignPackage(string url)
        {
            var signPackage = new SignPackageEntity()
            {
                agentId = Config.GetValue("DDAgentId"),
                corpId = Config.GetValue("DDCorpId"),
                timeStamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString(),
                nonceStr = Guid.NewGuid().ToString(),
                url = url,
                jsticket = GetTicket()
            };
            if (signPackage.jsticket.IsEmpty())
                return new SignPackageEntity();
            // 这里参数的顺序要按照 key 值 ASCII 码升序排序   
            signPackage.rawstring = "jsapi_ticket=" + signPackage.jsticket + "&noncestr=" + signPackage.nonceStr + "&timestamp=" + signPackage.timeStamp + "&url=" + signPackage.url;
            signPackage.signature = Sha1Hex(signPackage.rawstring).ToLower();
            return signPackage;
        }
        /// <summary>
        /// 获取ticket
        /// </summary>
        /// <returns></returns>
        public string GetTicket()
        {
            DingDingRequestBLL ddRequestBLL = new DingDingRequestBLL();
            string jsonStr = HttpMethods.HttpGet("https://oapi.dingtalk.com/get_jsapi_ticket?access_token=" + ddRequestBLL.Token);
            var result = jsonStr.ToObject<Dictionary<string, string>>();
            if (result["errcode"] != "0") return string.Empty;
            return result["ticket"];
        }

第三部:编码测试

var _config = {
            agentId: DDConfig.agentId,
            corpId: DDConfig.corpId,
            timeStamp: DDConfig.timeStamp,
            nonceStr: DDConfig.nonceStr,
            signature: DDConfig.signature,
            jsApiList: [
                'runtime.info',
                'biz.contact.choose',
                'device.notification.confirm',
                'device.notification.alert',
                'device.notification.prompt',
                'biz.ding.post',
                'biz.util.openLink'
            ]
        };
        dd.config(_config);
        dd.ready(function () {
            dd.runtime.info({
                onSuccess: function (info) {
                    logger.e('runtime info: ' + JSON.stringify(info));
                },
                onFail: function (err) {
                    logger.e('fail: ' + JSON.stringify(err));
                }
            });
            dd.runtime.permission.requestAuthCode({
                corpId: _config.corpId,
                onSuccess: function (result) {
                    alert(result);
                    $.ajax({
                        url: "",
                        type: "get",
                        data: { code: result.code },
                        success: function (data) {
                            if (!data.type != 1) {
                                alert(data.message);
                            }
                        },
                        error: function (msg) { alert(msg); }
                    })
                },
                onFail: function (err) {
                    alert("dd onFail result.code:" + JSON.stringify(err))
                }
            });
        });



版权声明:本文为qq_32131165原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。