钉钉的开发接口中,目前没有在服务端直接获取chatid的接口,但提供了客户端获取chatid的接口。所以有需要获取该接口的,我们可以暂时使用客户端的方式获取。为了能够使用jsapi,我们还需要利用jsticket来得到签名包,具体代码如下。
#region FetchSignPackage Function /// <summary> /// 获取签名包 /// </summary> /// <param name="url"></param> /// <returns></returns> public static SignPackage FetchSignPackage(String url, JSTicket jsticket) { int unixTimestamp = SignPackageHelper.ConvertToUnixTimeStamp(DateTime.Now); string timestamp = Convert.ToString(unixTimestamp); string nonceStr = SignPackageHelper.CreateNonceStr(); if (jsticket == null) { return null; } // 这里参数的顺序要按照 key 值 ASCII 码升序排序 string rawstring = $"{Keys.jsapi_ticket}=" + jsticket.ticket + $"&{Keys.noncestr}=" + nonceStr + $"&{Keys.timestamp}=" + timestamp + $"&{Keys.url}=" + url; string signature = SignPackageHelper.Sha1Hex(rawstring).ToLower(); var signPackage = new SignPackage() { agentId = ConfigHelper.FetchAgentID(),//取配置文件中的agentId,可依据实际配置而作调整 corpId = ConfigHelper.FetchCorpID(),//取配置文件中的coprId,可依据实际配置而作调整 timeStamp = timestamp, nonceStr = nonceStr, signature = signature, url = url, rawstring = rawstring, jsticket = jsticket.ticket }; return signPackage; } /// <summary> /// 获取签名包 /// </summary> /// <param name="url"></param> /// <returns></returns> public static SignPackage FetchSignPackage(String url) { int unixTimestamp = SignPackageHelper.ConvertToUnixTimeStamp(DateTime.Now); string timestamp = Convert.ToString(unixTimestamp); string nonceStr = SignPackageHelper.CreateNonceStr(); var jsticket = FetchJSTicket(); var signPackage = FetchSignPackage(url, jsticket); return signPackage; } #endregion
/// <summary> /// 签名包 /// </summary> public class SignPackage { public String agentId { get; set; } public String corpId { get; set; } public String timeStamp { get; set; } public String nonceStr { get; set; } public String signature { get; set; } public String url { get; set; } public String rawstring { get; set; } public string jsticket { get; set; } }
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; namespace DDSDK { public class SignPackageHelper { #region Sha1Hex public static 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; } #endregion #region CreateNonceStr /// <summary> /// 创建随机字符串 /// </summary> /// <returns></returns> public static string CreateNonceStr() { int length = 16; string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string str = ""; Random rad = new Random(); for (int i = 0; i < length; i++) { str += chars.Substring(rad.Next(0, chars.Length - 1), 1); } return str; } #endregion #region ConvertToUnixTimeStamp /// <summary> /// 将DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="time">时间</param> /// <returns>double</returns> public static int ConvertToUnixTimeStamp(DateTime time) { int intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); intResult = Convert.ToInt32((time - startTime).TotalSeconds); return intResult; } #endregion } }在界面上我们可以使用json的方式来获取数据,为了方便后续的使用,这里定义了一个global.js。
/// <reference path="zepto.min.js" /> var global = { corpId: '', configJsSdk: function (jsApiList, fnReady, fnError) { /// <summary>配置微信的JSSDK</summary> /// <param name="jsApiList" type="function">要请求的api列表,以['biz.chat.chooseConversationByCorpId','biz.chat.toConversation']的形式</param> /// <param name="fnReady" type="function">dd.ready调用的函数</param> /// <param name="fnError" type="function">dd.error调用的函数</param> var url = window.location.href; $.getJSON("/api/Auth/GetSignPackage", "url=" + window.location.href, function (response, textStatus, jqXHR) { try { var signPackage = response; global.corpId = signPackage.corpId; dd.config( { agentId: signPackage.agentId, corpId: signPackage.corpId, timeStamp: signPackage.timeStamp, nonceStr: signPackage.nonceStr, signature: signPackage.signature, jsApiList: jsApiList }); dd.ready(function () { if (fnReady != null) { fnReady(); } }); dd.error(function (err) { if (err == null) { alert('dd error: ' + JSON.stringify(err)); } else { fnError(); } }); } catch (e) { } }); } }其中"/api/Auth/GetSignPackage"是WebApi的方式,这将在后续中说到。这里需要引用zepto.min.js。
下面调用的结果
欢迎打描左侧二维码打赏。
转载请注明出处。