diff --git a/README.md b/README.md new file mode 100644 index 0000000..f9eb2ca --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +ActionStore for Yii2 +==================== +ActionStore for Yii2 + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +``` +php composer.phar require --prefer-dist yiier/yii2-action-store "*" +``` + +or add + +``` +"yiier/yii2-action-store": "*" +``` + +to the require section of your `composer.json` file. + + +Migrations +----------- + +Run the following command + +```shell +$ php yii migrate --migrationPath=@yiier/actionStore/migrations/ +``` + +Usage +----- + +Once the extension is installed, simply use it in your code by : + +```php +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..741bc71 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "yiier/yii2-action-store", + "description": "ActionStore for Yii2", + "type": "yii2-extension", + "keywords": ["yii2","extension","action-store"], + "license": "BSD-4-Clause", + "authors": [ + { + "name": "forecho", + "email": "caizhenghai@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0" + }, + "autoload": { + "psr-4": { + "yiier\\actionStore\\": "src" + } + } +} diff --git a/src/actions/ActionAction.php b/src/actions/ActionAction.php new file mode 100644 index 0000000..db2d7d3 --- /dev/null +++ b/src/actions/ActionAction.php @@ -0,0 +1,40 @@ + + * createTime : 2018/1/26 18:14 + * description: + */ + +namespace yiier\actionStore\actions; + +use Yii; +use yii\db\Exception; +use yii\web\Response; +use yiier\actionStore\models\ActionStore; + +class ActionAction 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 { + $model = new ActionStore(); + $model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), ''); + $model->validate(); + if (!$model->errors) { + if ($counter = ActionStore::createAction($model)) { + Yii::$app->response->format = Response::FORMAT_JSON; + return $counter; + } + } + throw new Exception(json_encode($model->errors)); + } + } +} \ No newline at end of file diff --git a/src/migrations/m171214_101829_create_action_store_table.php b/src/migrations/m171214_101829_create_action_store_table.php new file mode 100644 index 0000000..6c8b53d --- /dev/null +++ b/src/migrations/m171214_101829_create_action_store_table.php @@ -0,0 +1,44 @@ +createTable($this->tableName, [ + 'id' => $this->primaryKey(), + 'type' => $this->string()->notNull(), + 'value' => $this->integer()->defaultValue(1), + 'user_type' => $this->string()->defaultValue('user'), + 'user_id' => $this->integer()->notNull(), + 'model' => $this->string()->notNull(), + 'model_id' => $this->integer()->notNull(), + 'created_at' => $this->integer()->defaultValue(null), + 'updated_at' => $this->integer()->defaultValue(null), + ]); + + $this->addCommentOnTable($this->tableName, '用户行为表'); + $this->createIndex('fk_model_id', $this->tableName, ['model', 'model_id', 'type']); + $this->createIndex('fk_user_type_id', $this->tableName, ['user_type', 'user_id', 'type']); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable($this->tableName); + } +} diff --git a/src/models/ActionStore.php b/src/models/ActionStore.php new file mode 100644 index 0000000..d4fd10a --- /dev/null +++ b/src/models/ActionStore.php @@ -0,0 +1,142 @@ + 'user'], + [['type', 'user_type', 'model'], 'string', 'max' => 255], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'type' => Yii::t('app', 'Type'), + 'value' => Yii::t('app', 'Value'), + 'user_type' => Yii::t('app', 'User Type'), + 'user_id' => Yii::t('app', 'User ID'), + 'model' => Yii::t('app', 'Model'), + 'model_id' => Yii::t('app', 'Model ID'), + 'created_at' => Yii::t('app', 'Created At'), + ]; + } + + + /** + * @param $model ActionStore + * @return int + * @throws Exception + */ + public static function createAction($model) + { + $conditions = array_filter($model->attributes); + switch ($model->type) { + case self::LIKE_TYPE: + self::deleteAll(array_merge(['type' => self::DISLIKE_TYPE], $conditions)); + $data = array_merge(['type' => self::LIKE_TYPE], $conditions); + break; + case self::DISLIKE_TYPE: + self::deleteAll(array_merge(['type' => self::LIKE_TYPE], $conditions)); + $data = array_merge(['type' => self::DISLIKE_TYPE], $conditions); + break; + + default: + $data = array_merge(['type' => $model->type], $conditions); + break; + } + if ($model->type == self::CLAP_TYPE) { + $value = self::find()->filterWhere($data)->select('value')->scalar(); + $model->value = $value + 1; + } + self::deleteAll($data); + $model->load($data, ''); + if ($model->save()) { + return $model->resetCounter(); + } + throw new Exception(json_encode($model->errors)); + } + + /** + * @param $model ActionStore + * @return false|int + */ + public static function destroyAction($model) + { + return $model->delete(); + } + + + /** + * 返回计数器 + * @return int + */ + public function resetCounter() + { + $data = $this->attributes; + unset($data['id'], $data['created_at'], $data['updated_at'], $data['value']); + return self::find()->filterWhere($data)->select('value')->scalar(); + } +}