Preface, recently I became interested in wechat cloud development. I picked up a wechat scanning code recognition book and uploaded it to the cloud database, which involves the following aspects of knowledge: small program registration account, the use of small program scanning CODE API, small program cloud development, etc
Register an applet account
First of all, through the mobile phone number or email register a small program account, get the AppID, open the cloud development permission, it should be noted that each small program/public number/small game needs an email, the same mobile phone number can only register 5, the same ID card can only register 5
Development of cloud
- You must register the wechat mini program and cannot use the test number
- Free quota, cloud development management background
Actual scanning code recognition books
1. Use api-wx.scancode () to scan the bar code on the back of the book to get the ISBN number
wx.scanCode({
success: (res) = >{
console.log(res.result) // The number of the book is ISBN}})Copy the code
2. After getting the title number, create a cloud function getbook, which is actually NodeJS. By using a method getBookInfo, take ISBN as parameter and decode it with the NPM package function doubanbook to get the cover_url and title of the book
async function getBookInfo(isbn) {
const url = 'https://search.douban.com/book/subject_search?search_text=' + isbn;
const res = await axios.get(url)
const reg = /window\.__DATA__ = "(.*)"/
if (reg.test(res.data)) {
const bookData = RegExp$1.const decryptedData = doubanbook(bookData)
console.log(decryptedData);
return decryptedData[0]}return res
}
Copy the code
3. Save the book information to the cloud database, connect to the cloud database books in the main method of the cloud function, save the isBN, title and cover_URL of the books, where ISBN is set as the primary key, so that the books will not be uploaded repeatedly
// Cloud function entry function
exports.main = async (event, context) => {
// Get the ISBN of the book
const {
isbn
} = event
// Cloud function database
const db = cloud.database()
const res = await getBookInfo(isbn)
db.collection('books').add({
data: {
isbn,
title: res.title,
cover_url: res.cover_url
}
})
// Get all the books in the database
const booksData = await db.collection('books').get()
return {
booksData: booksData.data,
title: res.title,
cover_url: res.cover_url
}
}
Copy the code
4. Finally, call the cloud function getBook method on the app side to optimize and display the pictures. It’s very simple
// scan code to identify books
btnScan() {
wx.scanCode({
success: res1= > {
// res.result is the format number ISBN
wx.cloud.callFunction({
name: 'getbook'.data: {
isbn: res1.result
},
success: async res2 => {
const booksData = res2.result.booksData;
const isbn = res1.result
// Repeat upload prompts
const isRepeat = booksData.every(item= >item.isbn ! == isbn)if(! isRepeat) { wx.showToast({title: 'Already exists in database'.icon: "error"})}this.setData({
title: res2.result.title,
cover_url: res2.result.cover_url
})
}
})
}
})
},
Copy the code
For the complete code, go to github to download the scan code book