Files
yusheng-php/application/guildadmin/model/Admin.php
2025-12-06 00:16:17 +08:00

88 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\guildadmin\model;
use fast\Random;
use think\Db;
use think\Model;
use think\Session;
use app\guildadmin\library\Auth;
use think\Validate;
class Admin extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $table = 'fa_vs_guild_admin';
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $hidden = [
'password',
'salt'
];
public static function init()
{
self::beforeWrite(function ($row) {
$changed = $row->getChangedData();
//如果修改了用户或或密码则需要重新登录
if (isset($changed['username']) || isset($changed['password']) || isset($changed['salt'])) {
$row->token = '';
}
});
}
//创建管理员(公会创建调用)
public function createAdmin($data){
$params['salt'] = Random::alnum();
$params['username'] = $data['username'];
$params['nickname'] = $data['nickname'];
$params['password'] = md5(md5($data['password']) . $params['salt']);
$params['guild_id'] = $data['guild_id'];
$result = $this->save($params);
if ($result === false) {
return ['code'=>0,'msg'=>'失败', 'data' => null];
}
//创建角色组
$group_data = [
'guild_id' => $data['guild_id'],
'pid' => 1,
'name' => '超级管理员',
'rules' => '*',
'status' => 'normal'
];
$group = model('guildadmin/AuthGroup')->create($group_data);
if (!$group) {
return ['code'=>0,'msg'=>'失败', 'data' => null];
}
$dataset = [];
$dataset[] = ['uid' => $this->id, 'group_id' => $group['id']];
model('guildadmin/AuthGroupAccess')->saveAll($dataset);
return ['code'=>1,'msg'=>'成功', 'data' => null];
}
/**
* 编辑
*/
public function edit($id,$data)
{
if ($data['password']) {
if (!Validate::is($data['password'], '\S{6,30}')) {
return ['code'=>1,'msg'=>'密码长度必须在6-30位之间不能包含空格', 'data' => null];
}
$data['salt'] = Random::alnum();
$data['password'] = md5(md5($data['password']) . $data['salt']);
} else {
unset($data['password'], $data['salt']);
}
$result = db::name('vs_guild_admin')->where('id', $id)->update($data);
if($result){
return ['code'=>1,'msg'=>'成功', 'data' => null];
}else{
return ['code'=>0,'msg'=>'失败', 'data' => null];
}
}
}