From fe06d3bbee5733bf6ff0e23e8f4fd07c63fdd2b4 Mon Sep 17 00:00:00 2001 From: educoder_weapp Date: Mon, 11 May 2020 13:42:33 +0800 Subject: [PATCH] =?UTF-8?q?A=20=E5=A4=B4=E5=83=8F=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloudfunctions/openapi/config.json | 3 +- cloudfunctions/openapi/index.js | 48 +++--- .../profile/add-department/add-department.js | 4 +- .../account/pages/user_info/user_info.js | 1 - miniprogram/app.js | 28 +++- .../pages/course_invite/course_invite.js | 3 +- .../setting/pages/image_crop/image_crop.js | 143 +++++++++++++----- .../setting/pages/image_crop/image_crop.wxml | 9 +- .../setting/pages/image_crop/image_crop.wxss | 12 +- project.config.json | 6 + 10 files changed, 179 insertions(+), 78 deletions(-) diff --git a/cloudfunctions/openapi/config.json b/cloudfunctions/openapi/config.json index 7bcd3fc..4678407 100644 --- a/cloudfunctions/openapi/config.json +++ b/cloudfunctions/openapi/config.json @@ -3,7 +3,8 @@ "openapi": [ "wxacode.get", "wxacode.getUnlimited", - "security.msgSecCheck" + "security.msgSecCheck", + "security.imgSecCheck" ] } } \ No newline at end of file diff --git a/cloudfunctions/openapi/index.js b/cloudfunctions/openapi/index.js index 359b649..e17b789 100644 --- a/cloudfunctions/openapi/index.js +++ b/cloudfunctions/openapi/index.js @@ -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 }) diff --git a/miniprogram/account/pages/profile/add-department/add-department.js b/miniprogram/account/pages/profile/add-department/add-department.js index b09a97b..a680613 100644 --- a/miniprogram/account/pages/profile/add-department/add-department.js +++ b/miniprogram/account/pages/profile/add-department/add-department.js @@ -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({ diff --git a/miniprogram/account/pages/user_info/user_info.js b/miniprogram/account/pages/user_info/user_info.js index 26e27f7..b4e9fe8 100644 --- a/miniprogram/account/pages/user_info/user_info.js +++ b/miniprogram/account/pages/user_info/user_info.js @@ -8,7 +8,6 @@ Page({ this.setData({info:"获取中"}) wx.cloud.callFunction({name:"login"}) .then(res=>{ - ; this.setData(res.result); }).catch(e=>{ this.setData({info:"获取失败"}); diff --git a/miniprogram/app.js b/miniprogram/app.js index 0c2ff6d..0ce0297 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -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) }, diff --git a/miniprogram/course/pages/course_invite/course_invite.js b/miniprogram/course/pages/course_invite/course_invite.js index 89dc9a5..d240046 100644 --- a/miniprogram/course/pages/course_invite/course_invite.js +++ b/miniprogram/course/pages/course_invite/course_invite.js @@ -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}) diff --git a/miniprogram/setting/pages/image_crop/image_crop.js b/miniprogram/setting/pages/image_crop/image_crop.js index 00611a4..5e42dfa 100644 --- a/miniprogram/setting/pages/image_crop/image_crop.js +++ b/miniprogram/setting/pages/image_crop/image_crop.js @@ -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}) } } }) \ No newline at end of file diff --git a/miniprogram/setting/pages/image_crop/image_crop.wxml b/miniprogram/setting/pages/image_crop/image_crop.wxml index 85f9df1..fb9b803 100644 --- a/miniprogram/setting/pages/image_crop/image_crop.wxml +++ b/miniprogram/setting/pages/image_crop/image_crop.wxml @@ -1,14 +1,15 @@ - + + 轻触加载图片