Skip to content

Commit

Permalink
添加 action 操作
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Feb 1, 2018
1 parent d2414b5 commit f08be7d
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 6 deletions.
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,60 @@ $ php yii migrate --migrationPath=@yiier/notification/migrations/
Usage
-----

[Notification](/src/models/Notification.php)
**Config**

Configure Controller class as follows : :

```php
<?php
use yiier\notification\actions\NotificationAction;


class NotificationController extends Controller
{
public function actions()
{
return [
'do' => [
'class' => NotificationAction::className(),
]
];
}
}

```

**Url**

```
POST: http://xxxxxxxxxxxxxx/notification/do
Form Data: action=read_all
POST: http://xxxxxxxxxxxxxx/notification/do
Form Data: action=read_all&ids=1,2,3
POST: http://xxxxxxxxxxxxxx/notification/do
Form Data: action=delete_all
POST: http://xxxxxxxxxxxxxx/notification/do
Form Data: action=delete_all&ids=1,2,3
POST: http://xxxxxxxxxxxxxx/notification/do
Form Data: action=delete&id=1
```

`action=delete` you can use `action=Notification::DEL_ACTION`


http response success(code==200) return json:

```json
{"code":200,"data":0,"message":"success"}
```
http response failure(code==500) return json:

```json
{"code":500,"data":"","message":"xxxx"}
```

More [Notification](/src/models/Notification.php)
62 changes: 62 additions & 0 deletions src/actions/NotificationAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2018/1/31 18:14
* description:
*/

namespace yiier\notification\actions;

use Yii;
use yii\web\Response;
use yiier\notification\models\Notification;

class NotificationAction extends \yii\base\Action
{
public function init()
{
parent::init();
\Yii::$app->controller->enableCsrfValidation = false;
}

public function run()
{
if (Yii::$app->user->isGuest) {
Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl)->send();
} else {
Yii::$app->response->format = Response::FORMAT_JSON;
if (!Yii::$app->request->isPost) {
return ['code' => 404, 'data' => '', 'message' => 'please use post request'];
}
switch (Yii::$app->request->getBodyParam('action')) {
case Notification::DEL_ACTION:
$id = Yii::$app->request->getBodyParam('id');
$return = $id ? Notification::del($id) : $id;
break;
case Notification::DEL_ALL_ACTION:
$condition = [];
if ($ids = Yii::$app->request->getBodyParam('ids')) {
$condition = ['id' => explode(',', $ids)];
}
$return = Notification::delAll($condition);
break;

case Notification::READ_ALL_ACTION:
$condition = [];
if ($ids = Yii::$app->request->getBodyParam('ids')) {
$condition = ['id' => explode(',', $ids)];
}
$return = Notification::readAll($condition);
break;
default:
# code...
break;
}

if (isset($return)) {
return ['code' => 200, 'data' => $return, 'message' => 'success'];
}
return ['code' => 500, 'data' => '', 'message' => 'fail'];
}
}
}
5 changes: 5 additions & 0 deletions src/migrations/m180131_082652_create_notification_table.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2018/1/31 18:14
* description:
*/

use yii\db\Migration;

Expand Down
32 changes: 27 additions & 5 deletions src/models/Notification.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2018/1/31 18:14
* description:
*/

namespace yiier\notification\models;

Expand Down Expand Up @@ -32,6 +37,21 @@ class Notification extends \yii\db\ActiveRecord
*/
const STATUS_READ = 1;

/**
* @var string delete all
*/
const DEL_ALL_ACTION = 'delete_all';

/**
* @var string delete
*/
const DEL_ACTION = 'delete';

/**
* @var string read all
*/
const READ_ALL_ACTION = 'read_all';


/**
* @inheritdoc
Expand Down Expand Up @@ -90,11 +110,12 @@ public function beforeSave($insert)


/**
* @param array $condition
* @return int
*/
public static function readAll()
public static function readAll($condition = [])
{
return self::updateAll(['user_id' => Yii::$app->user->id]);
return self::updateAll(['status' => self::STATUS_READ], array_merge(['user_id' => Yii::$app->user->id], $condition));
}


Expand All @@ -104,16 +125,17 @@ public static function readAll()
*/
public static function read($id)
{
return self::updateAll(['user_id' => Yii::$app->user->id, 'id' => $id]);
return self::updateAll(['status' => self::STATUS_READ], ['user_id' => Yii::$app->user->id, 'id' => $id]);
}


/**
* @param array $condition
* @return int
*/
public static function delAll()
public static function delAll($condition = [])
{
return self::deleteAll(['user_id' => Yii::$app->user->id]);
return self::deleteAll(array_merge(['user_id' => Yii::$app->user->id], $condition));
}


Expand Down

0 comments on commit f08be7d

Please sign in to comment.