|
@@ -1,75 +1,88 @@
|
|
|
-/* eslint-disable */
|
|
|
-// @ts-nocheck
|
|
|
-import { Dict_Apis } from "@/dict";
|
|
|
import axios from "axios";
|
|
|
-var signSuccess = false; //是否已经签名成功
|
|
|
+
|
|
|
+declare const wx: any;
|
|
|
+
|
|
|
+const ua = navigator.userAgent.toLowerCase();
|
|
|
|
|
|
function isWeixinBrowser() {
|
|
|
- const ua = navigator.userAgent.toLowerCase();
|
|
|
return /micromessenger/.test(ua) ? true : false;
|
|
|
}
|
|
|
|
|
|
-const WxSdk = {
|
|
|
- defaults: {
|
|
|
+function isIos() {
|
|
|
+ if (/(iphone|ipad|ipod|ios)/i.test(ua)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function isAndroid() {
|
|
|
+ if (/(android)/i.test(ua)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export class wxController {
|
|
|
+ signSuccess = false;
|
|
|
+ requestUrl = ""; //获取signature地址
|
|
|
+
|
|
|
+ configData = {
|
|
|
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
|
|
appId: "", // 必填,公众号的唯一标识
|
|
|
timestamp: 0, // 必填,生成签名的时间戳
|
|
|
nonceStr: "", // 必填,生成签名的随机串
|
|
|
signature: "", // 必填,签名
|
|
|
jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"], // 必填,需要使用的JS接口列表
|
|
|
- },
|
|
|
+ };
|
|
|
+
|
|
|
//默认分享设置
|
|
|
- defaultShare: {
|
|
|
+ shareData = {
|
|
|
title: "",
|
|
|
link: location.href,
|
|
|
- imgUrl:
|
|
|
- "//infishwaibao.oss-cn-chengdu.aliyuncs.com/queenshow/img/Logo.8478c1b4.png",
|
|
|
+ imgUrl: "",
|
|
|
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,
|
|
|
- });
|
|
|
- },
|
|
|
+ init(url: string) {
|
|
|
+ if (isWeixinBrowser()) {
|
|
|
+ this.requestUrl = url;
|
|
|
+ const signUrl = window.location.href;
|
|
|
+ this.signSuccess = false;
|
|
|
+ this.sign(signUrl);
|
|
|
+ } else {
|
|
|
+ console.error("非微信浏览器");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- sign: function (url: string) {
|
|
|
- var that = this;
|
|
|
+ sign(url: string) {
|
|
|
+ const _this = this;
|
|
|
+
|
|
|
+ if (!url) url = window.location.href;
|
|
|
|
|
|
//签名接口
|
|
|
- axios(`${Dict_Apis.promotion}/wechat/share?`, {
|
|
|
+ axios(this.requestUrl, {
|
|
|
method: "get",
|
|
|
params: { url },
|
|
|
})
|
|
|
.then(function (response) {
|
|
|
const data = response.data.result;
|
|
|
- that.setConfig(data);
|
|
|
- wx.config(that.defaults);
|
|
|
+ _this.setConfig(data);
|
|
|
+ wx.config(_this.configData);
|
|
|
wx.ready(function () {
|
|
|
- signSuccess = true;
|
|
|
- that.setShare(that.defaultShare);
|
|
|
+ _this.signSuccess = true;
|
|
|
+ _this.setShare();
|
|
|
});
|
|
|
- wx.error(function (res) {
|
|
|
+ wx.error(function (res: any) {
|
|
|
+ _this.signSuccess = false;
|
|
|
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;
|
|
@@ -77,20 +90,30 @@ const WxSdk = {
|
|
|
imgUrl: string;
|
|
|
desc: string;
|
|
|
}) {
|
|
|
- shareData = Object.assign({}, this.defaultShare, shareData);
|
|
|
- this.defaultShare = shareData;
|
|
|
- },
|
|
|
-
|
|
|
- setShare: function (options: {
|
|
|
- link: any;
|
|
|
- title: any;
|
|
|
- desc: any;
|
|
|
- imgUrl: any;
|
|
|
+ shareData = Object.assign({}, this.shareData, shareData);
|
|
|
+ this.shareData = shareData;
|
|
|
+ console.error("shareData **************: ", shareData);
|
|
|
+ }
|
|
|
+
|
|
|
+ setConfig(options: any) {
|
|
|
+ const { app_id, nonce_str, signature, timestamp } = options;
|
|
|
+ this.configData = Object.assign(this.configData, {
|
|
|
+ appId: app_id,
|
|
|
+ timestamp: timestamp,
|
|
|
+ nonceStr: nonce_str,
|
|
|
+ signature: signature,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ setShare(options?: {
|
|
|
+ link: string;
|
|
|
+ title: string;
|
|
|
+ desc: string;
|
|
|
+ imgUrl: string;
|
|
|
}) {
|
|
|
- var that = this;
|
|
|
- options = Object.assign({}, that.defaultShare, options);
|
|
|
+ options = Object.assign({}, this.shareData, options);
|
|
|
|
|
|
- if (!signSuccess) return;
|
|
|
+ if (!this.signSuccess || !options.title) return;
|
|
|
|
|
|
wx.updateAppMessageShareData({
|
|
|
title: options.title, // 分享标题
|
|
@@ -112,8 +135,9 @@ const WxSdk = {
|
|
|
success: function () {
|
|
|
console.log("设置成功");
|
|
|
},
|
|
|
+ fail: function (msg: any) {
|
|
|
+ console.error("设置失败:" + JSON.stringify(msg));
|
|
|
+ },
|
|
|
});
|
|
|
- },
|
|
|
-};
|
|
|
-
|
|
|
-export default WxSdk;
|
|
|
+ }
|
|
|
+}
|