cloudFunction.vue 4.2 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<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>
A
Anne_LXM 已提交
17 18 19 20 21 22

      <!-- #ifdef APP-PLUS || MP-WEIXIN -->
      <navigator url="../secure-network/cloud-function">
        <button type="primary">安全网络</button>
      </navigator>
      <!-- #endif -->
study夏羽's avatar
study夏羽 已提交
23 24 25 26 27 28 29 30 31 32
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {
A
Anne_LXM 已提交
33
			add() {
study夏羽's avatar
study夏羽 已提交
34 35 36
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
37
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
38 39
					name: 'add',
					data: {
40 41
						product: 'uniCloud',
						create_time: Date.now()
study夏羽's avatar
study夏羽 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
					}
				}).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)
				})
			},
A
Anne_LXM 已提交
59
			remove() {
study夏羽's avatar
study夏羽 已提交
60 61 62
				uni.showLoading({
					title: '处理中...'
				})
63
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
					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)
				})
			},
A
Anne_LXM 已提交
81
			update() {
study夏羽's avatar
study夏羽 已提交
82 83 84
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
85
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
86 87
					name: 'update',
					data: {
88 89
						product: 'uni-app',
						create_time: Date.now()
study夏羽's avatar
study夏羽 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
					}
				}).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)
				})
			},
A
Anne_LXM 已提交
107
			get() {
study夏羽's avatar
study夏羽 已提交
108 109 110
				uni.showLoading({
					title: '处理中...'
				})
A
Anne_LXM 已提交
111
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
					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)
				})
			},
A
Anne_LXM 已提交
129
			useCommon() {
study夏羽's avatar
study夏羽 已提交
130
				console.log('请确保自己已经阅读并按照公用模块文档操作 https://uniapp.dcloud.io/uniCloud/cf-common')
A
Anne_LXM 已提交
131
				uniCloud.callFunction({
study夏羽's avatar
study夏羽 已提交
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 186 187
					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>