uniapp云对象敏感词校验

目录

1.创建云对象

2.编辑文本

3.编写vue调用云函数检验

4.演示结果


1.创建云对象

复制代码
const FormData = require('form-data');

// 获取 access_token 的函数
async function getAccessToken(appid, secret) {
    const res = await uniCloud.httpclient.request(
        `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`,
        {
            method: 'GET',
            dataType: 'json'
        }
    );
    if (res.status === 200 && res.data.access_token) {
        return res.data.access_token;
    } else {
        console.log('获取 access_token 失败:', res.data.errmsg);
        return null;
    }
}

// 检测是否有敏感词
exports.checkText = async function(access_token, content) {
    const res = await uniCloud.httpclient.request(
        `https://api.weixin.qq.com/wxa/msg_sec_check?access_token=${access_token}`,
        {
            method: 'POST',
            data: {
                content: content
            },
            contentType: 'json',
            dataType: 'json'
        }
    );
    if (res.status === 200 && res.data.errcode === 0) {
        return {
            code: 0,
            msg: '没有敏感词'
        };
    } else if (res.data.errcode === 40001) {
        // access_token 无效,重新获取
        const newAccessToken = await getAccessToken('你的appid', '你的secret');
        if (newAccessToken) {
            return await this.checkText(newAccessToken, content);
        }
    }
    console.log('接口返回错误信息:', res.data.errmsg);
    return {
        code: -1,
        msg: '含有敏感词'
    };
};


exports.main = async function(params) {
    let newParams = params.params ? params.params[0] : params;
    if (newParams && newParams.action === 'getAccessToken') {
        return await getAccessToken(newParams.appid, newParams.secret);
    } else(newParams && newParams.action === 'checkText') {
        return await this.checkText(newParams.access_token, newParams.content);
    } 
    console.log('未匹配到正确的 action');
    return {
        code: -1,
        msg: '未指定正确的操作'
    };
};

2.编辑文本

基础代码

复制代码
<template>
	<view>
		<textarea placeholder="请输入..." maxlength="140" v-model="content"></textarea>

		<view class="footers" @click="submitBtn()">
			<view>提交</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				accessToken: ''
			}
		},
		onLoad() {

		},
		methods: {
			
			submitBtn() {
				if (this.content == '') {
					uni.showToast({
						title: '请输入反馈内容',
						icon: 'none',
						duration: 1000
					});
					return
				}
				
			}
		}
	}
</script>

3.编写vue调用云函数检验

复制代码
<template>
    <view class="wraps">
        <view class="boxs">
            <textarea placeholder="请输入..." maxlength="140" v-model="content"></textarea>
        </view>

        <view class="footers" @click="submitBtn()">
            <view>提交反馈</view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            content: '',
            appid: '你的appid',
            secret: '你的secret'
        };
    },
    onLoad() {},
    methods: {
        async submitBtn() {
			console.log('触发')
            if (this.content === '') {
                uni.showToast({
                    title: '请输入反馈内容',
                    icon: 'none',
                    duration: 1000
                });
                return;
            }
            const cloudObj = uniCloud.importObject('你的云对象名称');
            const res = await cloudObj.main({
                action: 'checkText',
                appid: this.appid,
                secret: this.secret,
                content: this.content
            });
            if (res.code === 0) {
               console.log('提交成功')
            } else {
               console.log('提交失败')
            }
        }
    }
};
</script>

4.演示结果

相关推荐
CHB4 小时前
别再问uni-app x蒸汽模式是否成熟了,已经有大量App案例上架应用商店了
uni-app
anyup2 天前
uView Pro X 1.0.0 发布:全面支持 uni-app x 蒸汽模式,80+ 组件
前端·uni-app·ai编程
zhongben123_2 天前
从零开发一个打车APP:技术选型、系统架构与成本评估指南
大数据·运维·ios·系统架构·uni-app·web app
天蒙蒙亮3 天前
打安卓apk包+“应用商城”发布
前端·uni-app
ModyQyW3 天前
vite-plugin-uni-pages 0.6:统一扫描规则,开发期少重启
前端·uni-app
Software攻城狮3 天前
【uniapp和普通web 端开发的区别】
前端·uni-app·notepad++
2501_915921433 天前
iOS 实时日志怎么看?KeyMob 的日志过滤、崩溃报告与符号化
android·ios·小程序·https·uni-app·iphone·webview
郑州光合科技余经理4 天前
同城小程序开发:同一笔单在用户端和商家端各显示到哪
android·java·开发语言·前端·后端·uni-app·php
普通人64 天前
uniapp模版JS
开发语言·javascript·小程序·uni-app·1024程序员节
wiseyao12194 天前
uniapp scroll-view滚动到最后
前端·javascript·uni-app