您的当前位置:首页nodejs调取微信收货地址的方法

nodejs调取微信收货地址的方法

2020-11-27 来源:飒榕旅游知识分享网

本文实例为大家分享了nodejs调取微信收货地址的具体代码,供大家参考,具体内容如下

公众号调取微信收货地址条件:

1、微信调取收货地址API,公众号必须有支付权限。
2、配置对应使用收货地址功能的域名(域名配置请在公众平台网站-->开发者中心-->配置项-->网页服务-->网页授权获取用户基本信息中设置,)

router.js

var API = require('wechat-api'); 
var api = new API(config.wx_appid, config.wx_secret); 
 
var OAuth = require('wechat-oauth'); 
var client = new OAuth(config.wx_appid, config.wx_secret); 
 
/* auth */ 
app.get('/auth', function(req, res) { 
 var url = client.getAuthorizeURL('http://xxxxxxxxxx/getAddr','', 'snsapi_userinfo'); 
 res.redirect(url); 
}); 
 
 
app.get('/getAddr', function(req, res) { 
 var code = req.query.code; 
 
 client.getAccessToken(code, function (err, result) { 
 var accessToken = result.data.access_token; 
 var param = { 
 debug: false, 
 jsApiList: ['getLocation', 'onMenuShareAppMessage'], 
 url:"http://xxxxxxxxxx"+req._parsedUrl.href //这个URL是你要拉起收货地址的实际URL 
 }; 
 api.getJsConfig(param, function(err,addrConfig){ 
 console.log('addrConfig = ',addrConfig); 
 var stra = "accesstoken="+accessToken+"&appid="+config.wx_appid+"&noncestr="+addrConfig.nonceStr+"×tamp="+addrConfig.timestamp+"&url="+param.url;//拼接string1 
 //console.log('stra = ',stra); 
 /* 
 common.sha1 
 
 // SHA1加密算法 
 exports.sha1 = function(str) { 
 var md5sum = crypto.createHash('sha1'); 
 md5sum.update(str); 
 str = md5sum.digest('hex'); 
 console.log(str) 
 return str; 
 } 
 */ 
 var strb = common.sha1(stra); 
 addrConfig.addrSign = strb; 
 addrConfig.stra = stra; 
 //console.log('strb = ',strb); 
 res.render('addr', { 
 addr: addrConfig, 
 }); 
 }); 
 }); 
}); 

addr.ejs

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> 
<meta name="apple-mobile-web-app-capable" content="yes"> 
<meta name="apple-mobile-web-app-status-bar-style" content="black"> 
<meta content="telephone=no" name="format-detection"> 
<title>demo</title> 
 
 
</head> 
 
<body> 
<pre> 
<%= addr.stra %> 
 WeixinJSBridge.invoke('editAddress', { 
 "appId": '<%= addr.appId %>', 
 "scope": "jsapi_address", 
 "signType": "sha1", 
 "addrSign": '<%= addr.addrSign %>', 
 "timeStamp": '<%= addr.timestamp %>', 
 "nonceStr": '<%= addr.nonceStr %>', 
 }, 
 </pre> 
 <a href="javascript:;" rel="external nofollow" style="margin:40px;" onclick="editAddr()">获取微信收货地址</a> 
 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 
<script type="text/javascript"> 
 function editAddr() { 
 WeixinJSBridge.invoke('editAddress', { 
 "appId": '<%= addr.appId %>', 
 "scope": "jsapi_address", 
 "signType": "sha1", 
 "addrSign": '<%= addr.addrSign %>', 
 "timeStamp": '<%= addr.timestamp %>', 
 "nonceStr": '<%= addr.nonceStr %>', 
 }, function (res) { 
 if (res.err_msg == "edit_address:ok") { 
 alert(res); 
 alert(res.proviceFirstStageName); 
 } else { 
 alert("获取失败,请重试"); 
 alert(res.err_msg); 
 } 
 }); 
 
 } 
</script> 
 
</body> 
</html> 
显示全文