wxController.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import axios from "axios";
  2. declare const wx: any;
  3. const ua = navigator.userAgent.toLowerCase();
  4. function isWeixinBrowser() {
  5. return /micromessenger/.test(ua) ? true : false;
  6. }
  7. function isIos() {
  8. if (/(iphone|ipad|ipod|ios)/i.test(ua)) {
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. }
  14. function isAndroid() {
  15. if (/(android)/i.test(ua)) {
  16. return true;
  17. } else {
  18. return false;
  19. }
  20. }
  21. export class wxController {
  22. signSuccess = false;
  23. requestUrl = ""; //获取signature地址
  24. configData = {
  25. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  26. appId: "", // 必填,公众号的唯一标识
  27. timestamp: 0, // 必填,生成签名的时间戳
  28. nonceStr: "", // 必填,生成签名的随机串
  29. signature: "", // 必填,签名
  30. jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"], // 必填,需要使用的JS接口列表
  31. };
  32. //默认分享设置
  33. shareData = {
  34. title: "",
  35. link: location.href,
  36. imgUrl: "",
  37. desc: "",
  38. };
  39. init(url: string) {
  40. if (isWeixinBrowser()) {
  41. this.requestUrl = url;
  42. const signUrl = window.location.href;
  43. this.signSuccess = false;
  44. this.sign(signUrl);
  45. } else {
  46. console.error("非微信浏览器");
  47. }
  48. }
  49. sign(url: string) {
  50. const _this = this;
  51. if (!url) url = window.location.href;
  52. //签名接口
  53. axios(this.requestUrl, {
  54. method: "get",
  55. params: { url },
  56. })
  57. .then(function (response) {
  58. const data = response.data.result;
  59. _this.setConfig(data);
  60. wx.config(_this.configData);
  61. wx.ready(function () {
  62. _this.signSuccess = true;
  63. _this.setShare();
  64. });
  65. wx.error(function (res: any) {
  66. _this.signSuccess = false;
  67. console.error("error: ", res);
  68. });
  69. })
  70. .catch(function (error) {
  71. console.error(error);
  72. });
  73. }
  74. setShareData(shareData: {
  75. title: string;
  76. link: string;
  77. imgUrl: string;
  78. desc: string;
  79. }) {
  80. shareData = Object.assign({}, this.shareData, shareData);
  81. this.shareData = shareData;
  82. console.error("shareData **************: ", shareData);
  83. }
  84. setConfig(options: any) {
  85. const { app_id, nonce_str, signature, timestamp } = options;
  86. this.configData = Object.assign(this.configData, {
  87. appId: app_id,
  88. timestamp: timestamp,
  89. nonceStr: nonce_str,
  90. signature: signature,
  91. });
  92. }
  93. setShare(options?: {
  94. link: string;
  95. title: string;
  96. desc: string;
  97. imgUrl: string;
  98. }) {
  99. options = Object.assign({}, this.shareData, options);
  100. if (!this.signSuccess || !options.title) return;
  101. wx.updateAppMessageShareData({
  102. title: options.title, // 分享标题
  103. desc: options.desc, // 分享描述
  104. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  105. imgUrl: options.imgUrl, // 分享图标
  106. success: function () {
  107. console.log("设置成功");
  108. },
  109. fail: function (msg: any) {
  110. console.error("设置失败:" + JSON.stringify(msg));
  111. },
  112. });
  113. wx.updateTimelineShareData({
  114. title: options.title, // 分享标题
  115. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  116. imgUrl: options.imgUrl, // 分享图标
  117. success: function () {
  118. console.log("设置成功");
  119. },
  120. fail: function (msg: any) {
  121. console.error("设置失败:" + JSON.stringify(msg));
  122. },
  123. });
  124. }
  125. }