Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
H
H5Public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
CI / CD
CI / CD
流水线
作业
日程
统计图
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
前端开发小组
H5Public
Commits
4da6c493
提交
4da6c493
authored
4月 17, 2023
作者:
钟是志
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
34049 [离校]学生登录-学生处分中上次图片后不能正常提交【522622200506284524 123456】
上级
7e192255
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
43 行增加
和
35 行删除
+43
-35
index.js
HighStateComponent/UploadImage/index.js
+43
-35
没有找到文件。
HighStateComponent/UploadImage/index.js
浏览文件 @
4da6c493
import
React
,
{
Component
}
from
'react'
;
import
React
,
{
Component
}
from
'react'
;
import
PropTypes
from
'prop-types'
;
import
PropTypes
from
'prop-types'
;
import
{
ImagePicker
,
Toast
}
from
'antd-mobile'
;
import
{
ImagePicker
,
Toast
}
from
'antd-mobile'
;
import
config
from
'@/config/config'
;
import
config
from
'@/config/config'
;
import
{
getToken
}
from
'@/H5Public/utils/authority'
;
import
{
getToken
}
from
'@/H5Public/utils/authority'
;
/**
/**
...
@@ -17,6 +17,7 @@ import { getToken } from '@/H5Public/utils/authority';
...
@@ -17,6 +17,7 @@ import { getToken } from '@/H5Public/utils/authority';
// 全局loadinng
// 全局loadinng
let
loadingCount
=
0
;
let
loadingCount
=
0
;
class
Upload
extends
Component
{
class
Upload
extends
Component
{
constructor
(
props
)
{
constructor
(
props
)
{
super
(
props
)
super
(
props
)
...
@@ -25,15 +26,16 @@ class Upload extends Component {
...
@@ -25,15 +26,16 @@ class Upload extends Component {
urlList
:
[]
// 后端返回图片地址
urlList
:
[]
// 后端返回图片地址
}
}
}
}
componentDidMount
(){
componentDidMount
()
{
// 回显
// 回显
const
ImageArr
=
[];
const
ImageArr
=
[];
this
.
props
.
defaultFiles
.
map
(
item
=>
{
this
.
props
.
defaultFiles
.
map
(
item
=>
{
ImageArr
.
push
({
ImageArr
.
push
({
url
:
item
url
:
item
})
})
})
})
this
.
setState
({
files
:
ImageArr
})
this
.
setState
({
files
:
ImageArr
})
}
}
// 图片压缩函数
// 图片压缩函数
...
@@ -77,22 +79,27 @@ class Upload extends Component {
...
@@ -77,22 +79,27 @@ class Upload extends Component {
while
(
n
--
)
{
while
(
n
--
)
{
u8arr
[
n
]
=
bstr
.
charCodeAt
(
n
);
u8arr
[
n
]
=
bstr
.
charCodeAt
(
n
);
}
}
return
new
File
([
u8arr
],
filename
,
{
type
:
mime
});
return
new
File
([
u8arr
],
filename
,
{
type
:
mime
});
};
};
// 上传本地
// 上传本地
onChange
=
(
files
)
=>
{
onChange
=
(
files
)
=>
{
this
.
setState
({
files
});
this
.
setState
({
files
});
}
}
// 上传图片--方法
// 上传图片--方法
uploadImg
=
(
file
,
callback
)
=>
{
uploadImg
=
(
file
,
callback
)
=>
{
const
xhr
=
new
XMLHttpRequest
();
const
xhr
=
new
XMLHttpRequest
();
const
formData
=
new
FormData
();
const
formData
=
new
FormData
();
formData
.
append
(
'file'
,
file
);
formData
.
append
(
'file'
,
file
);
if
(
config
.
version
===
'bdk'
){
xhr
.
open
(
'post'
,
config
.
uploadUrl
,
true
);
}
else
{
xhr
.
open
(
'post'
,
config
.
uploadUrl
+
"?token="
+
getToken
(),
true
);
xhr
.
open
(
'post'
,
config
.
uploadUrl
+
"?token="
+
getToken
(),
true
);
}
xhr
.
setRequestHeader
(
"Accept"
,
"application/json;charset=UTF-8"
);
xhr
.
setRequestHeader
(
"Accept"
,
"application/json;charset=UTF-8"
);
xhr
.
setRequestHeader
(
"Authorization"
,
`bearer
${
getToken
()}
`
);
xhr
.
send
(
formData
);
xhr
.
send
(
formData
);
xhr
.
onreadystatechange
=
()
=>
{
xhr
.
onreadystatechange
=
()
=>
{
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
==
200
)
{
if
(
xhr
.
readyState
==
4
&&
xhr
.
status
=
==
200
)
{
const
json
=
JSON
.
parse
(
xhr
.
responseText
);
const
json
=
JSON
.
parse
(
xhr
.
responseText
);
callback
(
json
.
url
)
callback
(
json
.
url
)
}
}
...
@@ -101,31 +108,31 @@ class Upload extends Component {
...
@@ -101,31 +108,31 @@ class Upload extends Component {
// 上传后获取后端返回图片地址
// 上传后获取后端返回图片地址
uploadCall
=
(
item
)
=>
(
uploadCall
=
(
item
)
=>
(
this
.
uploadImg
(
item
,
(
res
)
=>
{
this
.
uploadImg
(
item
,
(
res
)
=>
{
this
.
setState
({
urlList
:
[...
this
.
state
.
urlList
,
res
]
},
()
=>
{
this
.
setState
({
urlList
:
[...
this
.
state
.
urlList
,
res
]
},
()
=>
{
if
(
this
.
state
.
files
.
length
===
this
.
state
.
urlList
.
length
)
{
if
(
this
.
state
.
files
.
length
===
this
.
state
.
urlList
.
length
)
{
Toast
.
hide
()
Toast
.
hide
()
loadingCount
=
0
;
loadingCount
=
0
;
const
{
handleSubmit
,
labelId
=
''
}
=
this
.
props
;
const
{
handleSubmit
,
labelId
=
''
}
=
this
.
props
;
handleSubmit
(
this
.
state
.
urlList
,
labelId
)
// 把后端返回url传给父组件
handleSubmit
(
this
.
state
.
urlList
,
labelId
)
// 把后端返回url传给父组件
}
}
})
})
})
})
)
)
// 提交
// 提交
handleSubmit
=
()
=>
{
handleSubmit
=
()
=>
{
const
{
files
}
=
this
.
state
;
const
{
files
}
=
this
.
state
;
Toast
.
loading
(
'Loading...'
,
0
);
Toast
.
loading
(
'Loading...'
,
0
);
// 判断是否是回显的图片--不是则进行压缩
// 判断是否是回显的图片--不是则进行压缩
const
httpFiles
=
files
.
filter
(
item
=>
item
.
url
.
startsWith
(
'http'
))
// 回显图片
const
httpFiles
=
files
.
filter
(
item
=>
item
.
url
.
startsWith
(
'http'
))
// 回显图片
const
oldUrlArr
=
[];
const
oldUrlArr
=
[];
httpFiles
.
map
(
item
=>
oldUrlArr
.
push
(
item
.
url
))
httpFiles
.
map
(
item
=>
oldUrlArr
.
push
(
item
.
url
))
const
Files
=
files
.
filter
(
item
=>
!
item
.
url
.
startsWith
(
'http'
))
// 本地图片
const
Files
=
files
.
filter
(
item
=>
!
item
.
url
.
startsWith
(
'http'
))
// 本地图片
this
.
setState
({
urlList
:
oldUrlArr
},()
=>
{
this
.
setState
({
urlList
:
oldUrlArr
},
()
=>
{
if
(
Files
.
length
==
0
)
{
if
(
Files
.
length
==
0
)
{
Toast
.
hide
()
Toast
.
hide
()
loadingCount
=
0
;
loadingCount
=
0
;
const
{
handleSubmit
,
labelId
=
''
}
=
this
.
props
;
const
{
handleSubmit
,
labelId
=
''
}
=
this
.
props
;
handleSubmit
(
this
.
state
.
urlList
,
labelId
)
// 把后端返回url传给父组件
handleSubmit
(
this
.
state
.
urlList
,
labelId
)
// 把后端返回url传给父组件
}
}
Files
.
map
(
item
=>
{
Files
.
map
(
item
=>
{
loadingCount
++
loadingCount
++
...
@@ -133,18 +140,19 @@ class Upload extends Component {
...
@@ -133,18 +140,19 @@ class Upload extends Component {
})
})
})
})
}
}
render
()
{
render
()
{
const
{
files
}
=
this
.
state
;
const
{
files
}
=
this
.
state
;
const
{
multiple
,
accept
,
length
,
readOnly
,
rest
}
=
this
.
props
;
const
{
multiple
,
accept
,
length
,
readOnly
,
rest
}
=
this
.
props
;
return
(
return
(
<
div
>
<
div
>
<
ImagePicker
<
ImagePicker
files
=
{
files
}
files
=
{
files
}
accept
=
{
accept
}
accept
=
{
accept
}
onChange
=
{
this
.
onChange
}
onChange
=
{
this
.
onChange
}
selectable
=
{
readOnly
?
false
:
(
files
.
length
<
length
)}
selectable
=
{
readOnly
?
false
:
(
files
.
length
<
length
)}
multiple
=
{
multiple
}
multiple
=
{
multiple
}
disableDelete
=
{
readOnly
?
true
:
false
}
disableDelete
=
{
!!
readOnly
}
{...
rest
}
{...
rest
}
/
>
/
>
<
/div
>
<
/div
>
...
@@ -156,20 +164,20 @@ Upload.propTypes = {
...
@@ -156,20 +164,20 @@ Upload.propTypes = {
multiple
:
PropTypes
.
bool
,
// 是否支持多选
multiple
:
PropTypes
.
bool
,
// 是否支持多选
accept
:
PropTypes
.
string
,
// 上传格式限制
accept
:
PropTypes
.
string
,
// 上传格式限制
length
:
PropTypes
.
number
,
// 最大支持上传张数
length
:
PropTypes
.
number
,
// 最大支持上传张数
handleSubmit
:
PropTypes
.
func
.
isRequired
,
// 上传函数
handleSubmit
:
PropTypes
.
func
.
isRequired
,
// 上传函数
rest
:
PropTypes
.
object
,
rest
:
PropTypes
.
object
,
defaultFiles
:
PropTypes
.
array
,
// 初始值
defaultFiles
:
PropTypes
.
array
,
// 初始值
readOnly
:
PropTypes
.
bool
,
readOnly
:
PropTypes
.
bool
,
labelId
:
PropTypes
.
any
// 组件id 标识
labelId
:
PropTypes
.
any
// 组件id 标识
}
}
Upload
.
defaultProps
=
{
Upload
.
defaultProps
=
{
multiple
:
true
,
// 是否支持多选
multiple
:
true
,
// 是否支持多选
accept
:
'image/*'
,
// 上传格式限制
accept
:
'image/*'
,
// 上传格式限制
length
:
10
,
// 最大支持上传张数
length
:
10
,
// 最大支持上传张数
defaultFiles
:
[],
defaultFiles
:
[],
readOnly
:
false
,
readOnly
:
false
,
labelId
:
''
labelId
:
''
}
}
export
default
Upload
;
export
default
Upload
;
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论