think.controller.rest
繼承自 think.controller.base,用來處理 Rest 接口。
注意:如果客戶端是get請求過來自動執(zhí)行g(shù)etAction()方法,是post請求就執(zhí)行postAction()方法,是delete請求就執(zhí)行deleteAction()方法,是put請求就執(zhí)行putAction()方法
// 使用 ES6 的語法繼承該類
export default class extends think.controller.rest {
}
// 使用普通方式繼承該類
module.exports = think.controller('rest', {
})
data = await this.modelInstance.where(filter).select()
data = data.map((obj)=> {
delete obj.USEROPENID; //刪除obj.USEROPENID這個key (因為我們不想返回這個USEROPENID給用戶)
delete obj.HELPEROPENID; //刪除obj.HELPEROPENID這個key(因為我們不想返回這個HELPEROPENID給用戶)
return obj
});
屬性
controller._isRest
標(biāo)識此 controller 對應(yīng)的是 Rest 接口。如果在 init 方法里將該屬性設(shè)置為 false,那么該 controller 不再是一個 Rest 接口。
controller._method
獲取 method 方式。默認(rèn)從 http method 中獲取,但有些客戶端不支持發(fā)送 DELETE, PUT 類型的請求,所以可以設(shè)置為從 GET 參數(shù)里獲取。
export default class extends think.controller.rest {
init(http){
super.init(http);
//設(shè)置 _method,表示從 GET 參數(shù)獲取 _method 字段的值
//如果沒有取到,則從 http method 中獲取
this._method = '_method';
}
}
controller.resource
當(dāng)前 Rest 對應(yīng)的 Resource 名稱。
controller.id
資源 ID
controller.modelInstance
資源對應(yīng) model 的實例。
方法
controller.__before()
可以在魔術(shù)方法 __before 中進(jìn)行字段過濾、分頁、權(quán)限校驗等功能。
export default class extends think.controller.rest{
__before(){
this.modelInstance.field('password', true); //過濾 password 字段(也就是不返回該字段)
//this.modelInstance.fieldReverse('user,name'); //隱藏 user 和 name 字段(也就是不返回該字段)
}
}
controller.getAction()
獲取資源數(shù)據(jù),如果有 id,拉取一條,否則拉取列表。
//方法實現(xiàn),可以根據(jù)需要修改
export default class extends think.controller.rest {
async getAction(){
let data;
// this.id是請求路徑后面帶有數(shù)字例如:http://127.0.0.1:8360/home/colleaguehelp/50 (模塊/控制器/具體主鍵的id)
if (this.id) {
//this.modelInstance.getPk()是獲取到數(shù)據(jù)庫中的所以主鍵集合
let pk = await this.modelInstance.getPk();
data = await this.modelInstance.where({[pk]: this.id}).find(); //find()找到某條返回
return this.success(data);
}
data = await this.modelInstance.select(); //select()返回全部
return this.success(data); //this.success(data)向客戶端返回查找到的數(shù)據(jù)
}
}
controller.postAction()
添加數(shù)據(jù)
//方法實現(xiàn),可以根據(jù)需要修改
export default class extends think.controller.rest {
async postAction(){
let pk = await this.modelInstance.getPk();
let data = this.get();
delete data[pk];
if(think.isEmpty(data)){
return this.fail('data is empty');
}
let insertId = await this.modelInstance.add(data);
return this.success({id: insertId});
}
}
controller.putAction()
更新數(shù)據(jù)
//方法實現(xiàn),可以根據(jù)需要修改
export default class extends think.controller.rest {
async putAction(){
if (!this.id) {
return this.fail('params error');
}
let pk = await this.modelInstance.getPk();
let data = this.get();
delete data[pk];
if (think.isEmpty(data)) {
return this.fail('data is empty');
}
let rows = await this.modelInstance.where({[pk]: this.id}).update(data);
return this.success({affectedRows: rows});
}
}
controller.__call()
找不到方法時調(diào)用
export default class extends think.controller.rest {
__call(){
return this.fail(think.locale('ACTION_INVALID', this.http.action, this.http.url));
}
}
請求的一些例子:
let data = [];
const now = new Date();
const today = now.getFullYear() * 10000 + (now.getMonth() + 1) * 100 + now.getDate();
data = await this.modelInstance.where({ //modelInstance會找到數(shù)據(jù)庫表名跟所在的控制器名對應(yīng)起來
STATE: [2, 5], //STATE為2或5
VALIDDATE: {'<' : today} //VALIDDATE小于today
}).limit(5).order('ENDTIME DESC').select(); //limit(5)限制5條,order('ENDTIME DESC')按字段ENDTIME進(jìn)行降序,DESC是降序
data = data.map((obj)=> {
delete obj.OPENID; //刪除OPENID
delete obj.USEROPENID; //刪除USEROPENID
return obj;
});