ss
jiangping
2025-07-01 89e540640b87f4be8656a5bc78f4a5dbcdf1c21f
admin/src/components/base/BaseOpera.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,131 @@
<script>
export default {
  name: 'BaseOpera',
  data () {
    return {
      title: '',
      visible: false,
      isWorking: false,
      // æŽ¥å£
      api: null,
      // é…ç½®æ•°æ®
      configData: {
        'field.id': 'id'
      }
    }
  },
  methods: {
    /**
     * é…ç½®
     *
     * @param extParams é…ç½®å‚æ•°
     */
    config (extParams = {}) {
      if (extParams == null) {
        throw new Error('Parameter can not be null of method \'config\' .')
      }
      if (extParams.api == null) {
        throw new Error('Missing config option \'api\'.')
      }
      this.api = require('@/api' + extParams.api)
      extParams['field.id'] && (this.configData['field.id'] = extParams['field.id'])
    },
    /**
     * æ‰“开窗口
     *
     * @param title çª—口标题
     * @param target è¡Œå¯¹è±¡ï¼ˆä»…编辑需该参数)
     */
    open (title, target) {
      this.title = title
      this.visible = true
      // æ–°å»º
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form[this.configData['field.id']] = null
        })
        return
      }
      // ç¼–辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
      })
    },
    /**
     * ç¡®è®¤ï¼ˆç‚¹å‡»ç¡®è®¤æŒ‰é’®åŽè§¦å‘)
     */
    confirm () {
      if (this.form[this.configData['field.id']] == null || this.form[this.configData['field.id']] === '') {
        this.__confirmCreate()
        return
      }
      this.__confirmEdit()
    },
    /**
     * ç¡®è®¤æ–°å»º
     *
     * @private
     */
    __confirmCreate () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        // è°ƒç”¨æ–°å»ºæŽ¥å£
        this.isWorking = true
        var that = this
        this.api.create(this.form)
          .then(() => {
            // that.$tip.apiSuccess('新建成功')
            // that.$dialog.actionConfirmButton('是否停留该页面继续新建数据?', '新建成功','继续添加','关闭返回')
            //   .then(() => {
                that.$refs.form.resetFields()
                that.visible = false
                that.form[this.configData['field.id']] = null
                that.$emit('success')
              // })
              // .catch(() => {
              //   that.visible = false
              //   that.$emit('success')
              // })
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    /**
     * ç¡®è®¤ä¿®æ”¹
     *
     * @private
     */
    __confirmEdit () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        // è°ƒç”¨æ›´æ–°æŽ¥å£
        this.isWorking = true
        this.api.updateById(this.form)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('修改成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    }
  }
}
</script>