产品要作一个分享传播的活动考虑微信公众或者小程序javascript
功能/是否支持 | 微信公众号 | 小程序 |
---|---|---|
一键分享好友 | ❎ | ✅ |
分享朋友圈 | ✅ | ❎ |
直接获取手机号 | ❎ | ✅ |
下载app | ✅ | ❎ |
权衡利弊之后决定使用微信公众号生态,获取手机号能够用户输入,分享好友能够引导用户进行操做。 要使用公众号分享后好友能看到标题的描述的内容须要使用JSSDK。前端
微信文档java
这里我走进了个误区,觉得使用JSSDK须要用户受权,实际不须要也能使用。 接下来咱们开搞。json
哇,好简单!固然不可能了,这么简单不就很差玩了。在第三步的填写wx.config时还需作比较多的工做小程序
主要是要生成signature后端
const signArgs = {
timestamp: parseInt(new Date().getTime() / 1000), // 时间戳
noncestr: Math.random().toString(36).substr(2), // 随机字符串
url: location.href.split('#')[0],
jsApiTicket
}
const preSha = `jsapi_ticket=${signArgs.jsApiTicket}&noncestr=${signArgs.noncestr}×tamp=${signArgs.timestamp}&url=${signArgs.url}`
const signResult = {
noncestr: signArgs.noncestr,
timestamp: signArgs.timestamp,
signature: sha1(preSha)
}
wx.config({
debug: false,
appId: weixinAppid,
timestamp: signResult.timestamp,
nonceStr: signResult.noncestr,
signature: signResult.signature,
jsApiList: self.jsApiList
})
复制代码
我在过程当中遇到两个坑安全
我在作的时候仍是作了用户受权(虽然作完发现不须要注释了) 这里仍是记录一下。 主要是从微信获取codeapp
const url = encodeURIComponent(window.location.href)
window.location.replace(
`${authorize}?appid=${weixinAppid}&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
)
复制代码
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
复制代码
{
"openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
复制代码