Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mp_estate_server
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
mp_estate_backstage
mp_estate_server
Commits
123767d6
Commit
123767d6
authored
Sep 15, 2020
by
成旭东
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
直传oss配置接口
parent
f06703ec
Pipeline
#28207
passed with stage
in 5 minutes 11 seconds
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
0 deletions
+73
-0
oss.ts
app/controller/oss.ts
+29
-0
router.ts
app/router.ts
+3
-0
file.ts
app/service/file.ts
+39
-0
index.d.ts
typings/app/controller/index.d.ts
+2
-0
No files found.
app/controller/oss.ts
0 → 100644
View file @
123767d6
import
{
Controller
}
from
'egg'
;
const
gmt_iso8601
=
(
time
:
any
)
=>
{
const
date
=
new
Date
(
time
*
1000
);
const
expireTime
=
date
.
toISOString
();
const
pos
=
expireTime
.
indexOf
(
'.'
);
const
expiration
=
expireTime
.
substring
(
0
,
pos
);
return
expiration
+
'Z'
;
};
export
default
class
OssController
extends
Controller
{
async
getSignature
()
{
const
{
ctx
}
=
this
;
const
date
=
parseInt
(
`
${
Date
.
now
()
/
1000
}
`
,
10
);
const
expire
=
30
;
const
end
=
(
date
+
expire
);
const
expiration
=
gmt_iso8601
(
end
);
const
json
=
JSON
.
stringify
({
expiration
,
conditions
:
[
[
'content-length-range'
,
0
,
500
*
1024
*
1024
],
// 设置上传文件的大小限制,5mb
],
});
const
policy
=
new
Buffer
(
json
).
toString
(
'base64'
);
const
signature
=
ctx
.
oss
.
get
(
'media'
).
signature
(
policy
);
ctx
.
success
({
res
:
{
policy
,
signature
,
OSSAccessKeyId
:
'LTAIRRW7SbrqKUIJ'
}
});
}
}
app/router.ts
View file @
123767d6
...
@@ -8,6 +8,9 @@ export default (app: Application) => {
...
@@ -8,6 +8,9 @@ export default (app: Application) => {
router
.
get
(
'/api/session/menus'
,
controller
.
user
.
index
.
menus
);
router
.
get
(
'/api/session/menus'
,
controller
.
user
.
index
.
menus
);
router
.
post
(
'/api/upload/media'
,
controller
.
util
.
uploadMedia
);
router
.
post
(
'/api/upload/media'
,
controller
.
util
.
uploadMedia
);
// oss直传配置
router
.
get
(
'/api/upload/oss/config'
,
controller
.
oss
.
getSignature
);
// 新房房源管理
// 新房房源管理
router
.
get
(
'/api/estate/list'
,
controller
.
mpEstate
.
list
.
list
);
router
.
get
(
'/api/estate/list'
,
controller
.
mpEstate
.
list
.
list
);
router
.
get
(
'/api/estate/:id'
,
controller
.
mpEstate
.
list
.
show
);
router
.
get
(
'/api/estate/:id'
,
controller
.
mpEstate
.
list
.
show
);
...
...
app/service/file.ts
View file @
123767d6
...
@@ -169,6 +169,45 @@ export default class FileService extends Service {
...
@@ -169,6 +169,45 @@ export default class FileService extends Service {
ctx
.
throw
(
500
,
'oss upload failed!'
);
ctx
.
throw
(
500
,
'oss upload failed!'
);
}
}
async
saveLocalToCDN
(
option
?:
any
,
stream
?:
any
)
{
const
{
ctx
}
=
this
;
try
{
const
localPathObj
=
await
this
.
saveToLocal
(
option
,
stream
);
const
localPath
=
localPathObj
.
url
;
const
localFilName
=
localPathObj
.
fileName
;
const
ext
=
path
.
extname
(
localFilName
).
toLocaleLowerCase
();
const
ossFilename
=
'zeus/'
+
moment
().
format
(
'YYYYMMDD'
)
+
'/'
+
uuid
.
v4
()
+
ext
;
let
oss_url
:
any
;
let
url
:
any
;
const
progress
=
(
p
:
any
,
_checkpoint
:
any
)
=>
{
console
.
log
(
p
);
// Object的上传进度。
console
.
log
(
_checkpoint
);
// 分片上传的断点信息。
};
try
{
const
result
=
await
ctx
.
oss
.
get
(
'media'
).
multipartUpload
(
ossFilename
,
localPath
,
{
progress
,
});
oss_url
=
result
.
url
;
url
=
`https://r.51gjj.com/
${
ossFilename
}
`
;
}
catch
(
e
)
{
ctx
.
throw
(
500
,
e
);
}
if
(
url
)
{
return
{
fileName
:
localFilName
,
url
,
oss_url
,
};
}
ctx
.
throw
(
500
,
'oss upload failed!'
);
}
catch
(
e
)
{
ctx
.
throw
(
500
,
e
);
}
}
async
saveToDB
({
fileName
,
url
}:
any
)
{
async
saveToDB
({
fileName
,
url
}:
any
)
{
const
{
ctx
}
=
this
;
const
{
ctx
}
=
this
;
const
row
=
await
ctx
.
model
.
File
.
create
({
const
row
=
await
ctx
.
model
.
File
.
create
({
...
...
typings/app/controller/index.d.ts
View file @
123767d6
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
// Do not modify this file!!!!!!!!!
// Do not modify this file!!!!!!!!!
import
'egg'
;
import
'egg'
;
import
ExportOss
from
'../../../app/controller/oss'
;
import
ExportUtil
from
'../../../app/controller/util'
;
import
ExportUtil
from
'../../../app/controller/util'
;
import
ExportMpEstateAppointment
from
'../../../app/controller/mpEstate/appointment'
;
import
ExportMpEstateAppointment
from
'../../../app/controller/mpEstate/appointment'
;
import
ExportMpEstateList
from
'../../../app/controller/mpEstate/list'
;
import
ExportMpEstateList
from
'../../../app/controller/mpEstate/list'
;
...
@@ -12,6 +13,7 @@ import ExportUserIndex from '../../../app/controller/user/index';
...
@@ -12,6 +13,7 @@ import ExportUserIndex from '../../../app/controller/user/index';
declare
module
'egg'
{
declare
module
'egg'
{
interface
IController
{
interface
IController
{
oss
:
ExportOss
;
util
:
ExportUtil
;
util
:
ExportUtil
;
mpEstate
:
{
mpEstate
:
{
appointment
:
ExportMpEstateAppointment
;
appointment
:
ExportMpEstateAppointment
;
...
...
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