cloudFunction.vue 4.1 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: {
27
			add() {
study夏羽's avatar
study夏羽 已提交
28 29 30
				uni.showLoading({
					title: '处理中...'
				})
31
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
					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)
				}).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
				})
			},
55
			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 68 69 70 71 72 73 74 75 76
					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)
				})
			},
77
			update() {
study夏羽's avatar
study夏羽 已提交
78 79 80
				uni.showLoading({
					title: '处理中...'
				})
81
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
					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)
study夏羽's avatar
study夏羽 已提交
102
					return err
study夏羽's avatar
study夏羽 已提交
103 104
				})
			},
105
			get() {
study夏羽's avatar
study夏羽 已提交
106 107 108
				uni.showLoading({
					title: '处理中...'
				})
109
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
					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)
				})
			},
127
			useCommon() {
study夏羽's avatar
study夏羽 已提交
128
				console.log('请确保自己已经阅读并按照公用模块文档操作 https://uniapp.dcloud.io/uniCloud/cf-common')
129
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
					name: 'use-common'
				}).then((res) => {
					uni.hideLoading()
					uni.showModal({
						content: '云函数use-common返回结果:' + JSON.stringify(res.result),
						showCancel: false
					})
					console.log(res)
				}).catch((err) => {
					uni.hideLoading()
					uni.showModal({
						content: `云函数use-common执行失败,错误信息为:${err.message}`,
						showCancel: false
					})
					console.error(err)
				})
			},
			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>