Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kayw-geek committed Sep 17, 2020
0 parents commit d1e8774
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "kayw-geek/yii2-follow",
"description": "This package helps you to add user based follow system to your model.",
"keywords": ["yii", "yii2", "yii2-model", "follow","fans"],
"type": "yii2-extension",
"license": "MIT",
"authors": [
{
"name": "Kay W",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "*"
},
"support": {
"issues": "https://github.com/kayw-geek/yii2-follow/issues"
},
"autoload": {
"psr-4": {
"kaywGeek\\follow\\": ""
}
}
}
32 changes: 32 additions & 0 deletions migrations/m200722_053912_create_follower_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `{{%user_follower}}`.
*/
class m200722_053912_create_follower_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%follower}}', [
'id' => $this->primaryKey(),
'user_id'=>$this->integer(11)->unsigned()->notNull()->comment('用户id'),
'follower_id'=>$this->integer(11)->unsigned()->notNull()->comment('追随者id'),
'follow_at'=>$this->dateTime()->comment('关注时间'),
]);
$this->createIndex('f-follower_id','{{%follower}}','follower_id');
$this->createIndex('f-user_id','{{%follower}}','user_id');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%follower}}');
}
}
56 changes: 56 additions & 0 deletions src/Follower.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* @name:
* @author: weikai
* @date: dt
*/

namespace kaywGeek\follow;

use Yii;
use yii\db\ActiveRecord;

/**
* This is the model class for table "user_follower".
*
* @property int $id
* @property int $user_id 用户id
* @property int $follower_id 追随者id
* @property string|null $follow_at 关注事件
*/
class Follower extends ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'follower';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id', 'follower_id'], 'required'],
[['user_id', 'follower_id'], 'integer'],
[['follow_at'], 'default','value' => date('Y-m-d H:i:s')],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'follower_id' => 'Follower ID',
'follow_at' => 'Follow At',
];
}
}
169 changes: 169 additions & 0 deletions src/FollowerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* Created by PhpStorm.
* @name:
* @author: weikai
* @date: dt
*/
namespace kaywGeek\follow;


use yii\base\Exception;
use yii\base\Model;

trait FollowerTrait
{

/**
* @param $user
* @return string
* @name:关注
* @author: weikai
* @date: 20.7.22 14:29
*/
public function follow( Model $user ) :bool
{
if (!$user instanceof Model || empty($this->id)){
throw new Exception('User Model Error');
}

if (!self::checkFollowed($user)){
return false;
}

return self::run([
'user_id'=>$this->id,
'follower_id'=>$user->id,
'follow_at'=>date('Y-m-d H:i:s'),
]);
}

/**
* @param $user
* @return bool
* @name:取消关注
* @author: weikai
* @date: 20.7.22 15:13
*/
public function unfollow( Model $user ) :bool
{
if (!$user instanceof Model || empty($this->id)){
throw new Exception('User Model Error');
}

if (self::checkFollowed($user)){
return false;
}

try{
if (!$this->getModel($user)){
return false;
}
return $this->getModel($user)->delete() ? true : false;
}catch (\Exception $exception){
\Yii::error($exception->getMessage(),__METHOD__);
return false;
}

}

/**
* @param $user
* @return bool|Follower|null
* @throws Exception
* @name:获取model
* @author: weikai
* @date: 20.9.17 14:07
*/
public function getModel( Model $user )
{
if (!$user instanceof Model || empty($this->id)){
throw new Exception('User Model Error');
}
return empty(Follower::findOne(['user_id'=>$this->id,'follower_id'=>$user->id])) ? false : Follower::findOne(['user_id'=>$this->id,'follower_id'=>$user->id]);
}

/**
* @return bool
* @name:已关注一对多关联
* @author: weikai
* @date: 20.7.22 15:41
*/
public function followeds( )
{
return $this->hasMany(Follower::className(),['follower_id'=>'id']);
}

/**
* @return mixed
* @name:粉丝一对多关联
* @author: weikai
* @date: 20.9.17 14:18
*/
public function followings( )
{
return $this->hasMany(Follower::className(),['user_id'=>'id']);
}

/**
* @param $user_id
* @return mixed
* @name:粉丝数量
* @author: weikai
* @date: 20.8.3 17:18
*/
public function followerCount( )
{
return intval($this->followings()->count());

}

/**
* @param $user_id
* @return mixed
* @name:关注数量
* @author: weikai
* @date: 20.8.3 17:20
*/
public function followedCount( )
{
return intval($this->followeds()->count());;
}

/**
* @param $user
* @return bool
* @name:检查是否已关注此用户
* @author: weikai
* @date: 20.7.22 15:03
*/
public function checkFollowed(Model $user ) :bool
{
return empty(Follower::findOne(['user_id'=>$this->id,'follower_id'=>$user->id])) ? true : false;
}


/**
* @param array $value
* @return string
* @name:执行数据保存
* @author: weikai
* @date: 20.7.22 14:27
*/
private function run( array $value )
{
try{
$model = new Follower();
$model->attributes = $value;
if (!$model->validate()){
\Yii::info($model->errors,__METHOD__);
return false;
}
return $model->save();
}catch (\Exception $exception){
\Yii::info($exception->getMessage(),__METHOD__);
return false;
}
}

}

0 comments on commit d1e8774

Please sign in to comment.