關(guān)于在vue-cli中使用微信自動(dòng)登錄和分享的實(shí)例
更新時(shí)間:2017年06月22日 09:33:30 作者:嘉爺
本篇文章主要介紹了關(guān)于在vue-cli中使用微信自動(dòng)登錄和分享的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
(以下所有接口由后臺(tái)提供)
一、微信自動(dòng)登錄
//定義事件
methods:{
//判斷是否微信登陸 是不是微信瀏覽器
isWeiXin() {
let ua = window.navigator.userAgent.toLowerCase();
console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
},
test(){
if(this.isWeiXin()){
//微信登錄,接口由后臺(tái)定義
this.$http.get('/wx/index/login/type/2').then((res) => {
if(res.data.code==0){ //微信登錄成功跳轉(zhuǎn)個(gè)人中心
this.$router.push({
name:'UserHome',
})
}else{ //微信登錄失敗,使用填寫信息登錄
this.$router.push({
name:'Login',
})
}
})
}
//頁面加載后執(zhí)行
mounted(){
if(this.isWeiXin()){ //是來自微信內(nèi)置瀏覽器
// 獲取微信信息,如果之前沒有使用微信登陸過,將進(jìn)行授權(quán)登錄
this.$http.get(this.$root.api+"/index/index/wx_info").then((res) => {
if(res.data.code!=0){
location.href='/wx/index/wxAutoLogin';
}
})
}
}
二、微信分享
methods:{
//判斷是否微信登陸
isWeiXin() {
let ua = window.navigator.userAgent.toLowerCase();
console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
},
//微信分享使用方法
wxInit(sd){
let links='http://www.kspxzx.com/index/index/wxshare_choiceOk/identity/Student/courseID/'+this.courseID+'/appointment_code/'+this.appointment_code; //分享出去的鏈接
let title='學(xué)車訓(xùn)練課程分享'; //分享的標(biāo)題
let desc=' 教練名字:'+this.codeName+' 所在駕校:'+this.drive+' 訓(xùn)練日期:'+this.date+' 訓(xùn)練項(xiàng)目:'+this.proje; //分享的詳情介紹
wx.config({
debug: false,
appId: sd.appId,
timestamp: sd.timestamp,
nonceStr: sd.nonceStr,
signature: sd.signature,
jsApiList: [
'onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo'
]
});
wx.ready(function () {
// alert("done")
// alert(title)
wx.onMenuShareTimeline({
title: title, // 分享標(biāo)題
link:links, // 分享鏈接'
imgUrl: sd.cover, // 分享圖標(biāo)
success: function () {
// 分享紀(jì)錄
shareRecord();
alert("分享到朋友圈成功")
},
cancel: function () {
alert("分享失敗,您取消了分享!")
}
});
// wx.onMenuShareAppMessage({
// title: title, // 分享標(biāo)題
// desc: description, // 分享描述
// link: link, // 分享鏈接
// imgUrl: cover, // 分享圖標(biāo)
// success: function () {
// alert("成功分享給朋友")
// },
// cancel: function () {
// alert("分享失敗,您取消了分享!")
// }
// });
//微信分享菜單測(cè)試
wx.onMenuShareAppMessage({
title:title, // 分享標(biāo)題
desc: desc, // 分享描述
link: links, // 分享鏈接
imgUrl: sd.cover, // 分享圖標(biāo)
success: function () {
// 分享紀(jì)錄
shareRecord();
alert("成功分享給朋友")
},
cancel: function () {
alert("分享失敗,您取消了分享!")
}
});
wx.onMenuShareQQ({
title:title, // 分享標(biāo)題
desc: desc, // 分享描述
link:links, // 分享鏈接
imgUrl: sd.cover, // 分享圖標(biāo)
success: function () {
// 分享紀(jì)錄
shareRecord();
alert("成功分享給QQ")
},
cancel: function () {
alert("分享失敗,您取消了分享!")
}
});
wx.onMenuShareWeibo({
title:title, // 分享標(biāo)題
desc: desc, // 分享描述
link: links, // 分享鏈接
imgUrl: sd.cover, // 分享圖標(biāo)
success: function () {
// 分享紀(jì)錄
shareRecord();
alert("成功分享給朋友")
},
cancel: function () {
alert("分享失敗,您取消了分享!")
}
});
});
wx.error(function(res){
// alert("error")
// config信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對(duì)于SPA可以在這里更新簽名。
});
},
},
mounted(){// 微信分享 'http://www.kspxzx.com/'
let old_this=this;
if(this.isWeiXin()){
var url = "/Index/index/wxShare"; //后臺(tái)接口
var data = {url:'http://www.kspxzx.com/'} //當(dāng)前網(wǎng)頁鏈接,必須跟當(dāng)前頁面鏈接一樣,單頁面則以首頁鏈接為準(zhǔn)
$.post(url,data,function(res){
if(res.code == 0){
// 調(diào)用微信分享
old_this.wxInit(res.data);
}
});
}
};
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue Element前端應(yīng)用開發(fā)之獲取后端數(shù)據(jù)
這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之獲取后端數(shù)據(jù),對(duì)vue感興趣的同學(xué),可以參考下2021-05-05
vue-cli3啟動(dòng)服務(wù)如何自動(dòng)打開瀏覽器配置
這篇文章主要介紹了vue-cli3啟動(dòng)服務(wù)如何自動(dòng)打開瀏覽器配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
2022-09-09
Vue+WebSocket頁面實(shí)時(shí)刷新長(zhǎng)連接的實(shí)現(xiàn)
最近vue項(xiàng)目要做數(shù)據(jù)實(shí)時(shí)刷新,數(shù)據(jù)較大,會(huì)出現(xiàn)卡死情況,所以本文主要介紹了頁面實(shí)時(shí)刷新長(zhǎng)連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
2021-06-06 
