Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
5
51business
Project
Project
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
方斌
51business
Commits
76072b19
Commit
76072b19
authored
Apr 17, 2020
by
任国军
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add uploadImage
parent
319a7d4e
Pipeline
#21801
passed with stage
in 13 seconds
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
27 deletions
+66
-27
oss.js
app/controller/common/oss.js
+19
-0
helper.js
app/extend/helper.js
+2
-2
common.js
app/router/common.js
+2
-0
oss.js
app/service/common/oss.js
+43
-25
No files found.
app/controller/common/oss.js
View file @
76072b19
...
...
@@ -7,5 +7,24 @@ class CommonController extends Controller {
const
ret
=
await
service
.
common
.
oss
.
get_business_signature
();
ctx
.
success
(
ret
);
}
async
uploadOss
()
{
const
{
ctx
,
service
}
=
this
;
const
input_params
=
ctx
.
request
.
body
;
if
(
typeof
(
input_params
.
image
)
===
'undefined'
)
{
ctx
.
failed
(
'image error'
);
}
if
(
typeof
(
input_params
.
height
)
===
'undefined'
)
{
ctx
.
failed
(
'height error'
);
}
if
(
typeof
(
input_params
.
width
)
===
'undefined'
)
{
ctx
.
failed
(
'width error'
);
}
const
result
=
await
service
.
common
.
oss
.
uploadOss
(
input_params
);
const
ret
=
{
ret
:
result
};
ctx
.
success
(
ret
);
}
}
module
.
exports
=
CommonController
;
app/extend/helper.js
View file @
76072b19
...
...
@@ -9,7 +9,7 @@ module.exports = {
const
{
ctx
}
=
this
;
const
bearerToken
=
ctx
.
request
.
header
.
authorization
;
if
(
!
bearerToken
)
{
this
.
throw
(
422
,
'error auth'
);
ctx
.
failed
(
'error auth'
);
}
return
bearerToken
&&
bearerToken
.
replace
(
'Bearer '
,
''
);
},
...
...
@@ -20,7 +20,7 @@ module.exports = {
const
decode_res
=
await
ctx
.
service
.
jwt
.
decode_token
(
token
);
const
token_black
=
await
this
.
app
.
memcache
.
get
(
'auth_token_'
+
decode_res
.
data
.
user_uuid
);
if
(
token_black
==
token
)
{
this
.
throw
(
422
,
'jwt校验失败'
);
ctx
.
failed
(
'jwt校验失败'
);
}
ctx
.
setUserUuid
(
decode_res
.
data
.
user_uuid
);
ctx
.
setOpenId
(
decode_res
.
data
.
openid
);
...
...
app/router/common.js
View file @
76072b19
...
...
@@ -4,5 +4,7 @@ module.exports = app => {
const
router
=
app
.
router
.
namespace
(
app
.
config
.
projectRootPath
+
'/common'
);
router
.
get
(
'third_oss_signature'
,
'/oss/signature'
,
'common.oss.signature'
);
// 业务的 oss 签名
router
.
post
(
'third'
,
'/oss/image'
,
app
.
middleware
.
verifyAuth
(),
'common.oss.uploadOss'
);
// 上传图片
router
.
get
(
'third_test'
,
'/test'
,
'common.test.test'
);
};
app/service/common/oss.js
View file @
76072b19
...
...
@@ -6,56 +6,57 @@ class CommonService extends Service {
async
get_business_signature
()
{
const
{
ctx
}
=
this
;
const
region
=
'oss-cn-hangzhou'
;
const
bucket
=
'51shequ'
;
const
region
=
'oss-cn-hangzhou'
;
const
bucket
=
'51shequ'
;
const
id
=
'LTAIbdayv82O7OYM'
;
const
key
=
'plCSzfiZCpMoh39omrzdaHHEltv4Un'
;
const
id
=
'LTAIbdayv82O7OYM'
;
const
key
=
'plCSzfiZCpMoh39omrzdaHHEltv4Un'
;
const
host
=
'//'
+
bucket
+
'.'
+
region
+
'.aliyuncs.com'
;
const
url
=
"//r.51gjj.com/"
;
let
now
=
Math
.
round
(
new
Date
().
getTime
()
/
1000
).
toString
();
const
url
=
'//r.51gjj.com/'
;
const
now
=
Math
.
round
(
new
Date
().
getTime
()
/
1000
).
toString
();
const
expire
=
120
;
le
t
end
=
now
+
expire
;
cons
t
end
=
now
+
expire
;
let
expiration
=
new
Date
().
toISOString
().
replace
(
/
\.
.+/
,
''
)
+
'Z'
;
const
expiration
=
new
Date
().
toISOString
().
replace
(
/
\.
.+/
,
''
)
+
'Z'
;
le
t
dir
=
'webpublic/resume/'
;
cons
t
dir
=
'webpublic/resume/'
;
//最大文件大小.用户可以自己设置
le
t
condition
=
new
Array
(
3
);
//
最大文件大小.用户可以自己设置
cons
t
condition
=
new
Array
(
3
);
condition
[
0
]
=
'content-length-range'
;
condition
[
1
]
=
0
;
condition
[
2
]
=
1048576000
;
le
t
start
=
new
Array
(
'starts-with'
,
'$key'
,
dir
);
cons
t
start
=
new
Array
(
'starts-with'
,
'$key'
,
dir
);
let
conditions
=
new
Array
(
condition
,
start
);
const
conditions
=
new
Array
(
condition
,
start
);
let
policy
=
{
expiration
:
expiration
,
conditions
:
conditions
};
const
policy
=
{
expiration
,
conditions
};
le
t
base64_policy
=
new
Buffer
(
JSON
.
stringify
(
policy
)).
toString
(
'base64'
);
cons
t
base64_policy
=
new
Buffer
(
JSON
.
stringify
(
policy
)).
toString
(
'base64'
);
le
t
string_to_sign
=
base64_policy
;
cons
t
string_to_sign
=
base64_policy
;
const
crypto
=
require
(
'crypto'
);
let
signature
=
crypto
.
createHmac
(
'sha1'
,
key
).
update
(
string_to_sign
).
digest
().
toString
(
'base64'
);;
let
config
=
{
const
signature
=
crypto
.
createHmac
(
'sha1'
,
key
).
update
(
string_to_sign
).
digest
()
.
toString
(
'base64'
);
const
config
=
{
region
,
accessKeyId
:
id
,
accessKeySecret
:
key
,
bucket
:
bucket
,
bucket
,
}
le
t
response
=
{
}
;
cons
t
response
=
{
accessid
:
id
,
static_host
:
url
+
dir
,
host
:
host
,
host
,
policy
:
base64_policy
,
signature
:
signature
,
signature
,
expire
:
end
,
config
:
config
,
dir
config
,
dir
,
};
return
response
;
...
...
@@ -63,6 +64,23 @@ class CommonService extends Service {
}
async
uploadOss
(
input
)
{
const
{
ctx
}
=
this
;
const
image
=
input
.
image
.
replace
(
/^data:image
\/\w
+;base64,/
,
''
);
const
imgBuffer
=
new
Buffer
(
image
,
'base64'
);
const
uuidv4
=
require
(
'uuid/v4'
);
const
uuid
=
uuidv4
();
const
now
=
new
Date
();
// 上传oss
const
name
=
'image/catalog/'
+
input
.
directory
+
'/'
+
uuid
+
now
.
getTime
()
+
'.png'
;
const
result
=
await
ctx
.
oss
.
put
(
name
,
imgBuffer
);
if
(
result
&&
typeof
(
result
.
url
)
!==
'undefined'
)
{
const
url
=
result
.
url
+
'?height='
+
input
.
height
+
'&width='
+
input
.
width
;
return
url
;
}
ctx
.
failed
(
'uploadOss failed'
);
}
}
module
.
exports
=
CommonService
;
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