作者 zhl

u

@@ -5,6 +5,7 @@ namespace App\Http\Middleware\Bside; @@ -5,6 +5,7 @@ namespace App\Http\Middleware\Bside;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Models\ProjectMenu; 6 use App\Models\ProjectMenu;
7 use App\Models\ProjectRole as ProjectRoleModel; 7 use App\Models\ProjectRole as ProjectRoleModel;
  8 +use App\Services\ProjectServer;
8 use Closure; 9 use Closure;
9 use Illuminate\Http\Request; 10 use Illuminate\Http\Request;
10 use Illuminate\Http\Response; 11 use Illuminate\Http\Response;
@@ -29,6 +30,9 @@ class LoginAuthMiddleware @@ -29,6 +30,9 @@ class LoginAuthMiddleware
29 if(empty($info)){ 30 if(empty($info)){
30 return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']); 31 return response(['code'=>Code::USER_ERROR,'msg'=>'当前用户未登录']);
31 } 32 }
  33 + // 设置数据信息
  34 + ProjectServer::useProject($info['project_id']);
  35 +
32 //操作权限设置 36 //操作权限设置
33 $projectRoleModel = new ProjectRoleModel(); 37 $projectRoleModel = new ProjectRoleModel();
34 $role_info = $projectRoleModel->read(['id'=>$info['role_id']]); 38 $role_info = $projectRoleModel->read(['id'=>$info['role_id']]);
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2023/4/17
  6 + * Time: 15:16
  7 + */
  8 +
  9 +namespace App\Services;
  10 +
  11 +use App\Models\Project;
  12 +
  13 +/**
  14 + * Class ProjectServer
  15 + * @package App\Services
  16 + */
  17 +class ProjectServer extends BaseService
  18 +{
  19 + /**
  20 + * @param $project_id
  21 + * @return bool
  22 + */
  23 + public static function useProject($project_id)
  24 + {
  25 + $project = Project::getProjectById($project_id);
  26 + if (empty($project))
  27 + return false;
  28 + // 设置 database.connections.custom_mysql 配置
  29 + config(['database.connections.custom_mysql.host' => $project->mysqlConfig()->host]);
  30 + config(['database.connections.custom_mysql.port' => $project->mysqlConfig()->port]);
  31 + config(['database.connections.custom_mysql.database' => $project->databaseName()]);
  32 + config(['database.connections.custom_mysql.username' => $project->mysqlConfig()->user]);
  33 + config(['database.connections.custom_mysql.password' => $project->mysqlConfig()->password]);
  34 + // 设置 redis 配置
  35 + return true;
  36 + }
  37 +}