|
@@ -0,0 +1,119 @@
|
|
|
+/* eslint-disable */
|
|
|
+// @ts-nocheck
|
|
|
+import { Dict_Apis } from "@/dict";
|
|
|
+import axios from "axios";
|
|
|
+var signSuccess = false; //是否已经签名成功
|
|
|
+
|
|
|
+function isWeixinBrowser() {
|
|
|
+ const ua = navigator.userAgent.toLowerCase();
|
|
|
+ return /micromessenger/.test(ua) ? true : false;
|
|
|
+}
|
|
|
+
|
|
|
+const WxSdk = {
|
|
|
+ defaults: {
|
|
|
+ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
|
|
+ appId: "", // 必填,公众号的唯一标识
|
|
|
+ timestamp: 0, // 必填,生成签名的时间戳
|
|
|
+ nonceStr: "", // 必填,生成签名的随机串
|
|
|
+ signature: "", // 必填,签名
|
|
|
+ jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"], // 必填,需要使用的JS接口列表
|
|
|
+ },
|
|
|
+ //默认分享设置
|
|
|
+ defaultShare: {
|
|
|
+ title: "",
|
|
|
+ link: location.href,
|
|
|
+ imgUrl:
|
|
|
+ "//infishwaibao.oss-cn-chengdu.aliyuncs.com/queenshow/img/Logo.8478c1b4.png",
|
|
|
+ desc: "",
|
|
|
+ },
|
|
|
+
|
|
|
+ setConfig: function (options: any) {
|
|
|
+ const { app_id, nonce_str, signature, timestamp } = options;
|
|
|
+ this.defaults = Object.assign(this.defaults, {
|
|
|
+ appId: app_id,
|
|
|
+ timestamp: timestamp,
|
|
|
+ nonceStr: nonce_str,
|
|
|
+ signature: signature,
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ sign: function (url: string) {
|
|
|
+ var that = this;
|
|
|
+
|
|
|
+ //签名接口
|
|
|
+ axios(`${Dict_Apis.promotion}/wechat/share?`, {
|
|
|
+ method: "get",
|
|
|
+ params: { url },
|
|
|
+ })
|
|
|
+ .then(function (response) {
|
|
|
+ const data = response.data.result;
|
|
|
+ that.setConfig(data);
|
|
|
+ wx.config(that.defaults);
|
|
|
+ wx.ready(function () {
|
|
|
+ signSuccess = true;
|
|
|
+ that.setShare(that.defaultShare);
|
|
|
+ });
|
|
|
+ wx.error(function (res) {
|
|
|
+ console.error("error: ", res);
|
|
|
+ });
|
|
|
+ })
|
|
|
+ .catch(function (error) {
|
|
|
+ console.error(error);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ init: function () {
|
|
|
+ if (isWeixinBrowser()) {
|
|
|
+ var signUrl = window.location.href;
|
|
|
+ signSuccess = false;
|
|
|
+ this.sign(signUrl);
|
|
|
+ } else {
|
|
|
+ console.error("非微信浏览器");
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setShareData(shareData: {
|
|
|
+ title: string;
|
|
|
+ link: string;
|
|
|
+ imgUrl: string;
|
|
|
+ desc: string;
|
|
|
+ }) {
|
|
|
+ shareData = Object.assign({}, this.defaultShare, shareData);
|
|
|
+ this.defaultShare = shareData;
|
|
|
+ },
|
|
|
+
|
|
|
+ setShare: function (options: {
|
|
|
+ link: any;
|
|
|
+ title: any;
|
|
|
+ desc: any;
|
|
|
+ imgUrl: any;
|
|
|
+ }) {
|
|
|
+ var that = this;
|
|
|
+ options = Object.assign({}, that.defaultShare, options);
|
|
|
+
|
|
|
+ if (!signSuccess) return;
|
|
|
+
|
|
|
+ wx.updateAppMessageShareData({
|
|
|
+ title: options.title, // 分享标题
|
|
|
+ desc: options.desc, // 分享描述
|
|
|
+ link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
|
|
|
+ imgUrl: options.imgUrl, // 分享图标
|
|
|
+ success: function () {
|
|
|
+ console.log("设置成功");
|
|
|
+ },
|
|
|
+ fail: function (msg: any) {
|
|
|
+ console.error("设置失败:" + JSON.stringify(msg));
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ wx.updateTimelineShareData({
|
|
|
+ title: options.title, // 分享标题
|
|
|
+ link: options.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
|
|
|
+ imgUrl: options.imgUrl, // 分享图标
|
|
|
+ success: function () {
|
|
|
+ console.log("设置成功");
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },
|
|
|
+};
|
|
|
+
|
|
|
+export default WxSdk;
|