'use strict'; const moment = require('moment') const request = require('superagent') function videoList (opts) { return async function (ctx) { try { const { models, } = ctx.fs.dc; const { app, yingshiTokenRes } = ctx let yingshiToken = '' if (yingshiTokenRes && yingshiTokenRes.token && yingshiTokenRes.expire && moment().isBefore(moment(yingshiTokenRes.expire))) { yingshiToken = yingshiTokenRes.token } else { const tokenRes = await app.fs.yingshiRequest.post(`lapp/token/get`, { query: { appKey: opts.yingshiKey, appSecret: opts.yingshiSecret } }) if (tokenRes.code == 200 && tokenRes.data) { const { accessToken, expireTime } = tokenRes.data ctx.yingshiTokenRes = { token: accessToken, expire: expireTime } yingshiToken = accessToken } else { throw '未能获取进行萤石鉴权' } } const deviceRes = await app.fs.yingshiRequest.post(`lapp/device/list`, { query: { accessToken: yingshiToken, } }) ctx.status = 200; ctx.body = [] || (deviceRes.data || []).map(item => { return { ...item, token: yingshiToken, } }) } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } } module.exports = { videoList };