h5-share 分享朋友圈

youmengting-dev
游梦婷 1 year ago
parent f87df3c3de
commit 1cb8eac39b
  1. 82
      common/share.js
  2. 2
      pages/welcome/welcome.vue

@ -10,26 +10,29 @@ export default {
desc: '', desc: '',
content: '' content: ''
}, },
localPath: '',
shareLink: '',
} }
}, },
methods: { methods: {
//分享 //分享
doShare() { doShare() {
const app = getApp(); const app = getApp();
let that = this; let that = this;
let theUrl = window.location.href; // let theUrl = window.location.href;
that.shareLink = app.globalData.h5url+'?accountId=0000016&key=&code=' + theUrl.split('#')[1]; that.localPath = window.location.href;
that.shareLink = app.globalData.h5url + '?accountId=0000016&key=&code=' + that.localPath.split('#')[1];
// console.log(theUrl.split('#')[0],decodeURIComponent(theUrl.split('#')[1]),theUrl,"theUrl",that.shareLink,"that.shareLink"); // console.log(theUrl.split('#')[0],decodeURIComponent(theUrl.split('#')[1]),theUrl,"theUrl",that.shareLink,"that.shareLink");
let datas = { let datas = {
url: theUrl.split('#')[0] url: that.localPath.split('#')[0]
} }
/* https://hsg.dctpay.com/hsgH5/index.html?code=081Zb4ml2gamsb4L3nnl2UArrO1Zb4mc&state=1#/physical-merchants/classify/goods-search/goods-search?goodsLabel=%E9%9B%B6%E5%85%83%E8%B4%AD */ /* https://hsg.dctpay.com/hsgH5/index.html?code=081Zb4ml2gamsb4L3nnl2UArrO1Zb4mc&state=1#/physical-merchants/classify/goods-search/goods-search?goodsLabel=%E9%9B%B6%E5%85%83%E8%B4%AD */
let api = require('../Utils/Api'); let api = require('../Utils/Api');
let wx = require('weixin-js-sdk'); let wx = require('weixin-js-sdk');
api.verifyWx(datas).then(res => { api.verifyWx(datas).then(res => {
@ -41,32 +44,41 @@ export default {
nonceStr: res.return_data.nonceStr, // 必填,生成签名的随机串 nonceStr: res.return_data.nonceStr, // 必填,生成签名的随机串
signature: res.return_data.signature, // 必填,签名,见附录1 signature: res.return_data.signature, // 必填,签名,见附录1
jsApiList: [ jsApiList: [
'onMenuShareAppMessage' 'updateAppMessageShareData',
'updateTimelineShareData',
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
}); });
let obj = {
title: '嗨森逛分享', // 分享标题
// desc: '', // 分享描述
link: that.shareLink, // 分享链接
imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
success: function(res) {
// 支付成功后的回调函数
uni.showToast({
title: '分享成功',
duration: 2000,
icon: 'none'
})
},
cancel: function(r) {},
fail: function(res) {}
}
/* configreadyconfig /* configreadyconfig
config是一个客户端的异步操作所以如果需要在页面加载时就调用相关接口则须把相关接口放在ready函数中调用来确保正确执行 config是一个客户端的异步操作所以如果需要在页面加载时就调用相关接口则须把相关接口放在ready函数中调用来确保正确执行
对于用户触发时才调用的接口则可以直接调用不需要放在ready函数中 */ 对于用户触发时才调用的接口则可以直接调用不需要放在ready函数中 */
wx.ready(function() { //需在用户可能点击分享按钮前就先调用 wx.ready(function() { //需在用户可能点击分享按钮前就先调用
wx.onMenuShareAppMessage({
title: '嗨森逛分享', // 分享标题
desc: '', // 分享描述
link: that.shareLink, // 分享链接
imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
success: function(res) {
// 支付成功后的回调函数
uni.showToast({
title: '分享成功',
duration: 2000,
icon: 'none'
})
},
cancel: function(r) {},
fail: function(res) {}
});
});
/* 分享给朋友 */
wx.updateAppMessageShareData(obj);
/* 自定义“分享到朋友圈” */
wx.updateTimelineShareData(obj);
});
// wx.error(function(res) { // wx.error(function(res) {
// console.log(res); // console.log(res);
// // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 // // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
@ -83,7 +95,7 @@ export default {
//获取当前路由 //获取当前路由
getCurrentRouteYm() { getCurrentRouteYm() {
let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组 let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
console.log(currentRoutes[currentRoutes.length - 1]); console.log(currentRoutes[currentRoutes.length - 1]);
let route = currentRoutes[currentRoutes.length - 1]; let route = currentRoutes[currentRoutes.length - 1];
// let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由 // let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
@ -101,19 +113,19 @@ export default {
// path: currentPath, // path: currentPath,
// query: currentQuery, // query: currentQuery,
// } // }
return decodeURIComponent(route.$page.fullPath) return decodeURIComponent(route.$page.fullPath)
}, },
}, },
onLoad(){ onLoad() {
this.$nextTick(()=>{ this.$nextTick(() => {
// #ifdef H5 // #ifdef H5
this.doShare() this.doShare()
// #endif // #endif
}) })
}, },
/* 小程序分享 */ /* 小程序分享 */
onShareAppMessage(res) { onShareAppMessage(res) {

@ -104,7 +104,7 @@
if(app.globalData.accountType == '0000016&key' && app.globalData.h5code){ if(app.globalData.accountType == '0000016&key' && app.globalData.h5code){
const value = uni.getStorageSync('goUrl'); const value = uni.getStorageSync('goUrl');
/* /physical-merchants/classify/goodsDetail/goodsDetail?goodsId=35#/ */ /* /physical-merchants/classify/goodsDetail/goodsDetail?goodsId=35#/ */
value = value.slice(0,value.length-2); value = value.slice(0,value.lastIndexOf("#"));
if(value == "/pages/tabBar/home/home" || value == "/pages/tabBar/category/category" || value == "/pages/tabBar/user/user"){ if(value == "/pages/tabBar/home/home" || value == "/pages/tabBar/category/category" || value == "/pages/tabBar/user/user"){
uni.switchTab({ uni.switchTab({

Loading…
Cancel
Save