微信/支付宝授权:
微信参考文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
支付宝参考文档:https://opendocs.alipay.com/open/02xtl4
引入sdk:
<!-- 微信sdk -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<!-- 支付宝sdk -->
<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.min.js"></script>
// 设备类型判断
var u = navigator.userAgent.toLowerCase();
var isWx = (u.indexOf('micromessenger') != -1);// 判断为微信
var isAli = (u.indexOf('alipay') != -1); // 判断为支付宝
if(isWx){
wxGet(encodeURIComponent('/attend.html?aid='+aid'));
}else if(isAli){
aliGet(encodeURIComponent('/attend.html?aid='+aid'));
}else{
// 提示在微信/支付宝打开
warn_fuc('请使用微信或支付宝打开链接');
}
// 调起微信授权
function wxGet(url) {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appId+'&redirect_uri='+url+'&response_type=code&scope=snsapi_userinfo&state=getUserBack#wechat_redirect';
}
// 调起支付宝授权
function aliGet(url) {
window.location.href = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='+appId+'&scope=auth_user&redirect_uri='+url+'&state=getUserBack';
}
分享:
参考文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
// 刚进来先初始化
wx.config({
debug: false,
appId: data.data.appId,
timestamp: data.data.timestamp,
nonceStr: data.data.nonceStr,
signature: data.data.signature,
jsApiList: [
'updateAppMessageShareData',
'updateTimelineShareData'
]
});
//转发样式
function setWxStyle() {
wx.ready(function(){
var initUrl = g_config.webUrl+'/activity.html?id='+aid;
var fw_url = (af_id?(initUrl+'&pid='+af_id):initUrl);
var shareData = {
imgUrl: share_img, // 分享显示的缩略图地址
link: fw_url, // 分享地址
desc: share_desc, // 分享描述
title: share_title, // 分享标题
success: function () {
// 分享成功可以做相应的数据处理
console.log('分享成功');
},
fail: function () {
console.log('调用失败');
},
complete: function () {
console.log('调用结束');
}
};
wx.updateAppMessageShareData(shareData);
wx.updateTimelineShareData(shareData);
});
}
需要注意的点:
- 分享只能通过二维码扫码或者公众号入口进入进行分享,不能点击链接进入分享,否则分享出来还是个链接,这个坑踩了好久出来!!
- 授权微信公众平台配置方面:
后台配置appid和密钥;
登陆公众号配置JS安全域名+网页授权域名(要跟网页链接保持统一)
本人小白, 有什么问题留言哦,不喜勿喷 !