-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DEMO-OCR and DEMO-SEC in miniprogram
- Loading branch information
1 parent
def03dd
commit 1914d82
Showing
43 changed files
with
1,149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 【通用文字识别】小程序云开发项目实战 | ||
|
||
## 项目介绍 | ||
|
||
此处为项目完整代码,可以直接部署使用; | ||
|
||
- 初始化云开发环境,如果有多个云开发环境造成wx.cloud.init错误,则在app.js处进行环境定义。 | ||
- 将cloudfunctions文件夹内的4个云函数创建并部署 | ||
|
||
## 参考文档 | ||
|
||
- [云开发文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"permissions": { | ||
"openapi": [ | ||
"ocr.bankcard", | ||
"ocr.printedText", | ||
"ocr.idcard" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* 云函数:添加并识别图片 | ||
* 用途:使用云调用识别文字能力,将小程序端上传的图片进行文字识别,将文字图片信息上传云数据库进行保存。 | ||
*/ | ||
const cloud = require('wx-server-sdk') | ||
|
||
//云开发初始化 | ||
cloud.init(); | ||
|
||
//将云开发数据库能力声明给db | ||
const db = cloud.database(); | ||
|
||
//将云开发数据库command能力声明给 _ | ||
const _ = db.command; | ||
|
||
//云函数运行主函数,为异步函数 | ||
exports.main = async (event, context) => { | ||
|
||
//声明一个mess,用于承载函数执行结果用于返回 | ||
var mess = {}; | ||
try { | ||
/* 此代码段用于解析云存储File ID为url,用于之后的业务逻辑使用 | ||
event.img为小程序端的请求数据,为上传的图片fileid | ||
在正常业务中应该选择使用cloud.getTempFileURL()来获取临时url返回给用户 */ | ||
let first = event.img.indexOf('.'); | ||
let end = event.img.indexOf('/', first); | ||
let httpsrc = 'https://' + event.img.slice(first + 1, end) + '.tcb.qcloud.la/' + event.img.slice(end + 1, event.img.length); | ||
//解析云存储ID为url代码片段结束 | ||
|
||
//云调用能力,进行图片转换文字请求,相比于正常http请求,免除鉴权流程,整个代码逻辑更加轻便。 | ||
let result = null; | ||
|
||
try{ | ||
/*==================从这里开始修复==========================*/ | ||
/* 当result=-1时会在前端显示识别失败! */ | ||
/* result应该是文字识别的结果集,它应该使用云开发的云调用能力 */ | ||
result = -1; | ||
/* 我们将37行代码替换为以下代码,result获得文字识别云调用结果 */ | ||
|
||
// result = await cloud.openapi.ocr.printedText({ | ||
// type: 'photo', | ||
// imgUrl: httpsrc | ||
// }) | ||
|
||
/*==================到这里修复结束==========================*/ | ||
/* 当你修改完成后,在左边文件栏,右键点击此代码所在的文件夹 */ | ||
/* 在出现的列表里,点击【上传并部署:云端安装依赖】 */ | ||
/* 等待部署成功,你可以重新尝试上传图片操作,一定可以成功 */ | ||
} | ||
catch (err) { | ||
console.log(err); | ||
result = -1;//约定信息,表示识别无效 | ||
} | ||
|
||
//构建对象,承载云调用结果和图片信息 | ||
var obimg = {}; | ||
obimg.src = event.img;//图片地址 | ||
obimg.content = result;//识别结果 | ||
|
||
//根据用户的openid,存入上边识别的图片信息 | ||
await db.collection('list').where({ | ||
openid: event.userInfo.openId | ||
}).update({ | ||
data: { | ||
//_.push为云开发数据库中的command操作,含义为往数组中增加传入元素数据。 | ||
ocrlist: _.push([obimg]) | ||
} | ||
}); | ||
mess.code = 0; | ||
|
||
} | ||
catch (e) { | ||
//当发生错误时,如解构FileID等,将执行此代码段,code=-1为异常 | ||
console.log(e); | ||
mess.code = -1; | ||
mess.err = e; | ||
} | ||
//返回mess给前端 | ||
return mess; | ||
} |
14 changes: 14 additions & 0 deletions
14
miniprogram/tcb-demo-ocr/cloudfunctions/addimg/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "addimg", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"wx-server-sdk": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 云函数:初始化列表 | ||
* 用途:根据用户的唯一openid构建数据库文档,用于存储用户的信息;每次调用时都要返回存储的信息。 | ||
*/ | ||
const cloud = require('wx-server-sdk') | ||
|
||
//云开发初始化 | ||
cloud.init(); | ||
|
||
//将云开发数据库能力声明给db | ||
const db = cloud.database(); | ||
|
||
//云函数运行主函数,为异步函数 | ||
exports.main = async (event, context) => { | ||
|
||
//声明一个mess,用于承载函数执行结果用于返回 | ||
var mess = {}; | ||
try { | ||
|
||
//利用数据库where查找,数据集中openid为用户所属的文档 | ||
const userdata = (await db.collection('list').where({ | ||
openid: event.userInfo.openId | ||
}).get()).data; | ||
|
||
//如果length不等于0,则证明存在用户所属文档 | ||
if (userdata.length != 0) { | ||
|
||
//将用户的识别列表读取出来 | ||
mess.list = userdata[0].ocrlist; | ||
//将文档id取出,用于小程序端上传图片时的文件夹命名。由于安全性,不可以将openid传给小程序端 | ||
mess.id = userdata[0]._id; | ||
//正常标志code=0 | ||
mess.code = 0; | ||
} | ||
//如果length等于0,则没有用户文档需要创建 | ||
else { | ||
|
||
//使用数据库add增加,根据data传入的JSON对象进行构建,返回的为构建的信息,包含文档id | ||
let result = await db.collection('list').add({ | ||
data: { | ||
openid: event.userInfo.openId, | ||
ocrlist: [] | ||
} | ||
}); | ||
|
||
//将文档id取出 | ||
mess.id = result._id; | ||
//新建则识别列表为空 | ||
mess.list = []; | ||
//正常标志code=0 | ||
mess.code = 0; | ||
} | ||
} | ||
catch (e) { | ||
//当发生错误时,如解构FileID等,将执行此代码段,code=-1为异常 | ||
console.log(e); | ||
mess.code = -1; | ||
mess.err = e; | ||
} | ||
//返回mess给前端 | ||
return mess; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "init", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"wx-server-sdk": "latest" | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
miniprogram/tcb-demo-ocr/cloudfunctions/removeimg/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 云函数: 删除识别的图片 | ||
* 用途:根据图片地址,将云数据库保存的识别图片进行删除。 | ||
*/ | ||
const cloud = require('wx-server-sdk') | ||
|
||
//云开发初始化 | ||
cloud.init(); | ||
|
||
//将云开发数据库能力声明给db | ||
const db = cloud.database(); | ||
|
||
//将云开发数据库command能力声明给 _ | ||
const _ = db.command; | ||
|
||
//云函数运行主函数,为异步函数 | ||
exports.main = async (event, context) => { | ||
|
||
//声明一个mess,用于承载函数执行结果用于返回 | ||
var mess = {}; | ||
try { | ||
|
||
//利用数据库where查找,数据集中openid为用户所属的文档,然后使用update进行更新操作 | ||
const userdata = await db.collection('list').where({ | ||
openid: event.userInfo.openId | ||
}).update({ | ||
data: { | ||
//_.pull为command能力,含义为将数组中src为event.img的元素删除 | ||
ocrlist: _.pull({ | ||
src: event.img | ||
}) | ||
} | ||
}); | ||
//使用云存储能力,根据列表的fileid地址删除文件 | ||
await cloud.deleteFile({ | ||
fileList: [event.img] | ||
}) | ||
|
||
//在处理完全后,返回自定义的code码,表示一定的逻辑含义;在这里code=0为正常成功 | ||
mess.code = 0; | ||
} | ||
catch (e) { | ||
//当发生错误时,如解构FileID等,将执行此代码段,code=-1为异常 | ||
console.log(e); | ||
mess.code = -1; | ||
mess.err = e; | ||
} | ||
//返回mess给前端 | ||
return mess; | ||
} |
14 changes: 14 additions & 0 deletions
14
miniprogram/tcb-demo-ocr/cloudfunctions/removeimg/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "removeimg", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"wx-server-sdk": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"permissions": { | ||
"openapi": [ | ||
"ocr.bankcard", | ||
"ocr.printedText", | ||
"ocr.idcard" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* 云函数: 重新识别图片 | ||
* 用途:根据图片地址,重新使用云调用能力识别图片,并更新数据库的存储。 | ||
*/ | ||
const cloud = require('wx-server-sdk') | ||
|
||
//云开发初始化 | ||
cloud.init(); | ||
|
||
//将云开发数据库能力声明给db | ||
const db = cloud.database(); | ||
|
||
//将云开发数据库command能力声明给 _ | ||
const _ = db.command; | ||
|
||
//云函数运行主函数,为异步函数 | ||
exports.main = async (event, context) => { | ||
var mess = {}; | ||
try { | ||
//首先先删除数据库中的该元素 | ||
//利用数据库where查找,数据集中openid为用户所属的文档,然后使用update进行更新操作 | ||
const userdata = await db.collection('list').where({ | ||
openid: event.userInfo.openId | ||
}).update({ | ||
data: { | ||
//_.pull为command能力,含义为将数组中src为event.img的元素删除 | ||
ocrlist: _.pull({ | ||
src: event.img | ||
}) | ||
} | ||
}); | ||
|
||
/* 此代码段用于解析云存储File ID为url,用于之后的业务逻辑使用 | ||
event.img为小程序端的请求数据,为上传的图片fileid | ||
在正常业务中应该选择使用cloud.getTempFileURL()来获取临时url返回给用户 */ | ||
let first = event.img.indexOf('.'); | ||
let end = event.img.indexOf('/', first); | ||
let httpsrc = 'https://' + event.img.slice(first + 1, end) + '.tcb.qcloud.la/' + event.img.slice(end + 1, event.img.length); | ||
//解析云存储ID为url代码片段结束 | ||
|
||
//云调用能力,进行图片转换文字请求,相比于正常http请求,免除鉴权流程,整个代码逻辑更加轻便。 | ||
let result = null; | ||
try { | ||
result = await cloud.openapi.ocr.printedText({ | ||
type: 'photo', | ||
imgUrl: httpsrc | ||
}) | ||
} catch (err) { | ||
//当正常业务操作出现问题,比如云调用超过限制则执行此代码段 | ||
console.log(err); | ||
result = -1;//约定信息,表示识别无效 | ||
} | ||
|
||
//构建对象,承载云调用结果和图片信息 | ||
var obimg = {}; | ||
obimg.content = result; | ||
obimg.src = event.img; | ||
|
||
//根据用户的openid,存入上边识别的图片信息 | ||
await db.collection('list').where({ | ||
openid: event.userInfo.openId | ||
}).update({ | ||
data: { | ||
//_.push为云开发数据库中的command操作,含义为往数组中增加传入元素数据。 | ||
ocrlist: _.push([obimg]) | ||
} | ||
}); | ||
|
||
//在处理完全后,返回自定义的code码,表示一定的逻辑含义;在这里code=0为正常成功 | ||
mess.code = 0; | ||
//将重新识别的文字信息也返回,用于即时展示 | ||
mess.content = result; | ||
} | ||
catch (e) { | ||
//当发生错误时,如解构FileID等,将执行此代码段,code=-1为异常 | ||
console.log(e); | ||
mess.code = -1; | ||
mess.err = e; | ||
} | ||
//返回mess给前端 | ||
return mess; | ||
} |
15 changes: 15 additions & 0 deletions
15
miniprogram/tcb-demo-ocr/cloudfunctions/reorc/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "reorc", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"tcb-service-sdk": "^0.1.0", | ||
"wx-server-sdk": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
App({ | ||
onLaunch: function () { | ||
if (!wx.cloud) { | ||
console.error('请使用 2.2.3 或以上的基础库以使用云能力') | ||
} else { | ||
wx.cloud.init({ | ||
traceUser: true, | ||
}) | ||
} | ||
}, | ||
globalData: { | ||
id:null, | ||
nowimage:null | ||
} | ||
}) |
Oops, something went wrong.