視頻講解
小程式端
云函式
const cloud = require("wx-server-sdk");
cloud.init({
env: "xly-ba27v",
});
const db = cloud.database();
const todo = db.collection("test");
// 云函式入口函式
exports.main = async (event, context) => {
// console.lo
const wxContext = cloud.getWXContext();
return await todo
.add({
data: {
todo: event.todo,
_openid: wxContext.OPENID,
other: "云函式端資料",
},
})
.then((res) => {
console.log(res);
});
};
小程式端呼叫云函式
data: {
todo:""
},
primary:function(){
wx.cloud.callFunction({
name:"test",
data:{
todo:"吃飯"
}
}).then(res => {
// this.setData({todo:res.data})
console.log(res)
})
},
//獲取臨時檔案路徑
chooseMessageFile(){
const files = this.data.files
wx.chooseMessageFile({
count: 2,
success: res => {
console.log('選擇檔案之后的res',res)
let tempFilePaths = res.tempFiles
this.setData({ tempFilePaths: tempFilePaths })
console.log('選擇檔案之后的files', tempFilePaths)
}
})
},
// 將臨時檔案上傳到云存盤指定云檔案里
uploadFiles(e) {
const filePath = this.data.tempFilePaths[0].path
const cloudPath = `cloudbase/${Date.now()}-${Math.floor(Math.random(0, 1) * 1000)}` + filePath.match(/\.[^.]+?$/)
wx.cloud.uploadFile({
cloudPath,filePath
}).then(res => {
console.log(res)
}).catch(error => {
console.log("檔案上傳失敗",error)
})
},
小程式端視圖層
<button type="primary" bindtap="primary">primary</button>
<image
src=https://www.cnblogs.com/ycoder/p/"cloud://xly-ba27v.786c-xly-ba27v-1301545001/1584691592129-64.png"
>
web 端
環境準備
登陸騰訊云新建環境,使用免費版
云函式
安裝 cli 工具npm i -g @cloudbase/cli
cloudbase 命令可以簡寫成 tcb
查看版本tcb -v
嘗試登陸tcb login
初始化一個專案mkdir my-cloudbase-app,tcb init
部署函式tcb functions:deploy app
查看函式串列tcb functions:list
下載云函式cloudbase functions:download <functionName> [destPath]
默認情況下,函式代碼會下載到 functionRoot 下,以函式名稱作為存盤檔案夾,你可以指定函式存放的檔案夾地址,函式的所有代碼檔案會直接下載到指定的檔案夾中
查看函式詳情cloudbase functions:detail app,查看所有詳情cloudbase functions:detail
洗掉函式cloudbase functions:delete app,洗掉所有cloudbase functions:delete
賦值函式cloudbase functions:copy app app2
更新函式代碼cloudbase functions:code:update app
functions:code:update 命令和 functions:deploy 命令的主要區別是:functions:code:update 命令只會更新函式的代碼以及執行入口,不會修改函式的其他配置,而 functions:deploy 命令則會修改函式的代碼、配置
云存盤
上傳檔案cloudbase storage:upload localPath cloudPath,當 CLI 檢測到 localPath 為檔案夾時,會自動上傳檔案內的所有檔案,
下載檔案cloudbase storage:download cloudPath localPath,下載檔案夾cloudbase storage:download cloudPath localPath --dir
查看日志cloudbase functions:log app
洗掉檔案cloudbase storage:delete cloudPath,洗掉檔案夾cloudbase storage:delete cloudPath --dir
檔案串列cloudbase storage:list cloudPath
獲取檔案訪問鏈接cloudbase storage:url cloudPath
獲取檔案詳細資訊cloudbase storage:detail cloudPath
查看權限cloudbase storage:get-acl,設定權限cloudbase storage:set-acl
呼叫云函式
需要起一個服務在根目錄下npx server或http-server
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src=https://www.cnblogs.com/ycoder/p/"https://imgcache.qq.com/qcloud/tcbjs/1.3.5/tcb.js"></script>
<body>
<script>
const app = tcb.init({
env: "web-d68cb1",
});
app
.auth()
.signInAnonymously()
.then(() => {
alert("登錄云開發成功!");
});
app
.callFunction({
name: "test",
})
.then((res) => {
console.log("hahah", res);
});
</script>
