# 新建/application/controllers/common/base.js
```
const fs = require('fs');
const path = require('path');
var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var controller = require(tools.rootPath + 'libs/controller.js');
const functions = require(tools.appPath + 'functions');
var ejs = require('ejs');
module.exports = class extends controller {
constructor() {
super();
//上一页地址
this.FORWARD = null;
//请求地址
this.REQUEST_URI = null;
//模板名称
this.TPL = null;
//req
this.req = null;
//res
this.res = null;
//session
this._session = {};
//模板变量
this._template_var = {};
//模块
this.MM = '';
//控制器
this.CC = '';
//操作
this.AA = '';
//是否是post请求
this.isPost = false;
//是否是get请求
this.isGet = false;
}
//初始化方法
async init() {
this.REQUEST_URI = this.req.REQUEST_URI;
this.TPL = this.req.TPL;
this._session = this.req.session;
this.MM = this.req.MM;
this.CC = this.req.CC;
this.AA = this.req.AA;
this.isPost = this.req.isPost;
this.isGet = this.req.isGet;
let FORWARD = this.post('FORWARD');
if (FORWARD) {
this.FORWARD = encodeURI(FORWARD);
} else {
this.FORWARD = this.REQUEST_URI;
}
//other
//ip地址
this.ip = this.req.headers['x-real-ip'];
//浏览器标识
this.userAgent = this.req.headers["user-agent"];
this.assign({
MM: this.MM,
CC: this.CC,
AA: this.AA,
FORWARD: this.FORWARD,
REQUEST_URI: this.REQUEST_URI,
VIEWROOT: tools.viewPath,
});
return true;
}
//前置方法
async __before() {
return true;
}
//消息提示函数
msg(code, msg = '', url = '', data = []) {
if (code === true) code = 1;
if (code === false) code = 0;
if (!this.isAjax()) {
let html = '';
if (code) {
if (!msg) msg = '操作成功';
let jump = `location.href='${url}';`;
if (!url) jump = "window.history.back();";
html = `<html><head><meta charset="utf-8"></head><body>加载中...<script>alert('${msg}');${jump}</script></body></html>`;
} else {
if (!msg) msg = '操作失败';
html = `<html><head><meta charset="utf-8"></head><body>加载中...<script>alert('${msg}');window.history.back();</script></body></html>`;
}
return this.res.end(html);
} else {
if (code) {
if (!msg) msg = '操作成功';
} else {
if (!msg) msg = '操作失败';
}
let d = {
'code': code,
'msg': msg,
'url': url,
'data': data,
};
return this.res.end(this.json(d));
}
}
//session操作函数
async session(name, val = '') {
if (val === null) {
delete this._session[name];
}
if (val === '') {
return typeof (this._session[name]) != 'undefined' ? this._session[name] : null;
}
if (name && val) {
this._session[name] = val;
}
}
//模板变量赋值函数
assign(obj, val = '') {
if (typeof (obj) == 'string') {
this._template_var[obj] = val;
} else {
Object.assign(this._template_var, obj);
}
}
//加载模板函数
tpl(path = '', params = {}) {
var f = path ? path : this.TPL;
var str = fs.readFileSync(tools.viewPath + f + '.html', 'utf-8');
let p = this._template_var;
Object.assign(p, params);
return ejs.render(str, p);
}
//json函数
json(v) {
return this.res.end(JSON.stringify(v));
}
//跳转函数
redirect(url) {
this.res.redirect(url);
}
//post变量函数
post(key = '') {
if (key) {
return typeof (this.req.body[key]) != 'undefined' ? this.req.body[key] : null;
}
return typeof (this.req.body) != 'undefined' ? this.req.body : {};
}
//get变量函数
get(key = '') {
if (key) {
return typeof (this.req.query[key]) != 'undefined' ? this.req.query[key] : null;
}
return typeof (this.req.query) != 'undefined' ? this.req.query : {};
}
//是否是ajax请求
isAjax() {
return this.req.xhr;
}
//referer函数
referer() {
return this.REQUEST_URI;
}
//输出函数
p(str) {
console.log(str);
this.res.end(str);
}
//model加载函数
model(name) {
var m = require(tools.modelPath + name + '.js');
return new m();
}
//操作调用函数
async action(controller, action = undefined, path = '') {
let c = require(path ? path + '/' + controller : tools.controllerPath + controller);
let object = new c();
object.req = this.req;
object.res = this.res;
await object.init();
await object.__before();
if (!action) return object;
return await object[action](this.req, this.res);
}
};
```
- 课程介绍
- 开发环境搭建
- 安装express.js框架
- 为diy自己的web框架做准备(1)
- 为diy自己的web框架做准备(2)
- 为应用绑定域名
- 封装控制器基类base.js
- 封装数据库操作基类model.js
- curd操作-准备工作
- curd操作-文章列表
- curd操作-添加文章
- curd操作-编辑文章
- curd操作-删除文章
- model文件的使用
- 文件上传
- session实现登录
- 邮件发送
- 文件下载
- 执行子任务
- 图片缩放
- 图片裁剪
- 图片验证码
- Excel读取与写入
- 编写计划任务
- 工具函数使用实例
- websocket
- 集成ckeditor
- 微信公众号开发-1:内网穿透
- 微信公众号开发-2:自动回复
- 微信公众号开发-3:api接口调用
- 微信公众号开发-4:oauth登录
- 微信公众号开发-5:沙箱支付
- 微信公众号开发-6:真实支付
- 项目上线运行
- 项目代码下载