Authored by lea guo

api 调用

... ... @@ -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);
});
},
};
... ...
/* 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);
}
};
... ...