wxController.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "openLocation",
  35. ], // 必填,需要使用的JS接口列表
  36. };
  37. //默认分享设置
  38. shareData = {
  39. title: "",
  40. link: location.href,
  41. imgUrl: "",
  42. desc: "",
  43. };
  44. constructor(options: { url: string }) {
  45. this.requestUrl = options.url;
  46. }
  47. setup(signUrl: string) {
  48. if (isWeixinBrowser()) {
  49. if (isIos()) {
  50. signUrl = signUrl.split("#")[0];
  51. }
  52. this.signSuccess = false;
  53. this.sign(signUrl);
  54. } else {
  55. console.error("非微信浏览器");
  56. }
  57. }
  58. sign(url: string) {
  59. const _this = this;
  60. if (!url) url = window.location.href;
  61. //签名接口
  62. axios(this.requestUrl, {
  63. method: "get",
  64. params: { url },
  65. })
  66. .then(function (response) {
  67. const data = response.data.result;
  68. _this.setConfig(data);
  69. wx.config(_this.configData);
  70. wx.ready(function () {
  71. _this.signSuccess = true;
  72. _this.setShare();
  73. });
  74. wx.error(function (res: any) {
  75. _this.signSuccess = false;
  76. console.error("error: ", res);
  77. });
  78. })
  79. .catch(function (error) {
  80. console.error(error);
  81. });
  82. }
  83. setShareData(shareData: {
  84. title: string;
  85. link: string;
  86. imgUrl: string;
  87. desc: string;
  88. }) {
  89. shareData = Object.assign({}, this.shareData, shareData);
  90. this.shareData = shareData;
  91. }
  92. setConfig(options: any) {
  93. const { app_id, nonce_str, signature, timestamp } = options;
  94. this.configData = Object.assign(this.configData, {
  95. appId: app_id,
  96. timestamp: timestamp,
  97. nonceStr: nonce_str,
  98. signature: signature,
  99. });
  100. }
  101. setShare(options?: {
  102. link: string;
  103. title: string;
  104. desc: string;
  105. imgUrl: string;
  106. }) {
  107. options = Object.assign({}, this.shareData, options);
  108. if (!this.signSuccess || !options.title) return;
  109. wx.updateAppMessageShareData({
  110. title: options.title, // 分享标题
  111. desc: options.desc, // 分享描述
  112. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  113. imgUrl: options.imgUrl, // 分享图标
  114. success: function () {
  115. console.log("设置成功");
  116. },
  117. fail: function (msg: any) {
  118. console.error("设置失败:" + JSON.stringify(msg));
  119. },
  120. });
  121. wx.updateTimelineShareData({
  122. title: options.title, // 分享标题
  123. link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  124. imgUrl: options.imgUrl, // 分享图标
  125. success: function () {
  126. console.log("设置成功");
  127. },
  128. fail: function (msg: any) {
  129. console.error("设置失败:" + JSON.stringify(msg));
  130. },
  131. });
  132. }
  133. setPreviewData(url: string, urls: string[]) {
  134. if (!this.signSuccess) return;
  135. wx.previewImage({
  136. current: url, // 当前显示图片的http链接
  137. urls: urls, // 需要预览的图片http链接列表
  138. });
  139. }
  140. openMap(options: any) {
  141. if (!this.signSuccess) return;
  142. options = {
  143. latitude: 0, // 纬度,浮点数,范围为90 ~ -90
  144. longitude: 0, // 经度,浮点数,范围为180 ~ -180。
  145. name: "", // 位置名
  146. address: "", // 地址详情说明
  147. scale: 1, // 地图缩放级别,整型值,范围从1~28。默认为最大
  148. infoUrl: "", // 在查看位置界面底部显示的超链接,可点击跳转
  149. ...options,
  150. };
  151. wx.openLocation(options);
  152. }
  153. }