cloudFunction.vue 4.3 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
					name: 'add',
					data: {
34 35
						product: 'uniCloud',
						create_time: Date.now()
study夏羽's avatar
study夏羽 已提交
36 37 38 39 40 41 42 43
					}
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: `成功添加一条数据,文档id为:${res.result.id}`,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
44
					return  res.result.id
study夏羽's avatar
study夏羽 已提交
45 46 47 48 49 50 51
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `添加数据失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
study夏羽's avatar
study夏羽 已提交
52
					return err
study夏羽's avatar
study夏羽 已提交
53 54
				})
			},
A
Anne_LXM 已提交
55
			async remove() {
study夏羽's avatar
study夏羽 已提交
56 57 58
				uni.showLoading({
					title: '处理中...'
				})
59
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
60 61 62 63 64 65 66 67
					name: 'remove'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: res.result.msg,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
68
					return res.result.msg
study夏羽's avatar
study夏羽 已提交
69 70 71 72 73 74 75
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `删除失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
76
					return err
study夏羽's avatar
study夏羽 已提交
77 78
				})
			},
A
Anne_LXM 已提交
79
			async update() {
study夏羽's avatar
study夏羽 已提交
80 81 82
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
83
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
84 85
					name: 'update',
					data: {
86 87
						product: 'uni-app',
						create_time: Date.now()
study夏羽's avatar
study夏羽 已提交
88 89 90 91 92 93 94 95
					}
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: res.result.msg,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
96
					return res.result.msg
study夏羽's avatar
study夏羽 已提交
97 98 99 100 101 102 103
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `更新操作执行失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
study夏羽's avatar
study夏羽 已提交
104
					return err
study夏羽's avatar
study夏羽 已提交
105 106
				})
			},
A
Anne_LXM 已提交
107
			async get() {
study夏羽's avatar
study夏羽 已提交
108 109 110
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
111
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
112 113 114 115 116 117 118 119
					name: 'get'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: `查询成功,获取数据列表为:${JSON.stringify(res.result.data)}`,
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
120
					return res.result.data
study夏羽's avatar
study夏羽 已提交
121 122 123 124 125 126 127
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `查询失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
128
					return err
study夏羽's avatar
study夏羽 已提交
129 130
				})
			},
A
Anne_LXM 已提交
131
			async useCommon() {
study夏羽's avatar
study夏羽 已提交
132
				console.log('请确保自己已经阅读并按照公用模块文档操作 https://uniapp.dcloud.io/uniCloud/cf-common')
A
Anne_LXM 已提交
133
				return await uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
134 135 136 137 138 139 140 141
					name: 'use-common'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: '云函数use-common返回结果:' + JSON.stringify(res.result),
						showCancel: false
					})
					console.log(res)
A
Anne_LXM 已提交
142
					return res.result
study夏羽's avatar
study夏羽 已提交
143 144 145 146 147 148 149
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `云函数use-common执行失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
A
Anne_LXM 已提交
150
					return err
study夏羽's avatar
study夏羽 已提交
151 152 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
				})
			},
			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>