1.應用場景:
企業微信中開發一個公共入口,用來外部各個子系統中的公用功能的整合;
2.業務分析:
集團公司為了適應市場變化,拆分為四個子公司;現有集團資產管理系統,上層的決策是把系統代碼直接復制為四套,分別部署在四個服務器中獨立域名,獨立數據庫(已實施);
為我們后面維護造成了很大的問題;五月份通知全體公司要進行資產年度盤點,指定必須用移動端進行盤點;
3.架構方案:
由於公司已有自己的企業微信平台,所以采用在企業微信工作台,新建一個盤點應用入口;開發一個H5站點作為UI(可以使用掃一掃進行掃碼錄入),在四套系統里開發相同的盤點接口供H5調用;
4.開發:
主要分為 1.前端H5 界面開發; 2 .調用企業微信API; 3.調用四個系統的接口;這里主要講下企業微信API的調用
首先需要管理員身份進入公司的企業微信后台 https://work.weixin.qq.com/,創建好應用,並配置我們應用的 回調站點,必須是有效域名,這里需要注意的是需要把獲取的 密匙txt文件WW_verify_wApslD6X0ysCCA8G.txt 放入服務器根目錄下進行驗證服務器
這里只說一下后台獲取企業微信的配置 后台使用的是MVC Controller

1 #region Get Post Http 2 /// <summary> 3 /// 創建GET方式的HTTP請求 4 /// </summary> 5 /// <param name="url">請求的URL</param> 6 /// <param name="timeout">請求的超時時間</param> 7 /// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param> 8 /// <param name="cookies">隨同HTTP請求發送的Cookie信息,如果不需要身份驗證可以為空</param> 9 /// <returns></returns> 10 public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies, string headerValue = "") 11 { 12 if (string.IsNullOrEmpty(url)) 13 { 14 throw new ArgumentNullException("url"); 15 } 16 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 17 request.Method = "GET"; 18 //request.UserAgent = DefaultUserAgent; 19 request.KeepAlive = false; 20 if (!string.IsNullOrEmpty(headerValue)) 21 { 22 request.Headers.Add("Authorization", headerValue); 23 } 24 if (!string.IsNullOrEmpty(userAgent)) 25 { 26 request.UserAgent = userAgent; 27 } 28 if (timeout.HasValue) 29 { 30 request.Timeout = timeout.Value; 31 } 32 if (cookies != null) 33 { 34 request.CookieContainer = new CookieContainer(); 35 request.CookieContainer.Add(cookies); 36 } 37 return request.GetResponse() as HttpWebResponse; 38 } 39 40 41 /// <summary> 42 /// 創建POST方式的HTTP請求 43 /// </summary> 44 public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies, string headerValue = "") 45 { 46 HttpWebRequest request = null; 47 //如果是發送HTTPS請求 48 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) 49 { 50 //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 51 request = WebRequest.Create(url) as HttpWebRequest; 52 //request.ProtocolVersion = HttpVersion.Version10; 53 } 54 else 55 { 56 request = WebRequest.Create(url) as HttpWebRequest; 57 } 58 request.Method = "POST"; 59 request.ContentType = "application/x-www-form-urlencoded"; 60 if (!string.IsNullOrEmpty(headerValue)) 61 { 62 request.Headers.Add("Authorization", headerValue); 63 } 64 //設置代理UserAgent和超時 65 //request.UserAgent = userAgent; 66 request.Timeout = timeout; 67 request.KeepAlive = false; 68 if (cookies != null) 69 { 70 request.CookieContainer = new CookieContainer(); 71 request.CookieContainer.Add(cookies); 72 } 73 //發送POST數據 74 if (!(parameters == null || parameters.Count == 0)) 75 { 76 StringBuilder buffer = new StringBuilder(); 77 int i = 0; 78 foreach (string key in parameters.Keys) 79 { 80 if (i > 0) 81 { 82 buffer.AppendFormat("&{0}={1}", key, parameters[key]); 83 } 84 else 85 { 86 buffer.AppendFormat("{0}={1}", key, parameters[key]); 87 i++; 88 } 89 } 90 byte[] data = Encoding.ASCII.GetBytes(buffer.ToString()); 91 using (Stream stream = request.GetRequestStream()) 92 { 93 stream.Write(data, 0, data.Length); 94 } 95 } 96 string[] values = request.Headers.GetValues("Content-Type"); 97 return request.GetResponse() as HttpWebResponse; 98 } 99 100 /// <summary> 101 /// 獲取請求的數據 102 /// </summary> 103 public static string GetResponseString(HttpWebResponse webresponse) 104 { 105 using (Stream s = webresponse.GetResponseStream()) 106 { 107 StreamReader reader = new StreamReader(s, Encoding.UTF8); 108 return reader.ReadToEnd(); 109 110 } 111 } 112 113 114 #endregion
首先需要獲取access_token
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT
https://work.weixin.qq.com/api/doc#10013/第一步:創建企業應用並獲取secret

1 //緩存網關token 2 private string GetCacheGatewayToken() 3 { 4 if (HttpRuntime.Cache["GatewayToken"] == null) 5 { 6 var token = CommonTools.GetGrantToken(Constants.GrantServiceUrl); 7 HttpRuntime.Cache.Insert("GatewayToken", token, null, System.DateTime.Now.AddHours(1), TimeSpan.Zero); 8 } 9 return HttpRuntime.Cache["GatewayToken"].ToString(); 10 } 11 12 13 //public static string GetGrantToken(string url) 14 //{ 15 // var result = string.Empty; 16 // IDictionary<string, string> postData = new Dictionary<string, string>(); 17 // postData.Add("client_id", Constants.WX_ClientID); 18 // postData.Add("client_secret", Constants.WX_ClientSecret); 19 // postData.Add("grant_type", Constants.WX_GrantType); 20 // postData.Add("scope", Constants.WX_GrantScope); 21 // //var param = string.Format("{{\"client_id\":{0},\"client_secret\":\"{1}\",\"grant_type\":\"{2}\",\"scope\":\"{3}\"}}", Constants.WX_ClientID, Constants.WX_ClientSecret, Constants.WX_GrantType, Constants.WX_GrantScope); 22 // var strResponseInfo = RequestTool.CreatePostHttpResponse(url, postData, 60000, null, null); 23 // if (strResponseInfo != null) 24 // result = RequestTool.GetResponseString(strResponseInfo); 25 // return result; 26 //}

private static string CreateRandCode(int codeLen) { string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z"; if (codeLen == 0) { codeLen = 16; } string[] arr = codeSerial.Split(','); string code = ""; int randValue = -1; Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < codeLen; i++) { randValue = rand.Next(0, arr.Length - 1); code += arr[randValue]; } return code; }

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區 long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒數 System.Console.WriteLine(timeStamp);
前端 獲取到wx.config后就可以引入 js了
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
這時頁面就可以調用企業微信api了;
后續應用中還設計到了一個網頁授權;
https://work.weixin.qq.com/api/doc#10028
這里我們使用了 自己構建一個空的 form表單 獲取到跳轉的url后 提交,跳轉的第二頁,再從url中獲取code ,通過code和 appid 獲取登錄企業微信的用戶ID