tijian_tieying/web/dccdc/Common/Deepleo.Weixin.SDK/JSSDK/JSAPI.cs
2025-02-20 12:14:39 +08:00

57 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Codeplex.Data;
using Deepleo.Weixin.SDK.Helpers;
namespace Deepleo.Weixin.SDK.JSSDK
{
/// <summary>
/// http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
/// 微信JS-SDK使用权限签名算法
/// </summary>
public class JSAPI
{
/// <summary>
/// 获取jsapi_ticket
/// jsapi_ticket是公众号用于调用微信JS接口的临时票据。
/// 正常情况下jsapi_ticket的有效期为7200秒通过access_token来获取。
/// 由于获取jsapi_ticket的api调用次数非常有限频繁刷新jsapi_ticket会导致api调用受限影响自身业务开发者必须在自己的服务全局缓存jsapi_ticket 。
/// </summary>
/// <param name="access_token">BasicAPI获取的access_token,也可以通过TokenHelper获取</param>
/// <returns></returns>
public static dynamic GetTickect(string access_token)
{
var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);
var client = new HttpClient();
var result = client.GetAsync(url).Result;
if (!result.IsSuccessStatusCode) return string.Empty;
var jsTicket = DynamicJson.Parse(result.Content.ReadAsStringAsync().Result);
return jsTicket;
}
/// <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 Util.Sha1(string1);
}
}
}