Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
xianyu-ufo-app-web
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
lea guo
5 years ago
Commit
084ff9bd5726ab015f56276bfb268f652a0b5ea2
1 parent
d510fb23
api 调用
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
13 deletions
apps/common/create-api-server.js
doraemon/middleware/ssr-api.js
apps/common/create-api-server.js
View file @
084ff9b
...
...
@@ -109,7 +109,7 @@ export const createApi = (context, store) => {
})
.
then
(
resolve
({
context
,
store
,
reqParams
}))
.
catch
(
err
=>
{
console
.
log
(
'----
--------
---'
,
err
);
console
.
log
(
'----
create----api--server--get
---'
,
err
);
});
},
post
(
url
,
reqParams
=
{})
{
...
...
@@ -121,7 +121,7 @@ export const createApi = (context, store) => {
})
.
then
(
resolve
({
context
,
store
,
reqParams
}))
.
catch
(
err
=>
{
console
.
log
(
'----
--------
---'
,
err
);
console
.
log
(
'----
create----api--server--post
---'
,
err
);
});
},
};
...
...
doraemon/middleware/ssr-api.js
View file @
084ff9b
/* eslint-disable operator-linebreak */
const
serviceApi
=
global
.
yoho
.
ServiceAPI
;
const
ufoAPI
=
global
.
yoho
.
UfoAPI
;
const
checkParams
=
require
(
'../../utils/check-params'
);
...
...
@@ -5,11 +6,12 @@ const handleResult = require('../../utils/handle-result');
const
apiMaps
=
require
(
'../../config/api-map'
);
const
errorHandler
=
require
(
'./error-handler'
);
module
.
exports
=
async
(
req
,
res
,
next
)
=>
{
// eslint-disable-next-line space-before-function-paren
module
.
exports
=
async
(
req
,
res
,
next
)
=>
{
res
.
set
({
'Cache-Control'
:
'no-cache'
,
Pragma
:
'no-cache'
,
Expires
:
(
new
Date
(
1900
,
0
,
1
,
0
,
0
,
0
,
0
)).
toUTCString
()
Expires
:
new
Date
(
1900
,
0
,
1
,
0
,
0
,
0
,
0
).
toUTCString
(),
});
const
apiInfo
=
apiMaps
[
req
.
path
];
...
...
@@ -19,7 +21,7 @@ module.exports = async(req, res, next) => {
const
baseParams
=
{};
req
.
route
=
{
path
:
req
.
path
path
:
req
.
path
,
};
if
(
!
apiInfo
.
service
)
{
...
...
@@ -32,15 +34,23 @@ module.exports = async(req, res, next) => {
return
req
.
user
.
uid
;
},
sessionKey
:
req
.
user
.
sessionKey
,
appSessionType
:
req
.
user
.
appSessionType
appSessionType
:
req
.
user
.
appSessionType
,
};
}
}
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
baseParams
.
debug
=
'XYZ'
;
}
try
{
const
reqParams
=
Object
.
assign
({},
req
.
query
,
req
.
body
,
baseParams
);
const
params
=
checkParams
.
getParams
(
reqParams
,
apiInfo
,
req
);
const
cache
=
(
req
.
method
.
toLowerCase
()
!==
'get'
||
apiInfo
.
auth
)
?
false
:
apiInfo
.
cache
;
const
cache
=
req
.
method
.
toLowerCase
()
!==
'get'
||
apiInfo
.
auth
?
false
:
apiInfo
.
cache
;
let
method
=
req
.
method
.
toLowerCase
()
===
'post'
?
'post'
:
'get'
;
...
...
@@ -55,7 +65,7 @@ module.exports = async(req, res, next) => {
data
:
params
,
param
:
{
cache
:
cache
,
}
}
,
});
}
else
if
(
apiInfo
.
ufo
)
{
result
=
await
apiCtx
[
method
]({
...
...
@@ -63,25 +73,26 @@ module.exports = async(req, res, next) => {
url
:
apiInfo
.
path
||
''
,
data
:
params
,
param
:
{
cache
:
cache
}
cache
:
cache
,
},
});
}
else
{
result
=
await
apiCtx
[
method
]({
data
:
params
,
url
:
apiInfo
.
path
||
''
,
param
:
{
cache
:
cache
}
cache
:
cache
,
},
});
}
if
(
result
)
{
return
res
.
json
(
handleResult
(
result
,
apiInfo
));
}
return
res
.
json
({
code
:
400
code
:
400
,
});
}
catch
(
error
)
{
console
.
error
(
'------ssr-api------'
,
error
);
return
errorHandler
.
serverError
(
error
,
req
,
res
,
next
);
}
};
...
...
Please
register
or
login
to post a comment