157 lines
4.8 KiB
Vue
157 lines
4.8 KiB
Vue
<template>
|
||
<view class="uni-container">
|
||
<uni-forms ref="form" :model="formData" validateTrigger="bind">
|
||
<uni-forms-item name="content" label="评论内容" required>
|
||
<uni-easyinput v-model="formData.content"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="root_id" label="">
|
||
<uni-easyinput placeholder="根评论ID(0表示自身是根评论)" v-model="formData.root_id"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="author_id" label="" required>
|
||
<uni-easyinput placeholder="作者ID(关联uni-id-users表)" v-model="formData.author_id"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="parent_id" label="">
|
||
<uni-easyinput placeholder="父评论ID(0表示无父评论)" v-model="formData.parent_id"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="article_id" label="" required>
|
||
<uni-easyinput placeholder="关联文章ID" v-model="formData.article_id"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="like_count" label="">
|
||
<uni-easyinput placeholder="点赞数" type="number" v-model="formData.like_count"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="create_time" label="" required>
|
||
<uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="update_time" label="">
|
||
<uni-datetime-picker return-type="timestamp" v-model="formData.update_time"></uni-datetime-picker>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="reply_count" label="">
|
||
<uni-easyinput placeholder="回复数" type="number" v-model="formData.reply_count"></uni-easyinput>
|
||
</uni-forms-item>
|
||
<uni-forms-item name="ip_location" label="">
|
||
<undefined v-model="formData.ip_location"></undefined>
|
||
</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/comment.js';
|
||
|
||
const db = uniCloud.database();
|
||
const dbCmd = db.command;
|
||
const dbCollectionName = 'comment';
|
||
|
||
function getValidator(fields) {
|
||
let result = {}
|
||
for (let key in validator) {
|
||
if (fields.includes(key)) {
|
||
result[key] = validator[key]
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
|
||
|
||
|
||
export default {
|
||
data() {
|
||
let formData = {
|
||
"content": "",
|
||
"root_id": "0",
|
||
"author_id": "",
|
||
"parent_id": "0",
|
||
"article_id": "",
|
||
"like_count": 0,
|
||
"create_time": null,
|
||
"update_time": null,
|
||
"reply_count": 0,
|
||
"ip_location": null
|
||
}
|
||
return {
|
||
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
|
||
})
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 获取表单数据
|
||
* @param {Object} id
|
||
*/
|
||
getDetail(id) {
|
||
uni.showLoading({
|
||
mask: true
|
||
})
|
||
db.collection(dbCollectionName).doc(id).field("content,root_id,author_id,parent_id,article_id,like_count,create_time,update_time,reply_count,ip_location").get().then((res) => {
|
||
const data = res.result.data[0]
|
||
if (data) {
|
||
this.formData = data
|
||
|
||
}
|
||
}).catch((err) => {
|
||
uni.showModal({
|
||
content: err.message || '请求服务失败',
|
||
showCancel: false
|
||
})
|
||
}).finally(() => {
|
||
uni.hideLoading()
|
||
})
|
||
}
|
||
}
|
||
}
|
||
</script>
|