wechat.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // jweixin.checkJsApi({
  29. // jsApiList: this.jsApiList,
  30. // success: function (res) {
  31. // console.log(res, 7897);
  32. // },
  33. // });
  34. //配置完成后,再执行分享等功能
  35. if (callback) {
  36. callback(result);
  37. }
  38. });
  39. },
  40. //在需要自定义分享的页面中调用
  41. share: function (data) {
  42. return new Promise((resolve, reject) => {
  43. if (!this.isWechat()) {
  44. reject();
  45. }
  46. this.initJssdkShare(function (signData) {
  47. let { title, desc, link, imgUrl } = data;
  48. jweixin.ready(function () {
  49. var shareData = {
  50. title,
  51. desc,
  52. link: link || window.location.href,
  53. imgUrl,
  54. success: function (res) {
  55. resolve(res);
  56. },
  57. cancel: function (res) {},
  58. };
  59. // 旧的
  60. // 分享给朋友接口
  61. jweixin.onMenuShareAppMessage(shareData);
  62. // 分享到朋友圈
  63. jweixin.onMenuShareTimeline(shareData);
  64. // 新的
  65. // 分享给朋友接口
  66. // jweixin.updateAppMessageShareData(shareData);
  67. // //分享到朋友圈接口
  68. // jweixin.updateTimelineShareData(shareData);
  69. });
  70. });
  71. });
  72. },
  73. };