















雲函數的使用
index.vue
<template>
<view class="content">
<!-- <image class="logo" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-62ae62c5-9a74-4911-a81a-e60651d1a985/bfcdf64a-3503-4c79-9d21-0fbac718664f.gif"></image> -->
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view class="">
<button type="default" @click="open">執行雲函數</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '傑帥666'
}
},
methods: {
open() {
// 前端代碼通過uniCloud.callFunction()方法調用雲函數
uniCloud.callFunction({
// name:雲函數的名字
name: 'get_list',
// 傳入參數
data: {
name: '呵呵',
age: 18
},
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
})
}
}
}
</script>
<style>
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
uniCloud-aliyun/cloudfunctions/get_list/index.js
'use strict';
// 運行在雲端(服務器端)的函數,在node環境執行
exports.main = async (event, context) => {
// event為客戶端上傳的參數
console.log('event : ', event)
// context:包含了調用信息、運行狀態,獲取每次調用的上下文
// 返回數據給客戶端
return {
code: 200,
status: '成功',
context,
event,
msg: `${event.name}的年齡是:${event.age}`
}
};
雲數據庫的添加和刪除

uniCloud-aliyun/cloudfunctions/get_list/index.js
'use strict';
const db = uniCloud.database()
const collection = db.collection('user') // 獲取集合的引用
// 運行在雲端(服務器端)的函數,在node環境執行
exports.main = async (event, context) => {
// event為客戶端上傳的參數
console.log('event : ', event)
// const collection = await db.collection('user') // 獲取集合的引用
// 要寫成數組的形式,只傳單個對象,也要寫成數組的形式,否則添加數據無效
let res = await collection.add([{
name: 'html',
age: 333
},
{
name: '前端',
age: 555
}
])
console.log('數據插入:');
console.log(JSON.stringify(res));
// doc:獲取對該集合中指定 id 的記錄的引用
// remove:刪除記錄(觸發請求)
const res2 = await collection.doc('602a55affac28b0001ab7a80').remove()
console.log(res2);
// 返回數據給客戶端
return {}
};
查詢

uniCloud-aliyun/cloudfunctions/get_list/index.js
'use strict';
const db = uniCloud.database()
// const collection = db.collection('user') // 獲取集合的引用
// 運行在雲端(服務器端)的函數,在node環境執行
exports.main = async (event, context) => {
const collection = db.collection('user') // 獲取集合的引用
// event為客戶端上傳的參數
console.log('event--- ', event)
// ----------------添加數據------------------------
// 要寫成數組的形式,只傳單個對象,也要寫成數組的形式,否則添加數據無效
const res = await collection.add([{
name: 'html',
age: 333
},
{
name: '前端',
age: 555
}
])
console.log('數據插入:');
console.log(JSON.stringify(res));
// ----------------刪除數據------------------------
// doc:獲取對該集合中指定 id 的記錄的引用
// remove:刪除記錄(觸發請求)
const res2 = await collection.doc('602a55affac28b0001ab7a80').remove()
console.log(res2);
// ---------------更新、添加數據-------------------------
// update:只能更新存在的記錄
// set: 記錄存在就更新,不存在就增加
const res3 = await collection.doc('602a560db6ce210001ac6af6').update({
name: 'vue',
age: 666,
job: 'coder'
})
console.log(res3);
const res5 = await collection.doc('123456').set({
name: 'css',
age: 777,
job: 'HR'
})
console.log(res5);
// ----------------查詢數據------------------------
const res6 = await collection.doc('602a5accd6547d00017f34e4').get()
console.log(res6); // { affectedDocs: 1, data: [ { _id: '602a5accd6547d00017f34e4', name: '前端', age: 555 } ] }
const res7 = await collection.where({ name: 'uniapp' }).get()
console.log(res7); // { affectedDocs: 1, data: [ { _id: '602a3f2fd6547d00017ee0b8', name: 'uniapp' } ] }
// 查詢字段通過客戶端傳入,使用event
const res8 = await collection.where({
"name": 'html'
}).get()
console.log(JSON.stringify(res8)); // {"affectedDocs":1,"data":[{"_id":"602a43e75dc5370001d7cf78","name":"html","age":333}]}
// 返回數據給客戶端
return {
code: 200,
msg: '成功',
data: res8.data
}
};

使用雲存儲上傳文件
<template>
<view class="content">
<image class="logo" :src="src"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
<view class="">
<button type="default" @click="open">上傳圖片</button>
</view>
<view class="">
<button type="default" @click="deleteImg">刪除圖片</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '傑帥666',
src: ''
}
},
methods: {
open() {
// 上傳圖片
uni.chooseImage({
count: 1,
success: res => {
// console.log('chooseImage---', res);
const tempFilePath = res.tempFilePaths[0]
// console.log('chooseImage---', tempFilePath);
// 注意,要使用uniCloud.uploadFile,而不是uni.uploadFile
uniCloud.uploadFile({
filePath: tempFilePath,
cloudPath: 'a.jpg',
success: res => {
// console.log('uploadFile---', res);
this.src = res.fileID
},
fail: rej => {
console.log('uploadFile---', rej);
}
})
},
fail: rej => {
console.log('chooseImage---', rej);
}
})
},
// 刪除圖片 【1、瀏覽器控制台輸出err:沒有權限刪除;2、阿里雲不支持then調用。】
deleteImg() {
uniCloud.deleteFile({
// fileList: ['20433d2a-4e11-43fb-823a-66d1b83b679d'],
// 文件的下載地址
fileList: ['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-62ae62c5-9a74-4911-a81a-e60651d1a985/25ef0b4c-e4a7-4172-b889-9a74d1bd4a62.jpg'],
success: res => {
console.log('deleteImg---', res);
},
fail: rej => {
console.log('deleteImg---', rej);
}
})
}
}
}
</script>
<style>
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>