6
0
Fork 0
jiqu-library-server/app/Models/Wallet.php

84 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Wallet extends Model
{
/**
* @var string
*/
protected $primaryKey = 'user_id';
/**
* @var bool
*/
public $incrementing = false;
/**
* @var array
*/
protected $attributes = [
'balance' => 0,
'total_revenue' => 0,
'total_expenses' => 0,
'withdrawable' => true,
'is_frozen' => false,
];
/**
* @var array
*/
protected $fillable = [
'user_id',
'balance',
'total_expenses',
'total_revenue',
'withdrawable',
'password', //安全密码
'is_frozen',
];
/**
* @var array
*/
protected $casts = [
'withdrawable' => 'bool',
'is_frozen'=>'bool',
];
public function getBalanceFormatAttribute()
{
return trim_trailing_zeros(bcdiv($this->attributes['balance'], 100, 2));
}
/**
* 设置此用户的安全密码
*
* @param string $value
* @return void
*/
public function setPasswordAttribute($value): void
{
if ((string) $value === '') {
$value = null;
} else {
$value = md5($value);
}
$this->attributes['password'] = $value;
}
/**
* 确认给定的密码是否正确
*
* @param string $password
* @return bool
*/
public function verifyPassword(string $password): bool
{
return $this->password && md5($password) === $this->password;
}
}