vue2.0 Hash模式下实现微信分享
1,通过后台,获取accessToken 和 签名jsApiTicket,并写入浏览器缓存(可以写在app.vue中)
<script type="text/ecmascript-6">
import Store from 'common/js/store.js';
const CODE_SUC = 1;
const CODE_ERR = 0;
export default {
name: 'app',
data () {
return {
wxToken: {
accessToken: ""
},
wxJsApiTicket: {
jsApiTicket: ""
}
};
},
created () {
setInterval(this.getAccessToken(), 7000);
// 接口入住权限验证配置
},
components: {
vFooter
},
methods: {
// 获取accessToken 和 签名jsApiTicket,并写入浏览器缓存
getAccessToken () {
this.axios.post("/api/user/getAccessToken", {
"token": null,
"uid": 0,
"devType": "wx"
}).then((res) => {
res = res.data;
if (res.code === CODE_SUC) {
// console.log(res.accessToken);
this.wxToken.accessToken = res.accessToken;
this.wxJsApiTicket.jsApiTicket = res.jsApiTicket;
Store.saveAccessToken(this.wxToken);
Store.saveJsApiTicket(this.wxJsApiTicket);
// console.log(res.accessToken);
} else if (res.code === CODE_ERR) {
console.log("获取accessToken失败");
}
}).catch((res) => {
window.alert('网络异常,登录请求失败');
});
}
}
};
</script>
2,上面的store.js(写入浏览器缓存的文件)如下
const STORAGE_KEY1 = 'memberInfo';
const STORAGE_KEY2 = 'token';
const STORAGE_KEY3 = 'accessToken';
const STORAGE_KEY4 = 'jsApiTicket'; export default {
fetchFromLocal () {
return JSON.parse(window.localStorage.getItem(STORAGE_KEY1) || "{}");
}, saveToLocal (obj) {
window.localStorage.setItem(STORAGE_KEY1, JSON.stringify(obj));
}, saveAccessToken (obj) {
window.localStorage.setItem(STORAGE_KEY3, JSON.stringify(obj));
}, saveJsApiTicket (obj) {
window.localStorage.setItem(STORAGE_KEY4, JSON.stringify(obj));
}
};
3,mian.js中,注册分享全局函数,并暴露出接口
import wx from 'weixin-js-sdk'; // 导入微信sdk
import Store from 'common/js/store.js'; // 导入store.js // 全局注册分享函数
Vue.prototype.wxShare = function (title, desc, link, imgUrl) {
// 获取签名
this.axios.post("/api/user/getSignature", {
"token": null,
"uid": 0,
"devType": "wx",
"ticket": Store.fetchjsApiTicket().jsApiTicket, // 获取浏览器缓存的签名
"url": encodeURIComponent(window.location.href.split('#')[0]) // 此处进行一次编码
}).then((res) => {
res = res.data;
if (res.code === CODE_SUC) {
wx.config({
debug: false,
appId: '', // 填写自己的appID
timestamp: res.timestamp,
nonceStr: res.noncestr,
signature: res.signature,
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
} else if (res.code === CODE_ERR) {
console.log("获取accessToken失败");
}
}).catch((res) => {
window.alert('网络异常,登录请求失败');
});
wx.ready(function () {
// 分享给朋友
wx.onMenuShareAppMessage({
title: title, // 分享标题
desc: desc, // 分享描述
link: link, // 分享链接
imgUrl: imgUrl, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
window.alert('已分享给朋友');
this.axios.post("/api/product/shareStatistics", {
"token": null,
"uid": 0,
"devType": "wx"
}).then((res) => {
res = res.data;
if (res.code === CODE_SUC) {
window.alert("分享返回数据成功");
} else if (res.code === CODE_ERR) {
console.log("获取失败");
}
}).catch((res) => {
window.alert('网络异常,登录请求失败');
});
},
cancel: function () {
// 用户取消分享后执行的回调函数
},
fail: function (res) {
window.alert(JSON.stringify(res));
}
}); // 分享到朋友圈
wx.onMenuShareTimeline({
title: title, // 分享标题
link: link,
imgUrl: imgUrl, // 分享图标
success: function () {
window.alert('已分享到朋友圈');
},
cancel: function () {
// 用户取消分享后执行的回调函数
this.axios.post("/api/product/shareStatistics", {
"token": null,
"uid": 0,
"devType": "wx"
}).then((res) => {
res = res.data;
if (res.code === CODE_SUC) {
window.aleryt("分享返回数据成功");
} else if (res.code === CODE_ERR) {
console.log("获取失败");
}
}).catch((res) => {
window.alert('网络异常,登录请求失败');
});
},
fail: function (res) {
window.alert(JSON.stringify(res));
}
});
});
};
4,在相应的位置调用注册好的wxShare (title, desc, link, imgUrl)函数,并传入所需的值:
例:以下为分享商品详情页面,link 为自己拼接路径的变量
this.wxShare('商品详情' + ' ' + this.good.goodsInfo.goodsName, this.good.goodsInfo.goodsName, link, this.IMG_BASE_URL + this.good.goodsInfo.goodsImage);
5,特别注意:在Hash模式下,安卓中会遇到分享到朋友圈之后,点击跳转会首页的情况,此时分享的路径与商品的真实路径是不一致的。需要在服务器端稍微处理一下。即把index文件,重新建一个文件夹,例如static,放进去。此时分享之后的路径,与真实路径才是一致的,也不会发生跳回首页的情况。苹果手机则不会有这个问题。
vue2.0 Hash模式下实现微信分享的更多相关文章
- vue2.0 如何在hash模式下实现微信分享
最近又把vue的demo拿出来整理下,正好要做"微信分享"功能,于是遇到新的问题: 由于hash模式下,带有"#",导致微信分享的签证无效:当改成history ...
- Vue单页式应用(Hash模式下)实现微信分享
前端微信分享的基本步骤: 一.绑定域名: 先登录微信公众平台进入"公众号设置"的"功能设置"里填写"JS接口安全域名".这个不多说,微信开发 ...
- vue history模式下的微信分享
// 微信验证 export function requireConfig() { let url = window.location.href systemApi.wxoption({ url: u ...
- vue hash模式下微信分享后打开首页,三种完美解决方案
微信分享功能给我们带来了很大的便利,使得基于微信开发出来的 H5 页面可以很好的通过微信平台进行传播.所以呢,基本上每个基于微信开发的 H5 都会集成微信分享功能.但是,前几天在对接微信分享 API ...
- C# 5.0 TAP 模式下的HTTP Get和Post
标题有点瘆人,换了工作之后很少写代码了,之前由于签了保密协议,不敢把代码拿出来分享给大家,只能摘抄网上的, 今斗胆拿出来晒晒,跪求指点,直接上代码吧 public class HTTPHelper : ...
- vue2.0 路由模式mode="history"的作用
特别提醒:开启mode="history"模式,需要服务端的支持,因为出现"刷新页面报错404"的问题: 备注:微信分享:vue项目路由带"#&quo ...
- vue history模式下的微信支付,及微信支付授权目录的填写,处理URL未注册
微信公众号配置网页授权域名:填写网址域名 微信开发者平台配置url: 访问url:http://www.baidu.com/pay/ment 支付授权目录:http://www.baidu.com/p ...
- vue2.0开发环境下解决跨域问题
1.找到vue 项目下的配置文件 /config/index.js 2.找到 proxyTable 配置项 proxyTable: { '/api': { target: 'http://www.xx ...
- Vue项目history模式下微信分享总结
原文 : http://justyeh.top/post/39/ 2019-07-02 Vue微信分享 每回遇到微信分享都是一个坑,目前的商城项目使用Vue开发,采用history的路由模式,配置微信 ...
随机推荐
- “error: command 'x86_64-linux-gnu-gcc' failed with exit status 1” in virtualenv
Most of the time these are dependency-issues. Following the stack-trace of the gcc compiler one ca ...
- exeption ORA-00907: missing right parenthesis
exeption ORA-00907: missing right parenthesis CreationTime--2018年8月16日11点11分 Author:Marydon 1.情景展示 ...
- 微信QQ的二维码登录原理js代码解析
这篇文章主要大家详细解析了微信QQ的二维码登录原理js代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 在很多地方就是都出现了使用二维码登录,二维码付款,二维码账户等应用(这里的二维码种马,诈骗 ...
- ContextMenu上下文菜单
上下文菜单一般是真针对ListView(多条数据的操作) 需求:在ListView中显示通话记录的电话号码,长按显示的上下文菜单为复制号码到拨号盘.发送信息.复制号码,与之相对应的事件. 布局代码: ...
- 执行存储过程报错——ora-01031:权限不足
1. 执行DDL报错 在oracle存储过程中,默认是可以直接执行DML和DQL的,但是执行CREATE这种的DDL则需要借助EXECUTE IMMEDIATE ···了,如下备份表语句 --抄表表备 ...
- Falsk-信号
Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. 安装:pip3 install blinker request_started = _sign ...
- 编译 boost
不想深究,只是可以编译出想要的lib文件 1.打开visual studio 2012 命令提示 2.进入 boost目录 3.运行booststrp.bat得到b2.exe.bjam.exe.pro ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
- 如何实现IOS_SearchBar搜索栏及关键字高亮
搜索框的效果演示: 这个就是所谓的搜索框了,那么接下来我们看看如何使用代码来实现这个功能. 我所使用的数据是英雄联盟的英雄名单,是一个JSON数据的txt文件, JSON数据的处理代码如下所示: // ...