A 头像安全检测

master
educoder_weapp 5 years ago
parent 8dc7062136
commit fe06d3bbee

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

@ -1,30 +1,40 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV})
// 云函数入口函数
exports.main = async (event, context) => {
switch (event.action||event.name) {
let {name, data} = event;
switch (event.name||event.action) {
case "wxacode.getUnlimited":
case "getWXACodeUnlimited":{
return getWXACodeUnlimited(event)
return getWXACodeUnlimited(data)
}
case "wxacode.get":
case 'getWXACode': {
return getWXACode(event)
return getWXACode(data)
}
case "security.msgSecCheck":{
//return cloud.openapi.security.msgSecCheck(event.data);
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;
}catch(e){
return e;
//e = new Error("输入内容有敏感词汇");
//e.code = e.errCode = 87014;
//throw e;
}
}
case 'getOpenData': {
@ -36,29 +46,23 @@ exports.main = async (event, context) => {
}
}
async function getWXACodeUnlimited(event){
let {scene, page} = event;
async function getWXACodeUnlimited(data){
let {scene, page} = data;
const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({
scene, page
})
const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/)
const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg'
const uploadResult = await cloud.uploadFile({
return cloud.uploadFile({
cloudPath: `images/wxacode/${(page+"?"+scene).replace(/[\/?&]/g, "_")}.${fileExtension}`,
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) {
let {path} = event.data;
async function getWXACode(data) {
let {path} = data;
const wxacodeResult = await cloud.openapi.wxacode.get({
path
})

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

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

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

@ -21,10 +21,9 @@ Page({
this.setData({code_loaded:1});
},
onImgError(e){
;
let page = this.getPageUrl();
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=>{
this.setData({invite_code_url:""});
this.setData({invite_code_url: res.result.fileID||res.result})

@ -1,10 +1,16 @@
import WeCropper from '../../we-cropper/dist/we-cropper.min.js'
const app = getApp();
Page({
data: {
},
touchStart(e) {
if(!this.mycropper)
return wx.showToast({
title: '请先选取图片',icon:"none"
})
this.mycropper.touchStart(e)
},
@ -14,7 +20,53 @@ Page({
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) => {
wx.getFileSystemManager().readFile({
filePath,
@ -27,63 +79,89 @@ Page({
});
});
},
getCropperImage() {
startUpload() {
this.mycropper.getCropperImage((avatar) => {
if (avatar) {
wx.showLoading({
title: '上传中',
});
this.upload_avartar(avatar).then(res => {
wx.navigateBack({
delta: 1
});
this.uploadAvartar(avatar)
.then(res => {
wx.hideLoading();
wx.showToast({
title: '更改成功',
});
}).catch(error => {
setTimeout(()=>{
wx.navigateBack({
delta: 1
});
},400);
}).catch(e => {
wx.showToast({
title: '失败',
title: '上传失败',
icon: "none"
});
wx.hideLoading();
});
} else {
wx.showToast({
title: '获取图片失败',
title: '图片截取失败',
icon: "none"
});
}
})
},
uploadTap() {
const self = this
chooseImage({navback=0}={}) {
wx.chooseImage({
count: 1,
success(res) {
const src = res.tempFilePaths[0]
self.mycropper.pushOrign(src)
sizeType:["compressed"],
success:(res) =>{
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;
Object.assign(cropperOpt, { src });
this.mycropper = new WeCropper(cropperOpt);
this.mycropper
.on('beforeImageLoad', (ctx) => {
wx.showLoading({
title: '上传中'
})
wx.showLoading({title: '上传中'})
})
.on('imageLoad', (ctx) => {
let {src} = this.mycropper;
this.checkImg({path:src});
wx.hideLoading();
})
.updateCanvas();
},
onLoad(option) {
setCanvasOptions:function(){
let device = wx.getSystemInfoSync();
let width = device.windowWidth;
let height = device.windowHeight - 42;
@ -101,27 +179,14 @@ Page({
}
}
this.setData({ cropperOpt});
},
onLoad(option) {
this.setCanvasOptions();
const { src } = option;
if (src) {
this.initCrop(src);
this.initCanvas(src);
} else {
wx.chooseImage({
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
});
}
})
this.chooseImage({navback: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}}">
<view class="cropper-wrapper">
<view class="tip">轻触加载图片</view>
<template is="we-cropper" data="{{...cropperOpt}}" />
</view>
<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 type="main" class="getCropperImage" bindtap="getCropperImage">
<button type="main" class="getCropperImage" bindtap="onConfirm">
</button>
</view>
</block>

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

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

Loading…
Cancel
Save