企业WeiXinApi组件,仅适用于Web版本。
属性 |
属性说明 |
---|---|
AppID |
公众号的唯一标识。 |
CorpID |
企业CorpID |
方法 |
方法说明 |
---|---|
config |
注入权限验证配置。 |
agentConfig |
注入应用的权限,需要先config 。 |
checkJsApi |
是否支持指定JS接口 |
login |
微信登陆 |
invoke |
invoke调用形式的jsapi |
事件 |
事件说明 |
---|---|
Afterlogin |
登陆后发生 |
C#: //参数:state: 重定向后会带上state参数,企业可以填写a-z A-Z 0-9的参数值,长度不可超过128个字节 weiXinENTApi1.login("12");
C#: //corpid 企业ID,corpsecret 应用的凭证密钥 ENTTicket = GetENTTicket(GetAccessToken(corpid, corpsecret)); Client.Session["ENTTicket"] = ENTTicket;
C#: //corpid 企业ID,corpsecret 应用的凭证密钥 AgentTicket = GetAgentTicket(GetAccessToken(corpid, corpsecret)); Client.Session["AgentTicket"] = AgentTicket;
C#: if (Client.Session["ENTTicket"] != null) { string nonceStr = CreatenNonce_str(); long timestamp = CreatenTimestamp(); string url = Client.WebUrl; string signature = GetSignature(ENTTicket, nonceStr, timestamp, url, out string1); string[] jsApiList = { "chooseImage", "onMenuShareAppMessage", "playVoice", "stopWifi", "openUserProfile", "openBluetoothAdapter", "closeBluetoothAdapter","setClipboardData", "scanQRCode" }; this.weiXinENTApi1.config(timestamp, nonceStr, signature, jsApiList, (obj, argss) => { Toast("isError:" + argss.isError + "ERROR:" + argss.error + " ARGS:" + argss.args); }, true); } /// <summary> /// 创建随机字符串 /// </summary> /// <returns></returns> public static string CreatenNonce_str() { Random r = new Random(); var sb = new StringBuilder(); var length = strs.Length; for ( int i = 0; i < 15; i++) { sb.Append(strs[r.Next(length - 1)]); } return sb.ToString(); } /// <summary> /// 创建时间戳 /// </summary> /// <returns></returns> public static longCreatenTimestamp() { return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; } /// <summary> /// 签名算法 /// </summary> /// <param name="jsapi_ticket">jsapi_ticket</param> /// <param name="noncestr">随机字符串(必须与wx.config中的nonceStr相同)</param> /// <param name="timestamp">时间戳(必须与wx.config中的timestamp相同)</param> /// <param name="url">当前网页的URL,不包含#及其后面部分(必须是调用JS接口页面的完整URL)</param> /// <returns></returns> public static string GetSignature(string jsapi_ticket, string noncestr, long timestamp, string url, out string string1) { var string1Builder = new StringBuilder(); string1Builder.Append( "jsapi_ticket=").Append(jsapi_ticket).Append( "&") .Append( "noncestr=").Append(noncestr).Append( "&") .Append( "timestamp=").Append(timestamp).Append( "&") .Append( "url=").Append(url.IndexOf( "#") >= 0 ? url.Substring(0, url.IndexOf( "#")) : url); string1 = string1Builder.ToString(); return Sha1(string1); } /// <summary> /// Sha1 /// </summary> /// <param name="orgStr"></param> /// <param name="encode"></param> /// <returns></returns> public static string Sha1(string orgStr, string encode = "UTF-8") { var sha1 = new SHA1Managed(); var sha1bytes = System.Text.Encoding.GetEncoding(encode).GetBytes(orgStr); byte[] resultHash = sha1.ComputeHash(sha1bytes); string sha1String = BitConverter.ToString(resultHash).ToLower(); sha1String = sha1String.Replace("-", ""); return sha1String; }
C#: if (Client.Session["AgentTicket"] != null) { string nonceStr = CreatenNonce_str(); long timestamp = CreatenTimestamp(); string url = Client.WebUrl; string signature = GetSignature(AgentTicket, nonceStr, timestamp, url, out string1); this.weiXinENTApi1.agentConfig(agentid, timestamp, nonceStr, signature, (obj, argss) => { Toast("isError:" + argss.isError + "ERROR:" + argss.error + " ARGS:" + argss.args); }, true); }
C#: string[] checkJsApiList = { "chooseImage", "playVoice", "stopWifi", "setClipboardData", "scanQRCode"}; weiXinENTApi1.checkJsApi(checkJsApiList, (obj, args) => { Toast("ERROR:" + args.error); });
C#: //使用invoke方法调用 微信扫一扫 //参考 https://work.weixin.qq.com/api/doc/90000/90136/90492 string[] mscanType = new string[2]; mscanType[0] = "qrCode"; mscanType[1] = "barCode"; Dictionary <string, object> dict = new Dictionary<string, object>(); dict.Add("desc", "'scanQRCode desc'"); dict.Add("needResult", 1); dict.Add("scanType", mscanType); weiXinENTApi1.invoke("scanQRCode", dict, (obj, args) => { Dictionary< string, object> paramsDict = args.args as Dictionary< string, object>; Toast("ERROR:" + args.error +" " + (string)paramsDict["resultStr"]); });
C#: private void weiXinENTApi1_Afterlogin(object sender, WeiXinENTApiLoginEventArgs e) { Toast("触发事件"); var userInfo = e.GetUserInfo(corpsecret); if (userInfo.errcode == 0) { Toast("UserId;" + userInfo.UserId + " OpenId:" + userInfo.OpenId + " DeviceId:" + userInfo.DeviceId); } }