upload.ts 1022 B

1234567891011121314151617181920212223242526272829
  1. // 公共图片上传方法
  2. import { useCacheStore } from '@/stores/cache'
  3. export function uploadImage(tempFilePath: string, onSuccess?: (url: string) => void, onFail?: (err: any) => void) {
  4. const cache = useCacheStore()
  5. uni.uploadFile({
  6. url: 'http://192.168.0.11:8080/jeecg-boot/sys/common/upload',
  7. filePath: tempFilePath,
  8. name: 'file',
  9. header: {
  10. 'x-access-token': cache.get('TOKEN')
  11. },
  12. success: (res) => {
  13. const data =JSON.parse(res.data)
  14. if (res.statusCode == 200){
  15. // uni.showToast({ title: '上传成功', icon: 'success' })
  16. onSuccess && onSuccess(data.message)
  17. } else {
  18. uni.showToast({ title: '上传失败', icon: 'none' })
  19. onFail && onFail(data)
  20. }
  21. },
  22. fail: (err) => {
  23. console.log(err)
  24. uni.showToast({ title: '上传失败', icon: 'none' })
  25. onFail && onFail(err)
  26. }
  27. })
  28. }