peng.peng
12 months ago
13 changed files with 260 additions and 51 deletions
@ -0,0 +1,38 @@ |
|||
/** |
|||
* Created by PengLing on 2017/7/3. |
|||
*/ |
|||
'use strict'; |
|||
|
|||
const request = require('superagent'); |
|||
|
|||
module.exports = { |
|||
entry: (app, router, opts) => { |
|||
const apiRoot = `${opts.host}${opts.match}`; |
|||
const req = { |
|||
get: (url) => { |
|||
return request.get(`${apiRoot}${url}`); |
|||
}, |
|||
|
|||
post: (url, data) => { |
|||
return request |
|||
.post(`${apiRoot}${url}`) |
|||
.set('Content-Type', 'application/json') |
|||
.send(data); |
|||
}, |
|||
|
|||
put: (url, data) => { |
|||
return request |
|||
.put(`${apiRoot}${url}`) |
|||
.set('Content-Type', 'application/json') |
|||
.send(data); |
|||
}, |
|||
|
|||
delete: (url) => { |
|||
return request.del(`${apiRoot}${url}`); |
|||
} |
|||
}; |
|||
|
|||
app.iota = app.iota || {}; |
|||
app.iota.request = req; |
|||
} |
|||
}; |
@ -0,0 +1,69 @@ |
|||
'use strict'; |
|||
const moment = require('moment') |
|||
|
|||
module.exports = function (app, opts) { |
|||
|
|||
let axyTokenCache = { |
|||
userId: null, |
|||
orgId: null, |
|||
token: null, |
|||
expireTime: null //过期时间
|
|||
} |
|||
|
|||
let getAnxinyunToken = async function () { |
|||
try { |
|||
if (!axyTokenCache.token || moment() > moment(axyTokenCache.expireTime)) { |
|||
const dataToAxy = { |
|||
pcode: 'fce4afe2-5b6a-408a-ab18-a2afa7fa027c', |
|||
username: '123456', |
|||
password: '123456', |
|||
} |
|||
const axyResponse = await app.fs.waterReq.post('/login', dataToAxy) |
|||
const axyData = axyResponse.body |
|||
if (axyData.authorized) { |
|||
axyTokenCache.userId = axyData.id //放进缓存
|
|||
axyTokenCache.orgId = axyData.orgId //放进缓存
|
|||
axyTokenCache.token = axyData.token //放进缓存
|
|||
axyTokenCache.expireTime = moment().add(23, 'hour').format('YYYY-MM-DD HH:mm:ss') |
|||
} |
|||
} |
|||
return axyTokenCache |
|||
} catch (err) { |
|||
app.fs.logger.error(`utils: fetch anxinyun token, error: ${err}`); |
|||
} |
|||
} |
|||
|
|||
let rslt = {}; |
|||
async function getDataFromAxy(url, method, data) { |
|||
try { |
|||
if (rslt[url]) { |
|||
getData(url, method, data) |
|||
return rslt[url] |
|||
} else { |
|||
let res = await getData(url, method, data) |
|||
return res |
|||
} |
|||
} catch (err) { |
|||
app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); |
|||
} |
|||
} |
|||
|
|||
async function getData(url, method = 'get', data) { |
|||
try { |
|||
let ache = await getAnxinyunToken() |
|||
if (ache) { |
|||
const connector = url.indexOf('?') > -1 ? '&' : '?' |
|||
const res = await app.fs.waterReq[method](`${url}`, { ...data }) |
|||
rslt[url] = res |
|||
return rslt[url] |
|||
} |
|||
} catch (err) { |
|||
app.fs.logger.error(`utils: fetch axy sensors Data, error: ${err}`); |
|||
} |
|||
} |
|||
|
|||
return { |
|||
getAnxinyunToken, |
|||
getDataFromAxy |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
/** |
|||
* Created by PengLing on 2017/7/3. |
|||
*/ |
|||
'use strict' |
|||
|
|||
const request = require('superagent'); |
|||
|
|||
class AxyReq { |
|||
constructor(root) { |
|||
this.apiRoot = root |
|||
} |
|||
|
|||
get(url) { |
|||
return request.get(`${this.apiRoot}${url}`); |
|||
} |
|||
|
|||
post(url, data) { |
|||
return request.post(`${this.apiRoot}${url}`).set('Content-Type', 'application/json').send(data) |
|||
} |
|||
|
|||
put(url, data) { |
|||
return request.put(`${this.apiRoot}${url}`).set('Content-Type', 'application/json').send(data) |
|||
} |
|||
|
|||
delete(url) { |
|||
return request.del(`${this.apiRoot}${url}`) |
|||
} |
|||
} |
|||
|
|||
module.exports = function (app, opts) { |
|||
const req = new AxyReq(opts.waterUrl) |
|||
app.fs.waterReq = req |
|||
} |
Loading…
Reference in new issue