首页禁用掉微信分享,子页面如果需要使用微信分享得添加显示微信按钮

wxApi.js

// import wx from 'weixin-js-sdk';
import Axios from "axios";
import axios from "axios/index";
import Vue from 'vue';
import {urlConfig} from './config/UrlConfig';
const baseURL = urlConfig.wxUrl;

const Sharefun = function (option) {
    //option是分享的配置内容*/
    // const url = window.location.href.split("#")[0];
    const url = window.location.href;
    window.console.log(url, JSON.stringify(option));
    let paramData = {
        url: url
    };

    Axios({
        method: 'post',
        url: `${baseURL}/wxApi`,
        headers: Object.assign({
            timeout: 60 * 1000,
            // 'Content-Type': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
        }),
        data: paramData,
    })
        .then((response) => {
            let resp = eval("(" + response.data + ")");
            console.log("微信配置接口返回:",resp);
            wx.config({
                debug: false,
                appId: resp.appId,
                timestamp: resp.timestamp,
                nonceStr: resp.noncestr,
                signature: resp.signature,
                jsApiList: [
                    // 所有要调用的 API 都要加到这个列表中
                    "showOptionMenu",
                    "onMenuShareTimeline",
                    "onMenuShareAppMessage",
                    "closeWindow"
                ]
            });
        }).catch((error) => {
        window.console.log(error);
    });
    wx.ready(function () {
        wx.showOptionMenu();
        wx.onMenuShareTimeline({
            title: option.shareTitle,
            desc: option.shareDesc,
            link: option.shareUrl,
            imgUrl: option.shareImg,
        });
        wx.onMenuShareAppMessage({
            title: option.shareTitle,
            desc: option.shareDesc,
            link: option.shareUrl,
            imgUrl: option.shareImg,
        });
    })
}

const hideShare = function (option) {
    //option是微信的配置内容*/
    const url = window.location.href;
    window.console.log(url, JSON.stringify(option));
    let paramData =
        url: url
    };

    Axios({
        method: 'post',
        url: `${baseURL}/wxApi`,
        headers: Object.assign({
            timeout: 60 * 1000,
            'Content-Type': 'application/x-www-form-urlencoded',
        }),
        data: paramData,
    })
        .then((response) => {
            let resp = eval("(" + response.data + ")");
            console.log("微信配置接口返回:",resp);
            wx.config({
                debug: false,
                appId: resp.appId,
                timestamp: resp.timestamp,
                nonceStr: resp.noncestr,
                signature: resp.signature,
                jsApiList: [
                    // 所有要调用的 API 都要加到这个列表中
                    "hideOptionMenu"
                ]
            });
        }).catch((error) => {
        window.console.log(error);
    });
    wx.ready(function () {
        wx.hideOptionMenu();
    })
}

export {ShareFun,hideShare};

urlConfig.js

/**
 * 配置网络请求环境
 * @type {string}
 */

let env = process.env.VUE_APP_CURRENTMODE || 'DEV';
console.log("当前环境变量:", env);

let GlobalConfig = {
    DEV: {
        env: 'DEV',
        baseUrl: '域名地址',
        wxUrl: '域名地址'

    }
}

let urlConfig = GlobalConfig[env];

export {
    urlConfig,
    env
}

页面使用微信配置

import {ShareFun} from '../common/wxApi';
 created() {
            let url = window.location.href;
            console.log('当前页面链接:',url);
            ShareFun({
                shareTitle: 'title',
                shareDesc: 'desc',
                shareUrl: url,
                shareImg: "imgUrl",
            });
        }

隐藏分享按钮

import {hideShare} from 'wxApi';
created() {
            hideShare();
        }

 

posted @ 2020-12-21 20:08  韩Jeor  阅读(176)  评论(0编辑  收藏  举报