[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"}}
}
}
};
```
- 內容介紹
- EcmaScript基礎
- 快速入門
- 常量與變量
- 字符串
- 函數的基本概念
- 條件判斷
- 數組
- 循環
- while循環
- for循環
- 函數基礎
- 對象
- 對象的方法
- 函數
- 變量作用域
- 箭頭函數
- 閉包
- 高階函數
- map/reduce
- filter
- sort
- Promise
- 基本對象
- Arguments 對象
- 剩余參數
- Map和Set
- Json基礎
- RegExp
- Date
- async
- callback
- promise基礎
- promise-api
- promise鏈
- async-await
- 項目實踐
- 標簽系統
- 遠程API請求
- 面向對象編程
- 創建對象
- 原型繼承
- 項目實踐
- Classes
- 構造函數
- extends
- static
- 項目實踐
- 模塊
- import
- export
- 項目實踐
- 第三方擴展庫
- immutable
- Vue快速入門
- 理解MVVM
- Vue中的MVVM模型
- Webpack+Vue快速入門
- 模板語法
- 計算屬性和偵聽器
- Class 與 Style 綁定
- 條件渲染
- 列表渲染
- 事件處理
- 表單輸入綁定
- 組件基礎
- 組件注冊
- Prop
- 自定義事件
- 插槽
- 混入
- 過濾器
- 項目實踐
- 標簽編輯
- 移動客戶端開發
- uni-app基礎
- 快速入門程序
- 單頁程序
- 底部Tab導航
- Vue語法基礎
- 模版語法
- 計算屬性與偵聽器
- Class與Style綁定
- 樣式與布局
- Box模型
- Flex布局
- 內置指令
- 基本指令
- v-model與表單
- 條件渲染指令
- 列表渲染指令v-for
- 事件與自定義屬性
- 生命周期
- 項目實踐
- 學生實驗
- 貝店商品列表
- 加載更多數據
- 詳情頁面
- 自定義組件
- 內置組件
- 表單組件
- 技術專題
- 狀態管理vuex
- Flyio
- Mockjs
- SCSS
- 條件編譯
- 常用功能實現
- 上拉加載更多數據
- 數據加載綜合案例
- Teaset UI組件庫
- Teaset設計
- Teaset使用基礎
- ts-tag
- ts-badge
- ts-button
- ta-banner
- ts-list
- ts-icon
- ts-load-more
- ts-segmented-control
- 代碼模版
- 項目實踐
- 標簽組件
- 失物招領客戶端原型
- 發布頁面
- 檢索頁面
- 詳情頁面
- 服務端開發技術
- 服務端開發環境配置
- Koajs快速入門
- 快速入門
- 常用Koa中間件介紹
- 文件上傳
- RestfulApi
- 一個復雜的RESTful例子
- 使用Mockjs生成模擬數據
- Thinkjs快速入門
- MVC模式
- Thinkjs介紹
- 快速入門
- RESTful服務
- RBAC案例
- 關聯模型
- 應用開發框架
- 服務端開發
- PC端管理界面開發
- 移動端開發
- 項目實踐
- 失物招領項目
- 移動客戶端UI設計
- 服務端設計
- 數據庫設計
- Event(事件)
- 客戶端設計
- 事件列表頁面
- 發布頁面
- 事件詳情頁面
- API設計
- image
- event
- 微信公眾號開發
- ui設計規范