From b7e5ee5fec7b7074938f0c0c6b83a006f0188030 Mon Sep 17 00:00:00 2001 From: robin <1032740078@qq.com> Date: Tue, 2 Aug 2022 19:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.test.js | 2 +- app/message/model/Message.js | 27 +- app/message/test/index.js | 37 +- app/permission/api/getUserInfo.js | 19 ++ app/permission/api/organizationAdd.js | 19 ++ app/permission/components/encrypt/index.js | 5 + .../components/organization/index.js | 10 + .../components/registered/accountPassword.js | 4 +- app/permission/model/Organization.js | 10 +- app/permission/model/OrganizationSetting.js | 13 + app/permission/model/Permission.js | 2 +- app/permission/model/Role.js | 2 +- app/permission/model/User.js | 4 +- app/permission/model/UserInfo.js | 6 +- app/table/api/getList.js | 27 ++ app/table/api/jsonTasks.js | 12 + app/table/model/Directory.js | 20 ++ bamboo.sql | 318 ++++++++++++++++++ extend/mysql/api.js | 28 +- extend/mysql/index.js | 30 +- extend/mysql/operatorsAliases.js | 83 +++-- extend/mysql/tool.js | 3 +- extend/redis/config.js | 4 +- middleware/modelToApi/config.js | 4 - middleware/modelToApi/index.js | 27 -- middleware/router/index.js | 3 +- package.json | 1 + 27 files changed, 564 insertions(+), 156 deletions(-) create mode 100644 app/permission/api/getUserInfo.js create mode 100644 app/permission/api/organizationAdd.js create mode 100644 app/permission/components/organization/index.js create mode 100644 app/permission/model/OrganizationSetting.js create mode 100644 app/table/api/getList.js create mode 100644 app/table/api/jsonTasks.js create mode 100644 app/table/model/Directory.js create mode 100644 bamboo.sql delete mode 100644 middleware/modelToApi/config.js delete mode 100644 middleware/modelToApi/index.js diff --git a/app.test.js b/app.test.js index 9965c87..170265c 100644 --- a/app.test.js +++ b/app.test.js @@ -36,7 +36,7 @@ beforeAll(() => { return new Promise((resolve, reject) => { bamboo.fullList.push(async (app) => { server = app.server - //回复测试数据镜像sql文件 + //恢复测试数据镜像sql文件 await app.db.tool.reduction() resolve() }) diff --git a/app/message/model/Message.js b/app/message/model/Message.js index a1ca813..eb3e24b 100644 --- a/app/message/model/Message.js +++ b/app/message/model/Message.js @@ -1,19 +1,20 @@ module.exports = { - doc: "消息表", - api: true,//是否需要生成api接口 + doc : "消息表", + api : true,//是否需要生成api接口 model: { - uid: {type: "STRING", comment: '发送人用户id'}, - name: {type: "STRING", comment: '发送人名称'}, - avatar: {type: "STRING", comment: '发送人头像'}, - to_uid: {type: "STRING", comment: '接收人用户id'}, - to_name: {type: "STRING", comment: '接收人名称'}, + oid : {type: "INTEGER", comment: '发送人用户机构id'}, + uid : {type: "INTEGER", comment: '发送人用户id'}, + name : {type: "STRING", comment: '发送人名称'}, + avatar : {type: "STRING", comment: '发送人头像'}, + to_uid : {type: "STRING", comment: '接收人用户id'}, + to_name : {type: "STRING", comment: '接收人名称'}, to_avatar: {type: "STRING", comment: '接收人头像'}, - status: {type: "STRING", comment: '状态:已读/未读:y/n'}, - type: {type: "STRING", comment: '消息类型:file / image / text / event'}, - sendTime: {type: "STRING", comment: '消息发送时间'}, - content: {type: "STRING", comment: '消息内容,如果type=file,此属性表示文件的URL地址'}, - fileSize: {type: "STRING", comment: '文件大小'}, - fileName: {type: "STRING", comment: '文件名称'}, + status : {type: "STRING", comment: '状态:已读/未读:y/n'}, + type : {type: "STRING", comment: '消息类型:file / image / text / event'}, + sendTime : {type: "STRING", comment: '消息发送时间'}, + content : {type: "STRING", fieldNameCus: '消息内容', comment: '消息内容,如果type=file,此属性表示文件的URL地址'}, + fileSize : {type: "STRING", comment: '文件大小'}, + fileName : {type: "STRING", comment: '文件名称'}, }, } diff --git a/app/message/test/index.js b/app/message/test/index.js index 5a5f0f5..7436288 100644 --- a/app/message/test/index.js +++ b/app/message/test/index.js @@ -1,39 +1,6 @@ module.exports = { - describe_name: "permission/权限管理", + describe_name: "message/消息中心", testList : [ - { - test_name: "账号密码注册", - fun : async (request, server) => { - const response = await request(server) - .post('/permission/api/registered') - .send({ - type: "accountPassword", - data: { - account : "test", - password: "123456", - }, - }) - // expect(response.status).toEqual(200) - expect(response.body.status).toEqual(200) - expect(response.body.data).toHaveProperty('token') - } - }, - { - test_name: "账号密码登录", - fun : async (request, server) => { - const response = await request(server) - .post('/permission/api/login') - .send({ - type: "accountPassword", - data: { - account : "test", - password: "123456", - }, - }) - // expect(response.status).toEqual(200) - expect(response.body.status).toEqual(200) - expect(response.body.data).toHaveProperty('token') - } - }, + ] } diff --git a/app/permission/api/getUserInfo.js b/app/permission/api/getUserInfo.js new file mode 100644 index 0000000..d9715a3 --- /dev/null +++ b/app/permission/api/getUserInfo.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = { + params: { + "uid": "string",//数据 + "general_management": "string",//数据 + }, + fun : async (ctx, app) => { + const {name, general_management} = ctx.params + if (await $components.registered[type](data)) { + return $res.success({ + token: await $components.jwt.sign({}) + }, "注册成功") + } + + return $res.error({}, "登录失败") + + } +} diff --git a/app/permission/api/organizationAdd.js b/app/permission/api/organizationAdd.js new file mode 100644 index 0000000..23c9a16 --- /dev/null +++ b/app/permission/api/organizationAdd.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = { + params: { + "name": "string",//数据 + "general_management": "string",//数据 + }, + fun : async (ctx, app) => { + const {name, general_management} = ctx.params + if (await $components.registered[type](data)) { + return $res.success({ + token: await $components.jwt.sign({}) + }, "注册成功") + } + + return $res.error({}, "登录失败") + + } +} diff --git a/app/permission/components/encrypt/index.js b/app/permission/components/encrypt/index.js index f7aa6ba..12e1b2a 100644 --- a/app/permission/components/encrypt/index.js +++ b/app/permission/components/encrypt/index.js @@ -1,4 +1,5 @@ const crypto = require('crypto'); +const shortid = require('shortid'); module.exports = { // 随机数(盐值) getRandomSalt() { @@ -20,4 +21,8 @@ module.exports = { cryptPwdVerification(password, salt, user_password_md5) { return this.cryptPwd(password, salt) === user_password_md5; }, + // 返回随机短uuid + shortid() { + return shortid.generate() + }, } diff --git a/app/permission/components/organization/index.js b/app/permission/components/organization/index.js new file mode 100644 index 0000000..25175e0 --- /dev/null +++ b/app/permission/components/organization/index.js @@ -0,0 +1,10 @@ +module.exports = { + //添加机构 + async add(name, general_management) { + $db.table('Organization').data({ + oid: $components.encrypt.shortid(), + name, + general_management + }).save() + }, +} diff --git a/app/permission/components/registered/accountPassword.js b/app/permission/components/registered/accountPassword.js index 7953a40..eb5e011 100644 --- a/app/permission/components/registered/accountPassword.js +++ b/app/permission/components/registered/accountPassword.js @@ -7,9 +7,9 @@ * @return {boolean} 注册 是否 成功. */ module.exports = async (data) => { - const {account, password} = data + const {account, password, oid} = data const salt = app.components.encrypt.getRandomSalt() const md5 = app.components.encrypt.cryptPwd(password, salt) - await app.db.table("User").data({account, password: md5, salt}).save() + await app.db.table("User").data({account, password: md5, salt, to_oid: oid}).save() return true } diff --git a/app/permission/model/Organization.js b/app/permission/model/Organization.js index 11fff7c..8e0f561 100644 --- a/app/permission/model/Organization.js +++ b/app/permission/model/Organization.js @@ -1,10 +1,10 @@ module.exports = { - doc: "组织表", - api: true,//是否需要生成api接口 + doc : "机构表", + api : true,//是否需要生成api接口 model: { - oid: {type: "STRING", comment: '组织id'}, - name: {type: "STRING", comment: '组织名称'}, - general_management: {type: "STRING", comment: '是否为总管理平台(可以管理所有组织)'}, + oid : {type: "INTEGER", comment: '机构id', primaryKey: true}, + name : {type: "STRING", comment: '机构名称'}, + general_management: {type: "STRING", comment: '是否为总管理平台(可以管理所有机构):y/n'}, }, } diff --git a/app/permission/model/OrganizationSetting.js b/app/permission/model/OrganizationSetting.js new file mode 100644 index 0000000..b9437ee --- /dev/null +++ b/app/permission/model/OrganizationSetting.js @@ -0,0 +1,13 @@ +module.exports = { + doc: "组织配置表", + api: true,//是否需要生成api接口 + model: { + oid: {type: "STRING", comment: '组织id'}, + name: {type: "STRING", comment: '名称'}, + type: {type: "STRING", comment: '类型:string/number/json'}, + code: {type: "STRING", comment: '识别码'}, + value: {type: "STRING", comment: '权限值'}, + }, +} + + diff --git a/app/permission/model/Permission.js b/app/permission/model/Permission.js index 996c014..1ddc25d 100644 --- a/app/permission/model/Permission.js +++ b/app/permission/model/Permission.js @@ -2,7 +2,7 @@ module.exports = { doc: "权限列表", api: true,//是否需要生成api接口 model: { - pid: {type: "STRING", comment: '权限id'}, + pid: {type: "INTEGER", comment: '权限id', primaryKey: true}, name: {type: "STRING", comment: '权限名称'}, type: {type: "STRING", comment: '权限类型:接口/字段/表格/按钮:jk/zd/bg/an'}, code: {type: "STRING", comment: '权限识别码'}, diff --git a/app/permission/model/Role.js b/app/permission/model/Role.js index cf1e48c..fd08bf8 100644 --- a/app/permission/model/Role.js +++ b/app/permission/model/Role.js @@ -2,7 +2,7 @@ module.exports = { doc: "角色表", api: true,//是否需要生成api接口 model: { - rid: {type: "STRING", comment: '角色id'}, + rid: {type: "INTEGER", comment: '角色id', primaryKey: true}, name: {type: "STRING", comment: '角色名称'}, to_oid: {type: "STRING", comment: '所属组织id'}, }, diff --git a/app/permission/model/User.js b/app/permission/model/User.js index 638df5b..016eba1 100644 --- a/app/permission/model/User.js +++ b/app/permission/model/User.js @@ -2,12 +2,10 @@ module.exports = { doc: "用户表", api: true,//是否需要生成api接口 model: { - uid: {type: "STRING", comment: '用户id'}, + uid: {type: "INTEGER", comment: '用户id', primaryKey: true}, account: {type: "STRING", comment: '用户账号'}, password: {type: "STRING", comment: '用户加密密码'}, salt: {type: "STRING", comment: '用户加密随机数'}, - to_oid: {type: "STRING", comment: '所属组织id'}, - to_rid: {type: "STRING", comment: '所属角色id'}, }, } diff --git a/app/permission/model/UserInfo.js b/app/permission/model/UserInfo.js index 1c9b7fe..f0647b5 100644 --- a/app/permission/model/UserInfo.js +++ b/app/permission/model/UserInfo.js @@ -2,13 +2,15 @@ module.exports = { doc: "用户资料表", api: true,//是否需要生成api接口 model: { - uid: {type: "STRING", comment: '用户id'}, + uid: {type: "INTEGER", comment: '用户id'}, + to_oid: {type: "INTEGER", comment: '所属组织id'}, + to_rid: {type: "INTEGER", comment: '所属角色id'}, account: {type: "STRING", comment: '用户账号'}, name: {type: "STRING", comment: '名称'}, avatar: {type: "STRING", comment: '头像'}, wechat_openid: {type: "STRING", comment: '微信openid'}, phone_number: {type: "STRING", comment: '手机号'}, - age: {type: "STRING", comment: '年龄'}, + age: {type: "INTEGER", comment: '年龄'}, sex: {type: "STRING", comment: '性别'}, province: {type: "STRING", comment: '省份'}, city: {type: "STRING", comment: '城市'}, diff --git a/app/table/api/getList.js b/app/table/api/getList.js new file mode 100644 index 0000000..7397800 --- /dev/null +++ b/app/table/api/getList.js @@ -0,0 +1,27 @@ +'use strict'; +//获取数据库模型列表 +module.exports = { + params: {}, + fun : async (ctx, app) => { + const {} = ctx.params + const data = Object.keys($db.models).map(item => { + let tableAttributes = $db.models[item].tableAttributes + let ta = {} + Object.keys(tableAttributes).forEach(ta_item => { + ta[ta_item] = { + fieldName: tableAttributes[ta_item].fieldName,//字段名 + field : tableAttributes[ta_item].field,//字段 + headName : tableAttributes[ta_item].fieldNameCus,//字段自定义名 + comment : tableAttributes[ta_item].comment,//注释 + + } + }) + return { + tableName : $db.models[item].tableName, + tableAttributes: ta, + primaryKeyField: $db.models[item].primaryKeyField, + } + }) + return data + } +} diff --git a/app/table/api/jsonTasks.js b/app/table/api/jsonTasks.js new file mode 100644 index 0000000..d76a34b --- /dev/null +++ b/app/table/api/jsonTasks.js @@ -0,0 +1,12 @@ +'use strict'; + +module.exports = { + params: { + "taskList": "array", + }, + fun : async (ctx, app) => { + const {taskList} = ctx.params + const data = await $db.jsonTasks(taskList) + return data + } +} diff --git a/app/table/model/Directory.js b/app/table/model/Directory.js new file mode 100644 index 0000000..c888cb7 --- /dev/null +++ b/app/table/model/Directory.js @@ -0,0 +1,20 @@ +module.exports = { + doc : "数据库模型映射到前端目录表", + api : true,//是否需要生成api接口 + model: { + // id : {type: "INTEGER", comment: '导航id', primaryKey: true}, + name: {type: "STRING", comment: '导航名称'}, + path: {type: "STRING", comment: '导航路径'}, + index: {type: "INTEGER", comment: '权重,越大越靠前'}, + icon : {type: "STRING", comment: '图标'}, + group: {type: "STRING", comment: '导航分组'}, + table: {type: "STRING", comment: '绑定的表名'}, + table_key: {type: "STRING", comment: '表的主键名称,一般是id'}, + search: {type: "STRING", comment: '模糊查询的字段用/分割'}, + table_field: {type: "JSON", comment: '表格字段'}, + table_field_key: {type: "JSON", comment: '已选择的字段'}, + user: {type: "JSON", comment: '查询表字段的时候,默认带上的用户数据,比如["oid"],这样用户就只能看到自己机构的数据'}, + }, +} + + diff --git a/bamboo.sql b/bamboo.sql new file mode 100644 index 0000000..3f80f1f --- /dev/null +++ b/bamboo.sql @@ -0,0 +1,318 @@ +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Contact +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Contact` ( + `uid` varchar(255) DEFAULT NULL COMMENT '用户id/群id', + `to_uid` varchar(255) DEFAULT NULL COMMENT '接收人用户id', + `type` varchar(255) DEFAULT NULL COMMENT '类型,单人/群:one/group', + `status` varchar(255) DEFAULT NULL COMMENT '状态:已结束/未结束:y/n', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Group +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Group` ( + `gid` varchar(255) DEFAULT NULL COMMENT '分组id', + `name` varchar(255) DEFAULT NULL COMMENT '分组名称', + `to_gid` varchar(255) DEFAULT NULL COMMENT '上级分组id', + `to_oid` varchar(255) DEFAULT NULL COMMENT '所属组织id', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Message +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Message` ( + `uid` varchar(255) DEFAULT NULL COMMENT '发送人用户id', + `name` varchar(255) DEFAULT NULL COMMENT '发送人名称', + `avatar` varchar(255) DEFAULT NULL COMMENT '发送人头像', + `to_uid` varchar(255) DEFAULT NULL COMMENT '接收人用户id', + `to_name` varchar(255) DEFAULT NULL COMMENT '接收人名称', + `to_avatar` varchar(255) DEFAULT NULL COMMENT '接收人头像', + `status` varchar(255) DEFAULT NULL COMMENT '状态:已读/未读:y/n', + `type` varchar(255) DEFAULT NULL COMMENT '消息类型:file / image / text / event', + `sendTime` varchar(255) DEFAULT NULL COMMENT '消息发送时间', + `content` varchar(255) DEFAULT NULL COMMENT '消息内容,如果type=file,此属性表示文件的URL地址', + `fileSize` varchar(255) DEFAULT NULL COMMENT '文件大小', + `fileName` varchar(255) DEFAULT NULL COMMENT '文件名称', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Organization +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Organization` ( + `oid` varchar(255) DEFAULT NULL COMMENT '组织id', + `name` varchar(255) DEFAULT NULL COMMENT '组织名称', + `general_management` varchar(255) DEFAULT NULL COMMENT '是否为总管理平台(可以管理所有组织):y/n', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: OrganizationSetting +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `OrganizationSetting` ( + `oid` varchar(255) DEFAULT NULL COMMENT '组织id', + `name` varchar(255) DEFAULT NULL COMMENT '名称', + `type` varchar(255) DEFAULT NULL COMMENT '类型:string/number/json', + `code` varchar(255) DEFAULT NULL COMMENT '识别码', + `value` varchar(255) DEFAULT NULL COMMENT '权限值', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Permission +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Permission` ( + `pid` varchar(255) DEFAULT NULL COMMENT '权限id', + `name` varchar(255) DEFAULT NULL COMMENT '权限名称', + `code` varchar(255) DEFAULT NULL COMMENT '权限识别码', + `value` varchar(255) DEFAULT NULL COMMENT '权限值', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + `type` varchar(255) DEFAULT NULL COMMENT '权限类型:接口/字段/表格/按钮:jk/zd/bg/an', + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: PermissionClass +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `PermissionClass` ( + `name` varchar(255) DEFAULT NULL COMMENT '权限分类名称', + `pccode` varchar(255) DEFAULT NULL COMMENT '权限分类识别码', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: Role +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `Role` ( + `rid` varchar(255) DEFAULT NULL COMMENT '角色id', + `name` varchar(255) DEFAULT NULL COMMENT '角色名称', + `to_oid` varchar(255) DEFAULT NULL COMMENT '所属组织id', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: RoleToPermission +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `RoleToPermission` ( + `to_rid` varchar(255) DEFAULT NULL COMMENT '角色id', + `to_pid` varchar(255) DEFAULT NULL COMMENT '权限id', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: User +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `User` ( + `uid` varchar(255) DEFAULT NULL COMMENT '用户id', + `account` varchar(255) DEFAULT NULL COMMENT '用户账号', + `password` varchar(255) DEFAULT NULL COMMENT '用户加密密码', + `salt` varchar(255) DEFAULT NULL COMMENT '用户加密随机数', + `to_oid` varchar(255) DEFAULT NULL COMMENT '所属组织id', + `to_rid` varchar(255) DEFAULT NULL COMMENT '所属角色id', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB AUTO_INCREMENT = 1043 DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: UserInfo +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `UserInfo` ( + `uid` varchar(255) DEFAULT NULL COMMENT '用户id', + `account` varchar(255) DEFAULT NULL COMMENT '用户账号', + `name` varchar(255) DEFAULT NULL COMMENT '名称', + `avatar` varchar(255) DEFAULT NULL COMMENT '头像', + `wechat_openid` varchar(255) DEFAULT NULL COMMENT '微信openid', + `phone_number` varchar(255) DEFAULT NULL COMMENT '手机号', + `age` varchar(255) DEFAULT NULL COMMENT '年龄', + `sex` varchar(255) DEFAULT NULL COMMENT '性别', + `province` varchar(255) DEFAULT NULL COMMENT '省份', + `city` varchar(255) DEFAULT NULL COMMENT '城市', + `area` varchar(255) DEFAULT NULL COMMENT '地区', + `address` varchar(255) DEFAULT NULL COMMENT '详细地址', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: autoRes +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `autoRes` ( + `type` varchar(255) DEFAULT NULL COMMENT '类型,自动回复/问候语:auto/hello', + `status` varchar(255) DEFAULT NULL COMMENT '状态:显示/隐藏:y/n', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# SCHEMA DUMP FOR TABLE: role +# ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS `role` ( + `uid` varchar(255) DEFAULT NULL COMMENT '用户id', + `age` int(11) DEFAULT '0' COMMENT '年龄', + `age2` int(11) DEFAULT '0' COMMENT '年龄', + `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表自增id', + `createdAt` datetime NOT NULL, + `updatedAt` datetime NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Contact +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Group +# ------------------------------------------------------------ + +INSERT INTO + `Group` ( + `gid`, + `name`, + `to_gid`, + `to_oid`, + `id`, + `createdAt`, + `updatedAt` + ) +VALUES + ( + '1', + '1', + NULL, + NULL, + 1, + '2022-06-18 03:53:29', + '2022-06-18 03:53:32' + ); + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Message +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Organization +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: OrganizationSetting +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Permission +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: PermissionClass +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: Role +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: RoleToPermission +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: User +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: UserInfo +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: autoRes +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# DATA DUMP FOR TABLE: role +# ------------------------------------------------------------ + + +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/extend/mysql/api.js b/extend/mysql/api.js index 9c5d870..e84371f 100644 --- a/extend/mysql/api.js +++ b/extend/mysql/api.js @@ -43,7 +43,11 @@ module.exports = class Api { * @param {object} value 筛选条件对象. */ where(value) { - this.tableWhere = value + if (!this.tableWhere) { + this.tableWhere = value + } else { + Object.assign(this.tableWhere, value) + } return this } @@ -84,12 +88,26 @@ module.exports = class Api { return this } + /** + * 模糊查询 + * @param {string} value 关键字:你好. + * @param {array} fieldList 字段列表['createdAt']. + */ + search({value, fieldList}) { + if (!value|| !fieldList) return this + if (!this.tableWhere) this.tableWhere = {} + this.tableWhere['$or'] = fieldList.map(item => { + return {[item]: {'$like': `%${value || ''}%`}} + }) + return this + } + /** * 常用时间筛选 * @param {string|array|Number} value 时间内容:按时间段:['2000-1-1','2000-1-2'],按常用时间:day,按最近60分钟:60. * @param {string} field 时间字段(默认createdAt字段) */ - time(value, field) { + time({value, field}) { if (!this.tableName) { throw "请传表名" } @@ -251,7 +269,7 @@ module.exports = class Api { * @param {number} min 字段减小后的值不能小于最小值 * @param {number} setValue 如果减小后的字段小于min,设置字段值为n */ - async setDec(min, setValue) { + async setDec({min, setValue}) { if (!this.tableName) { throw "请传表名" } @@ -317,7 +335,7 @@ module.exports = class Api { * @param {number} max 字段增加后的值不能大于最大值 * @param {number} setValue 如果增加后的字段大于max,设置字段值为n */ - async setInc(max, setValue) { + async setInc({max, setValue}) { if (!this.tableName) { throw "请传表名" } @@ -494,6 +512,7 @@ module.exports = class Api { ...options } ) + this.init() return res } @@ -523,6 +542,7 @@ module.exports = class Api { ...options } ) + this.init() return res } diff --git a/extend/mysql/index.js b/extend/mysql/index.js index 8e92c4d..b42527c 100644 --- a/extend/mysql/index.js +++ b/extend/mysql/index.js @@ -16,7 +16,7 @@ module.exports = async (app) => { const {database, username, password, options,} = config const sequelize = new Sequelize(database, username, password, { ...options, - operatorsAliases, + operatorsAliases:operatorsAliases, }); // 加载model文件 @@ -24,22 +24,32 @@ module.exports = async (app) => { list.forEach(item => { const {res, parse} = item const model = {} + let primaryKey = false for (let key of Object.keys(res.model)) { res.model[key].type = Sequelize[res.model[key].type] model[key] = res.model[key] + //如果表设置了主键,那么就不设置默认主键为id了 + if (res.model[key].primaryKey) { + primaryKey = true + res.model[key].allowNull = false + res.model[key].unique = key + res.model[key].autoIncrement = true + } } - model['id'] = { - type : Sequelize.INTEGER, - comment : '表自增id', - allowNull : false, - unique : 'id', - primaryKey : true, - autoIncrement: true, + //如果没有默认主键 + if (!primaryKey) { + model['id'] = { + type : Sequelize.INTEGER, + comment : '表自增id', + allowNull : false, + unique : 'id', + primaryKey : true, + autoIncrement: true, + } } + sequelize.define(parse.name, model) }) - - app.models = sequelize.models const api = new Api(sequelize, sequelize.models) app.db = api diff --git a/extend/mysql/operatorsAliases.js b/extend/mysql/operatorsAliases.js index 01fb063..91a0881 100644 --- a/extend/mysql/operatorsAliases.js +++ b/extend/mysql/operatorsAliases.js @@ -1,46 +1,43 @@ //定义别名 const Sequelize = require("sequelize"); -module.exports = () => { - const Op = Sequelize.Op; - //操作符别名 - const operatorsAliases = { - $eq: Op.eq, - $ne: Op.ne, - $gte: Op.gte, - $gt: Op.gt, - $lte: Op.lte, - $lt: Op.lt, - $not: Op.not, - $in: Op.in, - $notIn: Op.notIn, - $is: Op.is, - $like: Op.like, - $notLike: Op.notLike, - $iLike: Op.iLike, - $notILike: Op.notILike, - $regexp: Op.regexp, - $notRegexp: Op.notRegexp, - $iRegexp: Op.iRegexp, - $notIRegexp: Op.notIRegexp, - $between: Op.between, - $notBetween: Op.notBetween, - $overlap: Op.overlap, - $contains: Op.contains, - $contained: Op.contained, - $adjacent: Op.adjacent, - $strictLeft: Op.strictLeft, - $strictRight: Op.strictRight, - $noExtendRight: Op.noExtendRight, - $noExtendLeft: Op.noExtendLeft, - $substring: Op.substring, - $startsWith: Op.startsWith, - $endsWith: Op.endsWith, - $and: Op.and, - $or: Op.or, - $any: Op.any, - $all: Op.all, - $values: Op.values, - $col: Op.col - }; - return operatorsAliases +const Op = Sequelize.Op; + +module.exports = { + $eq : Op.eq, + $ne : Op.ne, + $gte : Op.gte, + $gt : Op.gt, + $lte : Op.lte, + $lt : Op.lt, + $not : Op.not, + $in : Op.in, + $notIn : Op.notIn, + $is : Op.is, + $like : Op.like, + $notLike : Op.notLike, + $iLike : Op.iLike, + $notILike : Op.notILike, + $regexp : Op.regexp, + $notRegexp : Op.notRegexp, + $iRegexp : Op.iRegexp, + $notIRegexp : Op.notIRegexp, + $between : Op.between, + $notBetween : Op.notBetween, + $overlap : Op.overlap, + $contains : Op.contains, + $contained : Op.contained, + $adjacent : Op.adjacent, + $strictLeft : Op.strictLeft, + $strictRight : Op.strictRight, + $noExtendRight: Op.noExtendRight, + $noExtendLeft : Op.noExtendLeft, + $substring : Op.substring, + $startsWith : Op.startsWith, + $endsWith : Op.endsWith, + $and : Op.and, + $or : Op.or, + $any : Op.any, + $all : Op.all, + $values : Op.values, + $col : Op.col } diff --git a/extend/mysql/tool.js b/extend/mysql/tool.js index ece5fa9..4e80d27 100644 --- a/extend/mysql/tool.js +++ b/extend/mysql/tool.js @@ -43,7 +43,8 @@ module.exports = class { database: config.database, }); this.file = this.app.root + '/' + config.database + '.sql' - await importer.import(this.file) + // const res = await importer.import(this.file) + // console.log(res); console.timeEnd("reduction") } diff --git a/extend/redis/config.js b/extend/redis/config.js index aa220bf..e50aa52 100644 --- a/extend/redis/config.js +++ b/extend/redis/config.js @@ -1,6 +1,6 @@ module.exports = { - port: 6379, // Redis port - host: "192.168.1.26", // Redis host + port: 6380, // Redis port + host: "127.0.0.1", // Redis host // username: "", // needs Redis >= 6 password: "9715115286", db: 0, // Defaults to 0 diff --git a/middleware/modelToApi/config.js b/middleware/modelToApi/config.js deleted file mode 100644 index 6f5f96f..0000000 --- a/middleware/modelToApi/config.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - path : "app/*/model/*.js", - prefix : "auto",//接口前缀 -} diff --git a/middleware/modelToApi/index.js b/middleware/modelToApi/index.js deleted file mode 100644 index 6a48d56..0000000 --- a/middleware/modelToApi/index.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; -/** - * 加载模型到路由 - * 加载app/api目录下的文件到路由 - */ -const Router = require('koa-router') -const config = require("./config") - -// 错误处理 -module.exports = { - sort : 999, //排序 - use : false, // 是否使用 - loadFun: async (app, fun) => { // 自行定义中间件的加载方式,将覆盖默认加载方法 - const router = await fun(app) - app.use(router.routes()).use(router.allowedMethods()) - }, - fun : async (app) => { - const router = new Router({ //设置前缀 - prefix: config.prefix - }); - let list = await app.load(config.path) - list.forEach(item => { - - }) - return router - } -} diff --git a/middleware/router/index.js b/middleware/router/index.js index 059f84c..5c88ec0 100644 --- a/middleware/router/index.js +++ b/middleware/router/index.js @@ -58,9 +58,8 @@ module.exports = { }) }) - + //默认根路由 router.all('/', async (ctx, next) => { - ctx.body = app.res.success("ok") ctx.status = 200 await next(); diff --git a/package.json b/package.json index d841b1e..25b68fc 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "require-directory": "^2.1.1", "sequelize": "^6.20.1", "shelljs": "^0.8.5", + "shortid": "^2.2.16", "socket.io": "^4.5.1", "sqlite3": "^5.0.2", "supertest": "^6.2.3",