A 头像安全检测

master
educoder_weapp 5 years ago
parent 8dc7062136
commit fe06d3bbee

@ -3,7 +3,8 @@
"openapi": [ "openapi": [
"wxacode.get", "wxacode.get",
"wxacode.getUnlimited", "wxacode.getUnlimited",
"security.msgSecCheck" "security.msgSecCheck",
"security.imgSecCheck"
] ]
} }
} }

@ -1,30 +1,40 @@
// 云函数入口文件
const cloud = require('wx-server-sdk') const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV}) cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV})
// 云函数入口函数
exports.main = async (event, context) => {
switch (event.action||event.name) { exports.main = async (event, context) => {
let {name, data} = event;
switch (event.name||event.action) {
case "wxacode.getUnlimited": case "wxacode.getUnlimited":
case "getWXACodeUnlimited":{ case "getWXACodeUnlimited":{
return getWXACodeUnlimited(event) return getWXACodeUnlimited(data)
} }
case "wxacode.get": case "wxacode.get":
case 'getWXACode': { case 'getWXACode': {
return getWXACode(event) return getWXACode(data)
} }
case "security.msgSecCheck":{ case "security.msgSecCheck":{
//return cloud.openapi.security.msgSecCheck(event.data);
try{ try{
var res = await cloud.openapi.security.msgSecCheck(event.data); var res = await cloud.openapi.security.msgSecCheck(data);
return res;
}catch(e){
return e;
}
}
case "security.imgSecCheck":{
try{
console.log(data);
var res = await cloud.openapi.security.imgSecCheck({
media:{
contentType:"image/jpg",
value:Buffer.from(data.media)
}
});
return res; return res;
}catch(e){ }catch(e){
return e; return e;
//e = new Error("输入内容有敏感词汇");
//e.code = e.errCode = 87014;
//throw e;
} }
} }
case 'getOpenData': { case 'getOpenData': {
@ -36,29 +46,23 @@ exports.main = async (event, context) => {
} }
} }
async function getWXACodeUnlimited(event){ async function getWXACodeUnlimited(data){
let {scene, page} = event; let {scene, page} = data;
const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({ const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({
scene, page scene, page
}) })
const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/) const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/)
const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg' const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg'
const uploadResult = await cloud.uploadFile({ return cloud.uploadFile({
cloudPath: `images/wxacode/${(page+"?"+scene).replace(/[\/?&]/g, "_")}.${fileExtension}`, cloudPath: `images/wxacode/${(page+"?"+scene).replace(/[\/?&]/g, "_")}.${fileExtension}`,
fileContent: wxacodeResult.buffer, fileContent: wxacodeResult.buffer,
}) })
if (!uploadResult.fileID) {
throw new Error(`upload failed with empty fileID and storage server status code ${uploadResult.statusCode}`)
}
return uploadResult.fileID
} }
async function getWXACode(event) { async function getWXACode(data) {
let {path} = event.data; let {path} = data;
const wxacodeResult = await cloud.openapi.wxacode.get({ const wxacodeResult = await cloud.openapi.wxacode.get({
path path
}) })

@ -39,15 +39,13 @@ Component({
wx.showLoading({ wx.showLoading({
title: '检查内容中...' title: '检查内容中...'
}) })
; app.callOpenapi({name:"security.msgSecCheck",data:{content},success:res=>{
app.openapi({name:"security.msgSecCheck",data:{content},success:res=>{
if(res.errCode==0){ if(res.errCode==0){
wx.showLoading({ wx.showLoading({
title: '正在添加' title: '正在添加'
}) })
app.api("add_department_applies")({school_id, name, remarks}) app.api("add_department_applies")({school_id, name, remarks})
.then(res=>{ .then(res=>{
;
this.triggerEvent("success",res); this.triggerEvent("success",res);
wx.hideLoading(); wx.hideLoading();
wx.showToast({ wx.showToast({

@ -8,7 +8,6 @@ Page({
this.setData({info:"获取中"}) this.setData({info:"获取中"})
wx.cloud.callFunction({name:"login"}) wx.cloud.callFunction({name:"login"})
.then(res=>{ .then(res=>{
;
this.setData(res.result); this.setData(res.result);
}).catch(e=>{ }).catch(e=>{
this.setData({info:"获取失败"}); this.setData({info:"获取失败"});

@ -12,11 +12,29 @@ App({
debug: config.debug debug: config.debug
}, },
client, client,
openapi({name, data, success, fail, complete}){ openapi(name){
return wx.cloud.callFunction({name:"openapi", data:{name, data}, return ({success, fail, complete, ...data})=>{
return this.callOpenapi({name, data, success,fail, complete});
}
},
callOpenapi({name, data, success, fail, complete}){
return this.callCloudapi({name:"openapi", data:{name, data}, success, fail, complete});
},
callCloudapi({name, data, success, fail, complete}){
return new Promise((resolve, reject)=>{
return wx.cloud.callFunction({name, data,
success:res=>{ success:res=>{
console.log(Date.now(), "success");
success&&success(res.result); success&&success(res.result);
}, fail, complete}); resolve(res.result);
complete&&complete(res.result);
}, fail:e=>{
fail&&fail(e);
reject(e);
complete&&complete(e);
}
});
});
}, },
realTimeLog:global.realTimeLog, realTimeLog:global.realTimeLog,
api(name, config) { return client.api(name, config) }, api(name, config) { return client.api(name, config) },

@ -21,10 +21,9 @@ Page({
this.setData({code_loaded:1}); this.setData({code_loaded:1});
}, },
onImgError(e){ onImgError(e){
;
let page = this.getPageUrl(); let page = this.getPageUrl();
let scene = this.getScene(); let scene = this.getScene();
wx.cloud.callFunction({ name: "openapi", data: { action:"getWXACodeUnlimited", name:"wxacode.getUnlimited", page, scene, data:{page, scene}}}) wx.cloud.callFunction({ name: "openapi", data: { name:"wxacode.getUnlimited", data:{page, scene}}})
.then(res=>{ .then(res=>{
this.setData({invite_code_url:""}); this.setData({invite_code_url:""});
this.setData({invite_code_url: res.result.fileID||res.result}) this.setData({invite_code_url: res.result.fileID||res.result})

@ -1,10 +1,16 @@
import WeCropper from '../../we-cropper/dist/we-cropper.min.js' import WeCropper from '../../we-cropper/dist/we-cropper.min.js'
const app = getApp(); const app = getApp();
Page({ Page({
data: { data: {
}, },
touchStart(e) { touchStart(e) {
if(!this.mycropper)
return wx.showToast({
title: '请先选取图片',icon:"none"
})
this.mycropper.touchStart(e) this.mycropper.touchStart(e)
}, },
@ -14,7 +20,53 @@ Page({
touchEnd(e) { touchEnd(e) {
this.mycropper.touchEnd(e) this.mycropper.touchEnd(e)
}, },
upload_avartar(filePath) { checkImg({path}){
if(this.promise&&path==this.tmp_path)
return this.promise;
let buffer = wx.getFileSystemManager().readFileSync(path);
this.promise = app.openapi("security.imgSecCheck")({media: buffer});
this.tmp_path = path;
this.promise.then(res=>{
console.log(res);
if(res.errCode==87014){
wx.showToast({
title:"检测所选的图片有敏感内容,请重新选择图片",icon: "none"
})
}
});
return this.promise;
},
onConfirm(){
if(!this.mycropper||!this.mycropper.src)
return wx.showToast({
title: '请选取图片',icon:"none"
})
let {src} = this.mycropper;
wx.showLoading({
title: '检查图片中...',
})
this.checkImg({path: src}).then(res=>{
if(res.errCode==0)
this.startUpload();
else if(res.errCode==87014){
wx.hideLoading();
wx.showModal({
content:"检测所选图片有敏感内容,请重新选择图片",showCancel: false
})
}else{
wx.showToast({
title: '发生了未知错误',icon:"none"
});
wx.reportMonitor('2', 1);
global.realTimeLog.error("未知错误 checkImg");
global.realTimeLog.setFilterMsg("unexpected");
}
}).catch(e=>{
wx.hideLoading();
app.showError(e);
})
},
uploadAvartar(filePath) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
wx.getFileSystemManager().readFile({ wx.getFileSystemManager().readFile({
filePath, filePath,
@ -27,63 +79,89 @@ Page({
}); });
}); });
}, },
getCropperImage() { startUpload() {
this.mycropper.getCropperImage((avatar) => { this.mycropper.getCropperImage((avatar) => {
if (avatar) { if (avatar) {
wx.showLoading({ wx.showLoading({
title: '上传中', title: '上传中',
}); });
this.upload_avartar(avatar).then(res => { this.uploadAvartar(avatar)
wx.navigateBack({ .then(res => {
delta: 1
});
wx.hideLoading(); wx.hideLoading();
wx.showToast({ wx.showToast({
title: '更改成功', title: '更改成功',
}); });
}).catch(error => { setTimeout(()=>{
wx.navigateBack({
delta: 1
});
},400);
}).catch(e => {
wx.showToast({ wx.showToast({
title: '失败', title: '上传失败',
icon: "none" icon: "none"
}); });
wx.hideLoading(); wx.hideLoading();
}); });
} else { } else {
wx.showToast({ wx.showToast({
title: '获取图片失败', title: '图片截取失败',
icon: "none" icon: "none"
}); });
} }
}) })
}, },
uploadTap() { chooseImage({navback=0}={}) {
const self = this
wx.chooseImage({ wx.chooseImage({
count: 1, count: 1,
success(res) { sizeType:["compressed"],
const src = res.tempFilePaths[0] success:(res) =>{
self.mycropper.pushOrign(src) let {path, size} = res.tempFiles[0];
if(!path&&navback){
wx.navigateBack({
delta:1
})
}
if(size>1e6){
// unexpected error
wx.reportMonitor('2', 1);
global.realTimeLog.error("头像超过大小",size);
global.realTimeLog.setFilterMsg("avatar");
wx.showToast({
title: '对不起,图片过大,请重新选择',icon:"none", duration:3000
});
return;
}
if(!this.mycropper)
this.initCanvas(path);
else{
this.mycropper.pushOrign(path)
}
},fail:e=>{
console.log(e);
if(navback)
wx.navigateBack({
delta:1
})
} }
}) })
}, },
initCrop(src) { initCanvas(src) {
const { cropperOpt } = this.data; const { cropperOpt } = this.data;
Object.assign(cropperOpt, { src }); Object.assign(cropperOpt, { src });
this.mycropper = new WeCropper(cropperOpt); this.mycropper = new WeCropper(cropperOpt);
this.mycropper this.mycropper
.on('beforeImageLoad', (ctx) => { .on('beforeImageLoad', (ctx) => {
wx.showLoading({title: '上传中'})
wx.showLoading({
title: '上传中'
})
}) })
.on('imageLoad', (ctx) => { .on('imageLoad', (ctx) => {
let {src} = this.mycropper;
this.checkImg({path:src});
wx.hideLoading(); wx.hideLoading();
}) })
.updateCanvas(); .updateCanvas();
}, },
onLoad(option) { setCanvasOptions:function(){
let device = wx.getSystemInfoSync(); let device = wx.getSystemInfoSync();
let width = device.windowWidth; let width = device.windowWidth;
let height = device.windowHeight - 42; let height = device.windowHeight - 42;
@ -101,27 +179,14 @@ Page({
} }
} }
this.setData({ cropperOpt}); this.setData({ cropperOpt});
},
onLoad(option) {
this.setCanvasOptions();
const { src } = option; const { src } = option;
if (src) { if (src) {
this.initCrop(src); this.initCanvas(src);
} else { } else {
wx.chooseImage({ this.chooseImage({navback:1})
count: 1,
success: res => {
const src = res.tempFilePaths[0];
if (src)
this.initCrop(src);
else
wx.navigateBack({
delta: 1
});
},
fail: e => {
wx.navigateBack({
delta: 1
});
}
})
} }
} }
}) })

@ -1,14 +1,15 @@
<import src="/avatar/we-cropper/dist/we-cropper.wxml" /> <import src="/setting/we-cropper/dist/we-cropper.wxml" />
<block wx:if="{{cropperOpt}}"> <block wx:if="{{cropperOpt}}">
<view class="cropper-wrapper"> <view class="cropper-wrapper">
<view class="tip">轻触加载图片</view>
<template is="we-cropper" data="{{...cropperOpt}}" /> <template is="we-cropper" data="{{...cropperOpt}}" />
</view> </view>
<view class="operations flex-row"> <view class="operations flex-row">
<button class="upload" type="main" plain="1" bindtap="uploadTap"> <button class="upload" type="main" plain="1" bindtap="chooseImage">
重选图片 重选图片
</button> </button>
<button type="main" class="getCropperImage" bindtap="getCropperImage"> <button type="main" class="getCropperImage" bindtap="onConfirm">
</button> </button>
</view> </view>
</block> </block>

@ -6,14 +6,24 @@ height: 100%
top: 0; top: 0;
left: 0; left: 0;
} }
.tip{
position: fixed;
color: #888;
font-size: 15px;
left: 50%;
top: 45%;
transform: translateX(-50%);
}
.operations{ .operations{
position: fixed; position: fixed;
bottom: 0; bottom: 0;
width: 100%; width: 100%;
height: 42px; height: 42px;
transform: translateX();
} }
.operations>button{ .operations>button{
flex: auto flex: auto;
border-radius: 0;
} }

@ -212,6 +212,12 @@
"pathName": "account/pages/account/account", "pathName": "account/pages/account/account",
"query": "exercise_id=6450", "query": "exercise_id=6450",
"scene": null "scene": null
},
{
"id": -1,
"name": "setting/pages/image_crop/image_crop",
"pathName": "setting/pages/image_crop/image_crop",
"scene": null
} }
] ]
} }

Loading…
Cancel
Save