微信小程序内容安全验证图片和文字

import Ctpic from '@/common/custom-picture.js'; 
文档查看 http://hhyang.cn/doc4/custom-picture/applet_URLtoPath.html
const _ctpic = new Ctpic();

function wxhelper() {
	let ACCESS_TOKEN = {
		token: '',
		expirationTimeStamp: 0
	};
	this.imgSecCheck = function(imgpath, webobj, callback) {

		let tok = getToken(webobj);
		tok.then(function(token) {
			console.log("token", token)
			if (token == '') {
				callback({
					"errcode": 404
				});
			}

			let basic = _ctpic.applet_appendFile({
				path: imgpath,
				isNet: true, //是否网络地址
				format:'png'
			});
			basic.then(function(e) {
				let blob = _ctpic.applet_URLtoPath({
					base64: e,
				})
				blob.then(function(file) {
					
					uni.request({
						url: 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' + token,
						method: 'POST',
						data: {
							media: {
								contentType: 'image/jpeg',
								value: file
							}
						},
						header: {
							'Content-Type': 'application/octet-stream'
						},
						success: (res) => {
							callback(res.data);
						},
						fail: () => {
							callback({
								"errcode": 404
							});
						},


					});
					//callback(res);
				})


			})
		});




	}

	this.msgSecCheck = function(msgs, webobj, callback) {
		let tok = getToken(webobj);
		tok.then(function(token) {
			console.log("token", token)

			if (token == '') {
				callback({
					"errcode": 404
				});
			}

			
			uni.request({
				url: 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' + token,
				method: 'POST',
				data: {
					content: msgs
				},
				// header: {
				// 	'Content-Type': 'application/octet-stream'
				// },
				success: (res) => {
					callback(res.data);
				},
				fail: () => {
					callback({
						"errcode": 404
					});
				},


			});
		})


	}

	function getToken(webobj) {
		let _this = this;
		return new Promise((resolve, reject) => {

			const sto_token = uni.getStorageSync('wx_access_token');
			if (sto_token) {
				ACCESS_TOKEN = sto_token;

			}

			if (ACCESS_TOKEN.expirationTimeStamp < (+new Date())) {
				//取服务器最新ACCESS_TOKEN
				uni.request({
					url: webobj.webapi + '/api/WXImg/GetToken',
					method: 'GET',
					header: {
						'Authorization': webobj.authtoken
					}

				}).then(data => { //data为一个数组,数组第一项为错误信息,第二项为返回数据
					var [error, res] = data;
					if (res.data.IsSuccess) {
						ACCESS_TOKEN.token = res.data.Data.access_token
						ACCESS_TOKEN.expirationTimeStamp = res.data.Data.TimeStamp
						//console.log("newtoken",ACCESS_TOKEN)
						uni.setStorageSync('wx_access_token', ACCESS_TOKEN);
						resolve(ACCESS_TOKEN.token);
					} else {
						reject('');
					}

				})
			} else {
				resolve(ACCESS_TOKEN.token);
			}

		})



	}



}
module.exports = wxhelper;

使用方式

引用了custom-picture.js,查看http://hhyang.cn/doc4/custom-picture/applet_URLtoPath.html

    import wxhelper from '@/common/wxhelper.js';

let _wxhelper=new wxhelper();

 _wxhelper.imgSecCheck(path,{webapi:_this.webapi,authtoken:_this.authtoken},function(e){
                                         console.log(e);

})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值