This commit is contained in:
2023-12-17 21:29:59 +08:00
parent 3ae0046c88
commit ec6b900318
20 changed files with 652 additions and 31 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace App\Models\Base;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
function checkColInFill($aItem)
{
foreach ($aItem as $key => $value) {
if (!in_array($key, $this->fillable)) {
unset($aItem[$key]);
}
}
return $aItem;
}
function addItem($aItem): Model|\Illuminate\Database\Eloquent\Builder|bool
{
$aItem = $this->checkColInFill($aItem);
if (empty($aItem)) return false;
return $this->newQuery()->create($aItem);
}
function delItem($id)
{
return $this->newQuery()->where($this->primaryKey, $id)->delete();
}
function updateItem($aItem): bool|int
{
$aItem = $this->checkColInFill($aItem);
if (empty($aItem)) return false;
if (isset($aItem[$this->primaryKey])) return false;
return $this->newQuery()->where($this->primaryKey,$aItem[$this->primaryKey])->update($aItem);
}
function findItem($id,$col=['*']): Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->find($id,$col);
}
function findItemByWhere($aWhere,$col=['*']): Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where($aWhere)->first($col);
}
function getItemsByWhere($aWhere,$col=['*']): \Illuminate\Database\Eloquent\Collection|array
{
return $this->newQuery()->where($aWhere)->get($col);
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace App\Models\Base;
class CustomerBaseModel extends BaseModel {
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Models\Customer;
use App\Const\RedisConst;
use App\Models\Base\CustomerBaseModel;
use App\Service\AuthService;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class CustomerUser extends CustomerBaseModel
{
protected $table = 'customer_users';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'status',
'im_user_id',
'country_name',
'username',
'password',
'nickname',
'email',
'phone_area',
'phone',
'is_google_auth',
'created_at',
'updated_at',
];
protected function password(): Attribute
{
return Attribute::make(
set: fn (string $value) => Hash::make($value),
);
}
function checkPasswd($iUid,$sPasswd):bool
{
$oUser = $this->where('id',$iUid)->first();
if(empty($oUser)) return false;
return Hash::check($sPasswd,$oUser->password);
}
function addUser($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool
{
// if(isset($aItem['password']) && !empty($aItem['password'])) $aItem['password'] = Hash::make($aItem['password']);
return $this->addItem($aItem);
}
function findItemByUsername($sUsername,$col=['*']): \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('username',$sUsername)->first($col);
}
function findUserByUidWithCache($iUid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return Cache::remember(RedisConst::ORM_CACHE_USER.$iUid,RedisConst::ORM_FIND_CACHE_SECOND,function ()use ($iUid){
return $this->findItem($iUid);
});
}
}