-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -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 | ||
|
@@ -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)); | ||
} | ||
|
||
|
||
|
@@ -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)); | ||
} | ||
|
||
|
||
|