server/app/Model/Account.php

252 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Kernel\Str;
use App\Kernel\Token;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
/**
* @property int $account_id
* @property int $account_type
* @property int $belong_id
* @property int $dept_id
* @property string $avatar
* @property string $username
* @property string $password
* @property int $master_flag
* @property int $status
* @property string $salt
* @property int $del_flag
* @property string $create_time
* @property string $update_time
*/
class Account extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'account';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['account_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'dept_id' => 'integer', 'master_flag' => 'integer', 'status' => 'integer', 'del_flag' => 'integer'];
/**
* 通过id获取账号信息
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
*/
public static function getById(int $account_id, array $field = ['*'])
{
return self::where("del_flag", 0)
->where("account_id", $account_id)
->first($field);
}
/**
* 通过用户名获取账号信息
* Author: cfn <cfn@leapy.cn>
* @param string $username
*/
public static function getByUsername(string $username)
{
return self::where("del_flag", 0)
->where("username", $username)
->first(['account_id', 'account_type', 'belong_id', 'password', 'master_flag', 'status', 'salt']);
}
/**
* 通过id获取账号信息
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
*/
public static function getByAccountId(int $account_id)
{
return self::where("del_flag", 0)
->where("account_id", $account_id)
->first(['account_id', 'account_type', 'belong_id', 'master_flag', 'status', 'create_time', 'username', 'avatar']);
}
/**
* 保存信息
* Author: cfn <cfn@leapy.cn>
* @param int $account_id
* @param array $param
* @return int
*/
public static function saveInfo(int $account_id, array $param)
{
$param['update_time'] = date("Y-m-d H:i:s");
return self::where("del_flag", 0)
->where("account_id", $account_id)
->update($param);
}
/**
* @param $param
* @return \Hyperf\Contract\LengthAwarePaginatorInterface
*/
static function list($account, $param)
{
$model = self::where('del_flag', 0)
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type']);
if (isset($param['username']) && $param['username'] != '') {
$model = $model->where('username', "like", "%$param[username]%");
}
if (isset($param['dept_id']) && $param['dept_id'] != '') {
$model = $model->where('dept_id', $param['dept_id']);
}
$paginate = $model->orderByDesc("account_id")
->paginate((int)$param['limit'], ['account_id', 'username', 'avatar', 'create_time', 'dept_id'], 'page', (int)$param['page']);
foreach ($paginate->items() as &$item) {
$item['roles'] = AccountRole::getRole($item['account_id']);
}
return $paginate;
}
/**
* 添加
* @param array $data
* @return bool
*/
public static function add($account, array $data)
{
$data['account_type'] = $account['account_type'];
$data['belong_id'] = $account['belong_id'];
$data['create_time'] = date("Y-m-d H:i:s");
$data['del_flag'] = 0;
$data['salt'] = Str::randStr(6);
$data['password'] = md5($data['salt'] . $data['password']);
$roles = $data['roles'];
unset($data['roles']);
Db::beginTransaction();
$account_id = self::insertGetId($data);
if (!$account_id) {
Db::rollBack();
return false;
}
$account_role = [];
foreach ($roles as $role_id) {
$account_role[] = ['role_id' => $role_id, 'account_id' => $account_id];
}
if (!empty($account_role)) {
$res = AccountRole::insert($account_role);
if (!$res) {
Db::rollBack();
return false;
}
}
Db::commit();
return true;
}
/**
* 修改
* @param array $account
* @param array $data
* @return bool
*/
public static function edit(array $account, array $data)
{
$data['update_time'] = date("Y-m-d H:i:s");
if ($data['password'] != '') {
$data['salt'] = Str::randStr(6);
$data['password'] = md5($data['salt'] . $data['password']);
}else{
unset($data['password']);
}
$roles = $data['roles'];
unset($data['roles']);
Db::beginTransaction();
$res = self::where("account_id", $data['account_id'])
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type'])
->where('del_flag', 0)
->update($data);
if (!$res) {
Db::rollBack();
return false;
}
// 删除之前的
$res1 = AccountRole::where('account_id', $data['account_id'])->delete();
if (!$res1) {
Db::rollBack();
return false;
}
$account_role = [];
foreach ($roles as $role_id) {
$account_role[] = ['role_id' => $role_id, 'account_id' => $data['account_id']];
}
// 重新添加
if (!empty($account_role)) {
if (!AccountRole::insert($account_role)) {
Db::rollBack();
return false;
}
}
Db::commit();
return true;
}
/**
* 删除
* @param string $ids
* @return int
*/
public static function del(array $account, string $ids)
{
return self::whereIn('account_id', explode(",", $ids))
->where("del_flag", 0)
->where('belong_id', $account['belong_id'])
->where('account_type', $account['account_type'])
->update([
'update_time' => date('Y-m-d H:i:s'),
'del_flag' => 1
]);
}
/**
* Author: cfn <cfn@leapy.cn>
* @param array $account
* @param string $method
* @param string $auth
* @return bool
*/
public static function checkAuth(array $account, string $method, string $auth)
{
return Menu::where("del_flag", 0)
->where("account_type", $account['account_type'])
->where("method", $method)
->where("flag", $auth)
->where("type", 2)
->exists();
}
/**
* Author: cfn <cfn@leapy.cn>
* @param $token
* @return false|mixed
*/
public static function checkToken($token)
{
$tokenData = Token::parseToken($token);
if (empty($tokenData)) return false;
$container = ApplicationContext::getContainer();
$redis = $container->get(\Hyperf\Redis\Redis::class);
$data = $redis->get($tokenData['key']);
if (!$data) return false;
return json_decode(Str::private_decrypt($data), true);
}
}