123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import BaseCurd from './BaseCurd'
- const operatorTypes = [{
- id: 1,
- name: '新增',
- method: 'POST'
- }, {
- id: 2,
- name: '修改',
- method: 'PUT'
- }, {
- id: 3,
- name: '查看'
- }]
- export default (Target) => ({
- name: 'BaseCurdEditor',
- extends: BaseCurd(Target),
- data: () => ({
- visible: false,
- data: null,
- oData: null,
- operatorTypeId: 1,
- refFormName: 'form',
- activeCollapseName: '1'
- }),
- computed: {
- operatorType () {
- return operatorTypes[this.operatorTypeId - 1]
- }
- },
- methods: {
- onOpen (data) {
- return Promise.resolve()
- },
- onGetDetail () {
- return Promise.resolve()
- },
- afterGetDetail (model) {},
- beforeSave () {
- return Promise.resolve()
- },
- onSave () {},
- open (data, isView) {
- this.onOpen(data).then(() => {
- if (data) {
- this.operatorTypeId = isView ? operatorTypes[2].id : operatorTypes[1].id
- this.oData = data
- this.getDetail(data)
- } else {
- this.operatorTypeId = operatorTypes[0].id
- this.model = this.initModel()
- this.clearFormValid()
- }
- this.visible = true
- })
- },
- initModel () {
- this.data = new Target(this)
- },
- getDetail (data) {
- this.onGetDetail().then(() => {
- this.$$request(this.$$api.getDetail, data, Target).then(data => {
- data = data || {}
- this.data = data
- this.clearFormValid()
- this.afterGetDetail(data)
- }).catch(console.error).finally(() => {})
- })
- },
- clearFormValid () {
- this.$nextTick(() => {
- let formEl = this.getForm()
- formEl && formEl.resetFields()
- })
- },
- save () {
- const formEl = this.getForm()
- formEl && formEl.validate(flag => {
- if (!flag) {
- return
- }
- this.beforeSave().then(() => {
- this.$$request(Target.$$api.save, this.data, this.operatorType.method, Target).then((data) => {
- if (data) {
- this.$notify({
- title: '成功',
- message: '保存成功',
- type: 'success',
- position: 'bottom-right'
- })
- this.onSave()
- this.$emit('saved', this.data)
- this.close()
- }
- }).catch(console.error).finally(() => {})
- })
- })
- },
- onSaveBtnClick () {
- this.save()
- },
- onCloseBtnClick () {
- this.close()
- },
- close () {
- this.visible = false
- this.$emit('close')
- },
- getForm () {
- return this.$refs[this.refFormName]
- }
- }
- })
|