249 lines
7.4 KiB
PHP
249 lines
7.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Utils\Str;
|
|
use Hyperf\DbConnection\Db;
|
|
|
|
/**
|
|
* @property int $account_id
|
|
* @property int $account_type
|
|
* @property int $belong_id
|
|
* @property int $dept_id
|
|
* @property string $username
|
|
* @property string $password
|
|
* @property string $salt
|
|
* @property int $master_flag
|
|
* @property int $status
|
|
* @property string $nickname
|
|
* @property string $avatar
|
|
* @property string $bio
|
|
* @property string $tags
|
|
* @property int $sex
|
|
* @property string $birthday
|
|
* @property string $deleted_at
|
|
* @property string $create_time
|
|
* @property string $update_time
|
|
*/
|
|
class Account extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*/
|
|
protected ?string $table = 'account';
|
|
|
|
protected string $primaryKey = 'account_id';
|
|
|
|
/**
|
|
* 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', 'sex' => 'integer'];
|
|
|
|
public static function getByUsername(string $username, array $field = ['*'])
|
|
{
|
|
return self::query()->where("username", $username)->first($field);
|
|
}
|
|
|
|
public static function getAuths(int $account_id, int $account_type, int $master_flag)
|
|
{
|
|
return $master_flag ? Menu::getAuth1($account_type) : Menu::getAuth2($account_id, $account_type);
|
|
}
|
|
|
|
public static function getInfo(int $account_id): array
|
|
{
|
|
$info = self::with('roles')
|
|
->with('posts')
|
|
->select(['account_id', 'username', 'nickname', 'avatar', 'bio', 'tags', 'sex', 'birthday', 'create_time', 'dept_id'])
|
|
->find($account_id);
|
|
if ($info) {
|
|
$info = $info->toArray();
|
|
// 获取所有上级
|
|
$dept = [];
|
|
Dept::getTop($info['dept_id'], $dept);
|
|
$info['dept'] = array_reverse(array_column($dept,"dept_name"));
|
|
}
|
|
return $info;
|
|
}
|
|
|
|
|
|
public static function getMenu(array $account)
|
|
{
|
|
// 总后台账号
|
|
$field = ['m.title', 'm.path', 'm.pid', 'm.name', 'm.menu_id', 'm.icon', 'm.hidden', 'm.type'];
|
|
// 获取角色
|
|
$roles = match ($account['account_type']) {
|
|
0 => ["ADMIN"],
|
|
21 => ["ORG"],
|
|
default => []
|
|
};
|
|
// 标识
|
|
if ($account['master_flag']) {
|
|
$menus = Menu::getMenu($account['account_type'], $field);
|
|
$buttons = Menu::getButton($account['account_type']);
|
|
$roles[] = 'MAIN';
|
|
} else {
|
|
$menus = Menu::getMenu2($account['account_id'], $account['account_type'], $field);
|
|
$buttons = Menu::getButton2($account['account_id'], $account['account_type']);
|
|
$roles[] = 'CHILD';
|
|
}
|
|
// 获取商户行业标识
|
|
return compact("menus", "buttons", "roles");
|
|
}
|
|
|
|
public static function list(array $where, int $belong_id, int $account_type)
|
|
{
|
|
$model = self::where('belong_id', $belong_id)
|
|
->where('account_type', $account_type);
|
|
if ($where['username'] != '') {
|
|
$model = $model->where('username', "like", "%$where[username]%");
|
|
}
|
|
if ($where['dept_id'] != '') {
|
|
$model = $model->where('dept_id', $where['dept_id']);
|
|
}
|
|
$paginate = $model->orderByDesc("account_id")
|
|
->paginate((int)$where['limit'], ['account_id', 'username', 'avatar', 'create_time', 'dept_id']);
|
|
$count = $paginate->total();
|
|
$data = $paginate->items();
|
|
foreach ($data as &$item) {
|
|
$item['roles'] = AccountRole::getRole($item['account_id']);
|
|
$item['posts'] = AccountPost::getPost($item['account_id']);
|
|
}
|
|
return compact("data", "count");
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public static function add(array $data)
|
|
{
|
|
$data['create_time'] = date("Y-m-d H:i:s");
|
|
$data['salt'] = Str::randStr(6);
|
|
$data['password'] = md5($data['salt'] . $data['password']);
|
|
$roles = $data['roles'];
|
|
$posts = $data['posts'];
|
|
unset($data['roles'], $data['posts']);
|
|
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;
|
|
}
|
|
}
|
|
$account_post = [];
|
|
foreach ($posts as $post_id) {
|
|
$account_post[] = ['post_id' => $post_id, 'account_id' => $account_id];
|
|
}
|
|
if (!empty($account_post)) {
|
|
$res = AccountPost::insert($account_post);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 修改
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public static function edit(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']);
|
|
unset($data['salt']);
|
|
}
|
|
$roles = $data['roles'];
|
|
$posts = $data['posts'];
|
|
unset($data['roles'], $data['posts']);
|
|
Db::beginTransaction();
|
|
$res = self::where("account_id", $data['account_id'])->update($data);
|
|
if (!$res) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
// 删除之前的
|
|
AccountRole::where('account_id', $data['account_id'])->delete();
|
|
$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;
|
|
}
|
|
}
|
|
// 删除之前的
|
|
AccountPost::where('account_id', $data['account_id'])->delete();
|
|
$account_post = [];
|
|
foreach ($posts as $post_id) {
|
|
$account_post[] = ['post_id' => $post_id, 'account_id' => $data['account_id']];
|
|
}
|
|
// 重新添加
|
|
if (!empty($account_post)) {
|
|
if (!AccountPost::insert($account_post)) {
|
|
Db::rollBack();
|
|
return false;
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}
|
|
|
|
public function roles()
|
|
{
|
|
return $this->belongsToMany(
|
|
Role::class,
|
|
'account_role',
|
|
'account_id',
|
|
'role_id'
|
|
);
|
|
}
|
|
|
|
public function posts()
|
|
{
|
|
return $this->belongsToMany(
|
|
Post::class,
|
|
'account_post',
|
|
'account_id',
|
|
'post_id'
|
|
);
|
|
}
|
|
|
|
public function dept()
|
|
{
|
|
return $this->hasOne(
|
|
Dept::class,
|
|
'dept_id',
|
|
'dept_id'
|
|
);
|
|
}
|
|
}
|