如图,我有多个发布信息页面,每个发布页面信息配置一个数据库,但是所有发布信息都需要执行内容检测,所有我的想法是把这些数据库集合放到一个带有内容检测的这个云函数中,求指教
我尝试了用if判断去实现,不知道这个if中的值该怎么传,我写了一段代码,会报错,第一个发布页面有报警,如图
代码如下:
云函数代码:
if(errcode == 0){
if(collection==recruit){
return await db.collection("recruit").add({
data:{
content:content,
author:author,
location:location,
varchar:varchar,
firstName:firstName,
position:position,
work:work,
education:education,
create_time:db.serverDate(),
}
})
}else if(collection==release-resume){
return await db.collection("release-resume").add({
data:{
content:content,
author:author,
education:education,
full_Name:full_Name,
Gender:Gender,
Age:Age
}
})
}
}else{
return await {"errcode":1,"errmsg":"您的内容有风险,请修改再发布!"}
};
页面JS代码:
/**
* 发布事件
*/
onSubmitEvent:function(event){
console.log(event);
const content = event.detail.value.content;
const author=app.globalData.userInfo;
wx.showLoading({
title:"正在发表中..."
})
// 特3456书yuuo莞6543李zxcz蒜7782法fgnv级
wx.cloud.callFunction({
name:"recruit",
data:{
content:content,
author:author,
full_Name:event.detail.value.full_Name,
Gender:event.detail.value.Gender,
Age:event.detail.value.Age
},
success:res => {
console.log(res);
const _id = res.result._id;
if(_id){
wx.hideLoading();
wx.showToast({
title:"恭喜!发送成功!"
});
// setTimeout(function(){
// wx.navigateBack({})
// },800)
}else{
wx.showToast({
title:res.result.errMsg,
})
}
}
})
},
这个报错跟执行多个没关系,你这个变量是干啥的?为啥没定义?
// 云函数入口文件
const cloud = require("wx-server-sdk";)
cloud.init()
const db = cloud.database();
const got = require("got";);
const APPID = "wx990cfc380f575596";
const APPSECRET = "16f932f1890ccc67d78a4ad1e327b2cf";
const TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"secret="+APPSECRET
const CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token="
// 云函数入口函数
exports.main = async (event, context) => {
const content = event.content;
const author = event.author;
const location = event.location;
const varchar = event.varchar;
const firstName = event.firstName;
const position = event.position;
const work = event.work;
const education = event.education;
const full_Name = event.full_Name;
const tokenResp = await got(TOKEN_URL);
const tokenBody = JSON.parse(tokenResp.body);
const token = tokenBody.access_token;
const chekResp = await got(CHECK_URL+token,{
body:JSON.stringify({
content:content,
})
});
const chekBody = JSON.parse(chekResp.body);
const errcode = chekBody.errcode;
if(errcode == 0){
if(collection==recruit){
return await db.collection("recruit").add({
data:{
content:content,
author:author,
location:location,
varchar:varchar,
firstName:firstName,
position:position,
work:work,
education:education,
create_time:db.serverDate(),
}
})
}else if(collection==release-resume){
return await db.collection("release-resume").add({
data:{
full_Name:full_Name
}
})
}
}else{
return await {”errcode“”:1,“”errmsg”:”您的内容有风险,请修改再发布!”}
};
}
<import src="releases/releases.wxml"/>
<view class="release-group">
<button class="btnborder" style="background-color:transparent" size="mini" wx:for=
{{releasess}}" wx:key="releases">
<view catchtap="toHotPush" data-index="{{index}}">
<template is="release-group" data="{{...item}}"></template>
</view>
</button>
</view>
toHotPush:function(event){
var index=event.currentTarget.dataset.index;
if(app.is_login()){
const tapIndex=tapIndex;
if(index===0){
wx.navigateTo({
url:”../release/release-recruit/release-recruit?type="+tapIndex,
})
}if(index===1){
wx.navigateTo({
url:"../release/release-resume/release-resume?type="+tapIndex,
})
}
if(index===2){
wx.navigateTo({
url:"../release/release-second-hand/release-second-hand?type="+tapIndex,
})
}
if(index===3){
wx.navigateTo({
url:"../release/release-carpooling/release-carpooling?type="+tapIndex,
})
}
if(index===4){
wx.navigateTo({
url:"../release/release-house-moving/release-house-moving?type="+tapIndex,
})
}
if(index===5){
wx.navigateTo({
url:"../release/release-mechanics/release-mechanics?type="+tapIndex,
})
}
}else{
wx.navigateTo({
url:";../login/login"
})
}
},