wechat.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var jweixin = require("jweixin-module");
  2. import { getShareGzh } from "@/utils/bill";
  3. export default {
  4. //判断是否在微信中
  5. isWechat: function () {
  6. var ua = window.navigator.userAgent.toLowerCase();
  7. return ua.match(/micromessenger/i) == "micromessenger";
  8. },
  9. jsApiList: [
  10. "updateTimelineShareData",
  11. "updateAppMessageShareData",
  12. "onMenuShareAppMessage",
  13. "onMenuShareTimeline",
  14. ],
  15. //初始化sdk配置
  16. initJssdkShare: function (callback) {
  17. getShareGzh({
  18. url: window.location.href,
  19. }).then((result) => {
  20. jweixin.config({
  21. debug: false,
  22. appId: result.appid,
  23. timestamp: result.timeStamp,
  24. nonceStr: result.nonceStr,
  25. signature: result.signature,
  26. jsApiList: this.jsApiList,
  27. });
  28. if (callback) {
  29. callback(result);
  30. }
  31. });
  32. },
  33. //在需要自定义分享的页面中调用
  34. share: function (data) {
  35. return new Promise((resolve, reject) => {
  36. if (!this.isWechat()) {
  37. reject();
  38. }
  39. this.initJssdkShare(function (signData) {
  40. let { title, desc, link, imgUrl } = data;
  41. jweixin.ready(function () {
  42. var shareData = {
  43. title,
  44. desc,
  45. link: link || window.location.href,
  46. imgUrl,
  47. success: function (res) {
  48. resolve(res);
  49. },
  50. cancel: function (res) {},
  51. };
  52. // 旧的
  53. // 分享给朋友接口
  54. jweixin.onMenuShareAppMessage(shareData);
  55. // 分享到朋友圈
  56. jweixin.onMenuShareTimeline(shareData);
  57. // 新的
  58. // 分享给朋友接口
  59. // jweixin.updateAppMessageShareData(shareData);
  60. // //分享到朋友圈接口
  61. // jweixin.updateTimelineShareData(shareData);
  62. });
  63. });
  64. });
  65. },
  66. };