• uniapp 微信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

  • 相关阅读:
    Highways(prim)
    Help Me with the Game(模拟)
    Parencodings
    The Pilots Brothers' refrigerator
    解决Geany 编辑器无法导入matplotlib包问题
    解决pycharm中导入自定义模块提示出错问题
    解决Pycharm中单元测试未发现问题(No tests were found)
    matplotlib设置中文的的一种方式
    matplotlib入门
    matplotlib入门
  • 原文地址:https://www.cnblogs.com/fogwang/p/15883031.html
Copyright © 2020-2023  润新知