asp.net 통합 위챗, 스파이크
23657 단어 B/S 애플리케이션비즈니스 기반 플랫폼
///
/// app( , )
///
public interface IApp
{
int PID4CropID { get; }
int PID4AppSecret { get; }
int PID4AgentID { get; }
string URL4AccessToken { get; }
string URL4Ticket { get; }
string URL4GetUser { get; }
string URL4SendMsg { get; }
string UserIDKey { get; }
string UserIDFieldName { get; }
string GetMediaDownloadUrl(string mediaId);
}
2 微信实现类
///
///
///
public class WxApp : IApp
{
public int PID4AgentID
{
get
{
return 202;
}
}
public int PID4AppSecret
{
get
{
return 201;
}
}
public int PID4CropID
{
get
{
return 200;
}
}
public string URL4AccessToken
{
get
{
return "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
}
}
public string URL4GetUser
{
get
{
return "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}&agentid={2}";
}
}
public string URL4SendMsg
{
get
{
return "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}";
}
}
public string URL4Ticket
{
get
{
return "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={0}";
}
}
public string UserIDFieldName
{
get
{
return "OpenID";
}
}
public string UserIDKey
{
get
{
return "UserId";
}
}
public string GetMediaDownloadUrl(string mediaId)
{
return string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", AppServer.GetAccessToken(this), mediaId);
}
}
3 钉钉实现类
///
///
///
public class DDApp : IApp
{
public int PID4AgentID
{
get
{
return 302;
}
}
public int PID4AppSecret
{
get
{
return 301;
}
}
public int PID4CropID
{
get
{
return 300;
}
}
public string URL4AccessToken
{
get
{
return "https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}";
}
}
public string URL4GetUser
{
get
{
return "https://oapi.dingtalk.com/user/getuserinfo?access_token={0}&code={1}";
}
}
public string URL4SendMsg
{
get
{
return "https://oapi.dingtalk.com/message/send?access_token={0}";
}
}
public string URL4Ticket
{
get
{
return "https://oapi.dingtalk.com/get_jsapi_ticket?access_token={0}";
}
}
public string UserIDFieldName
{
get
{
return "DDID";
}
}
public string UserIDKey
{
get
{
return "userid";
}
}
public string GetMediaDownloadUrl(string mediaId)
{
return mediaId;
}
}
4 获取tickit,token,Signature,实现自动登陆,从微信或钉钉服务器上下载手机上传的图片等
public class AppServer
{
internal static string GetSignature(long timeStamp, string url, string cropId, string appSecret, IApp app)
{
var token = GetTicket(cropId, appSecret,app);
var str = string.Format("jsapi_ticket={0}&noncestr=CwgHd0EAGathMlxt×tamp={1}&url={2}", token, timeStamp, url.Replace(":80/", "/"));
return SHA1Encrypt(str).ToLower();
}
internal static string SHA1Encrypt(string str)
{
byte[] StrRes = Encoding.Default.GetBytes(str);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in StrRes)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}
internal static string GetAccessToken(IApp app,string cropId = null, string appSecret = null)
{
if (cropId == null) cropId = ProfileMan.GetProfileValue(app.PID4CropID);
if (appSecret == null) appSecret = ProfileMan.GetProfileValue(app.PID4AppSecret);
return GetToken(false, cropId, appSecret,app);
}
internal static string GetTicket(string cropId, string appSecret, IApp app)
{
return GetToken(true, cropId, appSecret,app);
}
private static string GetToken(bool isGetTicket, string cropId, string appSecret, IApp app)
{
var prefix = SessionHelper.CloudMode ? SessionHelper.CurrentDBName : string.Empty;
var appType = app is DDApp ? 2 : 1;
var key = string.Format("{0}_{1}_{2}_{3}_{4}", prefix, cropId, appSecret, isGetTicket ? 1 : 0, appType);
if (HttpRuntime.Cache.Get(key) == null)
{
CacheAccessToken(appType, isGetTicket, prefix, cropId, appSecret);
}
return (string)HttpRuntime.Cache.Get(key);
}
private static void CacheAccessToken(int appType, bool isGetTicket, string prefix, string cropId, string appSecret)
{
var key = string.Format("{0}_{1}_{2}_{3}_{4}", prefix, cropId, appSecret, isGetTicket ? 1 : 0, appType);
IApp app = AppIntegerUtil.CreateApp(appType);
int expiresMin;
var url = !isGetTicket ? string.Format(app.URL4AccessToken, cropId, appSecret) : string.Format(app.URL4Ticket, GetAccessToken(app, cropId, appSecret));
var jsonKey = !isGetTicket ? "access_token" : "ticket";
HttpRuntime.Cache.Add(key, GetDataFromServer(url, jsonKey, out expiresMin), null, DateTime.Now.AddSeconds(expiresMin), Cache.NoSlidingExpiration, CacheItemPriority.Normal, CacheItemRemovedCallback);
}
internal static string GetUserID(Entities objContext, string code, IApp app)
{
int expiresMin;
return GetDataFromServer(string.Format(app.URL4GetUser, GetAccessToken(app, ProfileMan.GetProfileValue(app.PID4CropID, objContext), ProfileMan.GetProfileValue(app.PID4AppSecret, objContext)), code, ProfileMan.GetProfileValue(app.PID4AgentID, objContext)), app.UserIDKey, out expiresMin, false);
}
private static void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
var keys = key.Split('_');
if (keys.Length == 5) CacheAccessToken(Convert.ToInt32(keys[4]), keys[3] == "1", keys[0], keys[1], keys[2]);
}
private static string GetDataFromServer(string url, string jsonKey, out int expiresMin, bool hasExpiresMin=true)
{
expiresMin = 7200;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
var jo = JObject.Parse(responseContent);
JToken v;
if (jo.TryGetValue(jsonKey, out v))
{
expiresMin = hasExpiresMin ? Convert.ToInt32(jo["expires_in"]) : 0;
return v.ToString();
}
return null;
}
internal static bool DownloadMediaFromAppServer(IApp app, string mediaId, string fileName)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(app.GetMediaDownloadUrl(mediaId));
req.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
WebClient webClient = new WebClient();
var success = true;
try
{
webClient.DownloadFile(myResponse.ResponseUri.ToString(), fileName);
}
catch(Exception err)
{
LogManager.GetLogger(typeof(AppServer)).Error(err);
success = false;
}
return success;
}
}
5js 핸드폰 단말기 호출 카메라나 갤러리 이미지 업로드 실현
if (appType == 'wx') {
wx.chooseImage({
count: 1, // 9
sizeType: ['original', 'compressed'], // ,
sourceType: ['album', 'camera'], // ,
success: function (res) {
img[0].src = res.localIds; // ID ,localId img src
wx.uploadImage({
localId: res.localIds.toString(), // ID, chooseImage
isShowProgressTips: 1, // 1,
success: function (res) {
img.attr('serverid', res.serverId); // ID
}
});
}
});
}
else if (appType == 'dd') {
dd.biz.util.uploadImage({
onSuccess: function (result) {
if (result.length > 0) {
img[0].src = result[0];
img.attr('serverid', result[0]);
}
},
onFail: function (result) {
alert(JSON.stringify(result));
}
});
}
이로써 위챗 기업 번호, 스파이크의 통합, 주요 기능: 자동 로그인, 휴대전화 사진 업로드, 사용자에게 메시지 발송을 실현했다.기타 액세스 사용자 목록, 부서 정보 등은 봉인할 수 있다.