多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # Event控制器 Event为该应用的主要控制器,实现RESTful的API,对应丢失/捡到信息的发布、查询、删除、状态的更新 文件位置:src/controller/event.js ## 重载getAction 实现如下的请求: | Method | 调用实例 | 说明 | | --- | --- | --- | | GET | http://localhost:8360/event/1 | 获取ID=1的事件内容 | | GET | http://localhost:8360/event?type=lost | 检索类型为lost的所哟记录 | | GET | http://localhost:8360/event?page=1&pageSize=10 | 分页检索 | | GET | http://localhost:8360/event?key=钢笔&page=1&pageSize=10 | 分页检索关键字包含钢笔的记录 | > 关键字支持模糊匹配,支持多字段检索 ```js const BaseRest = require('./rest.js'); module.exports = class extends BaseRest { async getAction() { let data; if (this.id) { const pk = this.modelInstance.pk; data = await this.modelInstance.where({ [pk]: this.id }).find(); return this.success(data); } let map = {}; let type = this.get('type'); //丢失还是捡到 if (type) { map['type'] = type; //支持lost,found两种类型 } let key = this.get('key'); if (key) { map['title|address|mobile|linkman|description'] = ['LIKE', '%' + key + '%']; //支持关键字查找,模糊匹配 } const page = this.get('page'); if (page) { const pageSize = this.get('pageSize') || 10; data = await this.modelInstance.page(page, pageSize).where(map).countSelect(); this.success(data); } else { data = await this.modelInstance.where(map).select(); return this.success(data); } } }; ``` ## 增加/修改/删除记录 实现以下功能调用 | Method | 调用实例 | 说明 | | --- | --- | --- | | PUT |http://localhost:8360/event/1 | 更新ID=1的记录, 更新数据为JSON格式 | | POST|http://localhost:8360/event/1 | POST JSON格式的数据 | | DELETE|http://localhost:8360/event/1 | 删除ID=1的记录 | # 数据校验逻辑 文件 src/logic/event.js ```js module.exports = class extends think.Logic { async getAction() { // 验证逻辑错误 this.allowMethods = 'get'; // 允许 GET、POST 请求类型 const rules = { type: { // 处理事件类型 string: true, // 字段类型为 String 类型 required: false, // 字段必填 in: ['lost', 'found'], // 需要是 lost,found 其中一个 trim: true, // 字段需要trim处理 method: 'GET' // 指定获取数据的方式 }, key: { // 处理关键字检索 string: true, // 字段类型为 String 类型 required: false, // 字段必填 trim: true, // 字段需要trim处理 method: 'GET' // 指定获取数据的方式 } }; const flag = this.validate(rules); if (!flag) { return this.fail('validate error', this.validateErrors); // 如果校验失败,返回 // {"errno":1000,"errmsg":"validate error","data":{"username":"username can not be blank"}} } } async postAction() { this.allowMethods = 'post'; // 允许 GET、POST 请求类型 const rules = { address: { // 事件发生的地址 string: true, // 字段类型为 String 类型 required: true, // 字段必填 trim: true, // 字段需要trim处理 method: 'POST' // 指定获取数据的方式 }, title: { // 标题 string: true, // 字段类型为 String 类型 required: true, // 字段必填 trim: true, // 字段需要trim处理 method: 'POST' // 指定获取数据的方式 }, type: { // 处理事件类型 string: true, // 字段类型为 String 类型 required: true, // 字段必填 in: ['lost', 'found'], // 需要是 lost,found 其中一个 trim: true, // 字段需要trim处理 method: 'POST' // 指定获取数据的方式 }, mobile: { // 联系人手机号码 string: true, // 字段类型为 String 类型 required: true, // 字段必填 trim: true, // 字段需要trim处理 regexp: /^1[38][1-9]\d{8}/g, // 正则表达式验证手机号码 method: 'POST' // 指定获取数据的方式 } }; const flag = this.validate(rules); if (!flag) { return this.fail('validate error', this.validateErrors); // 如果校验失败,返回 // {"errno":1000,"errmsg":"validate error","data":{"username":"username can not be blank"}} } } }; ```