搜索结果(27)

Logic

当在 Action 里处理用户的请求时,经常要先获取用户提交过来的数据,然后对其校验,如果校验没问题后才能进行后续的操作。当参数校验完成后,有时候还要进行权限判断,等这些都判断无误后才能进行真正的逻辑处理。如果将这些代码都放在一个 Action 里,势必让 Action 的代码非常复杂且冗长。

为了解决这个问题, ThinkJS 在控制器前面增加了一层 `Logic`,Logic 里的 Action 和控制器里的 Action 一一对应,系统在调用控制器里的 Action 之前会自动调用 Logic 里的 Action

* index action logic

indexAction(){

其中,Logic 里的 Action 和 Controller 里的 Action 一一对应。Logic 里也支持 `__before` 和 `__after` 等魔术方法。

对应一个特定的 Action,有时候只需要一种或者二三种请求类型,需要将其他类型的请求给拒绝掉。可以通过配置特定的请求类型来完成校验。

indexAction(){

testAction(){

indexAction(){

indexAction(){

控制器

* index action

indexAction(){

* index action

indexAction: function(self){

* index action

async indexAction(){

* index action

indexAction: async function(){

ThinkJS 支持前置操作,方法名为 `__before`,该方法会在具体的 Action 调用之前自动调用。如果前置操作里阻止了后续代码继续执行,则不会调用具体的 Action,这样可以提前结束请求。

### Action

controller

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

indexAction(){

常见问题

默认情况下,视图文件路径为 `view/[module]/[controller]_[action].html`。其中控制器和操作之间是用 `_` 来连接的,如果想将连接符修改为 `/`,可以修改配置文件 `src/common/config/view.js`:

async indexAction(){

async indexAction(){

async indexAction(){

indexAction(self){

async indexAction(){

imageAction(){

### 如何让 Action 只允许命令行调用

默认情况下,Action 既可以用户访问,也可以命令行调用。但有些 Action 我们希望只在命令行下调用,这时可以通过 `isCli` 来判断。如:

indexAction(){

WebSocket

### 事件到 Action 的映射

ThinkJS 里对 WebSocket 的包装遵循了 `socket.io` 的机制,服务端和客户端之间通过事件来交互,这样服务端需要将事件名映射到对应的 Action,才能响应具体的事件。配置在 `messages` 字段,具体如下:

open: 'home/socketio/open', // WebSocket 建立连接时处理的 Action

close: 'home/socketio/close', // WebSocket 关闭时处理的 Action

adduser: 'home/socketio/adduser', //adduser 事件处理的 Action

### Action 处理

通过上面配置事件到 Action 的映射后,就可以在对应的 Action 作相应的处理。如:

openAction(self){

Action 里可以通过 `this.emit` 方法给当前 socket 发送事件,如:

openAction(self){

think.http.base

indexAction(){

#### action(controller, action)

* `action` {String} action名称

调用 controller 下的 action,返回一个 Promise。自动调用 `__before` 和 `__after` 魔术方法。

//调用当前模块下controller里的action

async indexAction(){

let value = await this.action('user', 'detail');

//跨模块调用controller里的action

async indexAction(){

let value = await this.action('admin/user', 'detail');

CRUD 操作

async addAction(){

async addAction(){

async addAction(){

async addAction(){

async updateAction(){

async updateAction(){

async updateAction(){

async listAction(){

async listAction(){

async listAction(){

事务

async indexAction(){

indexAction: function(self){

### transaction 方法

使用事务时,要一直使用 `startTrans`,`commit` 和 `rollback` 这 3 个方法进行操作,使用起来有一些不便。为了简化这一操作,模型中提供了 `transaction` 方法来更加方便的处理事务。

async indexAction(self){

let insertId = await model.transaction( function * (){

indexAction: function(self){

return model.transaction(function(){

transaction 接收一个回调函数,这个回调函数中处理真正的逻辑,并需要返回。

indexAction(){

错误处理

_400Action(){

_403Action(){

_404Action(){

_500Action(){

_503Action(){

##### 1、添加 _600Action

_600Action(){

添加完错误后,需要在对应地方调用显示错误才能让用户看到,可以通过 `think.statusAction` 方法实现。如:

indexAction(){

return think.statusAction(600, this.http); //显示 600 错误,需要将 http 对象传递进去

路由

* action 为 `detail`,对应的方法名为 `detailAction`

* action 为 `group`,对应的方法名为 `groupAction`

如果有多级控制器,那么会进行多级控制器的识别,然后才是 action 的识别。

路由识别后,`module`、`controller` 和 `Action` 值都会自动转为小写。如果 Action 值里有 `_`,会作一些转化,如:假设识别后的 Controller 值为 `index`,Action 值为 `user_add`,那么对应的 Action 方法名为 `userAddAction`,但模版名还是 `index_user_add.html`。

当解析 pathname 没有对应的值时,此时便使用对应的默认值。其中 module 默认值为 `home`,controller 默认值为 `index`,action 默认值为 `index`。

default_action: 'index',

detailAction(){

listAction(){

首页默认执行的是 index controller 里的 indexAction。有些项目希望对首页路由自定义,但配置 `['', 'index/list']` 并不管用。

rest controller

#### controller.getAction()

async getAction(){

#### controller.postAction()

async postAction(){

#### controller.deleteAction()

async deleteAction(){

#### controller.putAction()

async putAction(){

return this.fail(think.locale('ACTION_INVALID', this.http.action, this.http.url));

视图

indexAction(){

indexAction(){

indexAction(){

indexAction(){

async indexAction(){

indexAction: function(){

indexAction(){

Action 里给当前控制器添加了属性 `navType`,那么模版里就可以直接通过 `controller.navType` 来使用。

<li className="action">home</li>

定时任务

上面的命令表示执行 `home` 模块下 `index` Controller 里的 indexAction

Action 里就可以通过 `this.get` 方法来获取参数 `name` 了。

默认情况下,命令行执行的 Action 通过 URL 也可以访问到。如果禁止 URL 访问到该 Action,可以通过 `this.isCli` 来判断。如:

indexAction(){

//禁止 URL 访问该 Action

//调用一个 Action

model

async indexAction(){

async listAction(){

async indexAction(){

async listAction(){

#### model.transaction(fn)

return this.transaction(async () => {

think

* indexAction(){

#### think.statusAction(status, http, log)

indexAction(){

return think.statusAction(404, this.http);

async indexAction(){

async indexAction(){

项目结构

逻辑处理。每个操作执行前可以先进行逻辑校验,可以包含:参数是否合法、提交的数据是否正常、当前用户是否已经登录、当前用户是否有权限等。这样可以降低 `controller` 里的 `action` 的复杂度。

* index action logic

indexAction(){

控制器。一个 `url` 对应一个 `controller` 下的 `action`。

* index action

indexAction(){

配置

ACTION_NOT_FOUND: 'action `%s` not found. url is `%s`',

ACTION_INVALID: 'action `%s` is not valid. url is `%s`',

NOT_SUPPORT_TRANSACTION: 'table engine is not support transaction',

default_action: 'index', //默认的 Action

Cookie

indexAction(){

indexAction(){

indexAction(){

indexAction(){

国际化

testAction(){

indexAction(){

indexAction(){

介绍

//login action

async loginAction(self){

ThinkJS 里的 `Action` 除了可以响应用户的请求,同时支持在命令行下访问,借助这套机制就可以很方便的执行定时任务。

Session

async indexAction(){

async indexAction(){

async indexAction(){

http

当前请求的 pathname,路由识别会依赖该值,会在后续的处理中对其进行改变。所以在 action 拿到值可能跟初始解析出来的值不一致。

#### http.action

Service

indexAction(){

indexAction(){

Middleware

如果不想直接结束当前请求,而是返回一个错误页面,ThinkJS 提供了 `think.statusAction` 方法来实现,具体使用方式请见 [扩展功能 -> 错误处理](./error_handle.html)。

模型介绍

indexAction(){

数据库配置

indexAction(){

MongoDB

async indexAction(){