cloudFunction.vue 4.4 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<template>
	<view class="content">
		<view class="title">基础示例-云函数</view>
		<view class="tips">
			<view>1.在uniCloud目录右键"云服务空间初始化向导..."</view>
			<view>2.没有服务空间,就点击新建创建一个</view>
			<view>3.选择要绑定的服务空间</view>
			<view>4.点击下一步,再点击开始部署</view>
		</view>
		<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>
			<button type="primary" @click="useCommon">使用公用模块</button>
			<button type="primary" @click="toRedisPage">使用Redis</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {
A
Anne_LXM 已提交
27
			async add() {
study夏羽's avatar
study夏羽 已提交
28 29 30
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
31
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
					name: 'add',
					data: {
						name: 'DCloud',
						subType: 'uniCloud',
						createTime: Date.now()
					}
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: `成功添加一条数据,文档id为:${res.result.id}`,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
45
					return  res.result.id
study夏羽's avatar
study夏羽 已提交
46 47 48 49 50 51 52
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `添加数据失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
study夏羽's avatar
study夏羽 已提交
53
					return err
study夏羽's avatar
study夏羽 已提交
54 55
				})
			},
A
Anne_LXM 已提交
56
			async remove() {
study夏羽's avatar
study夏羽 已提交
57 58 59
				uni.showLoading({
					title: '处理中...'
				})
60
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
61 62 63 64 65 66 67 68
					name: 'remove'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: res.result.msg,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
69
					return res.result.msg
study夏羽's avatar
study夏羽 已提交
70 71 72 73 74 75 76
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `删除失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
77
					return err
study夏羽's avatar
study夏羽 已提交
78 79
				})
			},
A
Anne_LXM 已提交
80
			async update() {
study夏羽's avatar
study夏羽 已提交
81 82 83
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
84
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
					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)
A
Anne_LXM 已提交
98
					return res.result.msg
study夏羽's avatar
study夏羽 已提交
99 100 101 102 103 104 105
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `更新操作执行失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
study夏羽's avatar
study夏羽 已提交
106
					return err
study夏羽's avatar
study夏羽 已提交
107 108
				})
			},
A
Anne_LXM 已提交
109
			async get() {
study夏羽's avatar
study夏羽 已提交
110 111 112
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
113
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
114 115 116 117 118 119 120 121
					name: 'get'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: `查询成功,获取数据列表为:${JSON.stringify(res.result.data)}`,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
122
					return res.result.data
study夏羽's avatar
study夏羽 已提交
123 124 125 126 127 128 129
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `查询失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
130
					return err
study夏羽's avatar
study夏羽 已提交
131 132
				})
			},
A
Anne_LXM 已提交
133
			async useCommon() {
study夏羽's avatar
study夏羽 已提交
134
				console.log('请确保自己已经阅读并按照公用模块文档操作 https://uniapp.dcloud.io/uniCloud/cf-common')
A
Anne_LXM 已提交
135
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
136 137 138 139 140 141 142 143
					name: 'use-common'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: '云函数use-common返回结果:' + JSON.stringify(res.result),
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
144
					return res.result
study夏羽's avatar
study夏羽 已提交
145 146 147 148 149 150 151
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `云函数use-common执行失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
152
					return err
study夏羽's avatar
study夏羽 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
				})
			},
			toRedisPage(){
				uni.navigateTo({
					url:'/pages/cloudFunction/redis/redis'
				})
			}
		}
	}
</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>