知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產生價值,我們在追求其視覺表現(xiàn)的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
uniapp之h5公眾號分享和授權
發(fā)表時間:2020-11-3
發(fā)布人:葵宇科技
瀏覽次數(shù):329
一,微信分享
1,安裝jweixin-module
npm i jweixin-module
2,在需要分享的頁面引入
<script>
// #ifdef H5
let jweixin = require('jweixin-module')
// #endif
.....
</script>
3,設置分享內容(或者點擊觸發(fā)分享)
distributrShare() {
this.$http({
apiName: "wxJsdkConfig",
type: "POST",
data: {
url: encodeURIComponent(window.location.href),
//后臺通過域名進行授權
}
}).then(res = >{
var _self = this jweixin.config({
debug: true,
// 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。
appId: res.data.appId,
// 必填,公眾號的唯一標識
timestamp: res.data.timestamp,
// 必填,生成簽名的時間戳
nonceStr: res.data.nonceStr,
// 必填,生成簽名的隨機串
signature: res.data.signature,
// 必填,簽名
jsApiList: ['updateTimelineShareData', 'updateAppMessageShareData', ] // 必填,需要使用的JS接口列表
})
jweixin.ready(function(res) {
//分享給朋友
jweixin.updateAppMessageShareData({
title: _self.title,
desc: "快來嗨購吧!",
link: window.location.href + "&inviteCode=" + _self.userInfo.inviteCode,
imgUrl: _self.picUrl,
trigger: function trigger(res) {
// alert(res);
},
success: function success(res) {
// alert('已分享');
},
cancel: function cancel(res) {
// alert('已取消');
},
fail: function fail(res) {
// alert(2, JSON.stringify(res));
}
});
}) jweixin.error(err = >{
// console.log("jsapi錯誤:",err)
})
}).
catch(err = >{
// console.log(3,err)
})
},
注意幾點:
a:公眾號的分享只能通過右上角h5的分享觸發(fā),默認分享當前頁面,分享只是配置分享內容
b:當前分享頁面的鏈接必須在后臺配置JS接口安全域名和網(wǎng)頁授權域名,
c:傳到后臺的鏈接需要encodeURIComponent轉碼,不能包括端口號,
d:安卓上可以,IOS上報fail link must be in js secure domain list參考:解決微信H5網(wǎng)頁分享報錯:fail link must be in js secure domain list
e:invalid signature參考關于微信公眾號開發(fā)config:invalid signature錯誤的解決方法
二,授權登錄
頁面:
...async>//判斷是否是微信瀏覽器
isWxBrowser() {
if (window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger") {
return true
} else {
return false
}
},
//jdk配置
async configWeiXin(callback) {
//獲取公眾號配置
let resConfig = await http({
apiName: "wxJsdkConfig",
type: "POST",
data: {
url: encodeURIComponent(window.location.href),
//后臺通過域名進行授權
}
}) if (resConfig) {
let apiList = [ // 可能需要用到的能力
'onMenuShareAppMessage', 'onMenuShareTimeline', 'hideOptionMenu', 'showOptionMenu', 'chooseWXPay'];
let info = {
// debug: true, // 調試,發(fā)布的時候改為false
appId: resConfig.data.appId,
nonceStr: resConfig.data.nonceStr,
timestamp: resConfig.data.timestamp,
signature: resConfig.data.signature,
jsApiList: apiList
};
jweixin.config(info);
jweixin.error(err = >{
console.log('config fail:', err);
});
jweixin.ready(res = >{
console.log(3, res) if (callback) callback(jweixin, resConfig.data.appId); // 配置成功回調
// uni.setStorageSync('msg',JSON.stringify(res.data))
// uni.setStorageSync('msg',JSON.parse(res.message.replace(/"/,/'/)))
});
}
},
//公眾號授權
async wxJsdkAuthorize(_appid, isLogin = false) {
let link = window.location.href;
// let link=window.location.origin+'/pages/public/middlePage'
let _code = ''
if (link.includes("code=")) {
_code = link.split("code=")[1].split('&')[0]
}
// 如果拿到_code,調用授權接口,沒有拿到就跳轉微信授權鏈接獲取
if (_code) {
if (isLogin) {
await http({
apiName: "wxWebLogin",
type: 'POST',
data: {
code: _code
}
}).then(res = >{
uni.showToast({
title: '登錄成功',
mask: false,
duration: 1500
});
this.setSesion(res.data) this.getUserInfo() this.afterLoginJump()
}).
catch(err = >{
uni.setStorageSync('msg', err.data) console.log(uni.getStorageInfoSync('msg').openId) if (err.code == 500070) {
uni.redirectTo({
url: '/pages/public/bindMobile'
})
} else {
uni.redirectTo({
url: '/pages/set/loginPwd'
})
}
})
} else {
await http({
apiName: "wxJsdkLogin",
data: {
code: _code
}
}).then(res = >{
})
//code傳到后臺關聯(lián)賬戶
uni.setStorageSync("wxJsdkLogin", true)
}
} else {
let appid = _appid;
let uri = encodeURIComponent(link);
let authURL = `https: //open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
window.location.href = authURL;
}
},
jsApiCall(data, callback_succ_func, callback_error_func) {
//使用原生的,避免初始化appid問題
WeixinJSBridge.invoke('getBrandWCPayRequest', {
appId: data['appId'],
timeStamp: data['timeStamp'],
nonceStr: data['nonceStr'],
// 支付簽名隨機串,不長于 32 位
package: data['package'],
// 統(tǒng)一支付接口返回的prepay_id參數(shù)值,提交格式如:prepay_id=\*\*\*)
signType: data['signType'],
// 簽名方式,默認為'SHA1',使用新版支付需傳入'MD5'
paySign: data['paySign'],
// 支付簽名
},
function(res) {
var msg = res.err_msg ? res.err_msg: res.errMsg;
//WeixinJSBridge.log(msg);
switch (msg) {
case 'get_brand_wcpay_request:ok':
//支付成功時
if (callback_succ_func) {
callback_succ_func(res);
}
break;
default:
//支付失敗時
WeixinJSBridge.log('支付失敗!' + msg + ',請返回重試.');
if (callback_error_func) {
callback_error_func({
msg: msg
});
}
break;
}
})
},
//WeixinJSBridge判斷
wxJsPay(data, callback_succ_func, callback_error_func) {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', this.jsApiCall, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', this.jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall);
}
} else {
this.jsApiCall(data, callback_succ_func, callback_error_func);
}
},
相關案例查看更多
相關閱讀
- 云南軟件定制公司
- 云南網(wǎng)站建設方案 doc
- 云南微信小程序開發(fā)
- 百度自然排名
- 汽車報廢回收
- 百度小程序公司
- 手機網(wǎng)站建設
- 汽車報廢
- 報廢車
- 云南網(wǎng)站建設哪家強
- 云南網(wǎng)站建設首選公司
- 小程序開發(fā)
- 云南軟件公司
- 快排推廣
- 昆明網(wǎng)站制作
- 昆明小程序開發(fā)聯(lián)系方式
- 網(wǎng)站排名
- 網(wǎng)站建設電話
- 小程序的開發(fā)公司
- 云南網(wǎng)站建設
- 昆明做網(wǎng)站建設的公司排名
- 出入小程序
- 電商網(wǎng)站建設
- 云南網(wǎng)站建設哪家好
- 微分銷
- 云南網(wǎng)站建設公司哪家好
- 百度推廣
- 云南做網(wǎng)站
- 日歷組件
- web服務