const request = require('superagent'); let _weatherUrl = ''; module.exports.entry = function (app, router, opts) { const { weatherUrl } = opts; _weatherUrl = weatherUrl; router.get('/query/weather', weather); }; async function weather(ctx) { try { const { cityName } = ctx.query; const weatherRes = await request.post(_weatherUrl).send({ cityName }); const { body } = weatherRes; if (body && body.data) { ctx.status = 200; ctx.body = { ...body.data, cityName }; } else { ctx.status = 400; ctx.body = { msg: '获取天气错误' }; } } catch (error) { console.log('[*err]', error); ctx.status = 400; ctx.body = error; } }