Files
cycle_api/app/Models/Customer/CustomerUser.php
2023-12-21 22:01:33 +08:00

106 lines
3.3 KiB
PHP

<?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\Carbon;
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|array|null
{
$sDateTime = Carbon::now()->toDateTimeString();
$aItem['created_at'] = $sDateTime;
$aItem['updated_at'] = $sDateTime;
return $this->addItem($aItem);
}
function findItemByAccount($aData,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(!empty($aData['username'])){
return $this->findItemByUsername($aData['username'],$col);
}
if(!empty($aData['phone_area']) && !empty($aData['phone'])){
return $this->findItemByPhone($aData['phone_area'],$aData['phone'],$col);
}
if(!empty($aData['email'])){
return $this->findItemByEmail($aData['email'],$col);
}
return null;
}
function findItemByUsername($sUsername,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('username',$sUsername)->first($col);
}
function findItemByPhone($sPhoneArea,$sPhone,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('phone_area',$sPhoneArea)->where('phone',$sPhone)->first($col);
}
function findItemByEmail($sEmail,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('email',$sEmail)->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);
});
}
// function setUserInfo($iUid,$sNickname): bool|int
// {
// return $this->updateItem([
// 'id' => $iUid,
// 'nickname' => $sNickname,
// 'email' => $sNickname,
// 'phone_area' => $sNickname,
// ]);
// }
}