vue 微信分享至朋友圈&&分享至朋友程式碼封裝
阿新 • • 發佈:2019-01-02
1.新建share.js
檔案
import {shareSDK} from "../api/common";//分享api
import wx from 'weixin-js-sdk'
export const shareTitle = '測試';
export const shareUrl = '測試連線';
export const shareImg = '測試圖片';
export const shareDesc = '測試詳情';
/**
*分享
* @param _this
* @param shareTitle 標題
* @param shareUrl 連結
* @param shareImg 圖片
* @param shareDesc 描述
*/
export const commonShare = (_this, shareTitle, shareUrl, shareImg, shareDesc) => {
let url = window.location.href;
let data = {
url: url
};
shareSDK(data).then(res => {
if (res.status == 1) {
let data = res.data;
wx.config({
debug: false , // 開啟除錯模式,呼叫的所有api的返回值會在客戶端alert出來,若要檢視傳入的引數,可以在pc端開啟,引數資訊會通過log打出,僅在pc端時才會列印。
appId: data.appId, // 必填,公眾號的唯一標識
timestamp: data.timestamp, // 必填,生成簽名的時間戳
nonceStr: data.nonceStr, // 必填,生成簽名的隨機串
signature: data.signature, // 必填,簽名,見附錄1
jsApiList: ["onMenuShareTimeline" , "onMenuShareAppMessage"] // 必填,需要使用的JS介面列表,所有JS介面列表見附錄2
});
wx.ready(function () {
wx.onMenuShareTimeline({
title: shareTitle, // 分享標題
link: shareUrl, // 分享連結,該連結域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: shareImg, // 分享圖示
success: function () {
// 使用者確認分享後執行的回撥函式
_this.$vux.toast.text('分享成功!!!');
},
cancel: function () {
// 使用者取消分享後執行的回撥函式
_this.$vux.toast.text('取消分享!!!');
}
});
wx.onMenuShareAppMessage({
title: shareTitle, // 分享標題
desc: shareDesc, // 分享描述
link: shareUrl, // 分享連結,該連結域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: shareImg, // 分享圖示
type: "", // 分享型別,music、video或link,不填預設為link
dataUrl: "", // 如果type是music或video,則要提供資料鏈接,預設為空
success: function () {
// 使用者確認分享後執行的回撥函式
_this.$vux.toast.text('分享成功!!!');
},
cancel: function () {
// 使用者取消分享後執行的回撥函式
_this.$vux.toast.text('取消分享!!!');
}
});
});
}
}).catch(err => {
console.log(err)
})
};
2.元件中引入
import {commonShare, shareTitle, shareUrl, shareImg, shareDesc} from "./utils/share";
commonShare(this, shareTitle, shareUrl, shareImg, shareDesc);