cloud.js 6.7 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const egg_1=require("egg"),moment=require("moment"),validate_1=require("../utils/validate"),normalize_1=require("../utils/normalize"),responseCode_1=require("../utils/responseCode");class CloudController extends egg_1.Controller{async overview(){const{response:i,service:a}=this.ctx,b=moment().format(),c=moment().subtract(1,"day").format(),[d,e,[f,g]]=await Promise.all([a.evaluation.bestStationInRange(c),a.evaluation.summarizeRangeWithoutStation(b),a.station.checkUpdated(this.config.app.checkUpdateInterval)]);i.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,{bestStationInYesterday:d,statistic:e,stationNum:f,stationActive:g})}async statisticAll(){const{ctx:h,service:a}=this,b=["day","week","month","year"].includes(h.query.range)?h.query.range:"day",c="day"===b?1:"week"===b?7:"month"===b?30:365,d=moment().format(),e=moment().subtract(c-1,"day").format(),f=await a.evaluation.summarizeRangeWithoutStation(e,d);h.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,f)}async statisticAllByTime(){const{ctx:m,service:a,ctx:{response:b}}=this,c=["week","month","year"].includes(m.query.range)?m.query.range:"week",d="week"===c?7:"month"===c?30:365,e=moment().subtract(d-1,"day").format(),f=moment().format(),g=await a.evaluation.selectRange(e,f);if(!Array.isArray(g)||0===g.length)return void(b.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,{}));let h,n;"year"===c?(h="month",n="YYYY-MM"):(h="day",n="MM-DD");const o={};g.forEach(c=>{const a=moment(c.date,"YYYY-MM-DD").format(n);o[a]?(o[a].excellent+=c.excellent,o[a].good+=c.good,o[a].ordinary+=c.ordinary,o[a].bad+=c.bad,o[a].abandoned+=c.abandoned):o[a]=c});const k="year"===c?12:d;for(let c=0;c<k;c++){const a=moment().subtract(c,h).format(n);o[a]||(o[a]={excellent:0,good:0,ordinary:0,bad:0,abandoned:0})}b.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,o)}async rank(){const{ctx:h,service:a}=this;if(validate_1.validate(h,{start:{required:!0,type:"date"},end:{required:!0,type:"date"}},!0)){const{start:b,end:c}=h.request.query,d=await a.evaluation.rankInRange(b,c),e=h.query.limit&&parseInt(h.query.limit,10)===+h.query.limit?+h.query.limit:d.length,f=h.query.offset&&parseInt(h.query.offset,10)===+h.query.offset?+h.query.offset:0;h.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,d.slice(f,e+f))}}async searchAllStation(){const{service:f,ctx:a}=this,b=(a.query.search||"").trim(),c="true"===a.query.disable?1:0,d=await f.station.searchAll(b,c);a.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,d.map(b=>({stationId:b.id,stationName:b.name,disable:0!==b.disable,active:b.active})))}async evaluationInRange(){const{ctx:g,service:a}=this;if(validate_1.validate(g,{stationId:{required:!0,type:"string"},startDate:{required:!0,type:"date"},endDate:{required:!0,type:"date"}},!0)){const{stationId:b,startDate:c,endDate:d}=g.request.query,e=await a.evaluation.selectRange(c,d,b);g.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,{stationId:b,evaluations:e.map(b=>({date:b.date,excellent:b.excellent,good:b.good,ordinary:b.ordinary,bad:b.bad,abandoned:b.abandoned}))})}}async handleAdminLogin(){const{ctx:f,service:a}=this;if(validate_1.validate(f,{adminName:{required:!0,type:"string",min:1,max:20},password:{required:!0,type:"string",min:1,max:20}})){const{adminName:b,password:c}=f.request.body,d=await a.admin.isAdmin(b,c);if(f.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,d),d){const c=`${f.ip.replace(/\./g,"")}.${Date.now()}.${b}`;f.cookies.set("token",c,{httpOnly:!0,encrypt:!0,overwrite:!0}),a.record.add(b,f.ip,"login",""),f.logger.info(`Administrator ${b} from ${f.ip} logins.`)}}}async handleAdminLogout(){const{ctx:b}=this;b.cookies.set("token","",{httpOnly:!0,encrypt:!0,overwrite:!0,maxAge:100}),b.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK)}async handleStationAdd(){const{ctx:g,service:a}=this;if(validate_1.validate(g,{stationName:{required:!0,type:"string",min:1,max:20},stationId:{type:"string",required:!0,allowEmpty:!1,format:/^[\dA-z]{8}$/}})){const{stationName:b,stationId:c}=g.request.body,d=await a.station.isIdExisted(c);if(d)g.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.STATION_REPEAT,null,"\u6CB9\u7AD9\u7F16\u7801\u5DF2\u88AB\u4F7F\u7528");else{const d=await a.station.add(b,c);g.response.body=d?normalize_1.NMLResponse(responseCode_1.ResponseCode.OK):normalize_1.NMLResponse(responseCode_1.ResponseCode.FAIL);const e=g.cookies.get("token",{encrypt:!0}).split(".")[2];a.record.add(e,g.ip,"addStation",`${c} / ${b}`),g.logger.info(`Administrator ${e} from ${g.ip} adds station: ${c}, ${b}`)}}}async handleStationModify(){const{ctx:f,service:a}=this;if(validate_1.validate(f,{stationName:{required:!0,type:"string",min:1,max:20},stationId:{required:!0,type:"string",min:8,max:8}})){const{stationName:b,stationId:c}=f.request.body,d=await a.station.isIdExisted(c);if(d){const d=await a.station.updateName(c,b);if(d){f.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK);const d=f.cookies.get("token",{encrypt:!0}).split(".")[2];a.record.add(d,f.ip,"modifyStationName",`${c} / ${b}`),f.logger.info(`Administrator ${d} from ${f.ip} modify station's name: ${c} / ${b}`)}else f.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.UNKNOWN,null,"\u64CD\u4F5C\u5931\u8D25")}else f.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.PARAM_ERROR,null,"\u6CB9\u7AD9\u7F16\u7801\u9519\u8BEF")}}async disableStation(){const{ctx:e,service:a}=this;if(validate_1.validate(e,{stationId:{required:!0,type:"string",min:8,max:8}})){const{stationId:b}=e.request.body,c=await a.station.disable(b);if(0===c.affectedRows)e.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.STATION_ILEGAL,null,"\u6CB9\u7AD9\u4E0D\u5B58\u5728");else{e.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,null,"\u64CD\u4F5C\u6210\u529F");const c=e.cookies.get("token",{encrypt:!0}).split(".")[2];a.record.add(c,e.ip,"disableStation",`${b}`),e.logger.info(`Administrator ${c} from ${e.ip} disable station: ${b}`)}}}async enableStation(){const{ctx:e,service:a}=this;if(validate_1.validate(e,{stationId:{required:!0,type:"string",min:8,max:8}})){const{stationId:b}=e.request.body,c=await a.station.enable(b);if(0===c.affectedRows)e.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.STATION_ILEGAL,null,"\u6CB9\u7AD9\u4E0D\u5B58\u5728");else{e.response.body=normalize_1.NMLResponse(responseCode_1.ResponseCode.OK,null,"\u64CD\u4F5C\u6210\u529F");const c=e.cookies.get("token",{encrypt:!0}).split(".")[2];a.record.add(c,e.ip,"enableStation",`${b}`),e.logger.info(`Administrator ${c} from ${e.ip} enable station: ${b}`)}}}}exports.default=CloudController;