editor-config.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { IEditorConfig } from '@wangeditor/editor'
  2. import { getAccessToken, getTenantId } from '@/utils/auth'
  3. const message = useMessage()
  4. type InsertFnType = (url: string, alt: string, href: string) => void
  5. export const createEditorConfig = (
  6. server: string,
  7. accountId: number | undefined
  8. ): Partial<IEditorConfig> => {
  9. return {
  10. MENU_CONF: {
  11. ['uploadImage']: {
  12. server,
  13. // 单个文件的最大体积限制,默认为 2M
  14. maxFileSize: 5 * 1024 * 1024,
  15. // 最多可上传几个文件,默认为 100
  16. maxNumberOfFiles: 10,
  17. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  18. allowedFileTypes: ['image/*'],
  19. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  20. meta: {
  21. accountId: accountId,
  22. type: 'image'
  23. },
  24. // 将 meta 拼接到 url 参数中,默认 false
  25. metaWithUrl: true,
  26. // 自定义增加 http header
  27. headers: {
  28. Accept: '*',
  29. Authorization: 'Bearer ' + getAccessToken(),
  30. 'tenant-id': getTenantId()
  31. },
  32. // 跨域是否传递 cookie ,默认为 false
  33. withCredentials: true,
  34. // 超时时间,默认为 10 秒
  35. timeout: 5 * 1000, // 5 秒
  36. // form-data fieldName,后端接口参数名称,默认值wangeditor-uploaded-image
  37. fieldName: 'file',
  38. // 上传之前触发
  39. onBeforeUpload(file: File) {
  40. console.log(file)
  41. return file
  42. },
  43. // 上传进度的回调函数
  44. onProgress(progress: number) {
  45. // progress 是 0-100 的数字
  46. console.log('progress', progress)
  47. },
  48. onSuccess(file: File, res: any) {
  49. console.log('onSuccess', file, res)
  50. },
  51. onFailed(file: File, res: any) {
  52. message.alertError(res.message)
  53. console.log('onFailed', file, res)
  54. },
  55. onError(file: File, err: any, res: any) {
  56. message.alertError(err.message)
  57. console.error('onError', file, err, res)
  58. },
  59. // 自定义插入图片
  60. customInsert(res: any, insertFn: InsertFnType) {
  61. insertFn(res.data.url, 'image', res.data.url)
  62. }
  63. }
  64. }
  65. }
  66. }