wxController.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import axios from "axios";
  2. declare const wx: any;
  3. const ua = navigator.userAgent.toLowerCase();
  4. export 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: [
  31. "updateAppMessageShareData",
  32. "updateTimelineShareData",
  33. "previewImage",
  34. ], // 必填,需要使用的JS接口列表
  35. };
  36. //默认分享设置
  37. shareData = {
  38. title: "",
  39. link: location.href,
  40. imgUrl: "",
  41. desc: "",
  42. };
  43. constructor(options: { url: string }) {
  44. this.requestUrl = options.url;
  45. }
  46. setup(signUrl: string) {
  47. if (isWeixinBrowser()) {
  48. if (isIos()) {
  49. signUrl = signUrl.split("#")[0];
  50. }
  51. this.signSuccess = false;
  52. this.sign(signUrl);
  53. } else {
  54. console.error("非微信浏览器");
  55. }
  56. }
  57. sign(url: string) {
  58. const _this = this;
  59. if (!url) url = window.location.href;
  60. //签名接口
  61. axios(this.requestUrl, {
  62. method: "get",
  63. params: { url },
  64. })
  65. .then(function (response) {
  66. const data = response.data.result;
  67. _this.setConfig(data);
  68. wx.config(_this.configData);
  69. wx.ready(function () {
  70. _this.signSuccess = true;
  71. _this.setShare();
  72. });
  73. wx.error(function (res: any) {
  74. _this.signSuccess = false;
  75. console.error("error: ", res);
  76. });
  77. })
  78. .catch(function (error) {
  79. console.error(error);
  80. });
  81. }
  82. setShareData(shareData: {
  83. title: string;
  84. link: string;
  85. imgUrl: string;
  86. desc: string;
  87. }) {
  88. shareData = Object.assign({}, this.shareData, shareData);
  89. this.shareData = shareData;
  90. }
  91. setConfig(options: any) {
  92. const { app_id, nonce_str, signature, timestamp } = options;
  93. this.configData = Object.assign(this.configData, {
  94. appId: app_id,
  95. timestamp: timestamp,
  96. nonceStr: nonce_str,
  97. signature: signature,
  98. });
  99. }
  100. setShare(options?: {
  101. link: string;
  102. title: string;
  103. desc: string;
  104. imgUrl: string;
  105. }) {
  106. options = Object.assign({}, this.shareData, options);
  107. if (!this.signSuccess || !options.title) return;
  108. wx.updateAppMessageShareData({
  109. title: options.title, // 分享标题
  110. desc: options.desc, // 分享描述
  111. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  112. imgUrl: options.imgUrl, // 分享图标
  113. success: function () {
  114. console.log("设置成功");
  115. },
  116. fail: function (msg: any) {
  117. console.error("设置失败:" + JSON.stringify(msg));
  118. },
  119. });
  120. wx.updateTimelineShareData({
  121. title: options.title, // 分享标题
  122. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  123. imgUrl: options.imgUrl, // 分享图标
  124. success: function () {
  125. console.log("设置成功");
  126. },
  127. fail: function (msg: any) {
  128. console.error("设置失败:" + JSON.stringify(msg));
  129. },
  130. });
  131. }
  132. setPreviewData(url: string, urls: string[]) {
  133. if (!this.signSuccess) return;
  134. wx.previewImage({
  135. current: url, // 当前显示图片的http链接
  136. urls: urls, // 需要预览的图片http链接列表
  137. });
  138. }
  139. }