142 lines
4.2 KiB
PHP
142 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Models\Customer;
|
||
|
||
use App\Cache\Table\TableCustomerUserCache;
|
||
use App\Models\Base\CustomerBaseModel;
|
||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\Hash;
|
||
|
||
class CustomerUserModel 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',
|
||
'google_auth_secret',
|
||
'created_at',
|
||
'updated_at',
|
||
];
|
||
|
||
protected $hidden = [
|
||
'password',
|
||
'google_auth_secret',
|
||
];
|
||
|
||
//插入密码hash加密
|
||
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
|
||
{
|
||
$oQuery = $this->newQuery();
|
||
if (!empty($aData['username'])) {
|
||
$oQuery->orWhere('username', $aData['username']);
|
||
|
||
} elseif (!empty($aData['email'])) {
|
||
$oQuery->orWhere('email', $aData['email']);
|
||
|
||
} elseif (!empty($aData['phone']) && !empty($aData['phone_area'])) {
|
||
$oQuery->orWhere([
|
||
'phone' => $aData['phone'],
|
||
'phone_area' => $aData['phone_area'],
|
||
]);
|
||
|
||
} else {
|
||
throw new \Exception('findItemByAccount params error');
|
||
}
|
||
|
||
return $oQuery->first($col);
|
||
}
|
||
|
||
//查找账户-用户名
|
||
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);
|
||
}
|
||
|
||
//根据uid从缓存中查询用户信息,不存在则从数据库中查询
|
||
|
||
/**
|
||
* @throws \Exception
|
||
*/
|
||
function findUserByUidWithCache($iUid): array|null
|
||
{
|
||
$oTableCustomerUserCache = new TableCustomerUserCache();
|
||
$oTableCustomerUserCache->setPrimaryKey($iUid);
|
||
return $oTableCustomerUserCache->getCacheData();
|
||
// return Cache::remember($this->getCacheKey($iUid), RedisConst::ORM_FIND_CACHE_SECOND, function () use ($iUid) {
|
||
// return $this->findItem($iUid);
|
||
// });
|
||
}
|
||
|
||
// function delItemFromCache($iUid): bool
|
||
// {
|
||
// return Cache::delete($this->getCacheKey($iUid));
|
||
// }
|
||
|
||
// //生成user缓存key
|
||
// function getCacheKey($iUid): string
|
||
// {
|
||
// if(empty($iUid)) throw new \Exception('getCacheKey params error');
|
||
// return RedisConst::ORM_CACHE_USER . $iUid;
|
||
// }
|
||
|
||
// function setUserInfo($iUid,$sNickname): bool|int
|
||
// {
|
||
// return $this->updateItem([
|
||
// 'id' => $iUid,
|
||
// 'nickname' => $sNickname,
|
||
// 'email' => $sNickname,
|
||
// 'phone_area' => $sNickname,
|
||
// ]);
|
||
// }
|
||
|
||
|
||
}
|