UserLogic.php 3.5 KB
<?php

namespace App\Http\Logic\Bside\User;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Image;
use App\Models\User\User;

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->info($this->param);
        return $this->success($info);
    }
    /**
     * @name :添加会员
     * @author :liyuhang
     */
    public function user_add(){
        //验证当前用户是否存在
        $info = $this->model->read(['mobile'=>$this->param['mobile']]);
        if($info !== false){
            $this->fail('当前手机号码已注册');
        }
        $this->param['create_id'] = $this->user['id'];
        $this->param['operator_id'] = $this->user['id'];
        $this->param['project_id'] = $this->user['project_id'];
        //上传图片
        if(isset($this->param['image'])){
            $this->param['image'] = $this->upload();
        }
        //密码加密
        $this->param['password'] = base64_encode(md5($this->param['password']));
        $rs = $this->model->add($this->param);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :编辑用户
     * @author :liyuhang
     */
    public function user_edit(){
        $condition = [
            'id'=>['!=',$this->param['id']],
            'mobile'=>$this->param['mobile']
        ];
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前编辑的手机号码已存在');
        }
        $this->param['operator_id'] = $this->user['id'];
        try {
            //上传图片
            if(isset($this->param['image']) && is_file($this->param['image'])){
                //查看当前用户是否已有头像
                $info = $this->model->read(['id'=>$this->param['id']],['id','image']);
                if($info !== false && !empty($info['image'])){
                    //TODO::删除资源
                    $imageModel = new Image();
                    $image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
                    shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['image'] . '*');
                    $imageModel->del(['hash'=>$info['image']]);
                }
                $this->param['image'] = $this->upload();
                $this->model->edits($this->param);
            }
        }catch (\exception $e){
            $this->fail('参数错误或其他服务器原因,编辑失败');
        }
        return $this->success();
    }

    /**
     * @name :编辑状态/排序
     * @author :liyuhang
     */
    public function user_status(){
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error',Code::USER_ERROR);
        }
        return $this->success();
    }

    /**
     * @name :删除用户(逻辑删除)
     * @return void
     * @author :liyuhang
     * @method
     */
    public function user_del(){
        $ids = $this->param['id'];
        $this->param['id'] = ['in',$this->param['id']];
        $this->del($this->param,$ids);
        return $this->success();
    }

}