mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 19:52:10 +08:00
1
This commit is contained in:
57
app/controller/base.js
Normal file
57
app/controller/base.js
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
const Controller = require('egg').Controller;
|
||||
|
||||
class BaseController extends Controller {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* return success
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendSuccess(data, msg) {
|
||||
const { ctx } = this;
|
||||
ctx.body = {
|
||||
success: true,
|
||||
code: 0,
|
||||
msg,
|
||||
data,
|
||||
};
|
||||
ctx.status = 200;
|
||||
}
|
||||
|
||||
/*
|
||||
* return fail
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendFail(data, msg, code) {
|
||||
const { ctx } = this;
|
||||
ctx.body = {
|
||||
success: false,
|
||||
code,
|
||||
msg,
|
||||
data,
|
||||
};
|
||||
ctx.status = 200;
|
||||
}
|
||||
|
||||
/*
|
||||
* return sendData
|
||||
* @params: object data
|
||||
* @params: string msg
|
||||
* @return: object { success, code, msg, data }
|
||||
*/
|
||||
sendData(data) {
|
||||
const { ctx } = this;
|
||||
ctx.body = data;
|
||||
ctx.status = 200;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseController;
|
||||
22
app/controller/test.js
Normal file
22
app/controller/test.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const BaseController = require('./base');
|
||||
|
||||
class TestController extends BaseController {
|
||||
async index() {
|
||||
const { app, ctx, service } = this;
|
||||
const query = ctx.request.query;
|
||||
console.log('env:%j', app.config.env);
|
||||
const res = 0;
|
||||
const data = {
|
||||
env: app.config.env,
|
||||
};
|
||||
|
||||
|
||||
|
||||
console.log('res:%j', res);
|
||||
this.sendSuccess(data, 'ok');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TestController;
|
||||
28
app/controller/v1/home.js
Normal file
28
app/controller/v1/home.js
Normal file
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const BaseController = require('../base');
|
||||
|
||||
class HomeController extends BaseController {
|
||||
|
||||
async index() {
|
||||
const { ctx } = this;
|
||||
|
||||
const data = {
|
||||
title: 'hello electron-egg'
|
||||
};
|
||||
|
||||
await ctx.render('index.ejs', data);
|
||||
}
|
||||
|
||||
async hello() {
|
||||
const { ctx } = this;
|
||||
|
||||
const data = {
|
||||
title: 'hello'
|
||||
};
|
||||
|
||||
await ctx.render('hello.ejs', data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HomeController;
|
||||
30
app/controller/v1/setting.js
Normal file
30
app/controller/v1/setting.js
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
const BaseController = require('../base');
|
||||
|
||||
class SettingController extends BaseController {
|
||||
|
||||
async autoLaunchEnable() {
|
||||
const self = this;
|
||||
const { ctx } = this;
|
||||
|
||||
const data = {
|
||||
title: 'hello electron-egg'
|
||||
};
|
||||
|
||||
self.sendSuccess(data);
|
||||
}
|
||||
|
||||
async autoLaunchDisable() {
|
||||
const self = this;
|
||||
const { ctx } = this;
|
||||
|
||||
const data = {
|
||||
title: 'hello'
|
||||
};
|
||||
|
||||
self.sendSuccess(data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SettingController;
|
||||
Reference in New Issue
Block a user