edit.vue 4.2 KB
Newer Older
M
MicroMilo 已提交
1 2 3 4
<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="owner_id" label="评论人id">
5
        <uni-easyinput placeholder="评论人id" v-model="formData.owner_id" disabled></uni-easyinput>
M
MicroMilo 已提交
6 7
      </uni-forms-item>
      <uni-forms-item name="date" label="发布时间">
8 9 10 11 12
        <!-- <uni-easyinput placeholder="评论发布时间" v-model="formData.date"></uni-easyinput> -->
		<uni-datetime-picker
						type="datetime"
						v-model="formData.date"
					/>
M
MicroMilo 已提交
13 14
      </uni-forms-item>
      <uni-forms-item name="likes" label="点赞数">
15 16
        <!-- <uni-easyinput placeholder="评论点赞数" type="number" v-model="formData.likes"></uni-easyinput> -->
		<uni-number-box :value="0" :step="1" max="100000" value="100000" v-model="formData.likes" />
M
MicroMilo 已提交
17 18
      </uni-forms-item>
      <uni-forms-item name="post_id" label="帖子id">
19
        <uni-easyinput placeholder="帖子id" v-model="formData.post_id" disabled></uni-easyinput>
M
MicroMilo 已提交
20
      </uni-forms-item>
M
MicroMilo 已提交
21
<!--      <uni-forms-item name="check_status" label="状态">
M
MicroMilo 已提交
22
        <switch @change="binddata('check_status', $event.detail.value)" :checked="formData.check_status"></switch>
M
MicroMilo 已提交
23
      </uni-forms-item> -->
M
MicroMilo 已提交
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 84 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 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 143 144 145
      <uni-forms-item name="content" label="评论内容">
        <uni-easyinput placeholder="评论内容" v-model="formData.content"></uni-easyinput>
      </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-post-comment.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mustgo-post-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 = {
        "owner_id": "",
        "date": "",
        "likes": null,
        "post_id": "",
        "check_status": null,
        "content": ""
      }
      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("owner_id,date,likes,post_id,check_status,content").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>