UserLogic.php 7.1 KB
<?php

namespace App\Http\Logic\Aside\User;

use App\Helper\Common;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Manage\Manage;
use App\Models\Manage\MenuSpecial;
use App\Models\Manage\Mobile;
use App\Models\Project\Project;
use App\Models\User\ProjectRole;
use App\Models\User\User;
use App\Models\User\User as UserModel;
use Illuminate\Support\Facades\DB;

class UserLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new User();
        $this->param = $this->requestAll;
    }

    /**
     * @name :获取详情
     * @return void
     * @author :liyuhang
     * @method
     */
    public function user_info()
    {
        $info = $this->model->read($this->param, ['id', 'project_id', 'name', 'status', 'role_id' ,'mobile', 'operator_id']);
        if ($info === false) {
            $this->fail('当前数据不存在');
        }
        $info['project_name'] = (new Project())->read(['id' => $info['project_id']], ['title'])['title'];
        return $this->success($info);
    }

    /**
     * @remark :保存用户
     * @name   :projectUserSave
     * @author :lyh
     * @method :post
     * @time   :2023/9/22 14:35
     */
    public function projectUserSave()
    {
        //验证手机号是否存在
        $this->verifyMobile($this->param);
        //验证一个项目是否只有一个超级管理员
        $this->verifyRole($this->param);
        if (isset($this->param['id']) && !empty($this->param['id'])) {
            $info = $this->model->read(['id'=>$this->param['id']]);
            if($info['role_id'] == 0){
                //更新项目信息
                $projectModel = new Project();
                $projectModel->edit(['lead_name'=>$this->param['name'],'mobile'=>$this->param['mobile']],['id'=>$info['project_id']]);
            }
            $this->param = $this->editPassword($this->param);
            $rs = $this->model->edit($this->param, ['id' => $this->param['id']]);
            DB::table('gl_user_edit_log')->insert(['message'=>json_encode($this->param),'user_id'=>$this->manager['id'],'project_id'=>$info['project_id'],'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')]);
        } else {
            $this->param['password'] = base64_encode(md5($this->param['password']));
            $rs = $this->model->add($this->param);
        }
        if ($rs === false) {
            $this->fail('添加失败');
        }
        return $this->success();
    }

    /**
     * @remark :验证手机号是否可用
     * @name   :verifyMobile
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 9:14
     */
    public function verifyMobile($param){
        if(isset($param['id']) && !empty($param['id'])){
            $condition = [
                'mobile' => $param['mobile'],
                'project_id'=>$param['project_id'],
                'id' => ['!=', $param['id']]
            ];
        }else{
            $condition = [
                'mobile' => $param['mobile'],
                'project_id'=>$param['project_id'],
            ];
        }
        $info = $this->model->read($condition);
        if ($info !== false) {
            $this->fail('当前手机号码已存在');
        }
        return $this->success();
    }

    /**
     * @remark :验证一个项目超级管理员只允许存在一个
     * @name   :verifyRole
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 9:22
     */
    public function verifyRole($param){
        if(!isset($param['role_id']) || empty($param['role_id'])){
            $param['role_id'] = $this->model::ROLE_MANAGER;
        }
        if($param['role_id'] == $this->model::ROLE_MANAGER){
            if(isset($param['id']) && !empty($param['id'])){
                $condition = [
                    'project_id'=>$param['project_id'],
                    'id' => ['!=', $param['id']],
                    'role_id'=>$this->model::ROLE_MANAGER
                ];
            }else{
                $condition = [
                    'project_id'=>$param['project_id'],
                    'role_id'=>$this->model::ROLE_MANAGER,
                ];
            }
            $info = $this->model->read($condition);
            if ($info !== false) {
                $this->fail('当前项目已存在超级管理员');
            }
        }
    }

    /**
     * @name :编辑会员
     * @return void
     * @author :liyuhang
     * @method
     */
    public function editPassword($param)
    {
        //验证密码是否更改
        if (isset($param['password']) && !empty($param['password'])) {
            $info = $this->model->read(['id' => $param['id']]);
            if ($info['password'] != $param['password']) {
                $param['password'] = base64_encode(md5($param['password']));
            }
        }
        return $this->success($param);
    }

    /**
     * @name :删除会员
     * @return void
     * @author :liyuhang
     * @method
     */
    public function user_del()
    {
        foreach ($this->param['id'] as $id){
            $info = $this->model->read(['id'=>$id],['id','role_id','project_id']);
            if($info['role_id'] == 0) {
                //查看当前项目是否有其他的超级管理员
                $roleInfo = $this->model->read(['id' => ['!=', $id], 'role_id' => 0,'project_id'=>$info['project_id']]);
                if ($roleInfo === false) {
                    $this->fail('唯一超级管理员,禁止删除');
                }
            }
            $rs = $this->model->del(['id' => $id]);
            if ($rs === false) {
                $this->fail('系统错误,请联系管理员');
            }
            Common::del_user_cache($this->model, $id, 'A');
        }
        return $this->success();
    }

    /**
     * @remark :设置排序
     * @name   :setSort
     * @author :lyh
     * @method :post
     * @time   :2023/8/10 16:42
     */
    public function setParamStatus(){
        $rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('修改失败');
        }
        return $this->success();
    }

    /**
     * @remark :设置超级管理员
     * @name   :setRole
     * @author :lyh
     * @method :post
     * @time   :2023/9/22 14:38
     */
    public function setRole(){
        DB::beginTransaction();
        //获取当前用户的觉得
        $roleModel = new ProjectRole();
        $roleInfo = $roleModel->where('project_id',$this->param['project_id'])->orderBy('id','asc')->first();
        $info = $this->model->read(['role_id'=>0]);
        if(empty($info) || empty($roleInfo)){
            $this->fail('请先添加角色');
        }
        try {
            $this->model->edit(['role_id'=>$roleInfo['id']],['id'=>$info['id']]);
            $this->model->edit(['role_id'=>0],['id'=>$this->param['id']]);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        Common::del_user_cache($this->model, [$this->param['id'],$info['id']], 'A');
        return $this->success();
    }
}