|
導讀網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立... 網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立在超文本基礎之上的。超級文本標記語言之所以稱為超文本標記語言,是因為文本中包含了所謂“超級鏈接”點。 本篇文章給大家帶來的內容是關于微信jssdk邏輯在vue中的使用方法介紹(代碼示例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。微信 jssdk 在 vue 中的簡單使用 import wx from 'weixin-js-sdk';
wx.config({
debug: true,
appId: '',
timestamp: ,
nonceStr: '',
signature: '',
jsApiList: []
});
wx.ready(() => {
// do something...
});
wx.error((err) => {
// do something...
});以上是微信官方給出的示例代碼,但是對于實際項目使用,還需要進一步對代碼進行封裝。本文基于 vue 進行示范,其余類框架同理。 在微信公眾平臺的官方文檔中已經指出,由于安全性考慮,需要將簽名邏輯放在后端處理,所以簽名原理不在此贅述,主要講講如何使用后端返回后的簽名調用 jssdk。在邏輯層面,由于 wx.config 方法是調用任何接口前所必須的,所以我們可以盡可能將其抽離出來單獨放置。 # utils/
.
├── common.js # 通用函數
└── lib
└── wechat # 微信相關代碼
├── auth # 微信用戶登陸獲取信息相關代碼
│ ├── auth.js
│ └── index.js
├── config # jssdk 初始化相關代碼
│ └── index.js
├── helper.js # 微信相關操作
└── share # 分享接口相關代碼
└── index.jsimport sdk from 'weixin-js-sdk';
export function initSdk({ appid, timestamp, noncestr, signature, jsApiList }) { // 從后端獲取
sdk.config({
debug: process.env.VUE_APP_ENV !== 'production',
appId: appid,
timestamp: timestamp,
nonceStr: noncestr,
signature: signature,
jsApiList: jsApiList
});
}這樣就可以完成對 // example.vue
export default {
name: 'example',
wechatShareConfig() {
return {
title: 'example',
desc: 'example desc',
imgUrl: 'http://xxx/example.png',
link: window.location.href.split('#')[0]
};
}
}// wechatMixin.js
import { share } from '@/utils/lib/wechat/share';
// 獲取 wechat 分享接口配置
function getWechatShareConfig(vm) {
const { wechatShareConfig } = vm.$options;
if (wechatShareConfig) {
return typeof wechatShareConfig === 'function'
? wechatShareConfig.call(vm)
: wechatShareConfig;
}
}
const wechatShareMixin = {
created() {
const wechatShareConfig = getWechatShareConfig(this);
if (wechatShareConfig) {
share({ ...wechatShareConfig });
}
}
};
export default wechatShareMixin;// utils/lib/wechat/share
import { getTicket } from '@/utils/lib/wechat/helper'; // 簽名接口
import { initSdk } from '@/utils/lib/wechat/config';
import sdk from 'weixin-js-sdk';
// 接口清單
const JS_API_LIST = ['onMenuShareAppMessage', 'onMenuShareTimeline'];
// 消息分享
function onMenuShareAppMessage(config) {
const { title, desc, link, imgUrl } = config;
sdk.onMenuShareAppMessage({ title, desc, link, imgUrl });
}
// 朋友圈分享
function onMenuShareTimeline(config) {
const { title, link, imgUrl } = config;
sdk.onMenuShareTimeline({ title, link, imgUrl });
}
export function share(wechatShareConfig) {
if (!wechatShareConfig.link) return false;
// 簽名驗證
getTicket(wechatShareConfig.link).then(res => {
// 初始化 `jssdk`
initSdk({
appid: res.appid,
timestamp: res.timestamp,
noncestr: res.noncestr,
signature: res.signature,
jsApiList: JS_API_LIST
});
sdk.ready(() => {
// 初始化目標接口
onMenuShareAppMessage(wechatShareConfig);
onMenuShareTimeline(wechatShareConfig);
});
});
}寫完之后乍一看似乎沒什么毛病,但是每個 // router.js
//...
routes: [
{
path: '/',
component: Example,
meta: {
wechat: {
share: {
title: 'example',
desc: 'example desc',
imgUrl: 'https://xxx/example.png'
}
}
}
}
]
//...
// 初始化分享接口
function initWechatShare (config) {
if (config) {
share(config);
}
}
router.beforeEach((to, from, next) => {
const { shareConfig } = to.meta && to.meta.wechat;
const link = window.location.href;
if (!shareConfig) next();
initWechatShare({ ...shareConfig, link });
switchTitle(shareConfig.title); // 切換標題
next();
});這樣一來,會顯得 .vue 清爽很多,不會有太多業務邏輯之外的代碼。 以上就是微信jssdk邏輯在vue中的使用方法介紹(代碼示例)的詳細內容,更多請關注php中文網其它相關文章! 網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。 |
溫馨提示:喜歡本站的話,請收藏一下本站!