wechat.js 2.9 KB
// import request from './request'; //笔者自己封装的网络请求类,也可以直接使用uni.request
var jweixin = require('jweixin-module');

export default {
	//初始化跳转小程序配置
	initJssdkXcx:function(){
		uni.request({
			url:'https://car.goorcar.com/api/h5/getJsSdkConfig',
			method:'post',
			data:{
				url:window.location.href.split('#')[0],
				api:'checkJsApi,wx-open-launch-weapp'
			},
			success: (res) => {
				console.log(res)
				if(res.data){
				    jweixin.config({
				        debug: false,
				        appId: res.data.data.appId,
				        timestamp: res.data.data.timestamp,
				        nonceStr: res.data.data.nonceStr,
				        signature: res.data.data.signature,
				        jsApiList: [
				            'wx-open-launch-weapp'
				        ],
						openTagList: ['wx-open-launch-weapp'] 
				    });
				}
			}
		})
	},
    //初始化分享sdk配置
    initJssdkShare: function(callback, url) {
        //服务端进行签名 ,可使用uni.request替换。 签名算法请看文档
		uni.request({
			url:'http://obituaries.t.brotop.cn/api/Common/getJsSdkConfig',
			method:'post',
			data:{
                url: url,
				api:'updateTimelineShareData,updateAppMessageShareData,getLocation'
            },
			success: (res) => {
				if (res.data) {
				    jweixin.config({
				        debug: false,
						appId: res.data.data.config.appId,
						timestamp: res.data.data.config.timestamp,
						nonceStr: res.data.data.config.nonceStr,
						signature: res.data.data.config.signature,
				        jsApiList: [
				            'checkJsApi',
				            'updateTimelineShareData',
				            'updateAppMessageShareData',
				            'getLocation'
				        ]
				    });
				    //配置完成后,再执行分享等功能
				    if (callback) {
				        callback(res.data.data.config);
				    }
				}
			}
		})
    },
    //在需要自定义分享的页面中调用
    share: function(callback,data, url) {
		let configUrl = url.split('#')[0]
        if (!this.isWechat()) {
            return;
        }
        //每次都需要重新初始化配置,才可以进行分享
        this.initJssdkShare(function(signData) {
            jweixin.ready(function() {
                var shareData = {
                    title: data && data.title ? data.title : signData.site_name,
                    desc: data && data.desc ? data.desc : signData.site_description,
                    link: url,
                    imgUrl: data && data.img ? data.img : signData.site_logo,
					//设置成功
                    success: function(res) {
						callback(res)
                    }
                };
                //分享给朋友接口
                // jweixin.updateAppMessageShareData(shareData);
                //分享到朋友圈接口
                jweixin.updateTimelineShareData(shareData);
            });
        }, configUrl);
    },
    
}