C# 微信共享收货地址 V1.6
//使用微信共享收货地址在跳转到当前页面的路径上必须要包含Code和state这两个获取用户信息的参数
//例如 <a href="ProductOrder.aspx?OID=<%=dr["ID"].ToString() %>&SID=<%=SID %>&code=<%=code%>&state=1&showwxpaytitle=1" //class="right" >安全支付</a>
<script type="text/javascript">
//前台微信共享收货地址
function editAddress() {
var URL = window.location.href; //当前页面的路径
$.ajax({
type: "POST",
url: "/***/handler/OrderWaitHandler.ashx",
data: "otype=Address&path=" + URL,
async: false,
success: function (msg) {
if (msg == "-99") {
ShowFW_Guanzhu("请先关注该公众号,再进行下单操作!");
} else if (msg == "-98") {
ShowFW("出现未知的错误,请联系管理员!");
} else {
var argument = msg.split(",");
WeixinJSBridge.invoke('editAddress', {
"appId": argument[0], //公众号AppID
"scope": "jsapi_address", //获得权限
"signType": "sha1", //加密方法
"addrSign": argument[1], //签名
"timeStamp": argument[2], //时间戳
"nonceStr": argument[3] //随机字符串
}, function (res) {
if (res.err_msg == "edit_address:ok") {
//收货信息赋值
document.getElementById("UserName").value = res.userName;
document.getElementById("telNumber").value = res.telNumber;
document.getElementById("firstName").value = res.proviceFirstStageName;
document.getElementById("secondName").value = res.addressCitySecondStageName;
document.getElementById("thirdName").value = res.addressCountiesThirdStageName;
document.getElementById("detalInfo").value = res.addressDetailInfo;
}
});
}
}
});
}
</script>
/// <summary>
/// 共享收货地址
/// </summary>
/// <param name="context"></param>
/// <param name="returnMsg"></param>
/// <returns></returns>
private bool EditAddress(HttpContext context, ref string returnMsg)
{
try
{
string AccessToken = "";//AccessToken
string URL = context.Request["path"].Trim().ToString() + "&SID=1&code=" + context.Session["code"].ToString() + "&state=1&showwxpaytitle=1"; //当前页面的URL
if (context.Session["UID"] == null || context.Session["UID"].ToString() == "0" || context.Session["UID"].ToString() == "")
{
returnMsg = "-99";//请先关注公众号,才能进行下订单操作
return false;
}
if (context.Session["accessToken"] != null || context.Session["accessToken"].ToString() != "")
{
AccessToken = context.Session["accessToken"].ToString(); //通过Oauth2获取的AccessToken
string Nonce = CreateRandomCode(15).ToLower(); //随机字符串
string time = ConvertDateTimeInt(DateTime.Now).ToString();//时间戳
string sha1 = "accesstoken=" + AccessToken +
"&appid=" + PayHandle.AppID.ToString() + //公众号APPID
"&noncestr=" + Nonce +
"×tamp=" + time +
"&url=" + URL;
string sha =Hash(sha1).ToLower(); //HASH加密
returnMsg = PayHandle.AppID.ToString() + "," + sha + "," + time + "," + Nonce;//共享收货地址请求参数
return true;
}
else
{
returnMsg = "-99";//请先关注公众号,才能进行下订单操作
return false;
}
}
catch (Exception ex)
{
T9.Util.LogUtil.WriteLog(ex.Message + "\r\n" + ex.StackTrace, "WebLog");
returnMsg = "-98";
return false;
}
}
/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="codeCount"></param>
/// <param name="allChar"></param>
/// <returns></returns>
public static string CreateRandomCode(int codeCount)
{
//验证码中的出现的字符,避免了一些容易混淆的字符。
string allChar = "3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,W,X,Y";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
bool breCreate = (codeCount < 6 && allCharArray.Length > 15);
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Length);
if (temp == t && breCreate)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
/// <summary>
/// datetime转换为unixtime
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
/// <summary>
/// Hash算法
/// </summary>
/// <param name="myStr"></param>
/// <returns></returns>
public string Hash(string myStr)
{
//建立SHA1对象
SHA1 sha = new SHA1CryptoServiceProvider();
//将mystr转换成byte[]
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(myStr);
//Hash运算
byte[] dataHashed = sha.ComputeHash(dataToHash);
//将运算结果转换成string
string hash = BitConverter.ToString(dataHashed).Replace("-", "");
return hash;
}