edit.vue 5.0 KB
Newer Older
M
MicroMilo 已提交
1 2 3
<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
4 5
			<uni-forms-item name="name" label="用户名">
				<uni-easyinput v-model="formData.name"></uni-easyinput>
M
MicroMilo 已提交
6
			</uni-forms-item>
7 8
			<uni-forms-item name="icon" label="用户头像" @click="avatarChoose">
				<image :src="formData.icon" style="width: 120px; height: 80px;"></image>
M
MicroMilo 已提交
9
			</uni-forms-item>
10
			<uni-forms-item name="gender" label="性别">
M
MicroMilo 已提交
11 12
				<uni-data-checkbox :localdata="genders" v-model="formData.gender" />
				<!-- <uni-easyinput v-model="formData.gender"></uni-easyinput> -->
M
MicroMilo 已提交
13
			</uni-forms-item>
14 15
			<uni-forms-item name="password" label="密码">
				<uni-easyinput v-model="formData.password"></uni-easyinput>
M
MicroMilo 已提交
16
			</uni-forms-item>
17 18
			<uni-forms-item name="phone_num" label="手机号">
				<uni-easyinput v-model="formData.phone_num"></uni-easyinput>
M
MicroMilo 已提交
19
			</uni-forms-item>
20 21 22 23 24 25 26
			<uni-forms-item name="team_id" label="小组 id">
				<uni-easyinput v-model="formData.team_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="school" label="学校">
				<uni-easyinput v-model="formData.school"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="type" label="人员">
M
MicroMilo 已提交
27 28
					<uni-data-checkbox :localdata="types" v-model="formData.type" />
				<!-- <uni-easyinput v-model="formData.type"></uni-easyinput> -->
29 30
			</uni-forms-item>
			<uni-forms-item name="total_running_distance" label="跑步总距离">
0
052004122温宸杰 已提交
31
				<uni-number-box :value="0.0" :step="0.01" v-model="formData.total_running_distance" />
M
MicroMilo 已提交
32
				<!-- <uni-easyinput type="number" v-model="formData.total_running_distance"></uni-easyinput> -->
33 34
			</uni-forms-item>
			<uni-forms-item name="total_walking_distance" label="健走总距离">
0
052004122温宸杰 已提交
35
				<uni-number-box :value="0.0" :step="0.01" v-model="formData.total_walking_distance" />
M
MicroMilo 已提交
36
				<!-- <uni-easyinput type="number" v-model="formData.total_walking_distance"></uni-easyinput> -->
M
MicroMilo 已提交
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
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../../js_sdk/validator/mustgo-user.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'mustgo-user';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
69 70
			let formData = {
				"name": "",
M
MicroMilo 已提交
71
				"icon": "",
72 73 74 75 76 77 78 79 80
				"gender": "",
				"password": "",
				"phone_num": "",
				"team_id": "",
				"school": "",
				"type": "",
				"total_running_distance": 0,
				"total_walking_distance": 0
			}
M
MicroMilo 已提交
81
			return {
82 83 84
				// icon: [

				// ],
M
MicroMilo 已提交
85 86 87 88 89 90 91 92
				genders: [
					{text: '', value: ''},
					{text: '', value: ''}
				],
				types: [
					{text: '学生', value: '644a64c228064a7587cd79bf'},
					{text: '老师', value: '644a64d2e766bb0085e6048e'}
				],
M
MicroMilo 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
			avatarChoose() {
				let that = this;
				console.log("upload image loading")
				uniCloud.chooseAndUploadFile({
					type: 'image',
					success(res) {
						that.formData.icon = res["tempFiles"][0]["url"]
						console.log(res)
					},
					fail() {
						console.log("upload icon failed")
					},
					complete() {}
				});
			},
M
MicroMilo 已提交
158 159 160 161 162 163 164 165
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
166 167
				db.collection(dbCollectionName).doc(id).field(
					"name,icon,gender,password,phone_num,team_id,school,type,total_running_distance,total_walking_distance"
168
				).get().then((res) => {
M
MicroMilo 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>