uni-app 微信JSSDK分享封装


1、安装jweixin-module

 

npm install jweixin-module --save

2、封装jssdk存为wxShare.js

const wx = require('weixin-js-sdk');

export const weixinShare=async function(option={},func)
			{
			 let ua = window.navigator.userAgent.toLowerCase();
			 if(!ua.match(/MicroMessenger/i) == 'micromessenger'){
			 return;
			 }
			 let res=await func("/hy.ashx",{act:"jsapi",'url':encodeURIComponent(location.href)},true);
	
				if(res.code==200)
				{
					let apiInfo=res.data;
					wx.config({
						debug:true, 
						appId:apiInfo.appId, 
						timestamp:apiInfo.timestamp,
						nonceStr:apiInfo.nonceStr, 
						signature:apiInfo.signature, 
						jsApiList: ['updateAppMessageShareData','updateTimelineShareData'],
						fail: function () {
						      location.reload();
						    }
					});
				wx.ready(function() {
                let shareData={ 
					title: option.title, // 分享标题
					//desc: option.desc, // 分享描述
					link:option.link.split("#")[0], // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl:option.img!=''?option.img: 'http://xx/static/images/logo.gif', // 分享图标
					success: function (response) {
					console.log(response);
					}
					};
					wx.updateAppMessageShareData(shareData);
					wx.updateTimelineShareData(shareData);
				});
					
				}
			}

 

封装ajax 

getParamsRequest 为 ajax.js
 
const BASE_URL = "https://xx.com/api"
/**
 * 封装get请求
 */
export const getParamsRequest = (url, params, showLoading,method="GET") => (
	new Promise((resolve, reject) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		let token=uni.getStorageSync('token');
		uni.request({
			url: `${BASE_URL}${url}`,
			method: method,
			data:params,
			header:{"content-type":"application/x-www-form-urlencoded","token":token},
			//header:{"token":token},
			dataType:"json",
			duration:3000,
			success: (res) => {
				let redirectUrl='index';
				try
				{
					redirectUrl=/(\/pages\/[a-z_/0-9?=&]+)$/.exec(location.href)[0];
				}
				catch(e){}
				if (res.statusCode == 200) {
		
					if(res.data.code==201)
					{
						uni.redirectTo({
							url:'/pages/member/login?redirect='+redirectUrl
						})
					}
					resolve(res.data);
				} else {
					reject("出错了");
				}

			},
			fail: (res) => {
				reject("出错了");
			},
			complete: () => {
				if (showLoading) {
					uni.hideLoading();
				}
			}
		})
	})
);

  

 

3、在页面中使用

引入

import {getParamsRequest from "../../static/js/ajax.js";
import {weixinShare} from "../../static/js/wxShare.js";
使用:

weixinShare({title:'测试',link:location.href.split("#")[0],img:'http://xxx/test.jpg'},getParamsRequest);

  

4、效果

 

 

 

相关资料:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM