溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在vue-cli中如何使用微信自動登錄和分享

發(fā)布時間:2021-07-23 13:42:19 來源:億速云 閱讀:210 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)在vue-cli中如何使用微信自動登錄和分享的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

一、微信自動登錄

 //定義事件
 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){  //微信登錄成功跳轉(zhuǎn)個人中心
        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)練項目:'+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("分享失敗,您取消了分享!")
      //   }
      // });

      //微信分享菜單測試
      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)證失敗會執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對于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/'}   //當(dāng)前網(wǎng)頁鏈接,必須跟當(dāng)前頁面鏈接一樣,單頁面則以首頁鏈接為準(zhǔn)
      $.post(url,data,function(res){
        if(res.code == 0){
          // 調(diào)用微信分享
          old_this.wxInit(res.data);
        }
      });
    }
   };

感謝各位的閱讀!關(guān)于“在vue-cli中如何使用微信自動登錄和分享”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI