U 优化信息安全检查速度

master
educoder_weapp 5 years ago
parent fe06d3bbee
commit 9d1cbbeee4

@ -5,6 +5,7 @@ cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV})
exports.main = async (event, context) => { exports.main = async (event, context) => {
console.log("调用云函数openapi, 参数event", event);
let {name, data} = event; let {name, data} = event;
switch (event.name||event.action) { switch (event.name||event.action) {
case "wxacode.getUnlimited": case "wxacode.getUnlimited":
@ -17,7 +18,9 @@ exports.main = async (event, context) => {
} }
case "security.msgSecCheck":{ case "security.msgSecCheck":{
try{ try{
console.log("检查文本安全, 参数:", data);
var res = await cloud.openapi.security.msgSecCheck(data); var res = await cloud.openapi.security.msgSecCheck(data);
console.log("返回结果: ", res);
return res; return res;
}catch(e){ }catch(e){
return e; return e;
@ -25,13 +28,21 @@ exports.main = async (event, context) => {
} }
case "security.imgSecCheck":{ case "security.imgSecCheck":{
try{ try{
console.log(data); console.log("检查图片安全, 参数:", data);
if(data.media)
var value = Buffer.from(data.media);
else if(data.fileID){
var {fileID} = data;
var res = await cloud.downloadFile({fileID});
var value = res.fileContent;
}
var res = await cloud.openapi.security.imgSecCheck({ var res = await cloud.openapi.security.imgSecCheck({
media:{ media:{
contentType:"image/jpg", contentType:"image/png",
value:Buffer.from(data.media) value
} }
}); });
console.log("返回结果: ", res);
return res; return res;
}catch(e){ }catch(e){
return e; return e;

@ -1,4 +1,23 @@
const app = getApp(); const app = getApp();
import {throttle} from "../../../../js/utils";
const risk_message = "检测到内容含有敏感词汇";
function msgSecCheck({name="", remarks=""}){
let content = name+","+ remarks;
console.log("msgsecCheck", content);
if(this.tmp_promise&&this.tmp_content==content)
return this.tmp_promise;
this.tmp_promise = app.openapi("security.msgSecCheck")({content});
this.tmp_content = content;
this.tmp_promise.then(res=>{
if(res.errCode==87014){
wx.showToast({
title: risk_message,icon:"none",duration:4000
})
}
})
return this.tmp_promise;
}
Component({ Component({
properties: { properties: {
show:{ show:{
@ -12,7 +31,9 @@ Component({
buttons:[ buttons:[
{text:"取消"}, {text:"取消"},
{text:"提交"} {text:"提交"}
] ],
name: "",
remarks:""
}, },
methods: { methods: {
cancel(){ cancel(){
@ -20,9 +41,11 @@ Component({
}, },
updateName({detail:{value}}){ updateName({detail:{value}}){
this.setData({name:value}); this.setData({name:value});
this.throttledMsgSecCheck(this.data);
}, },
updateRemarks({detail:{value}}){ updateRemarks({detail:{value}}){
this.setData({remarks:value}); this.setData({remarks:value});
this.throttledMsgSecCheck(this.data);
}, },
checkInput(){ checkInput(){
if(!this.data.name) if(!this.data.name)
@ -31,15 +54,16 @@ Component({
})&&false; })&&false;
return true; return true;
}, },
throttledMsgSecCheck:throttle(msgSecCheck, 1000, {}),
msgSecCheck,
submit(){ submit(){
if(!this.checkInput()) if(!this.checkInput())
return; return;
let {remarks, name, school_id} = this.data; let {remarks, name, school_id} = this.data;
let content = name + (remarks?""+remarks:'');
wx.showLoading({ wx.showLoading({
title: '检查内容中...' title: '检查内容中...'
}) })
app.callOpenapi({name:"security.msgSecCheck",data:{content},success:res=>{ this.msgSecCheck({name, remarks}).then(res=>{
if(res.errCode==0){ if(res.errCode==0){
wx.showLoading({ wx.showLoading({
title: '正在添加' title: '正在添加'
@ -58,12 +82,18 @@ Component({
}else{ }else{
wx.hideLoading(); wx.hideLoading();
wx.showToast({ wx.showModal({
title: '内容含有敏感词汇',icon:"none" content:risk_message,
}); showCancel:false
})
} }
}}); }).catch(e=>{
; //app.showError(e);
wx.showToast({
title: '出错了\n>_<',icon:"none"
})
throw e;
})
}, },
onTapButton({detail}){ onTapButton({detail}){
if(detail.index==0) if(detail.index==0)

@ -1,19 +1,16 @@
<mp-dialog show="{{show}}" title="添加子单位" bindbuttontap="onTapButton" buttons="{{buttons}}"> <mp-dialog show="{{show}}" title="添加子单位" bindbuttontap="onTapButton" buttons="{{buttons}}">
<view wx:if="{{show}}"> <view wx:if="{{show}}">
<form>
<view class="input-wrap"> <view class="input-wrap">
<text class="key">所属单位</text> <text class="key">所属单位</text>
<input class="value disable" disabled="1" value="{{school_name}}"></input> <input class="value disable" disabled="1" value="{{school_name}}"></input>
</view> </view>
<input hidden="1" name="school_id" disabled="1" value="{{scholl_id}}"></input>
<view class="input-wrap"> <view class="input-wrap">
<text class="key require">子单位名称</text> <text class="key require">子单位名称</text>
<input class="value" name="name" bindinput="updateName"></input> <input class="value" bindinput="updateName"></input>
</view> </view>
<view class="input-wrap"> <view class="input-wrap">
<text class="key">说明</text> <text class="key">说明</text>
<input class="value" name="remarks" bindinput="updateRemarks"></input> <input class="value" bindinput="updateRemarks"></input>
</view> </view>
</form>
</view> </view>
</mp-dialog> </mp-dialog>

@ -1,5 +1,24 @@
import {throttle} from "../../../js/utils";
const app = getApp(); const app = getApp();
var locationData = require("./data.js"); var locationData = require("./data.js");
const risk_message = "检测到名称中含有敏感词汇";
function msgSecCheck({name, nickname}){
let content = name+","+nickname;
console.log("secCheck", content);
if(this.tmp_promise&&this.tmp_content==content)
return this.tmp_promise;
this.tmp_promise = app.openapi("security.msgSecCheck")({content});
this.tmp_content = content;
this.tmp_promise.then(res=>{
if(res.errCode==87014){
wx.showToast({
title: risk_message,icon:"none",duration:4000
})
}
})
return this.tmp_promise;
}
Page({ Page({
data: { data: {
@ -23,6 +42,16 @@ Page({
this.showError(e); this.showError(e);
}); });
}, },
msgSecCheck,
throttledMsgSecCheck: throttle(msgSecCheck, 1000, {}),
updateNickName({detail:{value}}){
this.setData({nickname: value});
this.throttledMsgSecCheck(this.data);
},
updateName({detail:{value}}){
this.setData({name: value});
this.throttledMsgSecCheck(this.data);
},
setInfo(res){ setInfo(res){
let locations = Object.keys(locationData); let locations = Object.keys(locationData);
let location_index = locations.indexOf(res.location); let location_index = locations.indexOf(res.location);
@ -83,7 +112,6 @@ Page({
this.setData({identity_index:value, technical_index:-1}); this.setData({identity_index:value, technical_index:-1});
}, },
onTechnicalChange(e){ onTechnicalChange(e){
;
let { detail: { value } } = e; let { detail: { value } } = e;
this.setData({technical_index: value}); this.setData({technical_index: value});
}, },
@ -140,7 +168,6 @@ Page({
}, },
checkProInfo(value){ checkProInfo(value){
let info = this.originInfo; let info = this.originInfo;
;
if (info.identity != value.identity) if (info.identity != value.identity)
return true; return true;
if(info.identity=='student'){ if(info.identity=='student'){
@ -156,7 +183,6 @@ Page({
}, },
onSubmit(e){ onSubmit(e){
let {detail:{value}} = e; let {detail:{value}} = e;
;
if(!this.checkInput(value)) return; if(!this.checkInput(value)) return;
if(this.data.pro_authen&&this.checkProInfo(value)){ if(this.data.pro_authen&&this.checkProInfo(value)){
wx.showModal({ wx.showModal({
@ -168,8 +194,9 @@ Page({
this.saveInfo(value); this.saveInfo(value);
} }
}); });
}else }else{
this.saveInfo(value); this.saveInfo(value);
}
}, },
checkInput(value){ checkInput(value){
var showTip = key=>{ var showTip = key=>{
@ -204,20 +231,33 @@ Page({
}, },
saveInfo(value){ saveInfo(value){
value.gender = parseInt(value.gender); value.gender = parseInt(value.gender);
app.api("users.accounts", { method: "PUT" })(value) this.msgSecCheck(value).then(res=>{
.then(res => { if(res.errCode==87014){
app.syncUser({refresh:1}); wx.showModal({
res.message = "更新成功"; content: risk_message,showCancel:false
app.showMsg(res); })
; }else{
setTimeout(() => { app.api("users.accounts", { method: "PUT" })(value)
wx.navigateBack({ .then(res => {
delta: 1 app.syncUser({refresh:1});
}) res.message = "更新成功";
}, 420); app.showMsg(res);
}).catch(e => { setTimeout(() => {
this.showError(e); wx.navigateBack({
}) delta: 1
})
}, 420);
}).catch(e => {
this.showError(e);
})
}
}).catch(e=>{
wx.showToast({
title: '出错了\n︿',icon:"none"
});
throw e;
})
}, },
onTap(e){ onTap(e){
; ;

@ -11,14 +11,14 @@
</view> </view>
<view class="form-item"> <view class="form-item">
<text class="key">昵称头像</text> <text class="key">昵称头像</text>
<input class="value" id="nickname" placeholder="输入昵称" name="nickname" value="{{nickname}}"></input> <input class="value" id="nickname" placeholder="输入昵称" name="nickname" value="{{nickname}}" bindinput="updateNickName"></input>
<image class="avatar" bindtap="catchAvatar" src="{{eduImgDir}}{{avatar_url}}"></image> <image class="avatar" bindtap="catchAvatar" src="{{eduImgDir}}{{avatar_url}}"></image>
</view> </view>
<view class="form-item"> <view class="form-item">
<view class="key"> <view class="key">
<text>姓名</text> <text>姓名</text>
</view> </view>
<input class="value" id="name" placeholder="输入姓名" disabled="{{authen}}" name="name" value="{{name}}"></input> <input class="value" id="name" placeholder="输入姓名" bindinput="updateName" disabled="{{authen}}" name="name" value="{{name}}"></input>
</view> </view>
<view class="form-item"> <view class="form-item">
<text class="key">显示姓名 <text class="key">显示姓名

@ -24,7 +24,6 @@ App({
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject)=>{
return wx.cloud.callFunction({name, data, return wx.cloud.callFunction({name, data,
success:res=>{ success:res=>{
console.log(Date.now(), "success");
success&&success(res.result); success&&success(res.result);
resolve(res.result); resolve(res.result);
complete&&complete(res.result); complete&&complete(res.result);

@ -1,5 +1,6 @@
import WeCropper from '../../we-cropper/dist/we-cropper.min.js' import WeCropper from '../../we-cropper/dist/we-cropper.min.js'
const rich_message = "检测到所选的图片中有敏感内容,请选择其他图片";
const app = getApp(); const app = getApp();
Page({ Page({
@ -20,17 +21,26 @@ Page({
touchEnd(e) { touchEnd(e) {
this.mycropper.touchEnd(e) this.mycropper.touchEnd(e)
}, },
async _checkImg({path}){
let cloudPath = "images/avatars/"+ path.replace(/[\/\\:]/g, "_");
let res = await wx.cloud.uploadFile({
cloudPath,
filePath: path
});
let {fileID} = res;
return app.openapi("security.imgSecCheck")({fileID});
},
checkImg({path}){ checkImg({path}){
if(this.promise&&path==this.tmp_path) if(this.promise&&path==this.tmp_path)
return this.promise; return this.promise;
let buffer = wx.getFileSystemManager().readFileSync(path); let buffer = wx.getFileSystemManager().readFileSync(path);
this.promise = app.openapi("security.imgSecCheck")({media: buffer}); this.promise = this._checkImg({path});
this.tmp_path = path; this.tmp_path = path;
this.promise.then(res=>{ this.promise.then(res=>{
console.log(res); console.log(res);
if(res.errCode==87014){ if(res.errCode==87014){
wx.showToast({ wx.showToast({
title:"检测所选的图片有敏感内容,请重新选择图片",icon: "none" title:rich_message,icon: "none",duration:5000
}) })
} }
}); });
@ -39,7 +49,7 @@ Page({
onConfirm(){ onConfirm(){
if(!this.mycropper||!this.mycropper.src) if(!this.mycropper||!this.mycropper.src)
return wx.showToast({ return wx.showToast({
title: '请选取图片',icon:"none" title: '请选取图片',icon:"none"
}) })
let {src} = this.mycropper; let {src} = this.mycropper;
wx.showLoading({ wx.showLoading({
@ -51,19 +61,22 @@ Page({
else if(res.errCode==87014){ else if(res.errCode==87014){
wx.hideLoading(); wx.hideLoading();
wx.showModal({ wx.showModal({
content:"检测所选图片有敏感内容,请重新选择图片",showCancel: false content:rich_message,showCancel: false
}) })
}else{ }else{
wx.showToast({ wx.showToast({
title: '发生了未知错误',icon:"none" title: '发生了未知错误',icon:"none"
}); });
wx.reportMonitor('2', 1); wx.reportMonitor('2', 1);
global.realTimeLog.error("未知错误 checkImg"); global.realTimeLog.error("未知错误 checkImg1");
global.realTimeLog.setFilterMsg("unexpected"); global.realTimeLog.setFilterMsg("unexpected");
} }
}).catch(e=>{ }).catch(e=>{
wx.hideLoading(); wx.hideLoading();
app.showError(e); app.showError(e);
wx.reportMonitor('2', 1);
global.realTimeLog.error("未知错误 checkImg2");
global.realTimeLog.setFilterMsg("unexpected");
}) })
}, },
uploadAvartar(filePath) { uploadAvartar(filePath) {
@ -95,7 +108,7 @@ Page({
wx.navigateBack({ wx.navigateBack({
delta: 1 delta: 1
}); });
},400); },500);
}).catch(e => { }).catch(e => {
wx.showToast({ wx.showToast({
title: '上传失败', title: '上传失败',

Loading…
Cancel
Save