UNPKG

thinknode

Version:

A fast, flexible and all-in-one web framework for node.js.

531 lines (399 loc) 12.6 kB
'use strict'; exports.__esModule = true; var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _Base = require('../../Base'); var _Base2 = _interopRequireDefault(_Base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _class = function (_base) { (0, _inherits3.default)(_class, _base); function _class() { (0, _classCallCheck3.default)(this, _class); return (0, _possibleConstructorReturn3.default)(this, _base.apply(this, arguments)); } _class.prototype.init = function init(http) { this.http = http; }; /** * 空方法 * @private */ _class.prototype.__empty = function __empty() { return THINK.statusAction(this.http, 404, 'action not found'); }; /** * 是否是GET请求 * @return {Boolean} [description] */ _class.prototype.isGet = function isGet() { return this.http.isGet(); }; /** * 是否是POST请求 * @return {Boolean} [description] */ _class.prototype.isPost = function isPost() { return this.http.isPost(); }; /** * 是否是特定METHOD请求 * @param {[type]} method [description] * @return {Boolean} [description] */ _class.prototype.isMethod = function isMethod(method) { return this.http.method === method.toUpperCase(); }; /** * 是否是AJAX请求 * @return {Boolean} [description] */ _class.prototype.isAjax = function isAjax(method) { return this.http.isAjax(method); }; /** * 是否是restful请求 */ _class.prototype.isRestful = function isRestful() { return this.http.isRestful; }; /** * 是否是jsonp接口 * @param name * @returns {Boolean} */ _class.prototype.isJsonp = function isJsonp(name) { return this.http.isJsonp(name); }; /** * token功能 * @param check 为true时验证token,否则获取token * @returns {boolean} */ _class.prototype.token = function token() { var check = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; return this.http.token(check); }; /** * 获取及构造QUERY参数 * @param name * @param value * @returns {*} */ _class.prototype.get = function get(name, value) { return this.http.get(name, value); }; /** * 获取及构造POST参数 * @param name * @param value * @returns {Object|String|type[]|*} */ _class.prototype.post = function post(name, value) { return this.http.post(name, value); }; /** * 获取post或get参数,post优先 * @param name * @returns {type[]|*|Object|String} */ _class.prototype.param = function param(name) { return this.http.param(name); }; /** * 获取及构造上传的文件 * @param name * @param value * @returns {*} */ _class.prototype.file = function file(name, value) { return this.http.file(name, value); }; /** * header操作 * @param name * @param value * @returns {type[]} */ _class.prototype.header = function header(name, value) { return this.http.header(name, value); }; /** * 获取userAgent * @returns {String|type[]|*} */ _class.prototype.userAgent = function userAgent() { return this.http.userAgent(); }; /** * 获取referrer * @param host * @returns {String|*} */ _class.prototype.referer = function referer(host) { return this.http.referrer(host); }; /** * cookie操作 * @param {[type]} name [description] * @param {[type]} value [description] * @param {[type]} options [description] * @return {[type]} [description] */ _class.prototype.cookie = function cookie(name, value, options) { return this.http.cookie(name, value, options); }; /** * session操作 * @param name * @param value * @param timeout * @returns {*|type[]} */ _class.prototype.session = function session(name, value, timeout) { return this.http.session(name, value, timeout); }; /** * 赋值变量到模版 * @param {[type]} name [description] * @param {[type]} value [description] * @return {[type]} [description] */ _class.prototype.assign = function assign(name, value) { !this.tVar && (this.tVar = {}); if (name === undefined) { return this.tVar; } if (THINK.isString(name) && arguments.length === 1) { return this.tVar[name]; } if (THINK.isObject(name)) { for (var key in name) { this.tVar[key] = name[key]; } } else { this.tVar[name] = value; } return null; }; /** * alias assign * @param {any} name * @param {any} value */ _class.prototype.set = function set(name, value) { return this.assign(name, value); }; /** * 输出模版 * @param templateFile * @param charset * @param contentType * @returns {*} */ _class.prototype.display = function display(templateFile, charset, contentType) { var _this2 = this; return this.fetch(templateFile).then(function (content) { charset = charset || THINK.config('encoding'); if (!_this2.http.typesend) { contentType = contentType || THINK.config('tpl_content_type'); _this2.http.type(contentType, charset); } if (THINK.config('show_exec_time')) { _this2.http.sendTime(); } return _this2.http.end(content || '', charset); }); }; /** * alias display * @param {any} templateFile * @param {any} charset * @param {any} contentType * @returns */ _class.prototype.render = function render(templateFile, charset, contentType) { return this.display(templateFile, charset, contentType); }; /** * 模板引擎渲染 * @param templateFile * @returns {*} */ _class.prototype.fetch = function fetch(templateFile) { var _this3 = this; //内容过滤 !this.tVar && (this.tVar = {}); return THINK.run('view_filter', this.http, this.tVar).then(function (data) { _this3.tVar = data; //渲染模板 return THINK.run('view_parse', _this3.http, { 'var': _this3.tVar, 'file': templateFile }); }); }; /** * 设置http响应状态码 * @param status */ _class.prototype.status = function status() { var _status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 200; return this.http.status(_status); }; /** * 阻止访问 * @param {Number} status [status code] * @return {[type]} [] */ _class.prototype.deny = function deny() { var status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 403; return THINK.statusAction(this.http, status); }; /** * 设置Cache-Control及失效时间 * @param time */ _class.prototype.expires = function expires(time) { return this.http.expires(time); }; /** * url跳转 * @param url * @param code * @returns {*} */ _class.prototype.redirect = function redirect(url, code) { return this.http.redirect(url, code); }; /** * 发送Content-Type * @param ext * @param encoding * @returns {*} */ _class.prototype.type = function type(ext) { var encoding = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : THINK.config('encoding'); return this.http.type(ext, encoding); }; /** * 发送执行时间 * @param name * @returns {type[]} */ _class.prototype.sendTime = function sendTime(name) { return this.http.sendTime(name); }; /** * json格式输出数据 * @param data * @returns {*} */ _class.prototype.json = function json(data) { this.type(THINK.config('json_content_type')); return this.http.end(data); }; /** * jsonp格式输出数据 * @param data * @returns {*} */ _class.prototype.jsonp = function jsonp(data) { var callback = this.get(THINK.config('url_callback_name')); //过滤callback值里的非法字符 callback = callback.replace(/[^\w\.]/g, ''); if (callback) { data = callback + '(' + (data !== undefined ? (0, _stringify2.default)(data) : '') + ')'; } this.type(THINK.config('json_content_type')); return this.http.end(data); }; /** * 操作成功后格式化的json数据输出 * @param errmsg * @param data * @param code * @returns {*} */ _class.prototype.success = function success(errmsg, data) { var _obj; var code = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 200; // let obj = THINK.getObject(['status', THINK.config('error_no_key'), THINK.config('error_msg_key')], [1, code, errmsg || '']); var obj = (_obj = { 'status': 1 }, _obj[THINK.config('error_no_key')] = code, _obj[THINK.config('error_msg_key')] = errmsg || '', _obj); if (data !== undefined) { obj.data = data; } else { obj.data = {}; } this.type(THINK.config('json_content_type')); return this.http.end(obj); }; /** * alias success * @param {any} errmsg * @param {any} data * @param {any} code * @returns */ _class.prototype.ok = function ok(errmsg, data, code) { return this.success(errmsg, data, code); }; /** * 操作异常后格式化的json数据输出 * @param errmsg * @param data * @param code * @returns {type[]} */ _class.prototype.error = function error(errmsg, data) { var _obj2; var code = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 500; // let obj = THINK.getObject(['status', THINK.config('error_no_key'), THINK.config('error_msg_key')], [0, code, (THINK.isError(errmsg) ? errmsg.message : errmsg) || 'error']); var obj = (_obj2 = { 'status': 0 }, _obj2[THINK.config('error_no_key')] = code, _obj2[THINK.config('error_msg_key')] = (THINK.isError(errmsg) ? errmsg.message : errmsg) || 'error', _obj2); if (data !== undefined) { obj.data = data; } else { obj.data = {}; } this.type(THINK.config('json_content_type')); return this.http.end(obj); }; /** * alias error * @param {any} errmsg * @param {any} data * @param {any} code * @returns */ _class.prototype.fail = function fail(errmsg, data, code) { return this.error(errmsg, data, code); }; /** * 输出内容 * 自动JSON.stringify * 自定将数字等转化为字符串 * @param obj * @param contentType * @param encoding * @returns {*} */ _class.prototype.echo = function echo(obj, contentType, encoding) { contentType = contentType || THINK.config('tpl_content_type'); this.type(contentType); return this.http.end(obj, encoding); }; return _class; }(_Base2.default); /** * * @author richen * @copyright Copyright (c) 2015 - <richenlin(at)gmail.com> * @license MIT * @version 15/11/26 */ exports.default = _class;