ComController.php 7.9 KB
<?php

namespace App\Http\Controllers\Bside\BCom;

use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Helper\Common;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\User\UserLogic;
use App\Models\CustomModule\CustomModule;
use App\Models\Project\DeployBuild;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\User\ProjectMenu as ProjectMenuModel;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User;
use Illuminate\Support\Facades\Cache;

/***
 * 当前为公共类
 */
class ComController extends BaseController
{
    /**
     * @name :获取当前用户权限菜单列表
     * @author :liyuhang
     * @method
     */
    public function get_menu(){
        //根据当前登录用户角色返回用户菜单列表
        $projectMenuModel = new ProjectMenuModel();
        if($this->user['role_id'] != 0){
            $this->map = $this->getNoAdminMenuCondition();
        }else{
            $this->map = $this->getAdminMenuCondition();
        }
        $lists = $projectMenuModel->list($this->map,'sort');
        foreach ($lists as $k => $v){
            $v = (array)$v;
            if(empty($this->user['is_upload_manage'])){
                if($v['rules'] == '/fileUpload'){
                    unset($lists[$k]);
                }
            }
        }
        $menu = array();
        foreach ($lists as $k => $v){
            $v = (array)$v;
            if ($v['pid'] == 0) {
                $v['sub'] = _get_child($v['id'], $lists);
                $menu[]   = $v;
            }
        }
        $this->response('当前用户菜单列表',Code::SUCCESS,$menu);
    }

    /**
     * @remark :获取当前菜单的自定义模块
     * @name   :getProjectCustomMenu
     * @author :lyh
     * @method :post
     * @time   :2023/12/13 16:48
     */
    public function getCustomMenu(){
        $customModel = new CustomModule();
        $list = $customModel->list(['status'=>0],['sort','id']);
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $v['sub'] = [$v['name'].'管理',$v['name'].'分类'];
                $list[$k] = $v;
            }
        }
        $this->response('success',Code::SUCCESS,$list);
    }

    /**
     * @name :获取当前项目详情
     * @author :liyuhang
     * @method
     */
    public function get_project(Project $projectModel){
        $info = $projectModel->read(['id'=>$this->user['project_id']]);
        if(empty($info)){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @remark :非超级管理员菜单列表
     * @name   :getRoleMenuLis
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 11:47
     */
    public function getNoAdminMenuCondition(){
        $code = $this->getIsHome();
        $projectRoleModel = new ProjectRoleModel();
        $info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
        if($info === false){
            $this->fail('当前登录角色不存在');
        }else{
            if($code != 1){
                $info['role_menu'] = trim(str_replace(',11,',',',','.$info['role_menu'].','),',');
            }
            $this->map = [
                'status'=>0,
                'is_role'=>0,
                'id'=>['in',explode(',',$info['role_menu'])]
            ];
        }
        return $this->map;
    }

    /**
     * @remark :超级管理员菜单列表
     * @name   :getAdminMenuCondition
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 13:53
     */
    public function getAdminMenuCondition(){
        $this->map['status'] = 0;
        $code = $this->getIsHome();
        if($code != 1){
            $this->map['id'] = ['!=',11];//排除菜单网站装修
        }
        return $this->map;
    }


    /**
     * @remark :查看是否显示网站装饰
     * @name   :getIsHome
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 11:30
     */
    public function getIsHome(){
        if(isset($this->user['manager_id'])){
            return 1;
        }
        $deployBuild = new DeployBuild();
        $info = $deployBuild->read(['project_id'=>$this->user['project_id']]);
        if(!empty($info['configuration'])){
            $configuration = Arr::s2a($info['configuration']);
            if(isset($configuration['is_home']) && ((int)$configuration['is_home'] != 0)){
                return 1;
            }
        }
        return 0;
    }

    /**
     * @name :登录用户编辑资料/修改密码
     * @author :liyuhang
     * @method
     */
    public function edit_info(){
            $this->request->validate([
//                'oldPassword'=>'required',
                'password' => 'required',
                'confirm'=>'required',
            ], [
//                'oldPassword.required' => '请输入原密码',
                'password.required' => '请输入新密码',
                'confirm.required' => '请再次输入新密码',
            ]);
            //查询员密码是否正确
            $userModel = new User();
            $info = $userModel->read(['id'=>$this->user['id']]);
//            if($info['password'] != base64_encode(md5($this->param['oldPassword']))){
//                $this->response('原密码错误',Code::USER_ERROR);
//            }
            if($this->param['password'] != $this->param['confirm']){
                $this->response('两次密码不一致');
            }
            $rs = $userModel->edit(['password'=>base64_encode(md5($this->param['password']))],['id'=>$this->user['id']]);
            if($rs === false){
                $this->response('系统错误',Code::SYSTEM_ERROR);
            }
            Cache::pull($info['token']);
            $this->response('success');
    }

    /**
     * @remark :退出登录
     * @name   :logout
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 9:14
     */
    public function logout(){
        $rs = Cache::pull($this->token);
        if($rs === false){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response('success');
    }

    /**
     * @remark :微信解绑
     * @name   :unbindWechat
     * @author :lyh
     * @method :post
     * @time   :2023/9/1 16:47
     */
    public function unbindWechat(){
        $userModel = new User();
        $rs = $userModel->edit(['wechat'=>''],['id'=>$this->user['id']]);
        if($rs === false){
            $this->response('error',Code::USER_ERROR);
        }
        $this->response('success',Code::SUCCESS,['status'=>0]);
    }

    /**
     * @remark :生成token
     * @name   :generateToken
     * @author :lyh
     * @method :post
     * @time   :2023/8/24 17:27
     */
    public function generateToken(){
        $data = [
            'phone' => $this->user['mobile'],
            'from_order_id' => $this->user['from_order_id'] ?? '5a179274si3j8z', // 提单系统 同步到个项目的唯一凭证(数字或者字符串)
            'timestamp' => time(),  // 接收到字符串解密出来以后需要 验证时间不超过30秒 超过时间视为无效授权
        ];
        $common = new Common();
        $str = $common->encrypt($data);
        $this->response('success',Code::SUCCESS,['str'=>$str]);
    }

    /**
     * @remark :获取访问链接
     * @name   :getLink
     * @author :lyh
     * @method :post
     * @time   :2023/11/10 15:18
     */
    public function getLink(){
        $this->request->validate([
            'type' => 'required',
        ], [
            'type.required' => '类型不能为空',
        ]);
        switch ($this->param['type']){
            case 'news':
                $url_link = $this->user['domain'].RouteMap::SOURCE_NEWS.'/';
                break;
            case 'blog':
                $url_link = $this->user['domain'].RouteMap::SOURCE_BLOG.'/';
                break;
            default:
                $url_link = $this->user['domain'];
        }
        $this->response('success',Code::SUCCESS,['url'=>$url_link]);
    }
}