device.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class DeviceController extends Controller {
  4. //获取服务器状态
  5. async getServerState() {
  6. await this.train("/train/device/state", "get");
  7. }
  8. async shutdownServer() {
  9. await this.train("/train/device/shutdownServer");
  10. }
  11. async startServer() {
  12. let {ctx } = this;
  13. await this.train("/train/device/startServer");
  14. }
  15. async restartServer() {
  16. let {ctx } = this;
  17. await this.train("/train/device/restartServer");
  18. }
  19. async mysql() {
  20. let {ctx } = this;
  21. let ret = await this.app.mysql.query("select id from admin_user where id=4");
  22. ctx.adminOK(ret,"ok");
  23. }
  24. async train( uri, get ) {
  25. let {ctx , app} = this;
  26. let cfg = app.config.serverhosts;
  27. let url = cfg.train + uri;
  28. try {
  29. console.log("====>", url);
  30. let ret = await ctx.curl(url, {headers:{
  31. "Content-Type":"application/json"
  32. },
  33. method:get?get:'post',
  34. dataType:"json"
  35. });
  36. console.log( ret.data );
  37. if( ret.data && ret.data.code == 200 ) {
  38. ctx.adminOK( ret.data.result, ret.data.message );
  39. return;
  40. }
  41. ctx.adminFail(ret.data.message);
  42. } catch (error) {
  43. console.log("curl train server error")
  44. ctx.adminFail("未启动");
  45. }
  46. }
  47. }
  48. module.exports = DeviceController;