-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
236 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,7 @@ | ||
{ | ||
"permissions": { | ||
"openapi": [ | ||
"wxacode.get" | ||
] | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
cloudfunctions/quickstartFunctions/createCollection/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,56 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
|
||
const db = cloud.database(); | ||
|
||
// 创建集合云函数入口函数 | ||
exports.main = async (event, context) => { | ||
try { | ||
// 创建集合 | ||
await db.createCollection('sales'); | ||
await db.collection('sales').add({ | ||
// data 字段表示需新增的 JSON 数据 | ||
data: { | ||
region: '华东', | ||
city: '上海', | ||
sales: 11 | ||
} | ||
}); | ||
await db.collection('sales').add({ | ||
// data 字段表示需新增的 JSON 数据 | ||
data: { | ||
region: '华东', | ||
city: '南京', | ||
sales: 11 | ||
} | ||
}); | ||
await db.collection('sales').add({ | ||
// data 字段表示需新增的 JSON 数据 | ||
data: { | ||
region: '华南', | ||
city: '广州', | ||
sales: 22 | ||
} | ||
}); | ||
await db.collection('sales').add({ | ||
// data 字段表示需新增的 JSON 数据 | ||
data: { | ||
region: '华南', | ||
city: '深圳', | ||
sales: 22 | ||
} | ||
}); | ||
return { | ||
success: true | ||
}; | ||
} catch (e) { | ||
// 这里catch到的是该collection已经存在,从业务逻辑上来说是运行成功的,所以catch返回success给前端,避免工具在前端抛出异常 | ||
return { | ||
success: true, | ||
data: 'create collection success' | ||
}; | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
cloudfunctions/quickstartFunctions/fetchGoodsList/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,11 @@ | ||
const cloud = require('wx-server-sdk'); | ||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); | ||
exports.main = async (event, context) => { | ||
return { | ||
dataList: [ | ||
{ _id: '1', title: '微信气泡徽章', price: 1800 }, | ||
{ _id: '2', title: '微信地球鼠标垫', price: 5800 }, | ||
{ _id: '3', title: '微信黄脸大贴纸', price: 500 } | ||
], | ||
} | ||
}; |
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,18 @@ | ||
const cloud = require('wx-server-sdk'); | ||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); | ||
|
||
exports.main = async (event, context) => { | ||
const pagePath = event.pagePath; | ||
// 获取小程序二维码的buffer | ||
const resp = await cloud.openapi.wxacode.get({ | ||
path: pagePath, | ||
}); | ||
const { buffer } = resp; | ||
// 将图片上传云存储空间 | ||
const upload = await cloud.uploadFile({ | ||
cloudPath: String(pagePath).replaceAll('/', '_') + '.png', | ||
fileContent: buffer | ||
}); | ||
return upload.fileID; | ||
}; | ||
|
20 changes: 20 additions & 0 deletions
20
cloudfunctions/quickstartFunctions/getMiniProgramCode/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,20 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
|
||
// 获取小程序二维码云函数入口函数 | ||
exports.main = async (event, context) => { | ||
// 获取小程序二维码的buffer | ||
const resp = await cloud.openapi.wxacode.get({ | ||
path: 'pages/index/index' | ||
}); | ||
const { buffer } = resp; | ||
// 将图片上传云存储空间 | ||
const upload = await cloud.uploadFile({ | ||
cloudPath: 'code.png', | ||
fileContent: buffer | ||
}); | ||
return upload.fileID; | ||
}; |
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,17 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
|
||
// 获取openId云函数入口函数 | ||
exports.main = async (event, context) => { | ||
// 获取基础信息 | ||
const wxContext = cloud.getWXContext(); | ||
|
||
return { | ||
openid: wxContext.OPENID, | ||
appid: wxContext.APPID, | ||
unionid: wxContext.UNIONID, | ||
}; | ||
}; |
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,31 @@ | ||
const getOpenId = require('./getOpenId/index'); | ||
const getMiniProgramCode = require('./getMiniProgramCode/index'); | ||
const createCollection = require('./createCollection/index'); | ||
const selectRecord = require('./selectRecord/index'); | ||
const updateRecord = require('./updateRecord/index'); | ||
const sumRecord = require('./sumRecord/index'); | ||
const fetchGoodsList = require('./fetchGoodsList/index'); | ||
const genMpQrcode = require('./genMpQrcode/index'); | ||
|
||
// 云函数入口函数 | ||
exports.main = async (event, context) => { | ||
switch (event.type) { | ||
case 'getOpenId': | ||
return await getOpenId.main(event, context); | ||
case 'getMiniProgramCode': | ||
return await getMiniProgramCode.main(event, context); | ||
case 'createCollection': | ||
return await createCollection.main(event, context); | ||
case 'selectRecord': | ||
return await selectRecord.main(event, context); | ||
case 'updateRecord': | ||
return await updateRecord.main(event, context); | ||
case 'sumRecord': | ||
return await sumRecord.main(event, context); | ||
case 'fetchGoodsList': | ||
return await fetchGoodsList.main(event, context); | ||
case 'genMpQrcode': | ||
return await genMpQrcode.main(event, context); | ||
} | ||
}; | ||
|
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": "quickstartFunctions", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"wx-server-sdk": "~2.4.0" | ||
} | ||
} |
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 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
const db = cloud.database(); | ||
|
||
// 查询数据库集合云函数入口函数 | ||
exports.main = async (event, context) => { | ||
// 返回数据库查询结果 | ||
return await db.collection('sales').get(); | ||
}; |
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,18 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
const db = cloud.database(); | ||
const $ = db.command.aggregate; | ||
|
||
// 聚合记录云函数入口函数 | ||
exports.main = async (event, context) => { | ||
// 返回数据库聚合结果 | ||
return db.collection('sales').aggregate() | ||
.group({ | ||
_id: '$region', | ||
sum: $.sum('$sales') | ||
}) | ||
.end(); | ||
}; |
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,32 @@ | ||
const cloud = require('wx-server-sdk'); | ||
|
||
cloud.init({ | ||
env: cloud.DYNAMIC_CURRENT_ENV | ||
}); | ||
const db = cloud.database(); | ||
|
||
// 修改数据库信息云函数入口函数 | ||
exports.main = async (event, context) => { | ||
try { | ||
// 遍历修改数据库信息 | ||
for (let i = 0; i < event.data.length; i++) { | ||
await db.collection('sales').where({ | ||
_id: event.data[i]._id | ||
}) | ||
.update({ | ||
data: { | ||
sales: event.data[i].sales | ||
}, | ||
}); | ||
} | ||
return { | ||
success: true, | ||
data: event.data | ||
}; | ||
} catch (e) { | ||
return { | ||
success: false, | ||
errMsg: e | ||
}; | ||
} | ||
}; |