You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
high-mini/common/share.js

212 lines
5.8 KiB

1 year ago
export default {
data() {
return {
//设置默认的分享参数
//如果页面不设置share,就触发这个默认的分享
share: {
title: '嗨森逛',
path: '/pages/tabBar/home/home',
imageUrl: '',
desc: '',
content: ''
},
localPath: '',
shareLink: '',
1 year ago
shareH5:{
title: '嗨森逛分享', // 分享标题
link:"", // 分享链接
imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
1 year ago
},
resVerifyWx:null,
1 year ago
}
},
methods: {
//分享
1 year ago
async doShare() {
1 year ago
1 year ago
let that = this;
that.localPath = window.location.href;
1 year ago
// that.shareLink = app.globalData.h5url + '?accountId=0000016&key=&code=' + that.localPath.split('#')[1];
1 year ago
1 year ago
let datas = {
url: that.localPath.split('#')[0]
1 year ago
}
/* 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');
1 year ago
that.resVerifyWx = await api.verifyWx(datas);
1 year ago
that.h5ShareMethod();
1 year ago
},
//获取当前路由
getCurrentRouteYm() {
let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
// console.log(currentRoutes[currentRoutes.length - 1]);
1 year ago
let route = currentRoutes[currentRoutes.length - 1];
return decodeURIComponent(route.$page.fullPath)
1 year ago
},
1 year ago
h5ShareMethod(){
let that = this;
1 year ago
const app = getApp();
1 year ago
if(!that.resVerifyWx){
return
}
1 year ago
if(this.isGoHome()){
that.shareLink = app.globalData.h5url + '?accountId=0000016&key=&code=/pages/tabBar/home/home'
}else{
that.shareLink = app.globalData.h5url + '?accountId=0000016&key=&code=' + that.localPath.split('#')[1];
}
that.$set(this.shareH5,"link",that.shareLink);
// console.log(that.shareLink,"that.shareLink");
1 year ago
let wx = require('weixin-js-sdk');
1 year ago
1 year ago
if (that.resVerifyWx.return_code == '000000') {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: that.resVerifyWx.return_data.appId, // 必填,公众号的唯一标识
timestamp: that.resVerifyWx.return_data.timestamp, // 必填,生成签名的时间戳
nonceStr: that.resVerifyWx.return_data.nonceStr, // 必填,生成签名的随机串
signature: that.resVerifyWx.return_data.signature, // 必填,签名,见附录1
jsApiList: [
'updateAppMessageShareData',
'updateTimelineShareData',
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
/* configreadyconfig
config是一个客户端的异步操作所以如果需要在页面加载时就调用相关接口则须把相关接口放在ready函数中调用来确保正确执行
对于用户触发时才调用的接口则可以直接调用不需要放在ready函数中 */
wx.ready(function() { //需在用户可能点击分享按钮前就先调用
/* 分享给朋友 */
wx.updateAppMessageShareData(that.shareH5);
/* 自定义“分享到朋友圈” */
wx.updateTimelineShareData(that.shareH5);
});
} else {
uni.showToast({
title: that.resVerifyWx.return_msg,
duration: 2000,
icon: 'none'
})
}
1 year ago
},
isGoHome(path){
1 year ago
/* 黑名单:不允许分享,分享的跳转至首页 */
1 year ago
let arr =[
"/physical-merchants/settleAccounts/settleAccounts",//订单确认
"/physical-merchants/classify/order/pay",//收银台页
"/physical-merchants/classify/order/paymentResult",//支付结果
"/physical-merchants/address/addNewAddress/addNewAddress",//添加地址
"/physical-merchants/classify/poster/poster",//生成海报
1 year ago
];
// #ifdef H5
1 year ago
let str = this.localPath.split('#')[1];
str = str.split("?")[0];
return arr.includes(str)
// #endif
// #ifdef MP-WEIXIN
return arr.includes(path)
// #endif
1 year ago
},
isNotDoShare(){
/* 不做分享 */
let arr =[
"/subPages/shareCounpon/shareCounpon",//分享领取优惠券
"/",
];
let str = window.location.href.split('#')[1];
str = str.split("?")[0];
// console.log(str,"str==================================")
return arr.includes(str)
}
1 year ago
},
1 year ago
onLoad() {
// #ifdef H5
this.$nextTick(() => {
if(this.isNotDoShare()){
return ;
}
// console.log(this.isNotDoShare(),"==================")
1 year ago
this.doShare();
1 year ago
})
1 year ago
// #endif
1 year ago
},
1 year ago
onShow(){
// #ifdef H5
this.$nextTick(()=>{
if(this.isNotDoShare()){
return ;
}
1 year ago
this.h5ShareMethod();
})
// #endif
1 year ago
1 year ago
},
1 year ago
// #ifdef H5
onBackPress(event){
/* h5分享页面的回退 */
const app = getApp();
let str = uni.getStorageSync("accountType");
if(str == "0000016&key"){
if(getCurrentPages().length == 2 && getCurrentPages()[0].route == "pages/welcome/welcome"){
uni.switchTab({
url:"/pages/tabBar/home/home"
})
return true;
}
}
return false;
},
// #endif
1 year ago
/* 小程序分享 */
1 year ago
// #ifdef MP-WEIXIN
1 year ago
onShareAppMessage(res) {
this.share.path = this.getCurrentRouteYm();
if(this.isGoHome(this.share.path.split('?')[0])){
this.share.path = "/pages/tabBar/home/home"
}
1 year ago
// console.log(this.share.path, "this.share.path", res);
return {
title: this.share.title,
path: this.share.path,
imageUrl: this.share.imageUrl,
desc: this.share.desc,
content: this.share.content,
success(res) {
uni.showToast({
title: '分享成功'
})
},
fail(res) {
uni.showToast({
title: '分享失败',
icon: 'none'
})
}
}
1 year ago
},
// #endif
1 year ago
}