123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- 'use strict';
- const jwt = require('jsonwebtoken');
- // const { parse } = require('urlencode');
- module.exports = {
- resultOK(result, desc) {
- this.body = { errorNo: 0, result, errorDesc: desc };
- this.status = 200;
- },
- resultFail(desc) {
- this.body = { errorNo: 500, errorDesc: desc };
- this.status = 200;
- },
- adminFail(desc) {
- this.body = { code: 500, success: false, result: null, message: desc };
- this.status = 200;
- },
- adminOK(result, desc) {
- this.body = { code: 200, success: true, result, message: desc };
- this.status = 200;
- },
- bodyResult(code, result, desc) {
- this.body = { errorNo: code, result, errorDesc: desc };
- this.status = 200;
- },
- async jwtSign(payload, secret, options) {
- return jwt.sign(payload, secret, options);
- },
- async jwtVerify(token, secret, options) {
- function verify() {
- return new Promise((resolve, reject) => {
- jwt.verify(token, secret, options, function(err, decoded) {
- let result = {};
- if (err) {
- result.verify = false;
- result.message = err.message;
- } else {
- result.verify = true;
- result.message = decoded;
- }
- resolve(result);
- });
- });
- }
- let ret = await verify();
- return ret;
- },
- SqlFmtTime(field, target) {
- return `date_format(${field},'%Y-%m-%d') as ${target?target:field}`;
- },
- async pageSelect(query, table, field, filter, it) {
- let mysql = this.app.mysql;
- let pageNo = query.pageNo;
- let pageSize = query.pageSize;
- if (pageNo == undefined) pageNo = 1;
- if (pageSize == undefined) pageSize = 10;
- pageNo = parseInt(pageNo);
- pageSize = parseInt(pageSize);
- let ret = { records: [], total: 0, };
- let where = filter;
- let sql = `select ${field} from ${table} ${where}`
- console.log(sql);
- let total = await mysql.query(`select count(*) as count from ${table} ` + where);
- ret.total = total[0].count;
- let data = await mysql.query(`${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
- if (it) {
- data.forEach(element => {
- it(element);
- });
- }
- ret.records = data;
- return ret;
- },
- async queryHandleByService(service, fnName) {
- let ctx = this;
- let data = ctx.request.query;
- let ret = await service[fnName](data);
- ctx.adminOK(ret);
- },
- async queryHandleByServiceInApp(service, fnName) {
- let ctx = this;
- let data = ctx.request.query;
- let ret = await service[fnName](data);
- ctx.resultOK(ret);
- },
- async bodyHandleByService(service, fnName, desc) {
- let ctx = this;
- let data = ctx.request.body;
- let ret = await service[fnName](data);
- ctx.adminOK(ret, desc);
- },
- async bodyHandleByServiceInApp(service, fnName, desc) {
- let ctx = this;
- let data = ctx.request.body;
- let ret = await service[fnName](data);
- ctx.resultOK(ret, desc);
- },
- nameFmt2Camel(v) {
- function parse(obj) {
- if (typeof obj != 'object') return;
- let keys = Object.keys(obj);
- keys.forEach(k => {
- let ks = k.split("_");
- if (ks.length > 1) {
- let n = ks.length;
- for (let i = 1; i < n; i++) {
- let kname = ks[i];
- ks[i] = kname[0].toUpperCase() + kname.slice(1);
- }
- let camelKey = ks.join("");
- obj[camelKey] = obj[k];
- delete obj[k];
- }
- });
- }
- let isArr = Array.isArray(v);
- if (isArr) {
- v.forEach(c => parse(c));
- return;
- }
- parse(v);
- },
- nameFmtCamel2UnderLine(v) {
- function parse(obj) {
- if (typeof obj != 'object') return;
- let keys = Object.keys(obj);
- keys.forEach(k => {
- let ks = k.split("_");
- if (ks.length == 1) { //下划线模式的不用转
- let n = k.length;
- let words = [];
- let lastStart = 0;
- for (let i = 0; i < n; i++) {
- let char = k[i];
- if (char >= 'A' && char <= 'Z') {
- if (i > 0) {
- let s = k.substr(lastStart, i - lastStart);
- words.push(s.toLocaleLowerCase());
- lastStart = i;
- }
- }
- if (i == n - 1 && lastStart > 0) {
- let s = k.substr(lastStart, i - lastStart + 1);
- words.push(s.toLocaleLowerCase());
- }
- }
- if (words.length > 1) {
- let nkey = words.join("_");
- obj[nkey] = obj[k];
- delete obj[k];
- }
- }
- });
- }
- let isArr = Array.isArray(v);
- if (isArr) {
- v.forEach(c => parse(c));
- return;
- }
- parse(v);
- },
- //要求数据结构id, parent_id=0
- fmt2Tree(ret, fn) {
- let nodes = {};
- ret.forEach(r => {
- nodes[r.id] = r;
- });
- let treeList = [];
- let cacheNodes = {};
- function parseNode(node) {
- let extObj = fn(node);
- if (!extObj) return;
- let parentId = node.parent_id;
- if (parentId == undefined) parentId = node.parentId;
- if (parentId == 0) { //根节点
- let cnode = cacheNodes[node.id];
- if (treeList.indexOf(cnode) < 0) { //还没有加入List
- let curNode = { children: [], ...extObj };
- treeList.push(curNode);
- cacheNodes[node.id] = curNode;
- return curNode;
- }
- return cnode;
- } else {
- //处理父亲节点
- let parentId = node.parent_id;
- if (parentId == undefined) parentId = node.parentId;
- let parentNode = nodes[parentId];
- let parent = cacheNodes[parentNode.id];
- if (!parent) {
- parent = parseNode(parentNode);
- if (!parent) return;
- }
- //处理自己
- let curNode = { children: [], ...extObj };
- parent.children.push(curNode);
- cacheNodes[node.id] = curNode;
- return curNode;
- }
- }
- ret.forEach(node => {
- parseNode(node);
- });
- function checkNode(node) {
- node.isLeaf = node.children.length < 1;
- if (node.children.length < 1) {
- node.children = null;
- node.isLeaf = true;
- return;
- }
- node.children.forEach(c => checkNode(c));
- }
- treeList.forEach(c => checkNode(c));
- return treeList;
- },
- getStaticUrl(uri) {
- let config = this.app.config;
- return config.server.publicHost + uri;
- },
- async getLoginedUser() {
- let ctx = this;
- let authToken = ctx.header.authorization;
- if (!authToken) {
- return null;
- }
- let config = this.app.config.loginAdmin;
- let ret = await ctx.jwtVerify(authToken, config.secret);
- if (!ret.verify) {
- null;
- }
- return ret.message;
- }
- };
|