add.vue 4.4 KB
Newer Older
芊里 已提交
1 2 3
<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" :rules="rules" validate-trigger="submit" err-show-type="toast">
4
			<uni-forms-item name="content" label="反馈内容" required>
芊里 已提交
5 6 7 8 9
				<uni-easyinput placeholder="请输入反馈内容" type="textarea" v-model="formData.content" trim="right" />
			</uni-forms-item>
			<uni-forms-item name="imgs" label="反馈截图">
				<feedback-imgs :isChoose="true" :imgs="formData.imgs" @close="close" @chooseImg="chooseImg"></feedback-imgs>
			</uni-forms-item>
10
			<uni-forms-item name="contact" label="联系人" required>
芊里 已提交
11 12
				<uni-easyinput placeholder="请输入联系人" v-model="formData.contact" trim="both" />
			</uni-forms-item>
13
			<uni-forms-item name="mobile" label="联系方式" required>
芊里 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
				<uni-easyinput placeholder="请输入手机号/邮箱" v-model="formData.mobile" trim="both" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCollectionName = 'opendb-feedback';
	import feedbackImgs from '../../components/feedback-imgs';
	export default {
		components:{feedbackImgs},
		data() {
			return {
				formData: {
					"user_id": (uni.getStorageSync('userInfo') || {
						_id: ''
					})._id,
					"create_date": null,
					"content": "",
					"imgs": [],
					"is_reply": null,
					"feedback_id": "",
					"contact": "",
					"mobile": "",
					"reply_count": null
				},
				formOptions: {},
				rules: {
					content: {
						rules: [{
							required: true,
							errorMessage: '请填写反馈内容',
						}]
					},
					contact:{
						rules:[{
							required: true,
							errorMessage: '请填写联系人',
						}]
					},
					mobile:{
						rules:[{
							required: true,
							errorMessage: '请填写联系方式',
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			async submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.uploadImgs(res.imgs)
					.then(imgs=>{
						res.imgs = imgs;
						res.create_date = Date.now();
芊里 已提交
84 85
						
						res.is_reply = true;
芊里 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
						this.submitForm(res)
					});
				}).catch((errors) => {
					console.log(errors);
					uni.hideLoading()
				})
			},
			/**
			 * 上传图片
			 */
			async uploadImgs(imgs){
				let cloud_list = [];
				for (let i = 0; i < imgs.length; i++) {
					let img = await uniCloud.uploadFile({
						filePath: imgs[i],
						cloudPath: 'feedback.jpg'
					});
					cloud_list.push(img.fileID);
				}
				return cloud_list;
			},
			submitForm(value) {
				// 使用 clientDB 提交数据
109 110 111 112 113 114 115 116
				db.collection(dbCollectionName).add(value).then((res) => {
					uni.showModal({
						title: '您的反馈已经提交成功,请耐心等待。',
						showCancel: false,
						success: res => {
							uni.navigateBack()
						},
					});
芊里 已提交
117 118
				}).catch((err) => {
					uni.showModal({
119
						content: err.message || '反馈失败',
芊里 已提交
120 121 122 123 124 125 126 127 128 129 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 186 187 188 189 190 191 192 193 194 195 196 197 198
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			/**
			 * 关闭图片
			 * @param {Object} e
			 */
			close(e) {
				this.formData.imgs.splice(e, 1);
			},
			/**
			 * 选择图片
			 */
			chooseImg() {
				//选择图片
				uni.chooseImage({
					sourceType: ['camera', 'album'],
					sizeType: 'compressed',
					count: 5 - this.formData.imgs.length,
					success: res => {
						this.formData.imgs = this.formData.imgs.concat(res.tempFilePaths);
					}
				});
			},
		}
	}
</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;

	}

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

	.uni-button-group {
		margin-top: 50px;
		display: flex;
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
	
	.uni-button-group {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}
</style>