vue實現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解
首先下載微信jssdk引入項目中,這里我就不說怎么去安裝了,如果不會的可以看一下npm教程和es6的教程。
第一步,引入微信jssdk,此處我是通過下載微信jssdk,然后用webpack引入進(jìn)項目的。
第二步,獲取詳情數(shù)據(jù),渲染頁面。
第三步,獲取詳情數(shù)據(jù)成功后再獲取微信簽名,token等配置信息。
第四步,通過api配置所想要的功能
代碼:
<template>
<div class="details">
<player :videoUrl="details.videoUrl" :coverUrl="details.coverUrl" :videoId="details.videoId"/>
<div class="description">
<span class="label" :style="{backgroundColor: details.videoLabelColor}">{{details.videoLabel}}</span>
<p class="title">{{details.videoTitle}}</p>
<p class="info">
<span>{{details.mtime}}</span>
<i class="iconfont icon--"></i>
{{details.videoPlayTimes}}
</p>
<p class="summary">簡介</p>
<p class="article ql-editor" v-html="details.videoDescription"></p>
</div>
</div>
</template>
<script>
import player from '@/components/player'
import { videoDtails, getApp } from '@/config/api'
/* eslint-disable no-undef */
export default {
components: {
player
},
data () {
return {
details: {},
appId: '',
signature: '',
timestamp: '',
nonceStr: ''
}
},
beforeDestroy () {
document.querySelector('.htmlTitle').text = 'title'
},
mounted () {
// 獲取詳情數(shù)據(jù)<span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;">let url = window.location.href.split("#")[0]</span>
this.$http.get(this, videoDtails, {videoId: this.$route.query.id}, res => {
this.details = res
document.querySelector('.htmlTitle').text = this.details.videoTitle
this.$http.get(this, getApp, {url: url, refresh: true}, res => {
this.appId = res.appId
this.signature = res.signature
this.timestamp = res.timestamp
this.nonceStr = res.nonceStr
this.shard(url)
})
})
},
methods: {
shard (url) {
wx.config({
debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。
appId: this.appId, // 必填,公眾號的唯一標(biāo)識
timestamp: this.timestamp, // 必填,生成簽名的時間戳
nonceStr: this.nonceStr, // 必填,生成簽名的隨機(jī)串
signature: this.signature, // 必填,簽名,見附錄1
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
})
wx.onMenuShareTimeline({
title: this.details.videoTitle, // 分享標(biāo)題
link: url+'#/...', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
imgUrl: this.details.coverUrl, // 分享圖標(biāo)
success () {
alert('分享朋友圈成功')
// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
},
cancel () {
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
})
wx.onMenuShareAppMessage({
title: this.details.videoTitle, // 分享標(biāo)題
desc: this.details.videoTitle, // 分享描述
link: url+'#/...', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致
imgUrl: this.details.coverUrl, // 分享圖標(biāo)
type: 'video', // 分享類型,music、video或link,不填默認(rèn)為link
dataUrl: this.details.videoUrl, // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空
success: function () {
alert('分享給朋友成功')
// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
},
cancel: function () {
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
})
}
}
}
</script>
<style lang="less" scoped>
.details {
overflow: hidden;
.description {
padding: 10px;
.label {
display: inline-block;
padding:0 10px;
height: 22px;
line-height: 22px;
color: #fff;
font-size: 12px;
text-align: center;
}
.title {
line-height: 30px;
font-size: 18px;
}
.info {
line-height: 26px;
color: #949494;
span {
margin-right: 15px;
}
.iconfont {
font-size: 12px;
}
}
.summary {
margin-top: 20px;
color: #4b4b4b;
font-size: 16px;
}
.article {
margin-top: 10px;
}
}
}
</style>
以上這篇vue實現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui修改el-form-item樣式的詳細(xì)示例
在使用element-ui組件庫的時候經(jīng)常需要修改原有樣式,下面這篇文章主要給大家介紹了關(guān)于element-ui修改el-form-item樣式的詳細(xì)示例,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
electron-vite工具打包后如何通過內(nèi)置配置文件動態(tài)修改接口地址
使用electron-vite?工具開發(fā)項目打包完后每次要改接口地址都要重新打包,對于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動態(tài)修改接口地址實現(xiàn)方法,需要的朋友可以參考下2024-05-05
Vue中Axios配置不同的baseURL,請求不同的域名接口方式
這篇文章主要介紹了Vue中Axios配置不同的baseURL,請求不同的域名接口方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
詳解vue-router 2.0 常用基礎(chǔ)知識點之router.push()
本篇文章主要介紹了vue-router 2.0 常用基礎(chǔ)知識點之router.push(),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
vue3中watch和watchEffect實戰(zhàn)梳理
這篇文章主要介紹了vue3中watch和watchEffect實戰(zhàn)梳理,watch和watchEffect都是vue3中的監(jiān)聽器,但是在寫法和使用上是有區(qū)別的。下文介紹他們之間的方法及區(qū)別,需要的朋友可以參考一下2022-07-07

