<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC=2,4] `think.controller.rest`?繼承自?[think.controller.base](https://thinkjs.org/zh-CN/doc/2.0/api_controller.html),用來處理 Rest 接口。 ##### 使用 ES6 的語法繼承該類 ~~~ export default class extends think.controller.rest { } ~~~ ##### 使用普通方式繼承該類 ~~~ module.exports = think.controller("rest", { }) ~~~ ### 屬性 #### controller._isRest 標識此 controller 對應的是 Rest 接口。如果在?`init`?方法里將該屬性設置為`false`,那么該 controller 不再是一個 Rest 接口。 #### controller._method 獲取 method 方式。默認從 http method 中獲取,但有些客戶端不支持發送 delete, put 之類的請求,所以可以設置為從 GET 里的一個參數獲取。 ~~~ export default class extends think.controller.rest { init(http){ super.init(http); //設置 _method,表示從 GET 參數獲取 _method 字段的值 //如果沒有取到,則從 http method 中獲取 this._method = "_method"; } } ~~~ #### controller.resource 當前 Rest 對應的 Resource 名稱。 #### controller.id 資源 ID #### controller.modelInstance 資源對應 model 的實例。 ### 方法 #### controller.__before() 可以在魔術方法`__before`中進行字段過濾,分頁,權限校驗等功能。 ~~~ export default class extends think.controller.rest{ __before(){ //過濾 password 字段 this.modelInstance.field("password", true); } } ~~~ #### controller.getAction() 獲取資源數據,如果有 id,拉取一條,否則拉取列表。 ~~~ //方法實現,可以根據需要修改 export default class extends think.controller.rest { * getAction(){ let data; if (this.id) { let pk = yield this.modelInstance.getPk(); data = yield this.modelInstance.where({[pk]: this.id}).find(); return this.success(data); } data = yield this.modelInstance.select(); return this.success(data); } } ~~~ #### controller.postAction() 添加數據 ~~~ //方法實現,可以根據需要修改 export default class extends think.controller.rest { * postAction(){ let pk = yield this.modelInstance.getPk(); let data = this.post(); delete data[pk]; if(think.isEmpty(data)){ return this.fail("data is empty"); } let insertId = yield this.modelInstance.add(data); return this.success({id: insertId}); } } ~~~ #### controller.deleteAction() 刪除數據 ~~~ //方法實現,可以根據需要修改 export default class extends think.controller.rest { * deleteAction(){ if (!this.id) { return this.fail("params error"); } let pk = yield this.modelInstance.getPk(); let rows = yield this.modelInstance.where({[pk]: this.id}).delete(); return this.success({affectedRows: rows}); } } ~~~ #### controller.putAction() 更新數據 ~~~ //方法實現,可以根據需要修改 export default class extends think.controller.rest { * putAction(){ if (!this.id) { return this.fail("params error"); } let pk = yield this.modelInstance.getPk(); let data = this.post(); delete data[pk]; if (think.isEmpty(data)) { return this.fail("data is empty"); } let rows = yield this.modelInstance.where({[pk]: this.id}).update(data); return this.success({affectedRows: rows}); } } ~~~ #### controller.__call() 找不到方法時調用 ~~~ export default class extends think.controller.rest { __call(){ return this.fail(think.locale("ACTION_INVALID", this.http.action, this.http.url)); } } ~~~ 文檔地址:[https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_controller_rest.md](https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_controller_rest.md)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看