Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cdsf-wx-app
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姜雷
cdsf-wx-app
Commits
b7d988e9
Commit
b7d988e9
authored
Oct 10, 2018
by
姜雷
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传图片后剪裁大小
parent
e83cda8f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
14 deletions
+63
-14
ImagePicker.js
src/components/Input/ImagePicker/ImagePicker.js
+61
-12
RepairItem.js
src/containers/Repair/components/RepairItem.js
+2
-2
No files found.
src/components/Input/ImagePicker/ImagePicker.js
View file @
b7d988e9
...
@@ -2,11 +2,24 @@ import React, { Component } from 'react';
...
@@ -2,11 +2,24 @@ import React, { Component } from 'react';
import
styles
from
'./style.css'
;
import
styles
from
'./style.css'
;
import
addimgIcon
from
'./images/add_pic_bg@2x.png'
;
import
addimgIcon
from
'./images/add_pic_bg@2x.png'
;
import
delIcon
from
'./images/delete_pic_btn@2x.png'
;
import
delIcon
from
'./images/delete_pic_btn@2x.png'
;
import
{
Toast
}
from
'antd-mobile'
;
class
ImagePicker
extends
Component
{
class
ImagePicker
extends
Component
{
constructor
(
props
)
{
constructor
(
props
)
{
super
(
props
);
super
(
props
);
let
canDom
=
document
.
getElementById
(
'ksCanvasBox'
);
if
(
!
canDom
)
{
canDom
=
document
.
createElement
(
'canvas'
);
canDom
.
id
=
'ksCanvasBox'
;
canDom
.
setAttribute
(
'width'
,
'590px'
);
canDom
.
setAttribute
(
'height'
,
'334px'
);
canDom
.
style
=
'display: none'
;
document
.
body
.
appendChild
(
canDom
);
}
this
.
canvas
=
canDom
;
this
.
state
=
{
this
.
state
=
{
aspectRatio
:
590
/
334
,
done
:
false
,
done
:
false
,
hasValue
:
hasValue
:
Object
.
keys
(
props
).
find
(
i
=>
i
===
'value'
)
===
-
1
?
false
:
true
,
Object
.
keys
(
props
).
find
(
i
=>
i
===
'value'
)
===
-
1
?
false
:
true
,
...
@@ -15,7 +28,8 @@ class ImagePicker extends Component {
...
@@ -15,7 +28,8 @@ class ImagePicker extends Component {
}
}
shouldComponentUpdate
(
nextProps
,
nextState
)
{
shouldComponentUpdate
(
nextProps
,
nextState
)
{
const
{
value
}
=
nextProps
;
const
{
value
}
=
nextProps
;
const
{
files
}
=
nextState
;
const
{
files
,
done
}
=
nextState
;
console
.
log
(
value
,
files
);
if
(
!
value
&&
files
.
length
)
{
if
(
!
value
&&
files
.
length
)
{
this
.
deleteImage
();
this
.
deleteImage
();
}
}
...
@@ -46,17 +60,23 @@ class ImagePicker extends Component {
...
@@ -46,17 +60,23 @@ class ImagePicker extends Component {
uploadImg
=
e
=>
{
uploadImg
=
e
=>
{
const
{
onChange
}
=
this
.
props
;
const
{
onChange
}
=
this
.
props
;
const
filesList
=
e
.
target
.
files
;
const
filesList
=
e
.
target
.
files
;
const
fileUrl
=
this
.
createObjectURL
(
filesList
[
0
]);
// const fileUrl = this.createObjectURL(filesList[0]);
this
.
setState
(()
=>
({
this
.
resizeImg
(
filesList
[
0
])
files
:
[
fileUrl
],
.
then
(({
file
,
url
})
=>
{
done
:
true
,
this
.
setState
(()
=>
({
}));
files
:
[
url
],
if
(
onChange
)
{
done
:
true
,
onChange
({
}));
filesList
,
if
(
onChange
)
{
url
:
fileUrl
,
onChange
({
filesList
:
[
file
],
url
:
url
,
});
}
})
.
catch
(
err
=>
{
Toast
.
fail
(
err
);
});
});
}
};
};
createObjectURL
=
blob
=>
{
createObjectURL
=
blob
=>
{
if
(
window
.
URL
)
{
if
(
window
.
URL
)
{
...
@@ -73,6 +93,35 @@ class ImagePicker extends Component {
...
@@ -73,6 +93,35 @@ class ImagePicker extends Component {
return
window
.
webkitURL
.
revokeObjectURL
(
url
);
return
window
.
webkitURL
.
revokeObjectURL
(
url
);
}
}
};
};
resizeImg
=
file
=>
new
Promise
((
resolve
,
reject
)
=>
{
const
{
aspectRatio
}
=
this
.
state
;
const
ctx
=
this
.
canvas
.
getContext
(
'2d'
);
const
fileUrl
=
this
.
createObjectURL
(
file
);
const
img
=
new
Image
();
img
.
onload
=
()
=>
{
console
.
log
(
img
.
width
,
img
.
height
);
let
imgRatio
=
img
.
width
/
img
.
height
;
let
x
,
y
;
if
(
imgRatio
>
aspectRatio
)
{
x
=
590
;
y
=
590
/
imgRatio
;
}
else
{
x
=
334
*
imgRatio
;
y
=
334
;
}
ctx
.
drawImage
(
img
,
(
590
-
x
)
/
2
,
(
334
-
y
)
/
2
,
x
,
y
);
this
.
canvas
.
toBlob
(
blob
=>
{
const
dataURL
=
this
.
canvas
.
toDataURL
(
'image/png'
);
console
.
log
(
file
.
size
,
dataURL
.
length
);
resolve
({
file
:
blob
,
url
:
dataURL
});
});
};
img
.
onerror
=
()
=>
{
reject
(
'背景加载失败'
);
};
img
.
src
=
fileUrl
;
});
render
()
{
render
()
{
const
{
className
}
=
this
.
props
;
const
{
className
}
=
this
.
props
;
const
{
files
,
done
}
=
this
.
state
;
const
{
files
,
done
}
=
this
.
state
;
...
...
src/containers/Repair/components/RepairItem.js
View file @
b7d988e9
...
@@ -82,7 +82,7 @@ class RepairItem extends Component {
...
@@ -82,7 +82,7 @@ class RepairItem extends Component {
<
div
className
=
{
styles
.
listLine2ItemRight
}
>
<
div
className
=
{
styles
.
listLine2ItemRight
}
>
<
div
className
=
{
styles
.
listLabel
}
>
报修人
tel
:
<
/div
>
<
div
className
=
{
styles
.
listLabel
}
>
报修人
tel
:
<
/div
>
<
div
className
=
{
styles
.
listValue
}
>
<
div
className
=
{
styles
.
listValue
}
>
{
data
.
apply
Cellphone
}
{
data
.
repair
Cellphone
}
<
/div
>
<
/div
>
<
/div
>
<
/div
>
<
/div>
,
<
/div>
,
...
@@ -112,7 +112,7 @@ class RepairItem extends Component {
...
@@ -112,7 +112,7 @@ class RepairItem extends Component {
<
div
className
=
{
styles
.
listLine2ItemRight
}
>
<
div
className
=
{
styles
.
listLine2ItemRight
}
>
<
div
className
=
{
styles
.
listLabel
}
>
报修人
tel
:
<
/div
>
<
div
className
=
{
styles
.
listLabel
}
>
报修人
tel
:
<
/div
>
<
div
className
=
{
styles
.
listValue
}
>
<
div
className
=
{
styles
.
listValue
}
>
{
data
.
apply
Cellphone
}
{
data
.
repair
Cellphone
}
<
/div
>
<
/div
>
<
/div
>
<
/div
>
<
/div>
,
<
/div>
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment