mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {
  hasOwn
} from 'uni-shared'
 
import {
  getRealPath
} from '../util'
 
import {
  publish
} from '../../bridge'
 
let uploadTaskId = 0
const uploadTasks = {}
 
const publishStateChange = (res) => {
  publish('onUploadTaskStateChange', res)
}
 
const createUploadTaskById = function (uploadTaskId, {
  url,
  filePath,
  name,
  files,
  header,
  formData,
  timeout
} = {}) {
  timeout =
    (timeout ||
      (__uniConfig.networkTimeout && __uniConfig.networkTimeout.uploadFile) ||
      60 * 1000) / 1000
  const uploader = plus.uploader.createUpload(url, {
    timeout,
    // 需要与其它平台上的表现保持一致,不走重试的逻辑。
    retry: 0,
    retryInterval: 0
  }, (upload, statusCode) => {
    if (statusCode) {
      publishStateChange({
        uploadTaskId,
        state: 'success',
        data: upload.responseText,
        statusCode
      })
    } else {
      publishStateChange({
        uploadTaskId,
        state: 'fail',
        data: '',
        statusCode
      })
    }
    delete uploadTasks[uploadTaskId]
  })
 
  for (const name in header) {
    if (hasOwn(header, name)) {
      uploader.setRequestHeader(name, String(header[name]))
    }
  }
  for (const name in formData) {
    if (hasOwn(formData, name)) {
      uploader.addData(name, String(formData[name]))
    }
  }
  if (files && files.length) {
    files.forEach(file => {
      uploader.addFile(getRealPath(file.uri || file.filePath), {
        key: file.name || 'file'
      })
    })
  } else {
    uploader.addFile(getRealPath(filePath), {
      key: name
    })
  }
  uploader.addEventListener('statechanged', (upload, status) => {
    if (upload.uploadedSize && upload.totalSize) {
      publishStateChange({
        uploadTaskId,
        state: 'progressUpdate',
        progress: parseInt(upload.uploadedSize / upload.totalSize * 100),
        totalBytesSent: upload.uploadedSize,
        totalBytesExpectedToSend: upload.totalSize
      })
    }
  })
  uploadTasks[uploadTaskId] = uploader
  uploader.start()
  return {
    uploadTaskId,
    errMsg: 'createUploadTask:ok'
  }
}
 
export function operateUploadTask ({
  uploadTaskId,
  operationType
} = {}) {
  const uploadTask = uploadTasks[uploadTaskId]
  if (uploadTask && operationType === 'abort') {
    delete uploadTasks[uploadTaskId]
    uploadTask.abort()
    publishStateChange({
      uploadTaskId,
      state: 'fail',
      errMsg: 'abort'
    })
    return {
      errMsg: 'operateUploadTask:ok'
    }
  }
  return {
    errMsg: 'operateUploadTask:fail'
  }
}
 
export function createUploadTask (args) {
  return createUploadTaskById(++uploadTaskId, args)
}