Share Model添加下载次数字段

现在支持对分享的下载次数进行统计
This commit is contained in:
Chenx221 2024-04-07 14:34:43 +08:00
parent d17863c7d6
commit 37638ad38f
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 33 additions and 2 deletions

View File

@ -271,6 +271,8 @@ class ShareController extends Controller
$model = $this->findModel($share_id, true);
DownloadLogs::addLog(Yii::$app->user->id, $model->share_id, Yii::$app->request->userIP, Yii::$app->request->userAgent); // logging for access(DL)
// add download count
$model->setDlCountPlus1();
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->sharer_id . '/' . $model->file_relative_path;
if (is_file($absolutePath)) {
return Yii::$app->response->sendFile($absolutePath);

View File

@ -14,12 +14,15 @@ use yii\db\ActiveRecord;
* @property string $access_code 分享密钥
* @property string $creation_date 分享创建日期
* @property int|null $status 分享是否启用
* @property int|null $dl_count 下载次数
*
* @property User $sharer
* @property DownloadLogs[] $downloadLogs
* * @property User $sharer
*/
class Share extends ActiveRecord
{
const string SCENARIO_UPDATE = 'update';
/**
* {@inheritdoc}
*/
@ -35,7 +38,7 @@ class Share extends ActiveRecord
{
return [
[['file_relative_path', 'access_code'], 'required'],
[['sharer_id', 'status'], 'integer'],
[['sharer_id', 'status', 'dl_count'], 'integer'],
[['creation_date'], 'safe'],
[['file_relative_path'], 'string', 'max' => 255],
[['access_code'], 'string', 'max' => 4],
@ -56,6 +59,8 @@ class Share extends ActiveRecord
'access_code' => '访问密码',
'creation_date' => '分享创建日期',
'status' => 'Status',
'dl_count' => '下载次数',
];
}
@ -68,8 +73,32 @@ class Share extends ActiveRecord
{
return $this->hasOne(User::class, ['id' => 'sharer_id']);
}
/**
* Gets query for [[DownloadLogs]].
*
* @return ActiveQuery
*/
public function getDownloadLogs(): ActiveQuery
{
return $this->hasMany(DownloadLogs::class, ['share_id' => 'share_id']);
}
/**
* @return string|null
*/
public function getSharerUsername(): ?string
{
return $this->sharer->username;
}
/**
* @return void
*/
public function setDlCountPlus1(): void
{
$this->dl_count += 1;
$this->save(true, ['dl_count']);
}
}