Skip to content

Commit

Permalink
docs: 增加云开发环境初始化文档
Browse files Browse the repository at this point in the history
  • Loading branch information
Honye committed Jul 25, 2023
1 parent e67a2fd commit 5340ecf
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ yarn # or npm install

#### 云开发环境

开通云开发环境后将项目 `/cloudfunctions/` 同步至云,修改 `app.js`
开通云开发环境后将项目 `/cloudfunctions/` 同步至云(参考 [云开发环境初始化](./docs/云开发环境初始化.md),修改 `app.js`

```javascript
wx.cloud.init({
Expand Down
102 changes: 102 additions & 0 deletions cloudbaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"version": "2.0",
"region": "sh",
"envId": "release-5g2g137xcedfade7",
"functionRoot": "./cloudfunctions",
"functions": [
{
"name": "app",
"runtime": "Nodejs16.13"
},
{
"name": "douban",
"runtime": "Nodejs16.13",
"triggers": [
{
"name": "CronDouban",
"type": "timer",
"config": "0 10 0 * * * *"
}
]
},
{
"name": "favCard",
"runtime": "Nodejs16.13"
},
{
"name": "fetch",
"runtime": "Nodejs16.13"
},
{
"name": "getCard",
"runtime": "Nodejs16.13"
},
{
"name": "getCards",
"runtime": "Nodejs16.13"
},
{
"name": "getCategories",
"runtime": "Nodejs16.13"
},
{
"name": "getFavCards",
"runtime": "Nodejs16.13"
},
{
"name": "initdb",
"runtime": "Nodejs16.13"
},
{
"name": "login",
"runtime": "Nodejs16.13"
},
{
"name": "nowPlaying",
"runtime": "Nodejs16.13"
},
{
"name": "showingSoon",
"runtime": "Nodejs16.13"
},
{
"name": "site",
"runtime": "Nodejs16.13",
"permissions": {
"openapi": [
"openapi.search.submitPages"
]
}
},
{
"name": "subscribeMessage",
"runtime": "Nodejs16.13"
},
{
"name": "subscription",
"runtime": "Nodejs16.13",
"triggers": [
{
"name": "trending",
"type": "timer",
"config": "*/10 * * * * * *"
}
],
"permissions": {
"openapi": [
"subscribeMessage.send"
]
}
},
{
"name": "wxacode",
"runtime": "Nodejs16.13",
"permissions": {
"openapi": [
"wxacode.getUnlimited",
"wxacode.get"
]
}
}
]
}
32 changes: 19 additions & 13 deletions cloudfunctions/initdb/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const cloud = require('wx-server-sdk')

cloud.init(
// {
// env: 'dv-963c46'
// }
)
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

const db = cloud.database()

Expand All @@ -13,6 +9,11 @@ const db = cloud.database()
*/
const initBanners = async () => {
console.log('*** start initBanners ***')
const collection = 'banners'
await db.createCollection(collection)
.catch((e) => {
// 集合已存在
})
const banners = [{
image: 'http://static.zhidao.manmankan.com/kimages/201609/26_1474884213331602.jpg',
type: 'image',
Expand All @@ -26,13 +27,13 @@ const initBanners = async () => {
},
]
let docLength = 0
await db.collection('banners').get().then(({
data
}) => {
docLength = data.length
})
await db.collection(collection)
.get()
.then(({ data }) => {
docLength = data.length
})
for (let i = 0, length = banners.length; i < length; ++i) {
await db.collection('banners').add({
await db.collection(collection).add({
data: {
id: docLength + i + 1,
createTime: db.serverDate(),
Expand Down Expand Up @@ -116,6 +117,11 @@ const initArticles = async () => {
*/
const initCards = async () => {
console.log('*** start init cards ***')
const collection = 'cards'
await db.createCollection(collection)
.catch(() => {
// 集合已存在
})
const cards = [
{
id: 11,
Expand Down Expand Up @@ -200,7 +206,7 @@ const initCards = async () => {
}
]
for (let i = 0, length = cards.length; i < length; ++i) {
await db.collection('cards').add({
await db.collection(collection).add({
data: {
createTime: db.serverDate(),
...cards[i]
Expand All @@ -216,7 +222,7 @@ const initCards = async () => {

exports.main = async(event, context) => {
console.log('*** 开始初始化 ***')
// await initBanners()
await initBanners()
// await initArticles()
await initCards()
console.log('*** 初始化结束 ***')
Expand Down
Binary file added docs/screenshots/cloudbase_env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/function_deploy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/function_deployed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/function_env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions docs/云开发环境初始化.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 云开发环境初始化

## 创建环境

登录腾讯云[云开发控制台](https://console.cloud.tencent.com/tcb/platform/env)

在环境管理创建一个环境,如果已有环境可跳过这一步

可以在网页端的控制台创建环境也可在微信开发者工具的云开发面板创建环境

![CloudBase Env](./screenshots/cloudbase_env.png)

## 部署云函数

可以使用微信开发者工具部署,也可以使用 CloudBase CLI 部署

微信开发者工具只能逐个部署,但稳

CloudBase CLI 可以批量部署,但可能会出现异常

### 使用微信开发者工具部署

右键项目根目录下 `/cloudfunctions/` 目录选择当前环境

![](./screenshots/function_env.png)

选择 `/cloudfunctions/` 目录下的一级目录创建并部署云函数

![](./screenshots/function_deploy.png)

### 使用 CloudBase CLI 部署

将项目根目录下的 `/cloudbaserc.json` 中的 `envId``region` 改为自己的环境 id 和地域

| 地域 | 全称 | 缩写 |
| :--- | :--- | :--- |
| 上海 | `ap-shanghai` | `sh` |
| 广州 | `ap-guangzhou` | `gz` |
| 北京 | `ap-beijing` | `bj` |

```json
{
"envId": "release-5g2g137xcedfade7",
"region": "sh"
}
```

全局安装 CloudBase CLI。可选择你喜欢的任意包管理工具,这里使用 pnpm 作为示例

```shell
pnpm add -g @cloudbase/cli
tcb -v
```

执行如下命令通过网页授权登录

```shell
tcb login
```

部署云函数

```shell
tcb fn deploy
```

云函数部署成功后微信开发者工具会显示为绿色

![云函数部署成功示意图](./screenshots/function_deployed.png)

## 初始化集合数据

终端进入 `/cloudfunctions/initdb/` 安装依赖

```shell
pnpm i
```

在微信开发者工具右键目录 `/cloudfunctions/initdb/` 开启云函数本地调试,手动调用一次云函数 `initdb` 即可

0 comments on commit 5340ecf

Please sign in to comment.