多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 数据库设计 ```mysql DROP DATABASE IF EXISTS lost; CREATE DATABASE lost; use lost; DROP TABLE IF EXISTS `think_event`; CREATE TABLE `think_event` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `user_id` int(11) COMMENT '用户ID', `type` varchar(10) NOT NULL COMMENT '事件类型:lost|found', `title` varchar(50) NOT NULL COMMENT '事件标题', `linkman` varchar(50) NOT NULL COMMENT '联系人', `mobile` varchar(50) NOT NULL COMMENT '联系电话', `create_time` int(11) COMMENT '事件发生时间', `image_ids` varchar(500) COMMENT '关联的图片ID串', `tags` varchar(500) COMMENT 'tags', `address` varchar(500) COMMENT '事件发生的地点', `description` TEXT COMMENT '简要描述', `status` int(11) COMMENT '状态', `finished` int(11) COMMENT '事件已经结束?', `deleted` int(11) DEFAULT 0 COMMENT '事件被发布人取消?' ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='失物招领事件表'; DROP TABLE IF EXISTS `think_image`; CREATE TABLE IF NOT EXISTS `think_image` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id自增', `storage` varchar(50) NOT NULL, `path` varchar(255) NOT NULL DEFAULT '' COMMENT '路径', `base_url` varchar(255) NOT NULL DEFAULT '' COMMENT '图片链接', `md5` char(32) NOT NULL DEFAULT '' COMMENT '文件md5', `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1; -- -- 表的结构 `think_user` -- /*用户基础信息,记录登陆账号信息*/ DROP TABLE IF EXISTS `think_user`; CREATE TABLE `think_user` ( `id` int(10) AUTO_INCREMENT PRIMARY KEY NOT NULL, `username` varchar(50) NOT NULL, `user_type` varchar(50) NOT NULL DEFAULT 'user', #注册用户的类型,user,school,student,teacher,company `mobile` varchar(11) DEFAULT NULL, `email` varchar(100) DEFAULT NULL, `password` varchar(32) NOT NULL, `encrypt` varchar(10) NOT NULL, `avator_id` int DEFAULT 0 COMMENT '头像在image库中的ID', `last_login_time` int DEFAULT 0, `last_login_ip` varchar(50) DEFAULT '', `login_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '登录次数', `login_fail_count` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '登录失败次数', `deletable` tinyint(2) NOT NULL DEFAULT 0 COMMENT '可删除,默认不可删除', `status` tinyint(2) NOT NULL DEFAULT 1 COMMENT '状态,默认激活', `deleted_flag` int NOT NULL DEFAULT 0, `create_time` int NOT NULL DEFAULT 0, `create_ip` varchar(50) NOT NULL DEFAULT '' ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; -- -- 转存表中的数据 `think_user` -- password:123456 -- INSERT INTO `think_user` (`id`, `username`, `password`, `encrypt`) VALUES (1, 'admin', 'ce9ac4724c59b5167ae89ecc345a57c9', 'Cc6bSCcc3N'), (2, 'guest', 'ce9ac4724c59b5167ae89ecc345a57c9', 'Cc6bSCcc3N'), (3, 'user', 'ce9ac4724c59b5167ae89ecc345a57c9', 'Cc6bSCcc3N'); /*用户的会员信息*/ DROP TABLE IF EXISTS `think_user_member`; CREATE TABLE `think_user_member` ( `uid` int NOT NULL, `puid` int NOT NULL DEFAULT 0, `invitation_code` VARCHAR(6) NULL DEFAULT NULL COMMENT '邀请码', `qq` varchar(15) DEFAULT NULL, `github` varchar(100) DEFAULT NULL, `zhihu` varchar(100) DEFAULT NULL, `weibo` varchar(100) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; INSERT INTO `think_user_member` (`uid`, `puid`, `invitation_code`) VALUES (1, 0, 'ROOT12'), (2, 0, 'ADMIN1'), (3, 0, 'USER12'); ``` ## 修改配置文件 修改`/src/config/adapter.js`,设置数据库连接 ```js /** * model adapter config * @type {Object} */ exports.model = { type: 'mysql', common: { logConnect: isDev, logSql: isDev, logger: msg => think.logger.info(msg) }, mysql: { handle: mysql, database: 'lost', prefix: 'think_', encoding: 'utf8', host: '127.0.0.1', port: '3306', user: 'root', password: 'root', dateStrings: true } }; ``` ## 添加测试数据 文件:src/controller/index.js ```js const Base = require('./base.js'); const Mock = require('mockjs'); module.exports = class extends Base { indexAction() { return this.display(); } async mockAction() { if (this.isPost) { //生成模拟数据 let mockOption = { 'data|20': [ { 'type|1': ['lost', 'found'], //事件类型 'user_id': 1, 'title|1': '@ctitle(10,20)', //学校名称 'address': '@county(true)', //学校地址 'image_ids': "1,2,3", "linkman": '@cname()', "mobile": /^1[38][1-9]\d{8}/, //联系电话 "create_time": "@integer(1547394126,1557762084)", //时间戳(秒) "status": 1, "finished": 0, 'description': '@cparagraph(10,20)', //介绍 }], }; let data = await Mock.mock(mockOption); //删除已有的数据 await this.model('event').where(true).delete(); //添加新的模拟数据 // for (let item of data.data) { // await this.model('event').add(item); // } await this.model('event').addMany(data.data); data = await this.model('event').select(); return this.success(data); } } }; ``` > 执行测试mockAction,添加测试数据 使用RESTer浏览器插件等发送POST请求执行 `http://localhost:8360/index/mock`。 ![](https://box.kancloud.cn/b96971c5eff04cc4ac34d2d59a27fc72_1014x356.png) 生成的模拟数据样本 ![](https://box.kancloud.cn/5a6bc82294cc8e2f9fa89309b4eadc9c_1172x429.png)