微信小程序 使用 uni-app uni-sec-check 内容安全检查

最近写了一个图片加水印的小程序

微信小程序提醒:

请接入并调用内容安全检测接口校验文本/图片是否含有敏感内容,加强提升小程序的信息安全防护能力,降低被恶意利用导致传播恶意内容的风险。

 官方文档

  1. 微信:文本内容安全识别 | 微信开放文档

  2. 微信:音视频内容安全识别 | 微信开放文档

  3. uni-app:uni-sec-check 公共模块 | uniCloud

拿到用户openid

自定义云函数

// 自定义云函数 cyc-wx-all uni-app uni-cloud 微信小程序下才能使用 
// 1.获取用户openid
'use strict';
const WeixinServer = {
	Code2Session_Url: 'https://api.weixin.qq.com/sns/jscode2session'
}
const wxApp = {
	appid: "wx6f2xxxxxxxxxx6e536",
	secret: "42ee01dxxxxxxxxxxxxxxxxf702e40c"
}
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const {
		action,
		params
	} = event;
	if (action === 'get_openid') {
		// 获取用户openId
		return uniCloud.httpclient.request(WeixinServer.Code2Session_Url, {
			dataType: 'json',
			data: {
				appid: wxApp.appid,
				secret: wxApp.secret,
				js_code: params.code,
				grant_type: 'authorization_code'
			}
		})
	} else if (action === 'img_is_upload') {
		// 判断图片是否还需上传
		return {
			status: 1
		}
	}
	// 其他方法...
};

通过uni-sec-check 内容安全检查

uni-app已经给出了云函数,我们直接调用就好

		// 检测文本
		checkText(text) {
			const openid = uni.getStorageSync('user_openid') || false;
			if (!openid) {
				uni.showModal({
					title: '温馨提醒',
					content: '用户openid不存储在'
				})
				return
			}
			// 请求云函数
			uniCloud.callFunction({
				name: 'content-sec-check',
				data: {
					content: text,
					openid: openid,
					scene: 2,
					version: 2
				},
				success: (res) => {
					if (res.result.errCode) {
						uni.showModal({
							title: res.result.errMsg,
							content: `涉嫌 ${res.result.result.label}`,
							showCancel: false,
							success: res => {
								this.errorChecktext()
							}
						})
					}
					if (res.result.errCode === 0) {
						// uni.showModal({
						// 	title: '检测结果',
						// 	content: '通过'
						// })
					}
				},
				fail(err) {
					uni.showModal({
						content: `请求服务失败,详细错误信息为:${err}`,
						showCancel: false
					})
				}
			})
		},

图片检查,有两个版本V1,V2 

		checkImageV1(url, version = 1) {
			const openid = uni.getStorageSync('user_openid') || false;
			if (!openid) {
				uni.showModal({
					title: '温馨提醒',
					content: '用户openid不存储在'
				})
				return
			}
			uniCloud.callFunction({
				name: 'media-sec-check',
				data: {
					image: url,
					mediaType: 'image',
					openid: openid,
					scene: 1,
					version: 1
				},
				success: (res) => {
					if (res.result.errCode) {
						uni.showModal({
							title: res.result.errMsg,
							content: `涉嫌 ${res.result.result?.label ?? '风险'}`,
							success: res => {
								this.errorCheckImg()
							}
						})
					}
					if (res.result.errCode === 0) {
						console.log("--img--检测结果--通过--")
						// uni.showModal({
						// 	title: '检测结果',
						// 	content: '通过'
						// })
					}
				},
				fail: (err) => {
					// uni.showModal({
					// 	content: `请求服务失败,详细错误信息为:${err}`,
					// 	showCancel: false
					// })
					this.errorCheckImg()
					console.log("--img--检测结果--请求服务失败--", err)
				}
			})
		}

微信小程序体验

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值