opendb-feedback.vue 3.1 KB
Newer Older
1 2 3
<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
4
			<uni-forms-item name="content" label="留言内容/回复内容" required>
5
				<textarea @input="binddata('content', $event.detail.value)" class="uni-textarea-border"
6
					v-model="formData.content" trim="right"></textarea>
7
			</uni-forms-item>
8 9 10
			<uni-forms-item name="imgs" label="图片列表">
				<uni-file-picker file-mediatype="image" :limit="6" return-type="array" v-model="formData.imgs">
				</uni-file-picker>
11
			</uni-forms-item>
12 13
			<uni-forms-item name="contact" label="联系人">
				<uni-easyinput v-model="formData.contact" trim="both"></uni-easyinput>
14
			</uni-forms-item>
15 16
			<uni-forms-item name="mobile" label="联系电话">
				<uni-easyinput v-model="formData.mobile" trim="both"></uni-easyinput>
17 18
			</uni-forms-item>
			<view class="uni-button-group">
19
				<button type="primary" class="uni-button" @click="submit">提交</button>
20 21 22 23 24 25 26 27
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
28
	} from '../../js_sdk/validator/opendb-feedback.js';
29 30 31 32
	const db = uniCloud.database();
	const dbCollectionName = 'opendb-feedback';

	function getValidator(fields) {
33
		let result = {}
34 35
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
36
				result[key] = validator[key]
37 38
			}
		}
39
		return result
40 41 42 43
	}

	export default {
		data() {
44 45 46 47 48 49
			let formData = {
				"content": "",
				"imgs": [],
				"contact": "",
				"mobile": ""
			}
50
			return {
51
				formData,
52 53
				formOptions: {},
				rules: {
54
					...getValidator(Object.keys(formData))
55 56
				}
			}
57
		},
58 59 60 61 62 63 64 65 66 67 68
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
69
				this.$refs.form.validate().then((res) => {
70
					this.submitForm(res)
71
				}).catch(() => {
72 73 74 75 76 77 78
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).add(value).then((res) => {
79 80 81 82 83 84
					uni.showToast({
						icon: 'none',
						title: '提交成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;
116

117 118 119 120 121 122 123 124 125
	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
126
		/* #ifndef APP-NVUE */
127
		display: flex;
128
		/* #endif */
129 130 131 132 133 134 135 136 137 138 139
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
140
</style>