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