查看appid:https://mp.weixin.qq.com/wxamp/devprofile/get_profile?token=1973888604&lang=zh_CN
版本管理:https://mp.weixin.qq.com/wxamp/wacodepage/getcodepage?token=1973888604&lang=zh_CN
服务空间:https://unicloud.dcloud.net.cn/home
https://api.bspapp.com
https://vkceyugu.cdn.bspapp.com
如果打包时报错,就重启hbuilder
pages/index/index.vue:
<template>
<view class="content">
<view class="btn-list">
<button type="primary" @click="add">新增一条数据</button>
<button type="primary" @click="remove">删除一条数据</button>
<button type="primary" @click="update">修改数据</button>
<button type="primary" @click="get">查询前10条数据</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {}
},
methods: {
add() {
uni.showLoading({
title: '处理中...'
})
uniCloud.callFunction({
name: 'add',
data: {
name: 'DCloud',
subType: 'uniCloud',
createTime: Date.now()
}
}).then((res) => {
uni.hideLoading()
uni.showModal({
content: `成功添加一条数据,文档id为:${res.result.id}`,
showCancel: true
})
console.log(res)
}).catch((err) => {
uni.hideLoading()
uni.showModal({
content: `添加数据失败,错误信息为:${err.message}`,
showCancel: false
})
console.error(err)
})
},
remove() {
uni.showLoading({
title: '处理中...'
})
uniCloud.callFunction({
name: 'remove'
}).then((res) => {
uni.hideLoading()
uni.showModal({
content: res.result.msg,
showCancel: false
})
console.log(res)
}).catch((err) => {
uni.hideLoading()
uni.showModal({
content: `删除失败,错误信息为:${err.message}`,
showCancel: false
})
console.error(err)
})
},
update() {
uni.showLoading({
title: '处理中...'
})
uniCloud.callFunction({
name: 'update',
data: {
name: 'DCloud',
subType: 'html 5+',
createTime: Date.now()
}
}).then((res) => {
uni.hideLoading()
uni.showModal({
content: res.result.msg,
showCancel: false
})
console.log(res)
}).catch((err) => {
uni.hideLoading()
uni.showModal({
content: `更新操作执行失败,错误信息为:${err.message}`,
showCancel: false
})
console.error(err)
})
},
get() {
uni.showLoading({
title: '处理中...'
})
uniCloud.callFunction({
name: 'get'
}).then((res) => {
uni.hideLoading()
uni.showModal({
content: `查询成功,获取数据列表为:${JSON.stringify(res.result.data)}`,
showCancel: false
})
console.log(res)
}).catch((err) => {
uni.hideLoading()
uni.showModal({
content: `查询失败,错误信息为:${err.message}`,
showCancel: false
})
console.error(err)
})
}
}
}
</script>
<style>
.content {
padding-bottom: 30px;
}
.title {
font-weight: bold;
text-align: center;
padding: 20px 0px;
font-size: 20px;
}
.tips {
color: #999999;
font-size: 14px;
padding: 20px 30px;
}
.btn-list {
padding: 0px 30px;
}
.btn-list button {
margin-bottom: 20px;
}
.upload-preview {
width: 100%;
}
</style>
云函数
add/index.js
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const collection = db.collection('unicloud-test')
const res = await collection.add(event)
return res
};
add/get.js
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const collection = db.collection('unicloud-test')
const res = await collection.limit(10).get()
return res
};
app/remove.js
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const collection = db.collection('unicloud-test')
const docList = await collection.limit(1).get()
console.log("docList",docList)
if (!docList.data || docList.data.length === 0) {
return {
status: -1,
msg: '集合unicloud-test内没有数据'
}
}
const res = await collection.doc(docList.data[0]._id).remove()
console.log("res",res)
if (res.deleted === 1) {
return {
status: 0,
msg: '成功删除unicloud-test内第一条数据'
}
} else {
return {
status: -2,
msg: '删除数据失败'
}
}
};
app/update.js
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const collection = db.collection('unicloud-test')
const docList = await collection.limit(2).get();
console.log("docList2",docList)
if (!docList.data || docList.data.length === 0) {
return {
status: -1,
msg: '集合unicloud-test内没有数据'
}
}
const res = await collection.doc(docList.data[0]._id).update(event);
if (res.updated === 1) {
let result = Object.assign({}, {
_id: docList.data[0]._id
}, event)
return {
status: 0,
msg: `集合第一条数据由${JSON.stringify(docList.data[0])}修改为${JSON.stringify(result)}`
}
} else {
return {
status: -1,
msg: `集合unicloud-test内没有数据`
}
}
};