# 新建/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:真實支付
- 項目上線運行
- 項目代碼下載