微信分享通用js

2020-07-07  本文已影响0人  _micang

/**

* 微信分享

*/

var WXOptions = function() {

    var rootPath = (function() {

        var root = location.href.split("//")

        var path = root[1].split("/")

        return root[0] + '//' + path[0];

    })()

    /**

    * 获取微信鉴权

    */

    var getAuth = function() {

        $.ajax({

            url: '/web/wxshare/auth',

            type: 'post',

            data: JSON.stringify({url: location.href.split('#')[0]}),

            dataType: 'json',

            cache: false,

            contentType: 'application/json;charset=utf-8',

            async: false,

            success: function(data) {

                wxInit(data)

            },

            error: function(error) {

                console.error("认证信息获取失败。", error)

            }

        })

    }

    var _this = this

    /**

    * 微信分享初始化

    *

    * @param data

    */

    var wxInit = function(data) {

        var defaultImgUrl = rootPath + '/web/home/images/wxshare.jpg'

        var defaultDesc = ''

        var defaultLink = location.href.split('#')[0]

        var defaultTitle = '' + document.title

        // 微信全局化配置

        wx.config({

            // debug: true,

            appId: data.attach.appId,

            timestamp: data.attach.timestamp,

            nonceStr: data.attach.nonceStr,

            signature: data.attach.signature,

            // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

            jsApiList: [

                'checkJsApi',

                'updateTimelineShareData',

                'updateAppMessageShareData'

            ]

        });

        wx.ready(function() {

            // 朋友圈

            // 缩略图将https 转成 http

            wx.updateTimelineShareData({

                title: !!_this.title ? _this.title : defaultTitle,

                link: !!_this.link ? _this.link : defaultLink,

                imgUrl: !!_this.imgUrl ? (rootPath.replace('https', 'http') + _this.imgUrl) : defaultImgUrl,

                success: function () {},

                cancel: function () {}

            })

            // 微信好友

            // 缩略图将https 转成 http

            wx.updateAppMessageShareData({

                title: !!_this.title ? _this.title : defaultTitle,

                desc: !!_this.desc ? _this.desc : defaultDesc,

                link: !!_this.link ? _this.link : defaultLink,

                imgUrl: !!_this.imgUrl ? (rootPath.replace('https', 'http') + _this.imgUrl) : defaultImgUrl,

                success: function () {},

                cancel: function () {}

            })

        })

    }

    /**

    * 获取浏览器信息

    */

    var getExplore = function() {

        var ua = navigator.userAgent.toLowerCase()

        return ua.match(/MicroMessenger/i) == 'micromessenger' ? true : false

    }

    /**

    * 初始化方法

    *

    * @returns {WXOptions}

    */

    this.init = function() {

        try {

            // 微信浏览器调用认证

            if (getExplore()) {

                getAuth()

            }

        } catch (e) {

            console.error('初始化微信分享异常', e)

        }

        return this

    }

}

// 标题

WXOptions.prototype.setTitle = function(title) {

    this.title = title

    return this

}

WXOptions.prototype.getTitle = function() {

    return this.title

}

// 描述

WXOptions.prototype.setDesc = function(desc) {

    this.desc = desc

    return this

}

// 链接

WXOptions.prototype.setLink = function(link) {

    this.link = link

    return this

}

// 图片地址

WXOptions.prototype.setImgUrl = function(imgUrl) {

    this.imgUrl = imgUrl

    return this

}

// 全局变量

var wxOptions = new WXOptions()

$(function() {

    var excludeTitles = ['', '']

    if (!excludeTitles.includes(document.title)) {

        wxOptions.init()

    }

})

上一篇 下一篇

猜你喜欢

热点阅读