164 lines
4.6 KiB
Vue
164 lines
4.6 KiB
Vue
<template>
|
|
<view class="uni-container">
|
|
<uni-forms ref="form" :model="formData" validateTrigger="bind">
|
|
<uni-forms-item name="parent_id" label="父评论ID">
|
|
<uni-easyinput placeholder="用于嵌套评论" v-model="formData.parent_id"></uni-easyinput>
|
|
</uni-forms-item>
|
|
<uni-forms-item name="user_id" label="用户ID" required>
|
|
<uni-easyinput placeholder="关联uni-id-users表" v-model="formData.user_id"></uni-easyinput>
|
|
</uni-forms-item>
|
|
<uni-forms-item name="content" label="评论内容" required>
|
|
<uni-easyinput v-model="formData.content" trim="both"></uni-easyinput>
|
|
</uni-forms-item>
|
|
<uni-forms-item name="like_count" label="点赞数">
|
|
<uni-easyinput type="number" v-model="formData.like_count"></uni-easyinput>
|
|
</uni-forms-item>
|
|
<uni-forms-item name="create_time" label="创建时间">
|
|
<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="status" label="状态">
|
|
<uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
|
|
</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_operation_logs.js';
|
|
|
|
const db = uniCloud.database();
|
|
const dbCmd = db.command;
|
|
const dbCollectionName = 'comment_operation_logs';
|
|
|
|
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 = {
|
|
"parent_id": "",
|
|
"user_id": "",
|
|
"content": "",
|
|
"like_count": 0,
|
|
"create_time": {
|
|
"env": "now"
|
|
},
|
|
"update_time": {
|
|
"env": "now"
|
|
},
|
|
"status": "published"
|
|
}
|
|
return {
|
|
formData,
|
|
formOptions: {
|
|
"status_localdata": [
|
|
{
|
|
"value": "draft",
|
|
"text": "草稿"
|
|
},
|
|
{
|
|
"value": "published",
|
|
"text": "已发布"
|
|
},
|
|
{
|
|
"value": "deleted",
|
|
"text": "已删除"
|
|
}
|
|
]
|
|
},
|
|
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("parent_id,user_id,content,like_count,create_time,update_time,status").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>
|