upload.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <div style="width: 100%;">
  3. <div class="color-666 mb-3" style="font-size: 13px;">* 仅支持.doc, .docx, .pdf文件</div>
  4. <CtForm ref="formPageRef" :items="items"></CtForm>
  5. </div>
  6. </template>
  7. <script setup>
  8. defineOptions({name: 'shareJob-form-upload'})
  9. import { reactive, ref, defineExpose } from 'vue'
  10. import { useI18n } from '@/hooks/web/useI18n'
  11. import { uploadFile } from '@/api/common'
  12. import Snackbar from '@/plugins/snackbar'
  13. const { t } = useI18n()
  14. const formPageRef = ref()
  15. let query = reactive({})
  16. // 上传简历
  17. const typeList = ['pdf', 'doc', 'docx']
  18. const handleUpload = async (e) => {
  19. const file = e
  20. const size = file.size
  21. if (size / (1024*1024) > 10) {
  22. Snackbar.warning(t('common.fileSizeExceed'))
  23. return
  24. }
  25. const arr = file.name.split('.')
  26. if (typeList.indexOf(arr[arr.length - 1]) < 0) {
  27. Snackbar.warning(t('common.fileFormatIncorrect'))
  28. return
  29. }
  30. const formData = new FormData()
  31. formData.append('file', file)
  32. const { data } = await uploadFile(formData)
  33. if (!data) return
  34. const urlItem = items.value.options.find(e => e.key === 'url')
  35. if (urlItem) urlItem.data = data
  36. query.fileName = file.name
  37. }
  38. const items = ref({
  39. options: [
  40. // {
  41. // type: 'text',
  42. // key: 'name',
  43. // value: '',
  44. // clearable: true,
  45. // label: '姓名 *',
  46. // rules: [v => !!v || '请填写姓名']
  47. // },
  48. // {
  49. // type: 'phoneNumber',
  50. // key: 'phone',
  51. // value: '',
  52. // clearable: true,
  53. // label: '手机号码 *',
  54. // rules: [v => !!v || '请填写手机号码']
  55. // },
  56. {
  57. type: 'upload',
  58. key: 'url',
  59. value: null,
  60. data: '',
  61. label: '简历 *',
  62. accept: '.doc, .docx, .pdf',
  63. placeholder: '请上传简历',
  64. rules: [v => !!v || '请上传简历'],
  65. change: handleUpload
  66. }
  67. ]
  68. })
  69. const getQuery = async () => {
  70. const { valid } = await formPageRef.value.formRef.validate()
  71. if (!valid) return
  72. const obj = {}
  73. items.value.options.forEach(e => {
  74. if (Object.prototype.hasOwnProperty.call(e, 'data')) return obj[e.key] = e.data
  75. obj[e.key] = e.value
  76. })
  77. query = Object.assign(query, obj)
  78. return query
  79. }
  80. defineExpose({
  81. getQuery
  82. })
  83. </script>