# we-admin-api **Repository Path**: 730399474/we-admin-api ## Basic Information - **Project Name**: we-admin-api - **Description**: we-admin的后端接口php版本实现 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-09 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 简介 这是一个基于lumen 结合[we-vue-admin]()的通过配置来快速开发后台的项目 [基本用法](./admin.md) ## 特性 - 自带菜单管理 权限管理 可以直接用于项目开发 - 简单的配置既可快速生成页面 - 配置与表结构相关联 - 前后端完全分离 - 前端只负责页面的渲染 - 业务相关逻辑和页面显示内容完全由后端通过接口控制 - 简单的开发流程 只需要重写跟业务相关的方法即可 ## 安装 ### 1. 环境要求 - nginx+mysql+php环境 - php版本要求 >=7.1.3 ### 2. 安装依赖 ``` composer install ``` ### 3. 配置 vhosts.conf - 配置到项目根目录的 /public 文件夹下 ``` server { listen 8188; server_name localhost; root "D:/we-demo/public"; # 指定允许跨域的方法,*代表所有 add_header Access-Control-Allow-Methods *; # 预检命令的缓存,如果不缓存每次会发送两次请求 add_header Access-Control-Max-Age 3600; # 带cookie请求需要加上这个字段,并设置为true add_header Access-Control-Allow-Credentials true; # 表示允许这个域跨域调用(客户端发送请求的域名和端口) # $http_origin动态获取请求客户端请求的域 不用*的原因是带cookie的请求不支持*号 add_header Access-Control-Allow-Origin $http_origin; add_header Access-Control-Allow-Header token; add_header Access-Control-Allow-Header Referer; # 表示请求头的字段 动态获取 add_header Access-Control-Allow-Headers $http_access_control_request_headers; # OPTIONS预检命令,预检命令通过时才发送请求 # 检查请求的类型是不是预检命令 if ($request_method = OPTIONS){ return 204; } location / { try_files $uri $uri/ /index.php?$args; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|ttf|woff)$ { root D:/we-demo/;#指定图片存放路径 expires max; log_not_found off; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } ``` ## 快速开始 下面以example_type表为例说明生成页面的流程 #### 1. 新建Model app/Model/Admin/ExampleTypeModel.php - 继承 App\Model\AdmModel - 重写 $table 表名为对应表 - 重写 $columns 配置字段的显示 - 重写 $operations 给对应字段配置操作 (可选) - 重写 $commonOperations 给配置公共操作 (可选) - 重写 $options 给对应字段配置选项 (可选) ```php namespace App\Model\Admin; use App\Model\AdmModel; class ExampleOperationModel extends AdmModel { public $table = 'example_type'; public $timestamps = null; /** * @var array 字段配置 */ public $columns = [ 'id' => '[ID]|show:1', 'int' => '[整形]|int:5,100,2', 'float' => '[浮点型]|float', 'string' => '[字符串]|string', 'image' => '[图片]|image', 'images' => '[图片数组]|images', 'file' => '[文件]|file', 'files' => '[文件数组]|files', 'select' => '[下拉框]|select', 'radio' => '[单选按钮]|radio:[失效],[生效]', 'switch' => '[开关]|switch:0[true],1[false]', 'slider' => '[滑块]|slider:0,50,2', 'timestamp' => '[时间戳]|timestamp', 'datetime' => '[日期]|datetime', 'textarea' => '[文本框]|textarea', 'markdown' => '[markdown编辑器]|markdown', ]; /** * @var array 字段操作 */ public $operations = [ '_action_' => [ [ 'action' => 'edit', 'type' => 'primary', 'label' => '编辑', ], [ 'action' => 'delete', 'type' => 'danger', 'label' => '删除', ], ] ]; /** * @var array 公共操作 */ public $commonOperations = [ [ 'action' => 'create', 'type' => 'success', 'label' => '新增', ], ]; /** * @var array 字段选项 */ public $options = [ 'select' => [ 'option1' => '选项1', 'option2' => '选项2', 'option3' => '选项3', 'option4' => '选项4', 'option5' => '选项5', ] ]; } ``` #### 2. 新建Service app/Service/Admin/ExampleTypeService.php - 继承 App\Service\AdmService - 重写 __construct() 把ExampleTypeModel赋值给$this->model - 重写 $model 注释为ExampleTypeModel (方便跳转) ```php namespace App\Service\Admin; use App\Model\Admin\ExampleOperationModel; use App\Service\AdmService; class ExampleOperationService extends AdmService { /** * @var ExampleOperationModel */ public $model; public function __construct() { $this->model = new ExampleOperationModel(); } public function modify($id, $data) { if (isset($data['images'])) { $data['images'] = \GuzzleHttp\json_encode($data['images']); } if (isset($data['files'])) { $data['files'] = \GuzzleHttp\json_encode($data['files']); } return parent::modify($id, $data); } } ``` #### 3. 新建Controller app/Http/AdminControllers/ExampleTypeController.php - 继承 Controller - 重写 __construct() 把ExampleOperationService赋值给$this->service - 重写 $model 注释为ExampleOperationService (方便跳转) ```php namespace App\Http\AdminControllers; use App\Service\Admin\ExampleOperationService; class ExampleOperationControllers extends Controller { /** * @var ExampleOperationService */ public $service; public function __construct() { $this->service = new ExampleOperationService(); } } ``` ### 4. 后台路由routers/adminWeb.php 添加相关路由 ```php /* @see \App\Http\AdminControllers\ExampleTypeControllers 类型例子 */ $router->group( [ 'prefix' => '/exampleType', ], function () use ($router){ generateAdmRouter($router, 'ExampleTypeControllers'); }); ``` ### 5. 配置菜单 - 在前端页面配置对应的菜单 - 注意组件 @/views/complex/assembly/index 是公共组件页面 可以一个页面对应多个后端逻辑 ![菜单编辑](./assets/README/202011061604659911790.png) ### 6. 给当前管理员的角色添加权限 ![设置权限](./assets/README/202011061604660133704.png) ### 7. 刷新页面 此时就会出现相关页面 - 后续逻辑的修改只需重写 新建的 controller service 和 model 里面父级的方法 已达到后端业务需求 - 也可以其他逻辑业务逻辑 ## 迭代规划 - 更多输入组件, 列表组件, 操作特性 - 颗粒度精细到操作的权限控制 - 操作日志