(以下所有接口由后台提供)
一、微信自動登錄
//定義事件 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()){ //微信登錄,接口由后台定義 this.$http.get('/wx/index/login/type/2').then((res) => { if(res.data.code==0){ //微信登錄成功跳轉個人中心 this.$router.push({ name:'UserHome', }) }else{ //微信登錄失敗,使用填寫信息登錄 this.$router.push({ name:'Login', }) } }) }
//頁面加載后執行 mounted(){ if(this.isWeiXin()){ //是來自微信內置瀏覽器 // 獲取微信信息,如果之前沒有使用微信登陸過,將進行授權登錄 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='學車訓練課程分享'; //分享的標題 let desc=' 教練名字:'+this.codeName+' 所在駕校:'+this.drive+' 訓練日期:'+this.date+' 訓練項目:'+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, // 分享標題 link:links, // 分享鏈接' imgUrl: sd.cover, // 分享圖標 success: function () { // 分享紀錄 shareRecord(); alert("分享到朋友圈成功") }, cancel: function () { alert("分享失敗,您取消了分享!") } }); // wx.onMenuShareAppMessage({ // title: title, // 分享標題 // desc: description, // 分享描述 // link: link, // 分享鏈接 // imgUrl: cover, // 分享圖標 // success: function () { // alert("成功分享給朋友") // }, // cancel: function () { // alert("分享失敗,您取消了分享!") // } // }); //微信分享菜單測試 wx.onMenuShareAppMessage({ title:title, // 分享標題 desc: desc, // 分享描述 link: links, // 分享鏈接 imgUrl: sd.cover, // 分享圖標 success: function () { // 分享紀錄 shareRecord(); alert("成功分享給朋友") }, cancel: function () { alert("分享失敗,您取消了分享!") } }); wx.onMenuShareQQ({ title:title, // 分享標題 desc: desc, // 分享描述 link:links, // 分享鏈接 imgUrl: sd.cover, // 分享圖標 success: function () { // 分享紀錄 shareRecord(); alert("成功分享給QQ") }, cancel: function () { alert("分享失敗,您取消了分享!") } }); wx.onMenuShareWeibo({ title:title, // 分享標題 desc: desc, // 分享描述 link: links, // 分享鏈接 imgUrl: sd.cover, // 分享圖標 success: function () { // 分享紀錄 shareRecord(); alert("成功分享給朋友") }, cancel: function () { alert("分享失敗,您取消了分享!") } }); }); wx.error(function(res){ // alert("error") // config信息驗證失敗會執行error函數,如簽名過期導致驗證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數中查看,對於SPA可以在這里更新簽名。 }); },
},
mounted(){// 微信分享 'http://www.kspxzx.com/' let old_this=this; if(this.isWeiXin()){ var url = "/Index/index/wxShare"; //后台接口 var data = {url:'http://www.kspxzx.com/'} //當前網頁鏈接,必須跟當前頁面鏈接一樣,單頁面則以首頁鏈接為准 $.post(url,data,function(res){ if(res.code == 0){ // 調用微信分享 old_this.wxInit(res.data); } }); } };