You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

289 lines
8.7 KiB

<?php
declare (strict_types=1);
namespace app\api\model;
use auth\PermissAuth;
use think\Model;
/**
* swagger response返回数据引用
* @OA\Schema(
* schema="ManagerMsgExport",
* required={"code","msg","count","data"},
* @OA\Property(
* property="code",
* type="integer",
* format="int32",
* description="状态码"
* ),
* @OA\Property(
* property="msg",
* type="string",
* description="提示消息"
* ),
* @OA\Property(
* property="count",
* type="integer",
* format="int32",
* description="记录总数",
* ),
* @OA\Property(
* property="data",
* type="array",
* description="请求结果",
* @OA\Items(
* @OA\Property(
* property="id",
* type="integer",
* description="ID"
* ),
* @OA\Property(
* property="site_id",
* type="integer",
* description="站点ID"
* ),
* @OA\Property(
* property="username",
* type="string",
* description="管理员名称",
* ),
* @OA\Property(
* property="truename",
* type="string",
* description="管理员真实姓名",
* ),
* @OA\Property(
* property="sex",
* type="integer",
* description="性别"
* ),
* @OA\Property(
* property="phone",
* type="integer",
* description="手机号"
* ),
* @OA\Property(
* property="email",
* type="string",
* description="邮箱",
* ),
* @OA\Property(
* property="status",
* type="integer",
* description="状态",
* ),
* @OA\Property(
* property="ip",
* type="integer",
* description="ip",
* ),
* @OA\Property(
* property="avatar",
* type="string",
* description="头像地址",
* ),
* @OA\Property(
* property="introduction",
* type="string",
* description="用户简介",
* ),
* @OA\Property(
* property="delete_time",
* type="integer",
* description="是否删除 null:未删除"
* ),
* @OA\Property(
* property="login_time",
* type="integer",
* description="最后登录时间"
* ),
* @OA\Property(
* property="update_time",
* type="integer",
* description="更新时间"
* ),
* @OA\Property(
* property="create_time",
* type="integer",
* description="创建时间"
* ),
* )
* ),
*
* ),
*/
/**
* @mixin think\Model
*/
class Manager extends Common
{
protected $table = 'system_user';
/**
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
//执行通用查询
return parent::parentLists();
}
/**
* 登录验证
* @param array $_post
* @return array|\type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function checkLogin($_post = [])
{
//站点域名检测
$site_id = (int)(new Site())->checkSite();
$_data = self::where('username', $_post['username'])->where('site_id', $site_id)->find();
if (empty($_data)) {
return send_http_status('', 40502);
}
if ($_data['status'] != 1) {
return send_http_status('', 40503);
}
if (!password_verify($_post['password'], $_data['password'])) {
return send_http_status('', 40504);
}
//登录成功写入登录信息
$this->setLoginIp($_data['id'], $site_id);
return $_data->hidden(['password'])->toArray();
}
/**
* 用户最后登录 IP
*/
private function setLoginIp(int $user_id = 0, int $site_id = 0)
{
$_data = [
'ip' => ip2long(request()->ip()),
'login_time' => time(),
];
$_op = $this->where('site_id', $site_id)->where('id', $user_id);
return $_op->save($_data);
}
/**
* 读取后
* @param Model $model
* @return bool|void
*/
public static function onAfterRead(Model $model)
{
if (in_array(request()->action(), ['edit', 'delete', 'status'])) {
return true;
}
$model->hidden(['password']);
$model->offsetSet('ip', long2ip((int)$model->getAttr('ip')));
$model->offsetSet('sexname', config('dictionary.sqlfields.sex')[$model->getAttr('sex')]);
//用户权限
$model->offsetSet('authorities', PermissAuth::getInstance()->getAuthList($model->getAttr('id'), 1, true));
//用户角色
$_roles = PermissAuth::getInstance()->getUserGroups($model->getAttr('id'));
$model->offsetSet('roles', $_roles);
$model->offsetSet('role_ids', implode(',', array_column($_roles, 'id')));
//省市区代码转换
$model->setAttr('province_city', [$model->getAttr('province'), $model->getAttr('city'), $model->getAttr('area')]);
}
/**
* 新增前
* @param Model $model
* @return mixed|void
*/
public static function onBeforeInsert(Model $model)
{
if (!in_array(request()->action(), ['add'])) {
return true;
}
//密码加密
$salt_password = password_hash($model->getAttr('password'), PASSWORD_DEFAULT);
$model->setAttr('password', $salt_password);
}
/**
* 修改前
* @param Model $model
* @return mixed|void
*/
public static function onBeforeUpdate(Model $model)
{
//禁止修改用户名
$model->offsetUnset('username');
$action = request()->action();
if (!in_array($action, ['edit', 'passwordEdit', 'passwordReset', 'editPersional'])) {
return true;
}
//密码修改验证逻辑
if ($action == 'passwordEdit') {
if (!password_verify($model->getData('old_password'), $model->getOrigin('password'))) {
return send_http_status('', 40504);
}
}
//密码加密
if ($action == 'passwordEdit' || $action == 'passwordReset') {
$salt_password = password_hash($model->getData('password'), PASSWORD_DEFAULT);
$model->setAttr('password', $salt_password);
}
//省市区代码拆分
$city_arr = $model->getAttr('province_city');
if (!empty($city_arr)) {
$model->setAttr('province', $city_arr[0]);
$model->setAttr('city', $city_arr[1]);
$model->setAttr('area', $city_arr[2]);
}
}
/**
* 新增前、修改前 同时
* @param Model $model
* @return mixed|void
*/
public static function onBeforeWrite(Model $model)
{
parent::onBeforeWrite($model);
if (!in_array(request()->action(), ['add', 'edit'])) {
return true;
}
//以下参数禁止新增或更改
$model->offsetUnset('ip');
}
public static function onAfterWrite(Model $model)
{
parent::onAfterWrite($model);
if (!in_array(request()->action(), ['add', 'edit'])) {
return true;
}
//写入用户权限
$id = $model->getAttr('id') ?? $model->getAttr('copy_safe_id');
PermissAuth::getInstance()->addUserGroupAccess((int)$id, $model->getAttr('role_ids'));
}
}