詳解在vue使用weixin-js-sdk常見使用方法
1.導(dǎo)入依賴包
npm install weixin-js-sdk
2.判斷是否是在微信瀏覽器中
env.js
<script>
var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
var isAndroid = ua.indexOf('android') != -1;
var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
if(!isWeixin) {
document.head.innerHTML = '<title>抱歉,出錯(cuò)了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" rel="external nofollow" >';
document.body.innerHTML = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">請(qǐng)?jiān)谖⑿趴蛻舳舜蜷_鏈接</h4></div></div>';
}
在main.js中引用:
import env from "./env";//運(yùn)行環(huán)境
微信登錄,通過code換取openid,在起始頁(yè)使用該方法:
<script>
methods:{
// 微信登陸
wxLogin() {
var that = this;
axios
.get("/common/loginAuth")
.then(function(res) {
console.log("后臺(tái)返回的鏈接地址", res.data);
window.location.href = res.data;//跳轉(zhuǎn)后臺(tái)返回的鏈接地址
})
.catch(function(error) {});
},
//換取用戶信息
postCode(res) {
var that = this;
axios
.post("/common/getUserInfo", {
code: res
})
.then(function(res) {
cookie.set("openid", res.data.openid);//code像后臺(tái)換取openid并存入
})
.catch(function(error) {
console.log(error);
});
}},
created() {
var r = window.location.href;//獲取當(dāng)前鏈接,拆分當(dāng)前鏈接
//當(dāng)前鏈接地址為后臺(tái)返回的參數(shù),有拆分得到鏈接中的code,通過postCode()方法獲取openid,沒有則通過wxLogin()方法開始微信登錄
if (r.indexOf("?") != -1) {
r = r.split("?");
r = r[1].split("&");
r = r[0].split("=");
r = r[1];
} else {
this.wxLogin();
}
if (r) {
this.postCode(r);
} else {
this.wxLogin();
}
},
</script>
3.前端頁(yè)面使用
import wx from 'weixin-js-sdk'
this.axios({
method: 'post',
url: 'url',
data:{ url:location.href.split('#')[0] } //向服務(wù)端提供授權(quán)url參數(shù),并且不需要#后面的部分
}).then((res)=>{
wx.config({
debug: true, // 開啟調(diào)試模式,
appId: res.appId, // 必填,企業(yè)號(hào)的唯一標(biāo)識(shí),此處填寫企業(yè)號(hào)corpid
timestamp: res.timestamp, // 必填,生成簽名的時(shí)間戳
nonceStr: res.nonceStr, // 必填,生成簽名的隨機(jī)串
signature: res.signature,// 必填,簽名,見附錄1
jsApiList: ['scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列
});
})
到此這篇關(guān)于 詳解在vue使用weixin-js-sdk常見使用方法的文章就介紹到這了,更多相關(guān)vue weixin-js-sdk內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)富文本編輯器詳細(xì)過程
Vue富文本的實(shí)現(xiàn)可以使用一些現(xiàn)成的第三方庫(kù),如Quill、Vue-quill-editor、wangEditor等,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)富文本編輯器的相關(guān)資料,需要的朋友可以參考下2024-01-01
從0到1搭建element后臺(tái)框架優(yōu)化篇(打包優(yōu)化)
這篇文章主要介紹了從0到1搭建element后臺(tái)框架優(yōu)化篇(打包優(yōu)化),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue2中的keep-alive使用總結(jié)及注意事項(xiàng)
vue2.0提供了一個(gè)keep-alive組件用來緩存組件,避免多次加載相應(yīng)的組件,減少性能消耗。本文給大家介紹vue2中的keep-alive使用總結(jié)及注意事項(xiàng),需要的朋友參考下吧2017-12-12
Element-ui之ElScrollBar組件滾動(dòng)條的使用方法
這篇文章主要介紹了Element-ui之ElScrollBar組件滾動(dòng)條的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
vuex如何在非組件中調(diào)用mutations方法
這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)及滾動(dòng)監(jiān)聽的方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)及滾動(dòng)監(jiān)聽的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

