song-operation.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import UploadFile from '@/components/upload-file'
  2. import deepClone from '@/utils/deep.clone'
  3. import { NForm, NInput, NSpace, NButton, useMessage, NFormItem } from 'naive-ui'
  4. import { defineComponent, onMounted, PropType, reactive, ref } from 'vue'
  5. import { subjectSave, subjectUpdate } from '../api'
  6. export default defineComponent({
  7. name: 'role-operation',
  8. props: {
  9. type: {
  10. type: String,
  11. default: 'add'
  12. },
  13. applyList: {
  14. type: Array as PropType<any>,
  15. default: () => []
  16. },
  17. data: {
  18. type: Object as PropType<any>,
  19. default: () => {}
  20. }
  21. },
  22. emits: ['close', 'getList'],
  23. setup(props, { slots, attrs, emit }) {
  24. const forms = reactive({
  25. name: null,
  26. code: null,
  27. img: null,
  28. parentId: 0
  29. })
  30. const btnLoading = ref(false)
  31. const formsRef = ref()
  32. const message = useMessage()
  33. const onSubmit = async () => {
  34. formsRef.value.validate(async (error: any) => {
  35. if (error) return false
  36. try {
  37. btnLoading.value = true
  38. if (props.type === 'add') {
  39. await subjectSave({ ...forms })
  40. message.success('添加成功')
  41. } else if (props.type === 'edit') {
  42. await subjectUpdate({
  43. ...forms,
  44. id: props.data.id
  45. })
  46. message.success('修改成功')
  47. }
  48. emit('close')
  49. emit('getList')
  50. } catch {}
  51. btnLoading.value = false
  52. })
  53. }
  54. onMounted(async () => {
  55. if (props.type === 'edit') {
  56. const data = props.data
  57. forms.img = data.img
  58. forms.name = data.name
  59. forms.code = data.code
  60. }
  61. })
  62. return () => (
  63. <div style="background: #fff; padding-top: 12px">
  64. <NForm model={forms} ref={formsRef} label-placement="left" label-width="auto">
  65. <NFormItem
  66. label="声部图片"
  67. path="img"
  68. rule={[
  69. {
  70. required: true,
  71. message: '请输入声部图片'
  72. }
  73. ]}
  74. >
  75. <UploadFile
  76. size={2}
  77. cropper
  78. v-model:fileList={forms.img}
  79. bucketName="gyt"
  80. path="basic/"
  81. tips="图片大小2M以内"
  82. />
  83. </NFormItem>
  84. <NFormItem
  85. label="声部名称"
  86. path="name"
  87. rule={[
  88. {
  89. required: true,
  90. message: '请输入声部名称'
  91. }
  92. ]}
  93. >
  94. <NInput
  95. v-model:value={forms.name}
  96. placeholder="请输入声部名称"
  97. clearable
  98. maxlength={100}
  99. ></NInput>
  100. </NFormItem>
  101. <NFormItem label="声部编码" path="code">
  102. <NInput v-model:value={forms.code} placeholder="请输入声部编码" clearable></NInput>
  103. </NFormItem>
  104. </NForm>
  105. <NSpace justify="end">
  106. <NButton type="default" onClick={() => emit('close')}>
  107. 取消
  108. </NButton>
  109. <NButton type="primary" onClick={() => onSubmit()} loading={btnLoading.value}>
  110. 保存
  111. </NButton>
  112. </NSpace>
  113. </div>
  114. )
  115. }
  116. })