first
This commit is contained in:
87
app/Traits/HasClicks.php
Normal file
87
app/Traits/HasClicks.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* 预期给所有拥有浏览计数的模型使用
|
||||
* 使用缓存,计算浏览量,定期更新缓存至数据库中
|
||||
*/
|
||||
trait HasClicks
|
||||
{
|
||||
protected int $saveRate = 20;
|
||||
|
||||
/**
|
||||
* Notes : 获取点击量的字段
|
||||
*
|
||||
* @Date : 2021/3/17 9:39 上午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
private function getClicksField(): string
|
||||
{
|
||||
return $this->clicks_filed ?? 'clicks';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取缓存前缀
|
||||
*
|
||||
* @Date : 2021/3/16 5:52 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
private function getClickCachePrefix(): string
|
||||
{
|
||||
return $this->cachePrefix ?? class_basename(__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 生成一个缓存KEY
|
||||
*
|
||||
* @Date : 2021/3/16 5:52 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string|null $appends
|
||||
* @return string
|
||||
*/
|
||||
private function getCacheKey(string $appends = null): string
|
||||
{
|
||||
return $this->getClickCachePrefix().':'.$this->getKey().':'.$appends;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 增加点击量
|
||||
*
|
||||
* @Date : 2021/3/17 9:20 上午
|
||||
* @Author : <Jason.C>
|
||||
* @param int $step
|
||||
*/
|
||||
public function incrementClicks(int $step = 1): void
|
||||
{
|
||||
Cache::increment($this->getCacheKey('clicks'), $step);
|
||||
|
||||
if (rand(1, $this->saveRate) === 1) {
|
||||
$this->update([$this->getClicksField() => $this->clicks]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取缓存的浏览次数
|
||||
*
|
||||
* @Date : 2021/3/16 5:52 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return int
|
||||
*/
|
||||
public function getClicksAttribute(): int
|
||||
{
|
||||
$clicks = Cache::get($this->getCacheKey('clicks'));
|
||||
|
||||
if (is_null($clicks)) {
|
||||
return Cache::rememberForever($this->getCacheKey('clicks'), function () {
|
||||
return $this->getAttributes()[$this->getClicksField()];
|
||||
});
|
||||
} else {
|
||||
return $clicks;
|
||||
}
|
||||
}
|
||||
}
|
||||
86
app/Traits/HasCovers.php
Normal file
86
app/Traits/HasCovers.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait HasCovers
|
||||
{
|
||||
/**
|
||||
* Notes : 获取封面图片字段(单图)
|
||||
*
|
||||
* @Date : 2021/3/16 4:34 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
public function getCoverField(): string
|
||||
{
|
||||
return $this->cover_field ?? 'cover';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取图片字段(多图)
|
||||
*
|
||||
* @Date : 2021/3/16 4:35 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
public function getPicturesField(): string
|
||||
{
|
||||
return $this->pictures_field ?? 'pictures';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析单图地址
|
||||
*
|
||||
* @Date : 2021/3/16 4:54 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
public function getCoverUrlAttribute(): string
|
||||
{
|
||||
$cover = $this->getAttribute($this->getCoverField());
|
||||
|
||||
return $this->parseImageUrl($cover);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析多图地址
|
||||
*
|
||||
* @Date : 2021/3/16 4:54 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return array
|
||||
*/
|
||||
public function getPicturesUrlAttribute(): array
|
||||
{
|
||||
$pictures = $this->getAttribute($this->getPicturesField());
|
||||
|
||||
if (empty($pictures)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect($pictures)->map(function ($picture) {
|
||||
return $this->parseImageUrl($picture);
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 解析图片文件的实际展示地址
|
||||
*
|
||||
* @Date : 2021/3/16 4:53 下午
|
||||
* @Author : <Jason.C>
|
||||
* @param string|null $image
|
||||
* @return string
|
||||
*/
|
||||
protected function parseImageUrl(?string $image): string
|
||||
{
|
||||
if (empty($image)) {
|
||||
return '';
|
||||
} elseif (Str::startsWith($image, 'http')) {
|
||||
return $image;
|
||||
} else {
|
||||
return Storage::url($image);
|
||||
}
|
||||
}
|
||||
}
|
||||
91
app/Traits/HasStatus.php
Normal file
91
app/Traits/HasStatus.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait HasStatus
|
||||
{
|
||||
/**
|
||||
* Notes : 获取状态字段,主模型可配置 $status_field
|
||||
*
|
||||
* @Date : 2021/3/16 4:34 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
protected function getStatusField(): string
|
||||
{
|
||||
return $this->status_field ?? 'status';
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取各状态的名称
|
||||
*
|
||||
* @Date : 2021/5/27 11:50 上午
|
||||
* @Author : <Jason.C>
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getStatusMap(): array
|
||||
{
|
||||
return isset($this->status_map) && ! empty($this->status_map) ? $this->status_map : [
|
||||
0 => '待审核',
|
||||
1 => '正常',
|
||||
2 => '驳回',
|
||||
3 => '关闭',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 正常显示的数据
|
||||
*
|
||||
* @Author:<Mr.Wang>
|
||||
* @Date :2021-04-09
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeShown(Builder $query): Builder
|
||||
{
|
||||
return $query->where($this->getStatusField(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不显示的数据
|
||||
*
|
||||
* @Author :<Mr.Wang>
|
||||
* @Date :2021-04-09
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeHidden(Builder $query): Builder
|
||||
{
|
||||
return $query->where($this->getStatusField(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 状态查询
|
||||
*
|
||||
* @Date : 2021/6/28 10:25 上午
|
||||
* @Author : <Jason.C>
|
||||
* @param Builder $query
|
||||
* @param int $status
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeOfStatus(Builder $query, int $status): Builder
|
||||
{
|
||||
return $query->where($this->getStatusField(), $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes : 获取状态的文本信息
|
||||
*
|
||||
* @Date : 2021/4/25 2:10 下午
|
||||
* @Author : <Jason.C>
|
||||
* @return string
|
||||
*/
|
||||
public function getStatusTextAttribute(): string
|
||||
{
|
||||
$map = $this->getStatusMap();
|
||||
|
||||
return $map[$this->{$this->getStatusField()}] ?? '未知';
|
||||
}
|
||||
}
|
||||
48
app/Traits/Macroable.php
Normal file
48
app/Traits/Macroable.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
trait Macroable
|
||||
{
|
||||
use \Illuminate\Support\Traits\Macroable {
|
||||
__call as macroCall;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRelationValue($key)
|
||||
{
|
||||
$relation = parent::getRelationValue($key);
|
||||
if (! $relation && static::hasMacro($key)) {
|
||||
return $this->getRelationshipFromMethod($key);
|
||||
}
|
||||
|
||||
return $relation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (static::hasMacro($method)) {
|
||||
return $this->macroCall($method, $parameters);
|
||||
}
|
||||
|
||||
return parent::__call($method, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
return parent::__callStatic($method, $parameters);
|
||||
}
|
||||
}
|
||||
21
app/Traits/OrderByIdDesc.php
Normal file
21
app/Traits/OrderByIdDesc.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Scopes\OrderByIdDescScope;
|
||||
|
||||
trait OrderByIdDesc
|
||||
{
|
||||
/**
|
||||
* Notes: 初始化trait,自动在模型中,注入作用域
|
||||
* 需要对拥有排序的模型,不需要使用排序的时候
|
||||
* Model::withoutGlobalScope(new OrderByIdDescScope)
|
||||
*
|
||||
* @Author: <C.Jason>
|
||||
* @Date : 2020/1/19 1:42 下午
|
||||
*/
|
||||
public static function bootOrderByIdDesc(): void
|
||||
{
|
||||
static::addGlobalScope(new OrderByIdDescScope);
|
||||
}
|
||||
}
|
||||
22
app/Traits/OrderByOrderAsc.php
Normal file
22
app/Traits/OrderByOrderAsc.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Scopes\OrderByIdDescScope;
|
||||
use App\Scopes\OrderByOrderAscScope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait OrderByOrderAsc
|
||||
{
|
||||
/**
|
||||
* Notes: 初始化trait,自动在模型中,注入作用域
|
||||
* 取消排序参考 OrderByIdDesc
|
||||
*
|
||||
* @Author: <C.Jason>
|
||||
* @Date : 2020/1/19 1:42 下午
|
||||
*/
|
||||
public static function bootOrderByOrderAsc(): void
|
||||
{
|
||||
static::addGlobalScope(new OrderByOrderAscScope());
|
||||
}
|
||||
}
|
||||
24
app/Traits/WithGoogle2FA.php
Normal file
24
app/Traits/WithGoogle2FA.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Google2FA;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
|
||||
trait WithGoogle2FA
|
||||
{
|
||||
public static function bootWithGoogle2FA()
|
||||
{
|
||||
self::created(function ($model) {
|
||||
$google2fa = app('pragmarx.google2fa');
|
||||
$model->google2fa()->create([
|
||||
'secret' => $google2fa->generateSecretKey(32),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
public function google2fa(): MorphOne
|
||||
{
|
||||
return $this->morphOne(Google2FA::class, 'subscriber');
|
||||
}
|
||||
}
|
||||
67
app/Traits/WithPosition.php
Normal file
67
app/Traits/WithPosition.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
trait WithPosition
|
||||
{
|
||||
/**
|
||||
* Notes : 获取定位的数组
|
||||
*
|
||||
* @Date : 2021/7/2 11:31 上午
|
||||
* @Author : <Jason.C>
|
||||
*/
|
||||
protected function getPositionMap(): array
|
||||
{
|
||||
return $this->position_map ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 定位查询作用域
|
||||
*
|
||||
* @Author: Mr.wang
|
||||
* @Date : 2021/5/11 10:48
|
||||
* @param Builder $query
|
||||
* @param int $pos
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeOfPosition(Builder $query, int $pos): Builder
|
||||
{
|
||||
return $query->whereRaw('position & '.$pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 设置定位
|
||||
*
|
||||
* @Author: Mr.wang
|
||||
* @Date : 2020/5/11 10:48
|
||||
* @param array $value
|
||||
*/
|
||||
protected function setPositionAttribute(array $value): void
|
||||
{
|
||||
if (! blank($value)) {
|
||||
$this->attributes['position'] = array_sum($value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取定位数据
|
||||
*
|
||||
* @Author: Mr.wang
|
||||
* @Date : 2020/5/11 10:48
|
||||
* @param int $value
|
||||
* @return array
|
||||
*/
|
||||
protected function getPositionAttribute(int $value): array
|
||||
{
|
||||
$position = [];
|
||||
foreach ($this->getPositionMap() as $k => $v) {
|
||||
if ($k & $value) {
|
||||
$position[] = $k;
|
||||
}
|
||||
}
|
||||
|
||||
return $position;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user