C# 拉取微信收货共享地址

wag_enu 2018-12-27 09:01:33
哪位朋友,请指教一下。C# 拉取微信收货共享地址


public static string wxEditAddrParam { get; set; }
public static string Access_Token = "", OpenId = "";
string host = "", path = "", url = "";
string code = "";

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Context.Request.QueryString["code"]))
{
//获取code码,以获取openid和access_token
code = Context.Request.QueryString["code"];

WxPayData data = new WxPayData();
data.SetValue("appid", WxPayConfig.APPID);
data.SetValue("secret", WxPayConfig.APPSECRET);
data.SetValue("code", code);
data.SetValue("grant_type", "authorization_code");
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();

//请求url以获取数据
string result = HttpService.Get(url);

//保存access_token,用于收货地址获取
JsonData jd = JsonMapper.ToObject(result);
Access_Token = (string)jd["access_token"];
//获取用户openid
OpenId = (string)jd["openid"];

Log.Debug(this.GetType().ToString(), "jd : " + jd.ToJson());

GetEditAddrParam();
}
else
{
//构造网页授权获取code的URL
string host = Context.Request.Url.Host;
string path = Context.Request.Path;
string redirect_uri = HttpUtility.UrlEncode("http://" + host + path);

WxPayData data = new WxPayData();
data.SetValue("appid", WxPayConfig.APPID);
data.SetValue("redirect_uri", redirect_uri);
data.SetValue("response_type", "code");
data.SetValue("scope", "snsapi_base");
data.SetValue("state", "STATE" + "#wechat_redirect");
string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();

//触发微信返回code码
Context.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
}
}

private void GetEditAddrParam()
{
try
{
host = Context.Request.Url.Host;
path = Context.Request.Path;
string queryString = Context.Request.Url.Query;
//这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
url = "http://" + host + path + queryString;

Log.Debug(this.GetType().ToString(), "url : " + url);

//构造需要用SHA1算法加密的数据
WxPayData signData = new WxPayData();
signData.SetValue("appid", WxPayConfig.APPID);
signData.SetValue("url", url);
signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
signData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
signData.SetValue("accesstoken", Access_Token);
string param = signData.ToUrl();

Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param);
//SHA1加密
string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1");

Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign);

//获取收货地址js函数入口参数
WxPayData afterData = new WxPayData();
afterData.SetValue("appId", WxPayConfig.APPID);
afterData.SetValue("scope", "jsapi_address");
afterData.SetValue("signType", "sha1");
afterData.SetValue("addrSign", addrSign);
afterData.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
afterData.SetValue("nonceStr", WxPayApi.GenerateNonceStr());

//转为json格式
wxEditAddrParam = afterData.ToJson();
Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + wxEditAddrParam);
}
catch (Exception ex)
{
Log.Error(this.GetType().ToString(), ex.ToString());
throw new WxPayException(ex.ToString());
}
}

<head id="Head1" runat="server">    
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>收货地址</title>
<script type="text/javascript">
function editAddress() {
WeixinJSBridge.invoke(
'editAddress',
<%=wxEditAddrParam%>,//josn串
function (res) {
alert(res.err_msg);
var addr1 = res.proviceFirstStageName;
var addr2 = res.addressCitySecondStageName;
var addr3 = res.addressCountiesThirdStageName;
var addr4 = res.addressDetailInfo;
var tel = res.telNumber;
var addr = addr1 + addr2 + addr3 + addr4;


}
);
}


window.onload = function () {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', editAddress, false);
}
else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', editAddress);
document.attachEvent('onWeixinJSBridgeReady', editAddress);
}
}
else {
editAddress();
}
};

</script>
</head>


调试了几天,结果始终如一 edit_address:fail

...全文
112 点赞 收藏 1
写回复
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_44019016 2019-01-09
看看我写的文档https://blog.csdn.net/weixin_44019016/article/details/86131984
回复
发动态
发帖子
微信开发
创建于2014-09-23

2702

社区成员

微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
申请成为版主
社区公告
暂无公告