3 Commits

Author SHA1 Message Date
wuyanwen
fd35d91f1c 修复角色名不显示 2020-01-06 09:28:14 +08:00
wuyanwen
f6e3ee1225 修改数据库字段 2019-12-30 17:07:30 +08:00
yanwenwu
7b782e916a 修改readme 2019-12-21 15:15:12 +08:00
995 changed files with 66226 additions and 26291 deletions

View File

@@ -1,24 +0,0 @@
# 环境
- 操作系统:
- php 版本:
- thinkphp 版本:
- Mysql 版本:
- web 服务器:
# 问题
- 问题描述:
- 问题截图:
# 结果
- 实际结果:
- 预期结果:
# 分析
- 所做的尝试:
-
-
# 方案:
- 解决方案:
> 请在问题解决后关闭 issue

View File

@@ -1,25 +0,0 @@
# 环境
- 操作系统:
- php 版本:
- thinkphp 版本:
- Mysql 版本:
- web 服务器:
# 问题
- 问题描述:
- 问题截图:
# 结果
- 实际结果:
- 预期结果:
# 分析
- 所做的尝试:
-
-
# 方案:
- 解决方案:
> 请在问题解决后关闭 issue

4
.gitignore vendored
View File

@@ -1,8 +1,6 @@
/.idea
/.vscode
/vendor
/package
/database
*.log
.env
composer.lock

View File

@@ -1,201 +1,32 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
ThinkPHP遵循Apache2开源协议发布并提供免费使用。
版权所有Copyright © 2006-2016 by ThinkPHP (http://thinkphp.cn)
All rights reserved。
ThinkPHP® 商标和著作权所有者为上海顶想信息科技有限公司。
1. Definitions.
Apache Licence是著名的非盈利开源组织Apache采用的协议。
该协议和BSD类似鼓励代码共享和尊重原作者的著作权
允许代码修改,再作为开源或商业软件发布。需要满足
的条件:
1 需要给代码的用户一份Apache Licence
2 如果你修改了代码,需要在被修改的文件中说明;
3 在延伸的代码中(修改和有源代码衍生的代码中)需要
带有原来代码中的协议,商标,专利声明和其他原来作者规
定需要包含的说明;
4 如果再发布的产品中包含一个Notice文件则在Notice文
件中需要带有本协议内容。你可以在Notice中增加自己的
许可但不可以表现为对Apache Licence构成更改。
具体的协议参考http://www.apache.org/licenses/LICENSE-2.0
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

179
README.md
View File

@@ -1,160 +1,47 @@
<p align="center">
<img src="https://cdn.learnku.com/uploads/images/202005/17/18206/zSuf7Ce5kM.png!large">
</p>
## CatchAdmin
<p align="center"><code>CatchAdmin</code>是一款基于<a href="http://www.thinkphp.cn/" target="_blank">thinkphp framework</a>
<a href="https://github.com/PanJiaChen/vue-element-admin/">element admin</a>二次开发而成后台管理系统。因为 thinkphp 的简单高效,文档齐全。在看了很多 thinkphp 生态中的后台管理系统,发现没有一款合适的前后端分离系统。遂开发了 CatchAdmin。
完全利用了 thinkphp6 的新版本特性 ServiceProvider将管理系统模块之间的耦合降到了最低限度。每个模块之间都有独立的 controller路由模型数据表`。在开发上尽可能将模块之间的影响降到最低,降低了开发上的难度。基于 CatchAdmin 可以开发 cmsCRMOA 等
等系统。也封装了很多实用的工具,提升开发体验。
</p>
<p align="center">
<a href="http://doc.catchadmin.com/">文档</a> |
<a href="http://vue.catchadmin.com">演示地址</a> |
<a href="http://apidoc.catchadmin.com">接口文档</a> |
<a href="https://gitee.com/jaguarjack/catchAdmin">项目源码</a> |
<a href="https://www.kancloud.cn/akasishikelu/thinkphp6">看云分析</a>
<a href="#extensions">扩展</a>
</p>
<p align="center">
<a href="https://gitee.com/jaguarjack/catchAdmin" target="_blank">
<img src="https://svg.hamm.cn/gitee.svg?type=star&user=jaguarjack&project=catchAdmin"/>
</a >
<a href="https://gitee.com/jaguarjack/catchAdmin" target="_blank">
<img src="https://svg.hamm.cn/gitee.svg?type=fork&user=jaguarjack&project=catchAdmin"/>
</a >
<img src="https://svg.hamm.cn/badge.svg?key=Base&value=ThinkPHP6"/>
<img src="https://svg.hamm.cn/badge.svg?key=Data&value=MySQL5.5"/>
<img src="https://svg.hamm.cn/badge.svg?key=Runtime&value=PHP7.1"/>
<img src="https://svg.hamm.cn/badge.svg?key=License&value=Apache-2.0"/>
</p >
## AntDV 版本
- 请使用 `v1`分支
## 功能
- [x] `用户管理` 后台用户管理
- [x] `部门管理` 配置公司的部门结构,支持树形结构
- [x] `岗位管理` 配置后台用户的职务
- [x] `菜单管理` 配置系统菜单,按钮等等
- [x] `角色管理` 配置用户担当的角色,分配权限
- [x] `数据字典` 管理后台表结构
- [x] `操作日志` 后台用户操作记录
- [x] `登录日志` 后台系统用户的登录记录
- [x] `代码生成` 生成 API 端的 CURD 操作
- [x] `敏感词` 支持敏感词配置
- [x] `附件管理` 可管理上传的文件
- [x] `定时任务` 可管理定时任务,而不依赖于 Crontab
- [x] `短信平台` 短信云管理,支持 阿里大于腾讯云UcloudSubmail
- [x] `云上传` 支持云上传七牛OSS腾讯
- [ ] `微信管理`
## 项目地址
- [github 地址](https://github.com/yanwenwu/catch-admin)
- [gitee 地址](https://gitee.com/jaguarjack/catchAdmin)
- [前端 Vue 项目地址](https://github.com/yanwenwu/catch-admin-vue)
- [文档地址](https://github.com/catch-admin/document)[个人精力实在有限,希望可以小伙伴们可以一起维护文档]
## 预览
<table>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wucNXq.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wucm6I.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wucZpd.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wuce1A.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wucnXt.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wucKnP.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wuc3tg.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wucM0f.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wucQ78.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wuc1AS.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wuc8hQ.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wucY1s.md.png"></td>
</tr>
<tr>
<td><img src="https://s1.ax1x.com/2020/09/07/wucJpj.md.png"></td>
<td><img src="https://s1.ax1x.com/2020/09/07/wuctcn.md.png"></td>
</tr>
</table>
## 环境要求
## 5.1 版本的请使用 tag1.0 版本
## 新版后台在开发中 请不要使用
### 环境要求
- php7.1+ (需以下扩展)
- [x] mbstring
- [x] json
- [x] openssl
- [x] xml
- [x] pdo
- mbstring
- json
- openssl
- xml
- pdo
- nginx
- mysql
### 如何安装
> 安装之前请确保已安装 Composer
### install
- curl -sS http://install.phpcomposer.com/installer | php
- composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
- composer update
- php think catch:install
#### 下载项目
- 通过 Git 下载(推荐)
```shell
git clone https://gitee.com/jaguarjack/catchAdmin && cd catchAdmin
### Use
- 配置虚拟域名 OR 在根目录下执行 php think run
- yourUrl/login
- 默认用户名 admin@gmail.com 密码 admin
curl -sS https://install.phpcomposer.com/installer | php
### Problem
> SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'updated_at'
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
composer install --ignore-platform-reqs
> 设置 sql_mode;
```
- composer 安装
```shell
composer create-project jaguarjack/catchadmin:dev-master
show variables like 'sql_mode' ;
remove 'NO_ZERO_IN_DATE,NO_ZERO_DATE'
```
> SET GLOBAL sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
#### 安装
下载完成之后通过命令来进行安装, 一键安装 🚀
```shell
php think catch:install
```
### Talking
- 可以提 ISSUE请按照 issue 模板提问
- 欢迎进入 Q 群,可以及时反馈一些问题。
- ![输入图片说明](https://images.gitee.com/uploads/images/2018/1219/110300_0257b6c0_810218.jpeg "微信图片_20181219105915.jpg")
仅供学习
## 体验地址
[体验地址](http://vue.catchadmin.com)
- 账号: catch@admin.com
- 密码: catchadmin
[catchadmin 文档地址](http://doc.catchadmin.com)
### 系列文章
如果是刚开始使用 thinkphp6, 以下文章可能会对你有些许帮助,文章基于 RC3 版本。整体架构是不变的。
- [Tp6 启动分析](https://www.kancloud.cn/akasishikelu/thinkphp6/1129385)
- [Tp6 Request 解析](https://www.kancloud.cn/akasishikelu/thinkphp6/1134496)
- [TP6 应用初始化](https://www.kancloud.cn/akasishikelu/thinkphp6/1130427)
- [Tp6 中间件分析](https://www.kancloud.cn/akasishikelu/thinkphp6/1136616)
- [Tp6 请求流程](https://www.kancloud.cn/akasishikelu/thinkphp6/1136608)
### Donate
如果你觉得项目对你有帮助,可以请作者喝杯咖啡☕️!鼓励下
<!--<img src="https://cdn.learnku.com/uploads/images/202008/11/18206/e6qAAM8Bod.jpg!large">-->
### Talking
- [论坛讨论](http://bbs.catchadmin.com)
- 可以提 `ISSUE`,请按照 `issue` 模板提问
- 加入 Q 群 `302266230` 暗号 `catchadmin`。
### Thanks
> 排名部分先后
- [top-think/think](https://github.com/top-think/think)
- [element-admin](https://panjiachen.gitee.io/vue-element-admin-site/zh/)
- [thans/tp-jwt-auth](https://packagist.org/packages/thans/tp-jwt-auth)
- [jaguarjack/think-filesystem-cloud](https://github.com/yanwenwu/think-filesystem-cloud)
- [overtrue/wechat](https://github.com/overtrue/wechat)
- [jaguarjack/migration-generator](https://github.com/yanwenwu/migration-generator)
- [phpoffice/phpspreadsheet](https://github.com/PHPOffice/PhpSpreadsheet)
[体验地址](http://demo.catchadmin.com/login)
- 账号: test@catch.com
- 密码: 123456

View File

@@ -3,8 +3,11 @@ declare (strict_types = 1);
namespace app;
use catcher\CatchAdmin;
use think\App;
use think\exception\ValidateException;
use think\facade\View;
use think\helper\Str;
use think\Validate;
/**

View File

@@ -1,6 +1,11 @@
<?php
namespace app;
use catcher\CatchResponse;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use catcher\exceptions\PermissionForbiddenException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
@@ -51,7 +56,10 @@ class ExceptionHandle extends Handle
*/
public function render($request, Throwable $e): Response
{
// if ($e instanceof CatchException){
return CatchResponse::fail($e->getMessage(), $e->getCode());
// }
// 其他错误交给系统处理
return parent::render($request, $e);
//return parent::render($request, $e);
}
}

View File

@@ -3,51 +3,12 @@ namespace app;
// 应用请求对象类
use catchAdmin\permissions\model\Users;
use catcher\CatchAuth;
use catcher\Code;
use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use thans\jwt\exception\TokenBlacklistException;
use thans\jwt\exception\TokenExpiredException;
use thans\jwt\exception\TokenInvalidException;
use catchAdmin\user\Auth;
class Request extends \think\Request
{
protected $auth;
/**
* login user
*
* @time 2020年01月09日
* @param null $guard
* @return mixed
*/
public function user($guard = null)
public function user()
{
if (!$this->auth) {
$this->auth = new CatchAuth;
}
try {
$user = $this->auth->guard($guard ? : config('catch.auth.default.guard'))->user();
if ($user->status == Users::DISABLE) {
throw new LoginFailedException('该用户已被禁用', Code::USER_FORBIDDEN);
}
} catch (\Exception $e) {
if ($e instanceof TokenExpiredException) {
throw new FailedException('token 过期', Code::LOGIN_EXPIRED);
}
if ($e instanceof TokenBlacklistException) {
throw new FailedException('token 被加入黑名单', Code::LOGIN_BLACKLIST);
}
if ($e instanceof TokenInvalidException) {
throw new FailedException('token 不合法', Code::LOST_LOGIN);
}
throw new FailedException('认证失败: '. $e->getMessage(), $e->getCode());
}
return $user;
return Auth::user();
}
}

View File

@@ -6,6 +6,5 @@ return [
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
// \think\middleware\SessionInit::class
\think\middleware\AllowCrossDomain::class,
\think\middleware\SessionInit::class
];

View File

@@ -1,4 +1,5 @@
<?php
return [
\catcher\CatchAdminService::class,
\jaguarjack\think\module\ThinkModuleService::class,
\catchAdmin\CatchAdminService::class,
];

BIN
catch/.DS_Store vendored

Binary file not shown.

View File

@@ -1,53 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catchAdmin\domain\support\contract\DomainRecordInterface;
use catchAdmin\domain\support\driver\aliyun\Domain;
use catchAdmin\domain\support\driver\aliyun\DomainRecord;
use catcher\ModuleService;
class DomainService extends ModuleService
{
public function register()
{
parent::register(); // TODO: Change the autogenerated stub
$this->registerInstance();
}
public function loadConfig()
{
return require __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
}
/**
* @return string
*/
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
/**
*
*/
protected function registerInstance()
{
$default = config('catch.domains.default');
$this->app->instance(DomainActionInterface::class, $this->app->make(__NAMESPACE__ . '\\support\\driver\\' . $default . '\Domain'));
$this->app->instance(DomainRecordInterface::class, $this->app->make(__NAMESPACE__ . '\\support\\driver\\' . $default . '\DomainRecord'));
}
}

View File

@@ -1,46 +0,0 @@
## 域名管理
- 阿里云(主支持)
- 腾讯云(需要做适配)
#### 配置
首先在 .env 文件设置
```
[ALIYUN]
ACCESS_KEY=
ACCESS_SECRET=
[QCLOUD]
ACCESS_KEY=
ACCESS_SECRET=
```
也可以在 config.php 文件配置
```
'domains' => [
// 默认阿里云
'default' => 'aliyun',
/**
* 阿里云配置
*
*/
'aliyun' => [
'api_domain' => 'http://alidns.aliyuncs.com',
'access_key' => Env::get('aliyun.access_key', ''),
'access_secret' => Env::get('aliyun.access_secret', ''),
],
/**
* 腾讯云配置
*
*/
'qcloud' => [
'api_domain' => 'cns.api.qcloud.com',
'access_key' => Env::get('qcloud.access_key', ''),
'access_secret' => Env::get('qcloud.access_secret', ''),
]
]
```

View File

@@ -1,43 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\facade\Env;
return [
'domains' => [
// 默认阿里云
'default' => 'aliyun',
/**
* 阿里云配置
*
*/
'aliyun' => [
'api_domain' => 'http://alidns.aliyuncs.com',
'access_key' => Env::get('aliyun.access_key', ''),
'access_secret' => Env::get('aliyun.access_secret', ''),
],
/**
* 腾讯云配置
*
*/
'qcloud' => [
'api_domain' => 'cns.api.qcloud.com',
'access_key' => Env::get('qcloud.access_key', ''),
'access_secret' => Env::get('qcloud.access_secret', ''),
]
]
];

View File

@@ -1,50 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\controller;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
class Domain extends CatchController
{
protected $domain;
public function __construct(DomainActionInterface $domain)
{
$this->domain = $domain;
}
/**
* 列表
*
* @time 2020/09/11 18:14
* @param Request $request
* @return \think\Response
*/
public function index(Request $request): \think\Response
{
return CatchResponse::paginate($this->domain->getList($request->param()));
}
/**
* 读取
*
* @time 2020/09/11 18:14
* @param $name
* @return \think\Response
*/
public function read($name)
{
return CatchResponse::success($this->domain->read(str_replace('-', '.', $name)));
}
}

View File

@@ -1,99 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\controller;
use catchAdmin\domain\support\contract\DomainRecordInterface;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
class DomainRecord extends CatchController
{
protected $domainRecord;
public function __construct(DomainRecordInterface $record)
{
$this->domainRecord = $record;
}
/**
* 列表
*
* @time 2020/09/11 18:14
* @param Request $request
* @return \think\Response
*/
public function index(Request $request): \think\Response
{
return CatchResponse::paginate($this->domainRecord->getList($request->param()));
}
/**
* 保存
*
* @time 2020/09/11 18:14
* @param Request Request
* @return \think\Response
*/
public function save(Request $request): \think\Response
{
return CatchResponse::success($this->domainRecord->store($request->post()));
}
/**
* 读取
*
* @time 2020/09/11 18:14
* @param $name
* @return \think\Response
*/
public function read($name): \think\Response
{
return CatchResponse::success($this->domainRecord->read($name));
}
/**
* 更新
*
* @time 2020/09/11 18:14
* @param Request $request
* @param $id
* @return \think\Response
*/
public function update($id, Request $request): \think\Response
{
return CatchResponse::success($this->domainRecord->update($id, $request->post()));
}
/**
* 删除
*
* @time 2020/09/11 18:14
* @param $id
* @return \think\Response
*/
public function delete($id): \think\Response
{
return CatchResponse::success($this->domainRecord->delete($id));
}
/**
* 设置状态
*
* @param $id
* @param $status
* @return \think\response\Json
*/
public function enable($id, $status)
{
return CatchResponse::success($this->domainRecord->enable($id, $status));
}
}

View File

@@ -1,97 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Seeder;
class DomainMenusSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
\catcher\Utils::importTreeData($this->getPermissions(), 'permissions', 'parent_id');
}
protected function getPermissions()
{
return array (
0 =>
array (
'id' => 72,
'permission_name' => '域名管理',
'parent_id' => 0,
'level' => '',
'route' => '/domain',
'icon' => 'el-icon-stopwatch',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domain',
'component' => 'layout',
'redirect' => '/domain/index',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 1,
'created_at' => 1601105834,
'updated_at' => 1601105834,
'deleted_at' => 0,
),
1 =>
array (
'id' => 73,
'permission_name' => '域名设置',
'parent_id' => 72,
'level' => '72',
'route' => '/domain/index',
'icon' => 'el-icon-setting',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domain',
'component' => 'domain',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 8,
'created_at' => 1601105879,
'updated_at' => 1601112604,
'deleted_at' => 0,
),
2 =>
array (
'id' => 84,
'permission_name' => '域名记录',
'parent_id' => 72,
'level' => '72',
'route' => '/domain/record/:domain',
'icon' => 'el-icon-document',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domainRecord',
'component' => 'domainRecord',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 2,
'sort' => 1,
'created_at' => 1601112569,
'updated_at' => 1601112606,
'deleted_at' => 0,
),
);
}
}

View File

@@ -1,15 +0,0 @@
{
"name": "域名管理",
"alias": "domain",
"description": "域名,阿里云,腾讯云",
"version": "1.0.0",
"keywords": [],
"order": 0,
"services": [
"\\catchAdmin\\domain\\DomainService"
],
"aliases": {},
"files": [],
"requires": [],
"enable": false
}

View File

@@ -1,23 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
// you should use `$router`
use catchAdmin\domain\controller\DomainRecord;
$router->group(function () use ($router){
// 域名管理
$router->get('domain', '\catchAdmin\domain\controller\Domain@index');
$router->get('domain/<name>', '\catchAdmin\domain\controller\Domain@read');
// 域名解析管理
$router->resource('record/domain', DomainRecord::class);
$router->put('record/domain/<id>/<status>', '\catchAdmin\domain\controller\DomainRecord@enable');
})->middleware('auth');

View File

@@ -1,70 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support;
use catchAdmin\domain\support\signature\Aliyun;
use catchAdmin\domain\support\signature\Qcloud;
/**
* 公共参数
*
* Class CommonParams
* @package catchAdmin\domain\support
*/
class CommonParams
{
/**
* 阿里云公共参数
*
* @param array $params
* @param string $method
* @return array
*/
public static function aliyun(array $params, $method = 'GET')
{
date_default_timezone_set('UTC');
$params = array_merge($params, [
'Format' => 'json',
'Version' => '2015-01-09',
'AccessKeyId' => config('catch.domains.aliyun.access_key'),
'SignatureMethod' => 'HMAC-SHA1',
'Timestamp' => date('Y-m-d\TH:i:s\Z'),
'SignatureVersion' => '1.0',
'SignatureNonce' => uniqid()
]);
$params['Signature'] = (new Aliyun($params))->signature($method);
return $params;
}
/**
* 腾讯云公共参数
*
* @param array $params
* @param string $method
* @return array
*/
public static function qcloud(array $params, $method = 'GET')
{
$params = array_merge($params, [
'SecretId' => config('catch.domains.qcloud.access_key'),
'SignatureMethod' => 'HmacSHA1',
'Timestamp' => time(),
'Nonce' => uniqid()
]);
$params['Signature'] = (new Qcloud($params))->signature($method);
return $params;
}
}

View File

@@ -1,112 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support;
use think\Paginator;
class Transformer
{
/**
* 分页展示
*
* @param array $data
* @return mixed
*/
public static function aliyunDomainPaginate(array $data)
{
$list = [];
foreach ($data['Domains']['Domain'] as $item) {
$list[] = [
'name' => $item['DomainName'],
'created_at' => date('Y-m-d', $item['CreateTimestamp']/1000),
'dns_server' => $item['DnsServers']['DnsServer'],
'from' => $item['VersionName'],
'free' => $item['VersionCode'] === 'mianfei',
'record_count' => $item['RecordCount'],
'tags' => $item['Tags']['Tag'],
'id' => $item['DomainId']
];
}
return Paginator::make($list, $data['PageSize'], $data['PageNumber'], $data['TotalCount']);
}
/**
* 腾讯云域名列表
*
* @param array $data
* @param $page
* @param $limit
* @return Paginator|\think\paginator\driver\Bootstrap
*/
public static function qcloudDomainPaginate(array $data, $page, $limit)
{
$info = $data['data']['info'];
$domains = $data['data']['domains'];
$list = [];
foreach ($domains as $item) {
$list[] = [
'name' => $item['name'],
'created_at' => $item['created_on'],
'dns_server' => [],
'from' => 'qcloud',
'free' => $item['grade'] === 'DP_Free',
'record_count' => $item['records'],
'tags' => [],
'id' => $item['id']
];
}
return Paginator::make($list, $limit, $page + 1,$info['domain_total']);
}
/**
* 阿里云域名解析
*
* @param array $data
* @return mixed
*/
public static function aliyunDomainRecordPaginate(array $data)
{
$list = [];
foreach ($data['DomainRecords']['Record'] as &$item) {
$list[] = array_change_key_case($item);
}
return Paginator::make($list, $data['PageSize'], $data['PageNumber'], $data['TotalCount']);
}
/**
* DNS 记录
*
* @param array $data
* @param $page
* @param $limit
* @return Paginator|\think\paginator\driver\Bootstrap
*/
public static function qcloudDomainRecordPaginate(array $data, $page, $limit)
{
$list = [];
foreach ($data['data']['records'] as &$item) {
$item['status'] = $item['status'] === 'enabled' ? 'ENABLE' : 'DISABLE';
$item['rr'] = $item['name'];
$item['recordid'] = $item['id'];
$list[] = array_change_key_case($item);
}
return Paginator::make($list, $limit, $page + 1, $data['data']['info']['record_total']);
}
}

View File

@@ -1,22 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\contract;
interface DomainActionInterface
{
public function getList(array $params);
public function store(array $params);
public function delete(array $params);
public function read($name);
}

View File

@@ -1,27 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\contract;
interface DomainRecordInterface
{
public function getList(array $params);
public function store(array $params);
public function delete($recordId);
public function read(array $params);
public function update($recordId, array $params);
public function enable($recordId, $status);
}

View File

@@ -1,32 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver;
use catcher\facade\Http;
use catchAdmin\domain\support\CommonParams;
trait ApiTrait
{
public function get(array $params)
{
$name = config('catch.domains.default');
$apiDomain = config('catch.domains.' . $name . '.api_domain');
if (strpos($apiDomain, 'https') === false &&
strpos($apiDomain, 'http') === false) {
$apiDomain = 'https://' . $apiDomain . '/v2/index.php';
}
return Http::ignoreSsl()->query(CommonParams::{$name}($params))
->get($apiDomain)->json();
}
}

View File

@@ -1,55 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\aliyun;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catchAdmin\domain\support\driver\ApiTrait;
use catchAdmin\domain\support\Transformer;
class Domain implements DomainActionInterface
{
use ApiTrait;
public function getList(array $params)
{
// TODO: Implement getList() method.
return Transformer::aliyunDomainPaginate($this->get([
'Action' => 'DescribeDomains',
'StarMark' => true,
'SearchModel' => 'LIKE',
'PageNumber' => $params['page'] ?? 1,
'PageSize' => $params['limit'] ?? 20,
]));
}
public function store(array $params)
{
// TODO: Implement add() method.
}
public function delete(array $params)
{
// TODO: Implement delete() method.
return $this->get([
'Action' => 'DeleteDomain',
'DomainName' => $params['name'],
]);
}
public function read($name)
{
// TODO: Implement info() method.
return $this->get([
'Action' => 'DescribeDomainInfo',
'DomainName' => $name
]);
}
}

View File

@@ -1,147 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\aliyun;
use catchAdmin\domain\support\contract\DomainRecordInterface;
use catchAdmin\domain\support\driver\ApiTrait;
use catchAdmin\domain\support\Transformer;
class DomainRecord implements DomainRecordInterface
{
use ApiTrait;
/**
* 列表
*
* @param array $params
* @return mixed
*/
public function getList(array $params)
{
$data = [
'Action' => 'DescribeDomainRecords',
'DomainName' => $params['name'],
'PageNumber' => $params['page'] ?? 1,
'PageSize' => $params['limit'] ?? 20,
];
if ($params['rr']) {
$data['RRKeyWord'] = $params['rr'];
}
if ($params['type']) {
$data['TypeKeyWord'] = $params['type'];
}
if ($params['value']) {
$data['ValueKeyWord'] = $params['value'];
}
if ($params['line']) {
$data['Line'] = $params['line'];
}
if ($params['status']) {
$data['Status'] = $params['status'];
}
// TODO: Implement getList() method.
return Transformer::aliyunDomainRecordPaginate($this->get($data));
}
/**
* 新增解析
*
* @param array $params
* @return array
*
*/
public function store(array $params)
{
// TODO: Implement add() method.
return $this->get([
'Action' => 'AddDomainRecord',
'DomainName' => $params['name'],
'RR' => $params['rr'],
'Type' => $params['type'],
'Value' => $params['value'],
'Line' => $params['line'],
'TTL' => $params['ttl'],
]);
}
/**
* 删除解析
*
* @param $recordId
* @return array
*/
public function delete($recordId)
{
// TODO: Implement delete() method.
return $this->get([
'Action' => 'DeleteDomainRecord',
'RecordId' => $recordId
]);
}
/**
* 获取解析记录
*
* @param array $params
* @return array
*/
public function read(array $params)
{
// TODO: Implement info() method.
return $this->get([
'Action' => 'DescribeDomainRecord',
'RecordId' => $params['record_id'],
]);
}
/**
* 更新解析
*
* @param array $params
* @param $recordId
* @return array
*/
public function update($recordId, array $params)
{
// TODO: Implement update() method.
return $this->get([
'Action' => 'UpdateDomainRecord',
'RecordId' => $recordId,
'RR' => $params['rr'],
'Type' => $params['type'],
'Value' => $params['value'],
'Line' => $params['line'],
'TTL' => $params['ttl'],
]);
}
/**
* 设置状态
*
* @param $recordId
* @param $status
* @return array
*/
public function enable($recordId, $status)
{
return $this->get([
'Action' => 'SetDomainRecordStatus',
'RecordId' => $recordId,
'Status' => ucfirst(strtolower($status))
]);
}
}

View File

@@ -1,56 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\qcloud;
use catchAdmin\domain\support\contract\DomainActionInterface;
use catchAdmin\domain\support\driver\ApiTrait;
use catchAdmin\domain\support\Transformer;
class Domain implements DomainActionInterface
{
use ApiTrait;
public function getList(array $params)
{
$offset = ($params['page'] ?? 1) - 1;
$length = $params['limit'] ?? 10;
// TODO: Implement getList() method.
return Transformer::qcloudDomainPaginate($this->get([
'Action' => 'DomainList',
'offset' => $offset,
'length' => $length
]), $offset, $length);
}
public function store(array $params)
{
// TODO: Implement add() method.
}
public function delete(array $params)
{
// TODO: Implement delete() method.
return $this->get([
'Action' => 'DeleteDomain',
'DomainName' => $params['name'],
]);
}
public function read($name)
{
// TODO: Implement info() method.
return $this->get([
'Action' => 'DescribeDomainInfo',
'DomainName' => $name
]);
}
}

View File

@@ -1,125 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\driver\qcloud;
use catchAdmin\domain\support\contract\DomainRecordInterface;
use catchAdmin\domain\support\driver\ApiTrait;
use catchAdmin\domain\support\Transformer;
class DomainRecord implements DomainRecordInterface
{
use ApiTrait;
/**
* 列表
*
* @param array $params
* @return mixed
*/
public function getList(array $params)
{
$data = [
'Action' => 'RecordList',
'domain' => $params['name'],
'offset' => ($params['page'] ?? 1) - 1,
'length' => $params['limit'] ?? 10,
];
if ($params['rr']) {
$data['subDomain'] = $params['rr'];
}
if ($params['type']) {
$data['recordType'] = $params['type'];
}
// TODO: Implement getList() method.
return Transformer::qcloudDomainRecordPaginate($this->get($data), $data['offset'], $data['length']);
}
/**
* 新增解析
*
* @param array $params
* @return array
*
*/
public function store(array $params)
{
// TODO: Implement add() method.
return $this->get([
'Action' => 'RecordCreate',
'domain' => $params['name'],
'subDomain' => $params['rr'],
'recordType' => $params['type'],
'value' => $params['value'],
'recordLine' => $params['line'],
'ttl' => $params['ttl'],
]);
}
/**
* 删除解析
*
* @param $recordId
* @return array
*/
public function delete($recordId)
{
// TODO: Implement delete() method.
return $this->get([
'Action' => 'RecordDelete',
'recordId' => $recordId
]);
}
/**
* 更新解析
*
* @param array $params
* @param $recordId
* @return array
*/
public function update($recordId, array $params)
{
// TODO: Implement update() method.
return $this->get([
'Action' => 'RecordModify',
'recordId' => $recordId,
'subDomain' => $params['rr'],
'recordType' => $params['type'],
'value' => $params['value'],
'recordLine' => $params['line'],
'ttl' => $params['ttl'],
]);
}
/**
* 设置状态
*
* @param $recordId
* @param $status
* @return array
*/
public function enable($recordId, $status)
{
return $this->get([
'Action' => 'RecordStatus',
'recordId' => $recordId,
'Status' => strtolower($status)
]);
}
public function read(array $params)
{
// TODO: Implement read() method.
}
}

View File

@@ -1,64 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\signature;
class Aliyun
{
/**
* @var array
*/
protected $params;
/**
* Aliyun constructor.
* @param $params
*/
public function __construct(array $params)
{
$this->params = $params;
}
/**
* encode
*
* @time 2020年09月25日
* @param $str
* @return string|string[]|null
*/
protected function percentEncode(string $str)
{
return preg_replace(['/\+/', '/\*/', '/%7E/'], ['%20', '%2A', '~'], urlencode($str));
}
/**
* 签名
*
* @time 2020年09月25日
* @param $method
* @return string
*/
public function signature(string $method)
{
ksort($this->params);
$queryString = '';
foreach ($this->params as $key => $param) {
$queryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($param);
}
$signString = $method . '&' .
$this->percentEncode('/') . '&' .
$this->percentEncode(substr($queryString, 1));
return base64_encode(hash_hmac('sha1', $signString, config('catch.domains.aliyun.access_secret'). '&', true));
}
}

View File

@@ -1,63 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\domain\support\signature;
class Qcloud
{
/**
* @var array
*/
protected $params;
/**
* Qcloud constructor.
* @param $params
*/
public function __construct(array $params)
{
$this->params = $params;
}
/**
* key 替换
*
* @param $key
* @return mixed|string|string[]
*/
protected function replaceKey($key)
{
return strpos($key, '_') === false ?
$key : str_replace('_', '.', $key);
}
/**
* 签名
*
* @time 2020年09月25日
* @param $method
* @return string
*/
public function signature(string $method)
{
ksort($this->params);
$queryString = '';
foreach ($this->params as $key => $param) {
$queryString .= '&' . $this->replaceKey($key) . '=' . $param;
}
$signString = $method . config('catch.domains.qcloud.api_domain') . '/v2/index.php?'
. substr($queryString, 1);
return base64_encode(hash_hmac('sha1', $signString, config('catch.domains.qcloud.access_secret'), true));
}
}

View File

@@ -1,30 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\login;
use catcher\ModuleService;
use think\Service;
class LoginService extends ModuleService
{
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
public function loadEvents()
{
return [
'loginLog' => [ LoginLogEvent::class ],
];
}
}

View File

@@ -1,127 +0,0 @@
<?php
namespace catchAdmin\login\controller;
use catchAdmin\login\request\LoginRequest;
use catchAdmin\permissions\model\Users;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\Code;
use catcher\exceptions\LoginFailedException;
use thans\jwt\facade\JWTAuth;
class Index extends CatchController
{
/**
* 登陆
*
* @time 2019年11月28日
* @param LoginRequest $request
* @param CatchAuth $auth
* @return bool|string
*/
public function login(LoginRequest $request, CatchAuth $auth)
{
$condition = $request->param();
try {
$token = $auth->attempt($condition);
$user = $auth->user();
$this->afterLoginSuccess($user, $token);
// 登录事件
$this->loginEvent($user->username);
return CatchResponse::success([
'token' => $token,
], '登录成功');
} catch (\Exception $exception) {
$this->detailWithLoginFailed($exception, $condition);
$code = $exception->getCode();
return CatchResponse::fail($code == Code::USER_FORBIDDEN ?
'该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED);
}
}
/**
* 处理登录失败
*
* @time 2020年10月26日
* @param $exception
* @param $condition
* @return void
*/
protected function detailWithLoginFailed($exception, $condition)
{
$message = $exception->getMessage();
if (strpos($message, '|') !== false) {
$username = explode('|', $message)[1];
} else {
$username = $condition['email'];
}
$this->loginEvent($username, false);
}
/**
* 用户登录成功后
*
* @time 2020年09月09日
* @param $user
* @param $token
* @return void
*/
protected function afterLoginSuccess($user, $token)
{
$user->last_login_ip = request()->ip();
$user->last_login_time = time();
if ($user->hasField('remember_token')) {
$user->remember_token = $token;
}
$user->save();
}
/**
* 登录事件
*
* @time 2020年09月09日
* @param $name
* @param bool $success
* @return void
*/
protected function loginEvent($name, $success = true)
{
$params['login_name'] = $name;
$params['success'] = $success ? 1 : 2;
event('loginLog', $params);
}
/**
* 登出
*
* @time 2019年11月28日
* @return \think\response\Json
*/
public function logout(): \think\response\Json
{
return CatchResponse::success();
}
/**
* refresh token
*
* @author JaguarJack
* @email njphper@gmail.com
* @time 2020/5/18
* @return \think\response\Json
*/
public function refreshToken()
{
return CatchResponse::success([
'token' => JWTAuth::refresh()
]);
}
}

View File

@@ -1,15 +0,0 @@
{
"name": "登陆",
"alias": "login",
"description": "login 模块",
"version": "1.0.0",
"keywords": [],
"order": 100,
"services": [
"\\catchAdmin\\login\\LoginService"
],
"aliases": {},
"files": [],
"requires": [],
"enable": true
}

View File

@@ -1,9 +0,0 @@
<?php
$router->group(function () use ($router){
# 登入
$router->post('login', '\catchAdmin\login\controller\Index@login');
$router->post('logout', '\catchAdmin\login\controller\Index@logout');
$router->post('refresh/token', '\catchAdmin\login\controller\Index@refreshToken');
});

View File

@@ -1,34 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor;
use catcher\ModuleService;
class MonitorService extends ModuleService
{
protected function loadConfig()
{
return require __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
}
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
public function loadCommands()
{
return [__NAMESPACE__, __DIR__ . DIRECTORY_SEPARATOR . 'command'];
}
}

View File

@@ -1,175 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace catchAdmin\monitor\command;
use catchAdmin\monitor\command\process\Master;
use catchAdmin\monitor\command\process\Process;
use catcher\facade\FileSystem;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\console\Table;
class CatchCrontabCommand extends Command
{
protected $pid;
protected function configure()
{
// 指令配置
$this->setName('catch:crontab')
->addArgument('action', Argument::OPTIONAL, '[start|reload|stop|restart]', 'start')
->addOption('daemon', '-d', Option::VALUE_NONE, 'daemon mode')
->addOption('pid', '-p', Option::VALUE_REQUIRED, 'you can send signal to the process of pid')
->addOption('static', '-s', Option::VALUE_REQUIRED, 'default static process number', 1)
->addOption('dynamic', '-dy', Option::VALUE_REQUIRED, 'default dynamic process number', 10)
->addOption('interval', '-i', Option::VALUE_REQUIRED, 'interval/seconds', 60)
->setDescription('start catch crontab schedule');
}
protected function execute(Input $input, Output $output)
{
if (!$input->hasOption('pid')) {
$this->pid = Master::getMasterPid();
} else {
$this->pid = $input->getOption('pid');
}
if (!extension_loaded('pcntl') || !extension_loaded('posix')) {
$output->error('you should install extension [pcntl && posix]');
} else {
$this->{$input->getArgument('action')}();
}
}
/**
* 进程启动
*
* @time 2020年09月14日
* @return void
*/
protected function start()
{
$worker = new Master();
$worker->staticNumber($this->input->getOption('static'))
->dynamic($this->input->getOption('dynamic'))
->interval($this->input->getOption('interval'))
->asDaemon($this->input->hasOption('daemon'))
->run();
}
/**
* 停止任务
*
* @time 2020年07月07日
* @return void
*/
protected function stop()
{
$retryTimes = 0;
if (Process::isAlive($this->pid)) {
$this->output->info('🔨 catch crontab is running.');
Process::kill($this->pid, SIGTERM);
// 睡眠 1 秒
$this->output->info('⌛️ killing catch crontab service, please waiting...');
sleep(1);
if (!Process::isAlive($this->pid)) {
$this->output->info('🎉 catch crontab stopped!');
} else {
while (true) {
Process::kill($this->pid, SIGKILL);
if (Process::isAlive($this->pid)) {
$retryTimes++;
$this->output->info('🔥 retry $retryTimes times');
usleep(500 * 1000);
if ($retryTimes >= 3) {
$this->output->info('💔 catch crontab is running, stop failed, please use [kill -9 {$this->pid}] to Stop it');
break;
}
} else {
$this->output->info('🎉 catch crontab stopped!');
break;
}
}
}
Master::exitMasterDo();
} else {
$this->output->error('🤔️ catch crontab is not running, Please Check it!');
}
$this->output->info('🎉 stop catch crontab successfully');
}
/**
* 重启任务
*
* @time 2020年07月07日
* @return void
*/
protected function reload()
{
Process::kill($this->pid, SIGUSR1);
}
/**
* 重启
*
* @time 2020年07月07日
* @return void
*/
protected function restart()
{
$this->stop();
$this->output->info('catch crontab restarting...');
usleep(500 * 1000);
$this->start();
}
/**
* status
*
* @time 2020年07月22日
* @return void
*/
protected function status()
{
if ($this->pid) {
if (Process::isAlive($this->pid)) {
Process::kill($this->pid, SIGUSR2);
usleep(100000);
$worker = new Master;
$table = new Table;
$table->setHeader(['PID', '名称', '内存', '处理任务', '开始时间', '运行时间', '状态'], Table::ALIGN_CENTER);
$table->setRows($worker->getWorkerStatus(), Table::ALIGN_CENTER);
$this->output->info($table->render());
} else {
$this->output->error('🤔️ catch crontab is not running, Please Check it!');
}
} else {
$this->output->error('🤔️ catch crontab is not running, Please Check it!');
}
}
}

View File

@@ -1,145 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use catcher\base\CatchCronTask;
trait Attributes
{
/**
* 是否以守护进程方式运行
*
* @var bool
*/
protected $daemon = false;
/**
* 静态进程数量
*
* @var int
*/
protected $static;
/**
* 动态进程数量
*
* @var int
*/
protected $dynamic;
/**
* 定时器触发时间
*
* @var int
*/
protected $interval;
/**
* set name
*
* @var
*/
protected $name = 'catch-crontab';
/**
* @var string
*/
protected $crontabQueueName = 'catch-crontab-task';
/**
* 安全退出
*
* @var bool
*/
protected $exitSafely = false;
/**
* 设置守护进程
*
* @time 2020年07月21日
* @param bool $daemon
* @return $this
*/
public function asDaemon($daemon = false)
{
$this->daemon = $daemon;
return $this;
}
public function staticNumber($n)
{
$this->static = $n;
return $this;
}
/**
* 可扩容
*
* @time 2020年07月21日
* @param $n
* @return $this
*/
public function dynamic($n)
{
$this->dynamic = $n;
return $this;
}
/**
* 定时
*
* @time 2020年07月21日
* @param $n
* @return $this
*/
public function interval($n)
{
$this->interval = $n;
return $this;
}
/**
* 设置 name
*
* @time 2020年07月23日
* @param $name
* @return $this
*/
public function name($name)
{
$this->name = $name;
return $this;
}
/**
* 设置报告错误
*
* @time 2020年07月24日
* @return void
*/
public function displayErrors()
{
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_WARNING);
ini_set('display_startup_errors', 1);
ini_set('ignore_repeated_errors', 1);
}
}

View File

@@ -1,361 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use Exception;
use catcher\facade\Filesystem;
use think\cache\driver\Redis;
use think\facade\Cache;
/**
*
* - 以后台形式工作 daemon
* - 分静态进程和动态扩容进程
* - 信号处理机制
* - 重启工作进程
* - 重启服务
* - 定时器 扩容工作进程
* - 关闭进程
* - 统计信息
* - 是否拉起进程,工作进程销毁后是否继续主进程保留
* - Fatal Error 处理
* - Exception 处理
* - 发生内存泄漏如何处理
* - 错误输出到哪里
* - 提供基础面板查看
* - Log 文件的记录
*
* @time 2020年07月29日
*/
class Master
{
use RegisterSignal, Attributes, Store, ParseTask;
/**
* 保存子进程 PID
*
* @var array
*/
protected $workerIds = [];
/**
* 开始时间
*
* @var
*/
public $start_at;
/**
* @var
*/
public $worker_start_at;
/**
* 保存当前工作进程的数量
*
* @var
*/
protected $allWorkersCount;
/**
* 保存当前重定向输出文件
*
* @var string
*/
protected static $stdout;
/**
* 任务对象
*
* @var
*/
protected $taskService;
/**
* 处理的任务数量
*
* @var int
*/
protected $dealNum = 0;
/**
* busy waiting
*
* @var
*/
protected $status = 'waiting';
/**
* @var
*/
protected $redis;
/**
* 启动
*
* @time 2020年07月21日
* @return void
*/
public function run()
{
try {
if ($this->daemon) {
Process::daemon();
}
if ($this->interval) {
Process::alarm($this->interval);
}
$this->init();
// 初始化进程池
$this->initWorkers();
// 设置进程名称
Process::setWorkerName($this->name . ' master');
// 注册信号
$this->registerSignal();
// 写入进程状态
$this->setWorkerStatus($this->name . ' master');
// 信号发送
while (true) {
Process::dispatch();
$pid = pcntl_waitpid(-1, $status, WNOHANG);
Process::dispatch();
if ($pid > 0) {
if (isset($this->workerIds[$pid])) {
unset($this->workerIds[$pid]);
$this->deleteWorkerStatusFile($pid);
$this->worker_start_at = time();
// 如果进程挂掉,正常退出码都是 0当然这里可以自己定义看 exit($status) 设置什么
// 真实的 exit code pcntl_wexitstatus 函数获取
// exit code > 0 都是由于异常导致的
$exitCode = pcntl_wexitstatus($status);
if (!in_array($exitCode, [255, 250])) {
$this->forkStatic();
}
}
// 如果静态工作进程全部退出,会发生 CPU 空转,所以这里需要 sleep 1
if (!count($this->workerIds)) {
// sleep(1);
self::exitMasterDo();
exit(0);
}
}
usleep(500 * 1000);
}
} catch (\Throwable $exception) {
// todo
echo sprintf('[%s]: ', date('Y-m-d H:i:s')) . $exception->getMessage();
}
}
/**
* 初始化
* @throws Exception
*/
protected function init()
{
$this->displayErrors();
$this->start_at = $this->worker_start_at = time();
// 记录 masterID
FileSystem::put(self::masterPidStorePath(), posix_getpid());
// 保存信息
$this->saveTaskInfo();
// 初始化进程数量
$this->allWorkersCount = $this->static;
// 显示UI
$this->display();
// 重定向
$this->dup();
}
/**
* 初始化进程池
*
* @time 2020年07月21日
* @return void
*/
protected function initWorkers()
{
$this->redis = $this->getRedisHandle();
for ($i = 0; $i < $this->static; $i++) {
$this->forkStatic();
}
}
/**
* fork 进程
*
* @time 2020年07月21日
* @return void
*/
protected function forkDynamic()
{
$process = new Process(function (Process $process) {
$redis = $this->getRedisHandle();
while($crontab = $redis->rpop($this->crontabQueueName)) {
$task = $this->getTaskObject(\json_decode($crontab, true));
$task->run();
}
$process->exit();
});
$process->start();
$this->workerIds[$process->pid] = true;
}
/**
* 静态进程
*
* @time 2020年07月21日
* @return void
*/
protected function forkStatic()
{
$process = new Process(function (Process $process) {
$process->initMemory();
$name = $this->name . ' worker';
$this->setWorkerStatus($name, $this->dealNum, $this->status);
Process::setWorkerName($name);
Process::signal(SIGUSR2, function ($signal) use ($name) {
$this->setWorkerStatus($name, $this->dealNum, $this->status);
});
// 该信号保证进程完成任务后安全退出
Process::signal(SIGTERM, function ($signal) {
$this->exitSafely = true;
});
while (true) {
/************** 任务 *********************/
$this->status = 'busying';
$this->setWorkerStatus($name, $this->dealNum, 'busying');
// 处理定时任务
while ($crontab = $this->redis->rpop($this->crontabQueueName)) {
$task = $this->getTaskObject(\json_decode($crontab, true));
$task->run();
if ($task->shouldExit()) {
$process->exit(250);
}
$this->dealNum += 1;
}
$this->status = 'waiting';
$this->setWorkerStatus($name, $this->dealNum, 'waiting');
/****************处理*********************/
// 暂停一秒 让出CPU调度
sleep(1);
// 信号调度
Process::dispatch();
// 是否需要安全退出 || 查看内存是否溢出
if ($this->exitSafely || $process->isMemoryOverflow()) {
$process->exit();
//exit(0);
}
}
});
$process->start();
$this->workerIds[$process->pid] = true;
}
/**
* 重定向文件流
*
* @time 2020年07月22日
* @return void
* @throws Exception
*/
protected function dup()
{
if (!$this->daemon) {
return;
}
global $stdout, $stderr;
// 关闭标准输入输出
fclose(STDOUT);
fclose(STDIN);
fclose(STDERR);
// 重定向输出&错误
$stdoutPath = self::$stdout ?: self::stdoutPath();
!file_exists($stdoutPath) && touch($stdoutPath);
// 等待 100 毫秒
usleep(100 * 1000);
$stdout = fopen($stdoutPath, 'a');
$stderr = fopen($stdoutPath, 'a');
return;
}
/**
* 输出
*
* @time 2020年07月21日
* @return string
*/
public function output()
{
$isShowCtrlC = $this->daemon ? '' : 'Ctrl+c to stop' . "\r\n";
$info = <<<EOT
---------------------------------------------------------------- 🚀
| _ _ _ _ |
| | | | | | | | | |
| ___ __ _| |_ ___| |__ ___ _ __ ___ _ __ | |_ __ _| |__ |
| / __/ _` | __/ __| '_ \ / __| '__/ _ \| '_ \| __/ _` | '_ \ |
| | (_| (_| | || (__| | | | | (__| | | (_) | | | | || (_| | |_) ||
| \___\__,_|\__\___|_| |_| \___|_| \___/|_| |_|\__\__,_|_.__/ |
| | |
|----------------------------------------------------------------|
$isShowCtrlC
EOT;
return file_put_contents(self::statusPath(), $info);
}
/**
* 显示
*
* @time 2020年07月22日
* @return false|int
*/
protected function display()
{
$this->output();
return fwrite(STDOUT, $this->renderStatus());
}
/**
* 获取 redis 句柄
*
* @time 2020年09月15日
* @return object|null
*/
protected function getRedisHandle()
{
return Cache::store('redis')->handler();
}
}

View File

@@ -1,47 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use catcher\facade\FileSystem;
use think\exception\ClassNotFoundException;
use think\helper\Str;
trait ParseTask
{
/**
* 获取任务
*
* @time 2020年09月15日
* @param $crontab
* @return mixed
*/
protected function getTaskObject($crontab)
{
$class = $this->getTaskNamespace() . ucfirst(Str::camel($crontab['task']));
if (class_exists($class)) {
return app()->make($class)->setCrontab($crontab);
}
throw new ClassNotFoundException('Task '. $crontab['task'] . ' not found');
}
/**
* 获取任务命名空间
*
* @time 2020年09月15日
* @return mixed
*/
protected function getTaskNamespace()
{
return config('catch.crontab.task_namespace');
}
}

View File

@@ -1,213 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use catcher\exceptions\FailedException;
class Process
{
/**
* 保存工作进程 PID
*
* @var
*/
public $pid;
/**
* 用户自定义方法
*
* @var callable
*/
protected $callable;
/**
* 申请最大内存 给出缓冲期
*
* @var string
*/
protected $initMemory = '256M';
/**
* 超过最大内存报警
*
* @var float|int
*/
protected $allowMaxMemory = 128 * 1024 * 1024;
public function __construct(callable $callable)
{
$this->callable = $callable;
}
/**
* 守护进程
*
* @time 2020年07月21日
* @return void
* @throws FailedException
*/
public static function daemon()
{
$pid = pcntl_fork();
if ($pid < 0) {
throw new FailedException('fork process failed');
}
// 退出父进程
if ($pid > 0) {
exit(0);
}
// 设置新的会话组
if (posix_setsid() < 0) {
exit(0);
}
chdir('/');
// 重置掩码 权限问题
umask(0);
}
/**
* 启动进程
*
* @time 2020年07月21日
* @return void
*/
public function start()
{
$pid = pcntl_fork();
if ($this->pid < 0) {
exit('fork failed');
}
if ($pid > 0) {
$this->pid = $pid;
} else {
call_user_func_array($this->callable, [$this]);
}
}
/**
* 信号
*
* @time 2020年07月21日
* @param $signal
* @param $callable
* @param $restartSysCalls
* @return void
*/
public static function signal($signal, $callable, $restartSysCalls = false)
{
pcntl_signal($signal, $callable, $restartSysCalls);
}
/**
* default 1 second
*
* @param $interval
* @return mixed
*/
public static function alarm($interval = 1)
{
return pcntl_alarm($interval);
}
/**
* linux 进程下设置进程名称
*
* @time 2020年07月21日
* @param $title
* @return void
*/
public static function setWorkerName($title)
{
if (strtolower(PHP_OS) === 'linux') {
cli_set_process_title($title);
}
}
/**
* 安全退出
*
* @time 2020年07月21日
* @param int $status
* @return void
*/
public function exit($status = 0)
{
exit($status);
}
/**
* kill
*
* @time 2020年07月22日
* @param $pid
* @param $signal
* @return bool
*/
public static function kill($pid, $signal)
{
return posix_kill($pid, $signal);
}
/**
*
* @time 2020年07月22日
* @return void
*/
public static function dispatch()
{
pcntl_signal_dispatch();
}
/**
* 是否存活
*
* @time 2020年07月22日
* @param $pid
* @return bool
*/
public static function isAlive($pid)
{
return posix_kill($pid, 0);
}
/**
* 初始化进程内存
*
* @time 2020年07月22日
* @param int $memory
* @return void
*/
public function initMemory($memory = 0)
{
if (ini_get('memory_limit') != $this->initMemory) {
// 这里申请一块稍微大的内存
ini_set('memory_limit', $memory ?: $this->initMemory);
}
}
/**
* 是否超过最大内存
*
* @time 2020年07月22日
* @return mixed
*/
public function isMemoryOverflow()
{
// 一旦超过了允许的内存 直接退出进程
return memory_get_usage() > $this->allowMaxMemory;
}
}

View File

@@ -1,158 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use catchAdmin\monitor\model\Crontab;
use catcher\facade\FileSystem;
use Cron\CronExpression;
use EasyWeChat\Kernel\Messages\News;
use think\cache\driver\Redis;
trait RegisterSignal
{
public function registerSignal()
{
// 退出信号
$this->exit();
// 等待子进程退出
$this->waitWorkersExit();
// 动态扩容
$this->workerChecked();
// 重启进程
$this->reload();
// 统计信息
$this->showStatus();
}
/**
* 进程退出
*
* @time 2020年07月21日
* @return void
*/
protected function exit()
{
Process::signal(SIGTERM, function ($signal) {
foreach ($this->workerIds as $pid => $v) {
if (isset($this->workerIds[$pid])) {
unset($this->workerIds[$pid]);
}
Process::kill($pid, SIGTERM);
}
Process::kill(self::getMasterPid(), SIGKILL);
});
Process::signal(SIGINT, function ($signal) {
foreach ($this->workerIds as $pid => $v) {
if (isset($this->workerIds[$pid])) {
unset($this->workerIds[$pid]);
}
Process::kill($pid, SIGKILL);
}
Process::kill(self::getMasterPid(), SIGKILL);
});
}
/**
* 子进程退出
*
* @time 2020年07月21日
* @return void
*/
protected function waitWorkersExit()
{
Process::signal(SIGCHLD, function ($signal) {
});
}
/**
* 进程检测
*
* @time 2020年07月21日
* @return void
*/
protected function workerChecked()
{
Process::signal(SIGALRM, function ($signal) {
$process = new Process(function (Process $process) {
$crontabs = Crontab::where('status', Crontab::ENABLE)
->where('tactics', '<>', Crontab::EXECUTE_FORBIDDEN)
->select()->toArray();
// 任务
foreach ($crontabs as $crontab) {
$can = date('Y-m-d H:i', CronExpression::factory(trim($crontab['cron']))
->getNextRunDate(date('Y-m-d H:i:s'), 0 , true)
->getTimestamp()) == date('Y-m-d H:i', time());
if ($can) {
// 如果任务只执行一次 之后禁用该任务
if ($crontab['tactics'] === Crontab::EXECUTE_ONCE) {
Crontab::where('id', $crontab['id'])->update([
'status' => Crontab::DISABLE,
]);
}
$redis = $this->getRedisHandle();
$redis->lpush($this->crontabQueueName, json_encode([
'id' => $crontab['id'],
'task' => $crontab['task'],
]));
}
}
$process->exit();
});
$process->start();
Process::alarm($this->interval);
});
}
/**
* 重启
*
* @time 2020年07月21日
* @return void
*/
protected function reload()
{
Process::signal(SIGUSR1, function ($signal) {
$this->worker_start_at = 0;
foreach ($this->workerIds as $pid => $v) {
Process::kill($pid, SIGTERM);
}
}, false);
}
/**
* 预留信号
*
* @time 2020年07月21日
* @return void
*/
protected function showStatus()
{
Process::signal(SIGUSR2, function ($signal) {
$this->setWorkerStatus($this->name . ' master');
foreach ($this->workerIds as $pid => $v) {
Process::kill($pid, SIGUSR2);
}
});
}
}

View File

@@ -1,243 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\command\process;
use catcher\facade\FileSystem;
trait Store
{
/**
* worker 根目录
*
* @time 2020年07月23日
* @return string
*/
public static function storeTaskPath()
{
$path = config('catch.crontab.store_path');
if (!Filesystem::exists($path)) {
FileSystem::makeDirectory($path, 0777, true);
}
return $path;
}
/**
* 保存信息备用
*
* @time 2020年07月29日
* @return void
*/
protected function saveTaskInfo()
{
FileSystem::put(self::storeTaskPath() . 'information.json', \json_encode([
'name' => $this->name,
'static' => $this->static,
'dynamic' => $this->dynamic,
'interval' => $this->interval,
], JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
}
/**
* worker master pid
*
* @time 2020年07月23日
* @return string
*/
public static function masterPidStorePath()
{
return self::storeTaskPath() . 'master.pid';
}
/**
* worker master status
*
* @time 2020年07月23日
* @return string
*/
public static function statusPath()
{
return self::storeTaskPath() . 'master.status';
}
/**
* worker status
*
* @time 2020年07月23日
* @param string $name
* @return string
*/
public static function workerStatusPath($name)
{
$path = self::storeTaskPath() . 'status/';
if (!FileSystem::exists($path)) {
FileSystem::makeDirectory($path, 0777, true);
}
return $path . $name . '.status';
}
/**
*
* @time 2020年07月23日
* @return string
*/
public static function getWorkerStatusPath()
{
return self::storeTaskPath() . 'status/';
}
/**
* worker log
*
* @time 2020年07月23日
* @return string
*/
public static function stdoutPath()
{
return self::storeTaskPath() . 'errors.log';
}
/**
* 获取 master pid
*
* @time 2020年07月21日
* @return false|string
*/
public static function getMasterPid()
{
$pidFile = config('catch.crontab.master_pid_file');
if (!file_exists($pidFile)) {
return 0;
}
return FileSystem::sharedGet($pidFile);
}
/**
* status
*
* @time 2020年07月21日
* @return false|string
*/
public function renderStatus()
{
return file_get_contents(self::statusPath());
}
/**
* 运行时间
*
* @time 2020年07月23日
* @param $runtime
* @return string
*/
protected function getRunningTime($runtime)
{
$day = 3600 * 24;
if ($runtime > $day) {
$days = floor($runtime / $day);
return $days . '天:' . gmstrftime('%H:%M:%S', $runtime % $day);
} else {
return gmstrftime('%H:%M:%S', $runtime);
}
}
/**
* 获取工作进程
*
* @time 2020年07月23日
* @return mixed
*/
public function getWorkerStatus()
{
usleep(500 * 1000);
$files = FileSystem::glob(self::storeTaskPath() . 'status/*.status');
$workerStatus = [];
foreach ($files as $file) {
$workerStatus[] = explode("\t", FileSystem::sharedGet($file));
}
return $workerStatus;
}
/**
* 设置进程状态
*
* @time 2020年07月23日
* @param $name
* @param int $dealNum
* @param string $status
* @return void
*/
protected function setWorkerStatus($name, $dealNum = 0, $status = 'running')
{
$startAt = strpos($name, 'worker') ? $this->worker_start_at : $this->start_at;
if ($this->daemon) {
FileSystem::put($this->workerStatusPath($this->workerStatusFileName($name)), implode("\t", [
posix_getpid(),
$name,
floor(memory_get_usage() / 1024 / 1024) . 'M',
$dealNum,
date('Y-m-d H:i:s', $startAt),
$this->getRunningTime(time() - $startAt),
$status
]));
}
}
/**
* 进程名称
*
* @time 2020年09月15日
* @param $name
* @return string
*/
protected function workerStatusFileName($name)
{
return $name . '_' . posix_getpid();
}
/**
* 删除进程状态文件
*
* @time 2020年09月15日
* @param $pid
* @return void
*/
protected function deleteWorkerStatusFile($pid)
{
@unlink(self::workerStatusPath($this->name . ' worker_' . $pid));
}
/**
* 退出
*
* @time 2020年09月15日
* @return void
*/
public static function exitMasterDo()
{
@unlink(self::masterPidStorePath());
@unlink(self::statusPath());
Filesystem::deleteDirectory(self::getWorkerStatusPath());
}
}

View File

@@ -1,59 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
return [
/**
*
* 定时任务配置
*
*/
'crontab' => [
/**
* 存储目录
*/
'store_path' => runtime_path('catch/crontab'),
/**
* 主进程 pid 存储
*/
'master_pid_file' => runtime_path('catch/crontab') . 'master.pid',
/**
* 日志配置
*/
'log' => [
// 日志记录方式
'type' => 'File',
// 日志保存目录
'path' => runtime_path('catch/schedule'),
// 单文件日志写入
'single' => false,
// 独立日志级别
'apart_level' => [],
// 最大日志文件数量
'max_files' => 0,
// 使用JSON格式记录
'json' => false,
// 日志处理
'processor' => null,
// 关闭通道日志写入
'close' => false,
// 日志输出格式化
'format' => '[%s][%s] %s',
// 是否实时写入
'realtime_write' => false,
],
/**
* crontab 任务命名空间
*/
'task_namespace' => '\\app\\task\\',
],
];

View File

@@ -1,105 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\controller;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\monitor\model\Crontab as CrontabModel;
use Cron\CronExpression;
class Crontab extends CatchController
{
protected $model;
public function __construct(CrontabModel $model)
{
$this->model = $model;
}
/**
* 列表
*
* @time 2020/09/14 20:35
*
* @return \think\Response
*/
public function index()
{
return CatchResponse::paginate($this->model->getList());
}
/**
* 保存
*
* @time 2020/09/14 20:35
* @param Request Request
* @return \think\Response
*/
public function save(Request $request)
{
CronExpression::factory($request->post('cron'));
return CatchResponse::success($this->model->storeBy($request->post()));
}
/**
* 读取
*
* @time 2020/09/14 20:35
* @param $id
* @return \think\Response
*/
public function read($id)
{
return CatchResponse::success($this->model->findBy($id));
}
/**
* 更新
*
* @time 2020/09/14 20:35
* @param Request $request
* @return \think\Response
*/
public function update(Request $request, $id)
{
CronExpression::factory($request->post('cron'));
return CatchResponse::success($this->model->updateBy($id, $request->post()));
}
/**
* 删除
*
* @time 2020/09/14 20:35
* @param $id
* @return \think\Response
*/
public function delete($id)
{
return CatchResponse::success($this->model->deleteBy($id));
}
/**
* 禁用启用
*
* @time 2020年09月15日
* @param $id
* @return \think\response\Json
*/
public function disOrEnable($id)
{
return CatchResponse::success($this->model->disOrEnable($id));
}
}

View File

@@ -1,50 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\controller;
use catchAdmin\monitor\model\CrontabLog as LogModel;
use catcher\base\CatchController;
use catcher\CatchResponse;
use think\Request;
class CrontabLog extends CatchController
{
protected $model;
public function __construct(LogModel $model)
{
$this->model = $model;
}
/**
* 日志列表
*
* @time 2020年09月15日
* @throws \think\db\exception\DbException
* @return \think\response\Json
*/
public function index()
{
return CatchResponse::paginate($this->model->getList());
}
/**
* 删除日志
*
* @time 2020年09月15日
* @param $id
* @return \think\response\Json
*/
public function delete($id)
{
return CatchResponse::success($this->model->deleteBy($id));
}
}

View File

@@ -1,46 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class Crontab extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('crontab', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '定时任务' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('name', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',])
->addColumn('group', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 默认 2 系统',])
->addColumn('task', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',])
->addColumn('cron', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => 'cron 表达式',])
->addColumn('tactics', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 立即执行 2 执行一次 3 放弃执行',])
->addColumn('status', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 正常 2 禁用',])
->addColumn('remark', 'string', ['limit' => 1000,'null' => false,'default' => '','signed' => false,'comment' => '备注',])
->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->create();
}
}

View File

@@ -1,51 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class CrontabLog extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('crontab_log', ['engine' => 'Myisam', 'collation' => 'utf8mb4_general_ci', 'comment' => '定时任务日志' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('crontab_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => 'crontab 任务ID',])
->addColumn('used_time', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '任务消耗时间',])
->addColumn('status', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 成功 2 失败',])
->addColumn('error_message', 'string', ['limit' => 1000,'null' => false,'default' => '','signed' => false,'comment' => '错误信息',])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->create();
}
}

View File

@@ -1,253 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Seeder;
class MonitorMenusSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
\catcher\Utils::importTreeData($this->getPermissions(), 'permissions', 'parent_id');
}
protected function getPermissions()
{
return array (
0 =>
array (
'id' => 103,
'permission_name' => '系统监控',
'parent_id' => 0,
'level' => '',
'route' => '/monitor',
'icon' => 'el-icon-view',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'monitor',
'component' => 'layout',
'redirect' => '/monitor/crontab',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600126383,
'updated_at' => 1600136975,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 104,
'permission_name' => '定时任务',
'parent_id' => 103,
'level' => '103',
'route' => '/monitor/crontab',
'icon' => 'el-icon-time',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab',
'component' => 'crontab',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600126931,
'updated_at' => 1600136975,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 105,
'permission_name' => '列表',
'parent_id' => 104,
'level' => '103-104',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab@index',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600127069,
'updated_at' => 1600136975,
'deleted_at' => 0,
),
1 =>
array (
'id' => 106,
'permission_name' => '保存',
'parent_id' => 104,
'level' => '103-104',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab@save',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600127078,
'updated_at' => 1600136975,
'deleted_at' => 0,
),
2 =>
array (
'id' => 107,
'permission_name' => '更新',
'parent_id' => 104,
'level' => '103-104',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab@update',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600127085,
'updated_at' => 1600136975,
'deleted_at' => 0,
),
3 =>
array (
'id' => 108,
'permission_name' => '删除',
'parent_id' => 104,
'level' => '103-104',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab@delete',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600127091,
'updated_at' => 1600136975,
'deleted_at' => 0,
),
4 =>
array (
'id' => 109,
'permission_name' => '禁用/启用',
'parent_id' => 104,
'level' => '103-104',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'crontab@disOrEnable',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600129279,
'updated_at' => 1600136975,
'deleted_at' => 0,
),
),
),
1 =>
array (
'id' => 110,
'permission_name' => '任务日志',
'parent_id' => 103,
'level' => '103-104',
'route' => '/monitor/crontab/log/:crontab_id?',
'icon' => 'el-icon-guide',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'CrontabLog',
'component' => 'crontabLog',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 2,
'sort' => 1,
'created_at' => 1600167562,
'updated_at' => 1600188651,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 111,
'permission_name' => '列表',
'parent_id' => 110,
'level' => '103-104-110',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'CrontabLog@index',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600167574,
'updated_at' => 1600168082,
'deleted_at' => 0,
),
1 =>
array (
'id' => 112,
'permission_name' => '删除',
'parent_id' => 110,
'level' => '103-104-110',
'route' => '',
'icon' => '',
'module' => 'monitor',
'creator_id' => 1,
'permission_mark' => 'CrontabLog@delete',
'component' => '',
'redirect' => '',
'keepalive' => 1,
'type' => 2,
'hidden' => 1,
'sort' => 1,
'created_at' => 1600167581,
'updated_at' => 1600168082,
'deleted_at' => 0,
),
),
),
),
),
);
}
}

View File

@@ -1,43 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\model;
use catchAdmin\monitor\model\search\CrontabSearch;
use catcher\base\CatchModel as Model;
class Crontab extends Model
{
use CrontabSearch;
protected $name = 'crontab';
protected $field = [
'id', //
'name', // 任务名称
'group', // 1 默认 2 系统
'task', // 任务名称
'cron', // cron 表达式
'tactics', // 1 立即执行 2 执行一次 3 放弃执行
'status', // 1 正常 2 禁用
'remark', // 备注
'creator_id', // 创建人ID
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
const EXECUTE_IMMEDIATELY = 1; // 立即执行
const EXECUTE_ONCE = 2; // 执行一次
const EXECUTE_FORBIDDEN = 3; // 停止执行
}

View File

@@ -1,53 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\model;
use catchAdmin\monitor\model\search\CrontabLogSearch;
use catcher\base\CatchModel;
class CrontabLog extends CatchModel
{
use CrontabLogSearch;
protected $name = 'crontab_log';
protected $field = [
'id', //
'crontab_id', // crontab 任务ID
'used_time', // 任务消耗时间
'status', // 1 成功 2 失败
'error_message', // 错误信息
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
const SUCCESS = 1;
const FAILED = 2;
/**
* 获取日志列表
*
* @time 2020年09月15日
* @throws \think\db\exception\DbException
* @return mixed|\think\Paginator
*/
public function getList()
{
return $this->catchLeftJoin(Crontab::class, 'id', 'crontab_id', ['name', 'group', 'task'])
->catchSearch()
->catchOrder()
->field(['used_time', 'error_message', 'crontab_log.status', 'crontab_log.id', 'crontab_log.created_at'])
->paginate();
}
}

View File

@@ -1,39 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\model\search;
trait CrontabLogSearch
{
public function searchCrontabIdAttr($query, $value, $data)
{
return $query->where('crontab_id', $value);
}
public function searchNameAttr($query, $value, $data)
{
return $query->whereLike('crontab.name', $value);
}
public function searchStatusAttr($query, $value, $data)
{
return $query->where('crontab_log.status', $value);
}
public function searchStartAtAttr($query, $value, $data)
{
return $query->where($this->aliasField('created_at'), '>=', strtotime($value));
}
public function searchEndAtAttr($query, $value, $data)
{
return $query->where($this->aliasField('created_at'), '<=', strtotime($value));
}
}

View File

@@ -1,24 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\monitor\model\search;
trait CrontabSearch
{
public function searchNameAttr($query, $value, $data)
{
return $query->whereLike('name', $value);
}
public function searchStatusAttr($query, $value, $data)
{
return $query->whereLike('status', $value);
}
}

View File

@@ -1,15 +0,0 @@
{
"name": "系统监控",
"alias": "monitor",
"description": "系统监控模块",
"version": "1.0.0",
"keywords": [],
"order": 0,
"services": [
"\\catchAdmin\\monitor\\MonitorService"
],
"aliases": [],
"files": [],
"requires": [],
"enable": true
}

View File

@@ -1,22 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
// you should use `$router`
$router->group('monitor', function () use ($router){
// crontab路由
$router->resource('crontab', '\catchAdmin\monitor\controller\Crontab');
$router->put('crontab/enable/<id>', '\catchAdmin\monitor\controller\Crontab@disOrEnable');
// crontab 日志
$router->get('crontab/log', '\catchAdmin\monitor\controller\CrontabLog@index');
$router->delete('crontab/log/<id>', '\catchAdmin\monitor\controller\CrontabLog@delete');
})->middleware('auth');

View File

@@ -1,54 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\permissions;
use catchAdmin\permissions\event\OperateLogEvent;
use catchAdmin\permissions\middleware\AuthTokenMiddleware;
use catchAdmin\permissions\middleware\PermissionsMiddleware;
use catchAdmin\permissions\middleware\RecordOperateMiddleware;
use catcher\ModuleService;
class PermissionService extends ModuleService
{
public function register()
{
parent::register(); // TODO: Change the autogenerated stub
$this->registerMiddleWares();
}
public function loadEvents()
{
return [
'operateLog' => [ OperateLogEvent::class ],
];
}
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
protected function registerMiddleWares()
{
$middleware = $this->app->config->get('middleware');
$middleware['alias']['auth'] = [
AuthTokenMiddleware::class,
PermissionsMiddleware::class,
RecordOperateMiddleware::class
];
$this->app->config->set($middleware, 'middleware');
}
}

View File

@@ -1,73 +0,0 @@
<?php
namespace catchAdmin\permissions\controller;
use catcher\base\CatchController;
use catchAdmin\permissions\model\Department as DepartmentModel;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\Tree;
class Department extends CatchController
{
protected $department;
public function __construct(DepartmentModel $department)
{
$this->department = $department;
}
/**
* 列表
*
* @time 2020年01月09日
* @param CatchRequest $request
* @return \think\response\Json
* @throws \think\db\exception\DbException
*/
public function index(): \think\response\Json
{
return CatchResponse::success($this->department->getList());
}
/**
* 保存
*
* @time 2020年01月09日
* @param CatchRequest $request
* @return \think\response\Json
*/
public function save(CatchRequest $request): \think\response\Json
{
return CatchResponse::success($this->department->storeBy($request->param()));
}
/**
* 更新
*
* @time 2020年01月09日
* @param $id
* @param CatchRequest $request
* @return \think\response\Json
*/
public function update($id, CatchRequest $request): \think\response\Json
{
return CatchResponse::success($this->department->updateBy($id, $request->param()));
}
/**
* 删除
*
* @time 2020年01月09日
* @param $id
* @return \think\response\Json
*/
public function delete($id): \think\response\Json
{
if ($this->department->where('parent_id', $id)->find()) {
throw new FailedException('存在子部门,无法删除');
}
return CatchResponse::success($this->department->deleteBy($id));
}
}

View File

@@ -1,80 +0,0 @@
<?php
namespace catchAdmin\permissions\controller;
use catchAdmin\permissions\model\Job as JobModel;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
class Job extends CatchController
{
protected $job;
public function __construct(JobModel $job)
{
$this->job = $job;
}
/**
* 列表
*
* @time 2020年01月09日
* @param CatchRequest $request
* @return \think\response\Json
* @throws \think\db\exception\DbException
*/
public function index(): \think\response\Json
{
return CatchResponse::paginate($this->job->getList());
}
/**
* 保存
*
* @time 2020年01月09日
* @param CatchRequest $request
* @return \think\response\Json
*/
public function save(CatchRequest $request): \think\response\Json
{
return CatchResponse::success($this->job->storeBy($request->post()));
}
/**
* 更新
*
* @time 2020年01月09日
* @param $id
* @param CatchRequest $request
* @return \think\response\Json
*/
public function update($id, CatchRequest $request): \think\response\Json
{
return CatchResponse::success($this->job->updateBy($id, $request->post()));
}
/**
* 删除
*
* @time 2020年01月09日
* @param $id
* @return \think\response\Json
*/
public function delete($id): \think\response\Json
{
return CatchResponse::success($this->job->deleteBy($id));
}
/**
* 获取所有
*
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAll()
{
return CatchResponse::success($this->job->field(['id', 'job_name'])->select());
}
}

View File

@@ -1,208 +0,0 @@
<?php
namespace catchAdmin\permissions\controller;
use catcher\base\CatchRequest as Request;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\library\ParseClass;
use catcher\Tree;
use catchAdmin\permissions\model\Permissions;
use think\helper\Str;
use think\response\Json;
class Permission extends CatchController
{
protected $permissions;
public function __construct(Permissions $permissions)
{
$this->permissions = $permissions;
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function index(Request $request): Json
{
// 获取菜单类型
$menuList = $this->permissions->getList(true);
// 获取按钮类型并且重新排列
$buttonList = [];
$this->permissions
->whereIn('parent_id', array_unique($menuList->column('id')))
->where('type', Permissions::BTN_TYPE)
->select()->each(function ($item) use (&$buttonList){
$buttonList[$item['parent_id']][] = $item->toArray();
});
// 子节点的 key
$children = $request->param('actionList') ?? 'children';
// 返回树结构
return CatchResponse::success($menuList->each(function (&$item) use ($buttonList, $children){
$item[$children] = $buttonList[$item['id']] ?? [];
})->toTree());
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function save(Request $request): Json
{
$params = $request->param();
// 如果是子分类 自动写入父类模块
$parentId = $params['parent_id'] ?? 0;
// 按钮类型寻找上级
if ($params['type'] == Permissions::BTN_TYPE && $parentId) {
$permissionMark = $params['permission_mark'];
// 查找父级
$parentPermission = $this->permissions->findBy($parentId);
// 如果父级是顶级 parent_id = 0
if ($parentPermission->parent_id) {
if (Str::contains($parentPermission->permission_mark, '@')) {
list($controller, $action) = explode('@', $parentPermission->permission_mark);
$permissionMark = $controller . '@' . $permissionMark;
} else {
$permissionMark = $parentPermission->permission_mark .'@'. $permissionMark;
}
}
$params['permission_mark'] = $permissionMark;
$params['module'] = $parentPermission->module;
}
return CatchResponse::success($this->permissions->storeBy($params));
}
/**
*
* @time 2019年12月11日
* @param $id
* @param Request $request
* @return Json
*/
public function update($id, Request $request): Json
{
$permission = $this->permissions->findBy($id);
$params = $request->param();
// 按钮类型
if ($params['type'] == Permissions::BTN_TYPE && $permission->parent_id) {
$parentPermission = $this->permissions->findBy($permission->parent_id);
$permissionMark = $params['permission_mark'];
if ($parentPermission->parent_id) {
if (Str::contains($parentPermission->permission_mark, '@')) {
list($controller, $action) = explode('@', $parentPermission->permission_mark);
$permissionMark = $controller . '@' . $permissionMark;
} else {
$permissionMark = $parentPermission->permission_mark .'@'. $permissionMark;
}
}
$params['permission_mark'] = $permissionMark;
$this->permissions->where('id',$id)->update(array_merge($params, [
'parent_id' => $permission->parent_id,
'level' => $permission->level,
'updated_at' => time()
]));
return CatchResponse::success();
}
$params = array_merge($request->param(), [
'parent_id' => $permission->parent_id,
'level' => $permission->level
]);
if ($permission->updateBy($id, $params)) {
if ($params['module'] ?? false) {
$this->permissions->updateBy($permission->id, [
'module' => $params['module'],
], 'parent_id');
}
return CatchResponse::success();
}
throw new FailedException('更新失败');
}
/**
*
* @time 2019年12月11日
* @param $id
* @throws FailedException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Json
*/
public function delete($id): Json
{
if ($this->permissions->where('parent_id', $id)->find()) {
throw new FailedException('存在子菜单,无法删除');
}
$this->permissions->findBy($id)->roles()->detach();
return CatchResponse::success($this->permissions->deleteBy($id));
}
/**
* 显示/隐藏
*
* @author JaguarJack
* @email njphper@gmail.com
* @time 2020/5/19
* @param $id
* @return Json
*/
public function show($id)
{
$this->permissions->show($id);
return CatchResponse::success();
}
/**
*
* @time 2020年06月05日
* @param $id
* @param ParseClass $parseClass
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Json
*/
public function getMethods($id, ParseClass $parseClass)
{
$permission = Permissions::where('id', $id)->find();
$module = $permission->module;
$controller = explode('@', $permission->permission_mark)[0];
try {
$methods = $parseClass->setModule('catch')->setRule($module, $controller)->onlySelfMethods();
return CatchResponse::success($methods);
}catch (\Exception $e) {
return CatchResponse::success([]);
}
}
}

View File

@@ -1,244 +0,0 @@
<?php
namespace catchAdmin\permissions\controller;
use catchAdmin\permissions\excel\UserExport;
use catcher\base\CatchRequest as Request;
use catchAdmin\permissions\model\Permissions;
use catchAdmin\permissions\model\Roles;
use catchAdmin\permissions\model\Users;
use catchAdmin\permissions\request\CreateRequest;
use catchAdmin\permissions\request\UpdateRequest;
use catchAdmin\permissions\request\ProfileRequest;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchCacheKeys;
use catcher\CatchResponse;
use catcher\library\excel\Excel;
use catcher\Tree;
use catcher\Utils;
use think\facade\Cache;
class User extends CatchController
{
protected $user;
public function __construct(Users $user)
{
$this->user = $user;
}
/**
*
* @time 2020年04月24日
* @throws \think\db\exception\DbException
* @return \think\response\Json
*/
public function index()
{
return CatchResponse::paginate($this->user->getList());
}
/**
* 获取用户信息
*
* @time 2020年01月07日
* @param CatchAuth $auth
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\response\Json
*/
public function info(CatchAuth $auth)
{
$user = $auth->user();
$roles = $user->getRoles()->column('identify');
$permissionIds = $user->getPermissionsBy($user->id);
// 缓存用户权限
Cache::set(CatchCacheKeys::USER_PERMISSIONS . $user->id, $permissionIds);
$user->permissions = Permissions::getCurrentUserPermissions($permissionIds);
$user->roles = $roles;
// 用户数据权限
// $user->data_range = Roles::getDepartmentUserIdsBy($roles);
return CatchResponse::success($user);
}
/**
*
* @param CreateRequest $request
* @time 2019年12月06日
* @return \think\response\Json
*/
public function save(CreateRequest $request)
{
$this->user->storeBy($request->param());
$this->user->attachRoles($request->param('roles'));
if ($request->param('jobs')) {
$this->user->attachJobs($request->param('jobs'));
}
return CatchResponse::success('', '添加成功');
}
/**
*
* @time 2019年12月04日
* @param $id
* @return \think\response\Json
*/
public function read($id)
{
$user = $this->user->findBy($id);
$user->roles = $user->getRoles();
$user->jobs = $user->getJobs();
return CatchResponse::success($user);
}
/**
*
* @time 2019年12月04日
* @param $id
* @param UpdateRequest $request
* @return \think\response\Json
*/
public function update($id, UpdateRequest $request)
{
$this->user->updateBy($id, $request->param());
$user = $this->user->findBy($id);
$user->detachRoles();
$user->detachJobs();
if (!empty($request->param('roles'))) {
$user->attachRoles($request->param('roles'));
}
if (!empty($request->param('jobs'))) {
$user->attachJobs($request->param('jobs'));
}
return CatchResponse::success();
}
/**
*
* @time 2019年12月04日
* @param $id
* @return \think\response\Json
*/
public function delete($id)
{
$ids = Utils::stringToArrayBy($id);
foreach ($ids as $_id) {
$user = $this->user->findBy($_id);
// 删除角色
$user->detachRoles();
// 删除岗位
$user->detachJobs();
$this->user->deleteBy($_id);
}
return CatchResponse::success();
}
/**
*
* @time 2019年12月07日
* @param $id
* @return \think\response\Json
*/
public function switchStatus($id): \think\response\Json
{
$ids = Utils::stringToArrayBy($id);
foreach ($ids as $_id) {
$user = $this->user->findBy($_id);
$this->user->updateBy($_id, [
'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
]);
}
return CatchResponse::success([], '操作成功');
}
/**
*
* @time 2019年12月07日
* @param $id
* @return \think\response\Json
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function recover($id): \think\response\Json
{
$trashedUser = $this->user->findBy($id, ['*'], true);
if ($this->user->where('email', $trashedUser->email)->find()) {
return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
}
return CatchResponse::success($this->user->recover($id));
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @param Roles $roles
* @return \think\response\Json
*/
public function getRoles(Request $request, Roles $roles): \think\response\Json
{
$roles = Tree::done($roles->getList());
$roleIds = [];
if ($request->param('uid')) {
$userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
foreach ($userHasRoles as $role) {
$roleIds[] = $role->pivot->role_id;
}
}
return CatchResponse::success([
'roles' => $roles,
'hasRoles' => $roleIds,
]);
}
/**
* 导出
*
* @time 2020年09月08日
* @param Excel $excel
* @param UserExport $userExport
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @return \think\response\Json
*/
public function export(Excel $excel, UserExport $userExport)
{
return CatchResponse::success($excel->save($userExport, Utils::publicPath('export/users')));
}
/**
* 更新个人信息
*
* @time 2020年09月20日
* @param ProfileRequest $request
* @return \think\response\Json
*/
public function profile(ProfileRequest $request)
{
return CatchResponse::success($this->user->updateBy($request->user()->id, $request->param()));
}
}

View File

@@ -1,45 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class Department extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('departments',['engine'=>'Innodb', 'comment' => '部门表', 'signed' => false]);
$table->addColumn('department_name', 'string',['limit' => 15,'default'=>'','comment'=>'部门名称'])
->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false])
->addColumn('principal', 'string', ['default' => '', 'comment' => '负责人', 'limit' => 20])
->addColumn('mobile', 'string', ['default' => '', 'comment' => '联系电话', 'limit' => 20])
->addColumn('email', 'string', ['default' => '', 'comment' => '联系又想', 'limit' => 100])
->addColumn('creator_id', 'integer',['default' => 0, 'comment'=>'创建人ID'])
->addColumn('status', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 1,'comment'=>'1 正常 2 停用'])
->addColumn('sort', 'integer',['default'=> 0,'comment'=>'排序字段'])
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态null 未删除 timestamp 已删除', 'signed' => false))
->create();
}
}

View File

@@ -1,43 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class Job extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('jobs',['engine'=>'Innodb', 'comment' => '岗位表', 'signed' => false]);
$table->addColumn('job_name', 'string',['limit' => 15,'default'=>'','comment'=>'岗位名称'])
->addColumn('coding', 'string', ['default' => '', 'comment' => '编码', 'limit' => 50])
->addColumn('creator_id', 'integer',['default' => 0, 'comment'=>'创建人ID'])
->addColumn('status', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 1,'comment'=>'1 正常 2 停用'])
->addColumn('sort', 'integer',['default'=> 0,'comment'=>'排序字段'])
->addColumn('description', 'string', ['default' => '', 'comment' => '描述', 'limit' => 255])
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))
->addColumn('deleted_at', 'integer', array('default'=>0,'comment'=>'删除状态null 未删除 timestamp 已删除', 'signed' => false))
->create();
}
}

View File

@@ -1,36 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class UserRelateJob extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('user_has_jobs',['engine'=>'Innodb', 'comment' => '用户角色表', 'signed' => false]);
$table->addColumn('uid', 'integer',['comment'=>'用户ID', 'signed' => false])
->addColumn('job_id', 'integer', ['comment'=>'岗位ID', 'signed' => false])
->create();
}
}

View File

@@ -1,36 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class RoleHasDepartments extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('role_has_departments',['engine'=>'Innodb', 'comment' => '角色部门表', 'signed' => false]);
$table->addColumn('role_id', 'integer',['comment'=>'角色ID', 'signed' => false])
->addColumn('department_id', 'integer', ['comment'=>'部门ID', 'signed' => false])
->create();
}
}

View File

@@ -1,41 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class ChangePermissions extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('permissions')) {
$table = $this->table('permissions');
$table->addColumn('component', 'string', ['default' => '', 'comment' => '组件名称', 'limit' => '255', 'after' => 'permission_mark'])
->addColumn('redirect', 'string', ['default' => '', 'comment' => '跳转地址', 'limit' => '255', 'after' => 'component'])
->addColumn('hide_children_in_menu', 'integer', ['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY, 'default' => 1, 'comment' => '1 显示 2隐藏', 'after' => 'redirect'])
->addColumn('keepalive', 'integer', ['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY, 'default' => 1, 'comment' => '1 缓存 2 不存在 ', 'after' => 'hide_children_in_menu'])
->update();
}
}
}

View File

@@ -1,38 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class PermissionsAddColumn extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('permissions')) {
$table = $this->table('permissions');
$table->addColumn('level', 'string', ['default' => '', 'comment' => '层级', 'limit' => '50', 'after' => 'parent_id'])
->update();
}
}
}

View File

@@ -1,42 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class AddPermissionStatusColumn extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('permissions')) {
$table = $this->table('permissions');
$table->addColumn('status', 'integer', [
'limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,
'default' => 1,
'comment' => '1 显示 2 隐藏',
'after' => 'type'])
->update();
}
}
}

View File

@@ -1,42 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class UpdateRoles extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('roles')) {
$table = $this->table('roles');
$table->addColumn('identify', 'string', [
'limit' => 20,
'default' => 1,
'comment' => '角色的标识,用英文表示,用于后台路由权限',
'after' => 'role_name'])
->update();
}
}
}

View File

@@ -1,40 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class UpdatePermissions extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('permissions')) {
$table = $this->table('permissions');
$table->removeColumn('method')
->removeColumn('hide_children_in_menu')
->renameColumn('status', 'hidden')
->update();
}
}
}

View File

@@ -1,51 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Migrator;
use think\migration\db\Column;
class AddUserAvatar extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('users')) {
$table = $this->table('users');
$table->addColumn('avatar', 'string', [
'limit' => 255,
'default' => '',
'comment' => '用户头像',
'after' => 'email'])
->update();
}
}
}

View File

@@ -1,51 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Migrator;
use think\migration\db\Column;
class AddUserRememberToken extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
if ($this->hasTable('users')) {
$table = $this->table('users');
$table->addColumn('remember_token', 'string', [
'limit' => 512,
'default' => '',
'comment' => '用户token',
'after' => 'avatar'])
->update();
}
}
}

View File

@@ -1,39 +0,0 @@
<?php
use think\migration\Seeder;
class DepartmentsSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$data = [
[
'id' => 1,
'department_name' => '总部',
'parent_id' => 0,
],
[
'id' => 2,
'department_name' => '北京总部',
'parent_id' => 1,
],
[
'id' => 3,
'department_name' => '南京总部',
'parent_id' => 1,
],
];
foreach ($data as $item) {
\catchAdmin\permissions\model\Department::create($item);
}
}
}

View File

@@ -1,820 +0,0 @@
<?php
use think\migration\Seeder;
class PermissionsMenusSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
\catcher\Utils::importTreeData($this->getPermissions(), 'permissions', 'parent_id');
}
protected function getPermissions()
{
return array (
0 =>
array (
'id' => 1,
'permission_name' => '权限管理',
'parent_id' => 0,
'level' => '',
'route' => '/permissions',
'icon' => 'el-icon-cpu',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission',
'component' => 'layout',
'redirect' => '/permissions/users',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 100,
'created_at' => 1587461455,
'updated_at' => 1599362429,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 2,
'permission_name' => '用户管理',
'parent_id' => 1,
'level' => '1',
'route' => '/permissions/users',
'icon' => 'el-icon-user',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user',
'component' => 'users',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 10,
'created_at' => 1587461597,
'updated_at' => 1599362429,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 3,
'permission_name' => '列表',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@index',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461647,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
1 =>
array (
'id' => 4,
'permission_name' => '创建',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@create',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461696,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
2 =>
array (
'id' => 5,
'permission_name' => '保存',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@save',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461721,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
3 =>
array (
'id' => 6,
'permission_name' => '查看',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@edit',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461742,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
4 =>
array (
'id' => 7,
'permission_name' => '更新',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@update',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461762,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
5 =>
array (
'id' => 8,
'permission_name' => '删除',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@delete',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461841,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
6 =>
array (
'id' => 9,
'permission_name' => '禁用',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@switchStatus',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461876,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
7 =>
array (
'id' => 10,
'permission_name' => '恢复',
'parent_id' => 2,
'level' => '1-2',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'user@recover',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461901,
'updated_at' => 1599030266,
'deleted_at' => 0,
),
),
),
1 =>
array (
'id' => 11,
'permission_name' => '角色管理',
'parent_id' => 1,
'level' => '1',
'route' => '/permissions/roles',
'icon' => 'el-icon-s-custom',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role',
'component' => 'roles',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 9,
'created_at' => 1587461939,
'updated_at' => 1599362438,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 12,
'permission_name' => '列表',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@index',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587461984,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
1 =>
array (
'id' => 13,
'permission_name' => '创建',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@create',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462007,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
2 =>
array (
'id' => 14,
'permission_name' => '保存',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@save',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462021,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
3 =>
array (
'id' => 15,
'permission_name' => '查看',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@edit',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462040,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
4 =>
array (
'id' => 16,
'permission_name' => '更新',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@update',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462058,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
5 =>
array (
'id' => 17,
'permission_name' => '删除',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@delete',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462070,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
6 =>
array (
'id' => 18,
'permission_name' => '权限获取',
'parent_id' => 11,
'level' => '1-11',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'role@getPermissions',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462094,
'updated_at' => 1599362438,
'deleted_at' => 0,
),
),
),
2 =>
array (
'id' => 19,
'permission_name' => '菜单管理',
'parent_id' => 1,
'level' => '1',
'route' => '/permissions/rules',
'icon' => 'el-icon-collection-tag',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission',
'component' => 'rules',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 8,
'created_at' => 1587462147,
'updated_at' => 1599362475,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 20,
'permission_name' => '列表',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@index',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462205,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
1 =>
array (
'id' => 21,
'permission_name' => '创建',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@create',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462232,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
2 =>
array (
'id' => 22,
'permission_name' => '保存',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@save',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462250,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
3 =>
array (
'id' => 23,
'permission_name' => '禁用/启用',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@show',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462273,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
4 =>
array (
'id' => 24,
'permission_name' => '更新',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@update',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462284,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
5 =>
array (
'id' => 25,
'permission_name' => '删除',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@delete',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462296,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
6 =>
array (
'id' => 106,
'permission_name' => '权限方法',
'parent_id' => 19,
'level' => '1-19',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'permission@getMethods',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1599221913,
'updated_at' => 1599362475,
'deleted_at' => 0,
),
),
),
3 =>
array (
'id' => 26,
'permission_name' => '部门管理',
'parent_id' => 1,
'level' => '1',
'route' => '/permissions/departments',
'icon' => 'el-icon-monitor',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'departments',
'component' => 'departments',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 7,
'created_at' => 1587462488,
'updated_at' => 1599362429,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 27,
'permission_name' => '列表',
'parent_id' => 26,
'level' => '1-26',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'department@index',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462529,
'updated_at' => 1599030565,
'deleted_at' => 0,
),
1 =>
array (
'id' => 28,
'permission_name' => '保存',
'parent_id' => 26,
'level' => '1-26',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'department@save',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462548,
'updated_at' => 1599030565,
'deleted_at' => 0,
),
2 =>
array (
'id' => 29,
'permission_name' => '更新',
'parent_id' => 26,
'level' => '1-26',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'department@update',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462579,
'updated_at' => 1599030565,
'deleted_at' => 0,
),
3 =>
array (
'id' => 30,
'permission_name' => '删除',
'parent_id' => 26,
'level' => '1-26',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'department@delete',
'component' => '',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462592,
'updated_at' => 1599030565,
'deleted_at' => 0,
),
),
),
4 =>
array (
'id' => 31,
'permission_name' => '岗位管理',
'parent_id' => 1,
'level' => '1',
'route' => '/permissions/jobs',
'icon' => 'el-icon-setting',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job',
'component' => 'jobs',
'redirect' => '',
'hidden' => 1,
'keepalive' => 1,
'type' => 1,
'sort' => 1,
'created_at' => 1587462707,
'updated_at' => 1599362429,
'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 32,
'permission_name' => '列表',
'parent_id' => 31,
'level' => '1-31',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job@indexs',
'component' => '',
'redirect' => '',
'hidden' => 2,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462757,
'updated_at' => 1598959522,
'deleted_at' => 0,
),
1 =>
array (
'id' => 33,
'permission_name' => '保存',
'parent_id' => 31,
'level' => '1-31',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job@save',
'component' => '',
'redirect' => '',
'hidden' => 2,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462774,
'updated_at' => 1598959522,
'deleted_at' => 0,
),
2 =>
array (
'id' => 34,
'permission_name' => '更新',
'parent_id' => 31,
'level' => '1-31',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job@update',
'component' => '',
'redirect' => '',
'hidden' => 2,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462785,
'updated_at' => 1598959522,
'deleted_at' => 0,
),
3 =>
array (
'id' => 35,
'permission_name' => '删除',
'parent_id' => 31,
'level' => '1-31',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job@delete',
'component' => '',
'redirect' => '',
'hidden' => 2,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462794,
'updated_at' => 1598959522,
'deleted_at' => 0,
),
4 =>
array (
'id' => 36,
'permission_name' => '获取全部',
'parent_id' => 31,
'level' => '1-31',
'route' => '',
'icon' => '',
'module' => 'permissions',
'creator_id' => 1,
'permission_mark' => 'job@getAll',
'component' => '',
'redirect' => '',
'hidden' => 2,
'keepalive' => 1,
'type' => 2,
'sort' => 1,
'created_at' => 1587462818,
'updated_at' => 1598959522,
'deleted_at' => 0,
),
),
),
),
),
);
}
}

View File

@@ -1,38 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
use think\migration\Seeder;
class RolesSeed extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
\catchAdmin\permissions\model\Roles::create([
'role_name' => '超级管理员',
'identify' => 'admin',
'description' => 'super user',
'data_range' => 1,
'creator_id' => 1,
]);
\think\facade\Db::name( 'user_has_roles')->insert([
'role_id' => 1,
'uid' => 1,
]);
}
}

View File

@@ -1,38 +0,0 @@
<?php
namespace catchAdmin\permissions\event;
use catchAdmin\permissions\model\Permissions;
use catchAdmin\system\model\OperateLog;
use catcher\CatchAdmin;
use think\facade\Db;
class OperateLogEvent
{
public function handle($params)
{
$permission = $params['permission'];
$parentPermission = Permissions::where('id', $permission->parent_id)->value('permission_name');
$requestParams = request()->param();
// 如果参数过长则不记录
if (!empty($requestParams)) {
if (strlen(\json_encode($requestParams)) > 1000) {
$requestParams = [];
}
}
app(OperateLog::class)->storeBy([
'creator_id' => $params['creator_id'],
'module' => $parentPermission ? : '',
'method' => request()->method(),
'operate' => $permission->permission_name,
'route' => $permission->permission_mark,
'params' => !empty($requestParams) ? json_encode($requestParams, JSON_UNESCAPED_UNICODE) : '',
'created_at' => time(),
'ip' => request()->ip(),
]);
}
}

View File

@@ -1,78 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\permissions\excel;
use catchAdmin\permissions\model\Users;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
class UserExport implements ExcelContract
{
/**
* 设置头部
*
* @time 2020年09月08日
* @return string[]
*/
public function headers(): array
{
// TODO: Implement headers() method.
return [
'id', '用户名', '邮箱', '状态', '创建日期'
];
}
/**
* 处理数据
*
* @time 2020年09月08日
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\Collection
*/
public function sheets()
{
// TODO: Implement sheets() method.
$users = Users::field(['id', 'username', 'email', 'status', 'created_at'])->select();
foreach ($users as &$user) {
$user->status = $user->status == Users::ENABLE ? '启用' : '停用';
}
return $users;
}
/**
* 设置开始行
*
* @time 2020年09月08日
* @return int
*/
public function setRow()
{
return 2;
}
/**
* 设置标题
*
* @time 2020年09月08日
* @return array
*/
public function setTitle()
{
return [
'A1:G1', '导出用户', Alignment::HORIZONTAL_CENTER
];
}
}

View File

@@ -1,34 +0,0 @@
<?php
namespace catchAdmin\permissions\middleware;
use catcher\Code;
use catcher\exceptions\FailedException;
use thans\jwt\exception\TokenBlacklistException;
use thans\jwt\exception\TokenExpiredException;
use thans\jwt\exception\TokenInvalidException;
use thans\jwt\facade\JWTAuth;
use think\Middleware;
class AuthTokenMiddleware extends Middleware
{
public function handle($request, \Closure $next)
{
try {
JWTAuth::auth();
} catch (\Exception $e) {
if ($e instanceof TokenExpiredException) {
throw new FailedException('token 过期', Code::LOGIN_EXPIRED);
}
if ($e instanceof TokenBlacklistException) {
throw new FailedException('token 被加入黑名单', Code::LOGIN_BLACKLIST);
}
if ($e instanceof TokenInvalidException) {
throw new FailedException('token 不合法', Code::LOST_LOGIN);
}
throw new FailedException('登录用户不合法', Code::LOST_LOGIN);
}
return $next($request);
}
}

View File

@@ -1,127 +0,0 @@
<?php
namespace catchAdmin\permissions\middleware;
use app\Request;
use catchAdmin\permissions\model\Permissions;
use catcher\CatchCacheKeys;
use catcher\Code;
use catcher\exceptions\PermissionForbiddenException;
use think\facade\Cache;
use catcher\Utils;
class PermissionsMiddleware
{
/**
*
* @time 2019年12月12日
* @param Request $request
* @param \Closure $next
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws PermissionForbiddenException
*/
public function handle(Request $request, \Closure $next)
{
$rule = $request->rule()->getName();
if (!$rule) {
return $next($request);
}
// 模块忽略
[$module, $controller, $action] = Utils::parseRule($rule);
// toad
if (in_array($module, $this->ignoreModule())) {
return $next($request);
}
// 用户未登录
$user = $request->user();
if (!$user) {
throw new PermissionForbiddenException('Login is invalid', Code::LOST_LOGIN);
}
// 超级管理员
if (Utils::isSuperAdmin()) {
return $next($request);
}
// Get 请求
if ($this->allowGet($request)) {
return $next($request);
}
// 判断权限
$permission = property_exists($request, 'permission') ? $request->permission :
$this->getPermission($module, $controller, $action);
if (!$permission || !in_array($permission->id, Cache::get(CatchCacheKeys::USER_PERMISSIONS . $user->id))) {
throw new PermissionForbiddenException();
}
return $next($request);
}
/**
*
* @time 2019年12月14日
* @param $module
* @param $controllerName
* @param $action
* @param $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|bool|\think\Model|null
*/
protected function getPermission($module, $controllerName, $action)
{
$permissionMark = sprintf('%s@%s', $controllerName, $action);
return Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
}
/**
* 忽略模块
*
* @time 2020年04月16日
* @return array
*/
protected function ignoreModule()
{
return ['login'];
}
/**
* 操作日志
*
* @time 2020年04月16日
* @param $creatorId
* @param $permission
* @return void
*/
protected function operateEvent($creatorId, $permission)
{
// 操作日志
$permission && event('operateLog', [
'creator_id' => $creatorId,
'permission' => $permission,
]);
}
/**
* get allow
*
* @time 2020年10月12日
* @param $request
* @return bool
* @throws \ReflectionException
*/
protected function allowGet($request)
{
if (Utils::isMethodNeedAuth($request->rule()->getName())) {
return false;
}
return $request->isGet() && config('catch.permissions.is_allow_get');
}
}

View File

@@ -1,76 +0,0 @@
<?php
namespace catchAdmin\permissions\middleware;
use app\Request;
use catchAdmin\permissions\model\Permissions;
use catcher\CatchCacheKeys;
use catcher\Code;
use catcher\exceptions\PermissionForbiddenException;
use think\facade\Cache;
use catcher\Utils;
class RecordOperateMiddleware
{
/**
*
* @time 2019年12月12日
* @param Request $request
* @param \Closure $next
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws PermissionForbiddenException
*/
public function handle(Request $request, \Closure $next)
{
$rule = $request->rule()->getName();
// 模块忽略
[$module, $controller, $action] = Utils::parseRule($rule);
$permission = $this->getPermission($module, $controller, $action);
$this->operateEvent($request->user()->id, $permission);
// 将权限带入
$request->permission = $permission;
return $next($request);
}
/**
*
* @time 2019年12月14日
* @param $module
* @param $controllerName
* @param $action
* @param $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|bool|\think\Model|null
*/
protected function getPermission($module, $controllerName, $action)
{
$permissionMark = sprintf('%s@%s', $controllerName, $action);
return Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
}
/**
* 操作日志
*
* @time 2020年04月16日
* @param $creatorId
* @param $permission
* @return void
*/
protected function operateEvent($creatorId, $permission)
{
// 操作日志
$permission && event('operateLog', [
'creator_id' => $creatorId,
'permission' => $permission,
]);
}
}

View File

@@ -1,96 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catcher\Utils;
trait DataRangScopeTrait
{
/**
* 数据范围查询
*
* @param $roles
* @return mixed
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
public function dataRange($roles = [])
{
if (Utils::isSuperAdmin()) {
return $this;
}
$userIds = $this->getDepartmentUserIdsBy($roles);
if (empty($userIds)) {
return $this;
}
return $this->whereIn($this->aliasField('creator_id'), $userIds);
}
/**
* 获取部门IDs
*
* @param $roles
* @return array
* @author JaguarJack <njphper@gmail.com>
* @date 2020/6/6
*/
public function getDepartmentUserIdsBy($roles)
{
$userIds = [];
$isAll = false;
$user = request()->user();
if (empty($roles)) {
$roles = $user->getRoles();
}
foreach ($roles as $role) {
switch ($role->data_range) {
case Roles::ALL_DATA:
$isAll = true;
break;
case Roles::SELF_CHOOSE:
$departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
$userIds = array_merge($userIds, $this->getUserIdsByDepartmentId($departmentIds));
break;
case Roles::SELF_DATA:
$userIds[] = $user->id;
break;
case Roles::DEPARTMENT_DOWN_DATA:
// 查一下下级部门
$departmentIds = Department::where('parent_id', $user->department_id)->column('id');
$departmentIds[] = $user->department_id;
$userIds = array_merge([$user->id], $this->getUserIdsByDepartmentId($departmentIds));
break;
case Roles::DEPARTMENT_DATA:
$userIds = array_merge($userIds, $this->getUserIdsByDepartmentId([$user->department_id]));
break;
default:
break;
}
// 如果有全部数据 直接跳出
if ($isAll) {
break;
}
}
return $userIds;
}
/**
* 获取UserID
*
* @time 2020年07月04日
* @param $id
* @return array
*/
protected function getUserIdsByDepartmentId(array $id)
{
return Users::whereIn('department_id', $id)->column('id');
}
}

View File

@@ -1,61 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\DepartmentSearch;
use catcher\base\CatchModel;
use think\db\exception\DbException;
class Department extends CatchModel
{
use DepartmentSearch;
protected $name = 'departments';
protected $field = [
'id', //
'department_name', // 部门名称
'parent_id', // 父级ID
'principal', // 负责人
'mobile', // 联系电话
'email', // 联系又想
'creator_id', // 创建人ID
'status', // 1 正常 2 停用
'sort', // 排序字段
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
];
/**
* 列表数据
*
* @time 2020年01月09日
* @return array
* @throws DbException
*/
public function getList(): array
{
return $this->catchSearch()
->catchOrder()
->select()->toTree();
}
/**
* 获取子部门IDS
*
* @time 2020年11月04日
* @param $id
* @throws DbException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @return mixed
*/
public static function getChildrenDepartmentIds($id)
{
$departmentIds = Department::field(['id', 'parent_id'])->select()->getAllChildrenIds([$id]);
$departmentIds[] = $id;
return $departmentIds;
}
}

View File

@@ -1,57 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
trait HasDepartmentsTrait
{
/**
*
* @time 2019年12月08日
* @return mixed
*/
public function departments()
{
return $this->belongsToMany(Department::class, 'role_has_departments', 'department_id', 'role_id');
}
/**
*
* @time 2019年12月08日
* @return mixed
*/
public function getDepartments()
{
return $this->departments()->select();
}
/**
*
* @time 2019年12月08日
* @param array $departments
* @return mixed
*/
public function attachDepartments(array $departments)
{
if (empty($departments)) {
return true;
}
sort($departments);
return $this->departments()->attach($departments);
}
/**
*
* @time 2019年12月08日
* @param array $departments
* @return mixed
*/
public function detachDepartments(array $departments = [])
{
if (empty($departments)) {
return $this->departments()->detach();
}
return $this->departments()->detach($departments);
}
}

View File

@@ -1,58 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
trait HasJobsTrait
{
/**
*
* @time 2019年12月08日
* @return mixed
*/
public function jobs()
{
return $this->belongsToMany(Job::class, 'user_has_jobs', 'job_id', 'uid');
}
/**
*
* @time 2019年12月08日
* @param array $fields
* @return mixed
*/
public function getJobs()
{
return $this->jobs()->select();
}
/**
*
* @time 2019年12月08日
* @param array $jobs
* @return mixed
*/
public function attachJobs(array $jobs)
{
if (empty($jobs)) {
return true;
}
sort($jobs);
return $this->jobs()->attach($jobs);
}
/**
*
* @time 2019年12月08日
* @param array $jobs
* @return mixed
*/
public function detachJobs(array $jobs = [])
{
if (empty($jobs)) {
return $this->jobs()->detach();
}
return $this->jobs()->detach($jobs);
}
}

View File

@@ -1,76 +0,0 @@
<?php
/**
* @filename HasPermissionsTrait.php
* @createdAt 2020/1/14
* @project https://github.com/yanwenwu/catch-admin
* @document http://doc.catchadmin.com
* @author JaguarJack <njphper@gmail.com>
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
*/
namespace catchAdmin\permissions\model;
trait HasPermissionsTrait
{
/**
*
* @time 2019年12月09日
* @return \think\model\relation\BelongsToMany
*/
public function permissions(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'permission_id', 'role_id');
}
/**
*
* @time 2019年12月08日
* @param array $condition
* @param array $field
* @return mixed
*/
public function getPermissions($condition = [], $field = [])
{
return $this->permissions()
->when(!empty($field), function ($query) use ($field){
$query->field($field);
})
->when(!empty($condition), function ($query) use ($condition){
$query->where($condition);
})
->select();
}
/**
*
* @time 2019年12月08日
* @param array $permissions
* @return mixed
* @throws \think\db\exception\DbException
*/
public function attachPermissions(array $permissions)
{
if (empty($permissions)) {
return true;
}
sort($permissions);
return $this->permissions()->attach($permissions);
}
/**
*
* @time 2019年12月08日
* @param array $permissions
* @return mixed
*/
public function detachPermissions(array $permissions = [])
{
if (empty($permissions)) {
return $this->permissions()->detach();
}
return $this->permissions()->detach($permissions);
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\JobsSearch;
use catcher\base\CatchModel;
class Job extends CatchModel
{
use JobsSearch;
protected $name = 'jobs';
protected $field = [
'id', //
'job_name', // 岗位名称
'coding', // 编码
'creator_id', // 创建人ID
'status', // 1 正常 2 停用
'sort', // 排序字段
'description', // 描述
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
];
/**
* 列表
*
* @time 2020年01月09日
* @param $params
* @throws \think\db\exception\DbException
* @return \think\Paginator
*/
public function getList()
{
return $this->catchSearch()
->paginate();
}
}

View File

@@ -1,142 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\PermissionsSearch;
use catcher\base\CatchModel;
use think\Model;
class Permissions extends CatchModel
{
use PermissionsSearch;
protected $name = 'permissions';
protected $field = [
'id', //
'permission_name', // 菜单名称
'parent_id', // 父级ID
'icon',
'component', // 组件
'redirect',
'keepalive',
'creator_id',
'hidden',
'module', // 模块
'route', // 路由
'permission_mark', // 权限标识
'type', // 1 菜单 2 按钮
'sort', // 排序字段
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态null 未删除 timestamp 已删除
];
public const MENU_TYPE = 1;
public const BTN_TYPE = 2;
public const GET = 'get';
public const POST = 'post';
public const PUT = 'put';
public const DELETE = 'delete';
public function getList($isMenu = false)
{
return $this->catchSearch()
->catchOrder()
->when($isMenu, function ($query){
$query->where('type', self::MENU_TYPE);
})
->select();
}
public function roles(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
}
/**
* 获取当前用户权限
*
* @time 2020年01月14日
* @param array $permissionIds
* @return \think\Collection
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public static function getCurrentUserPermissions(array $permissionIds): \think\Collection
{
return parent::whereIn('id', $permissionIds)
->field(['permission_name as title', 'id', 'parent_id',
'route', 'icon', 'component', 'redirect', 'module',
'keepalive as keepAlive', 'type', 'permission_mark', 'hidden'
])
->catchOrder()
->select();
}
/**
* 插入后回调 更新 level
*
* @time 2020年04月22日
* @param Model $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|bool|Model|void|null
*/
public static function onAfterInsert(Model $model)
{
$model = self::where('id', $model->id)->find();
if ($model && $model->parent_id) {
$parent = self::where('id', $model->parent_id)->find();
$level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id;
return $model->where('id', $model->id)->update([
'level' => $level
]);
}
return true;
}
public function show($id)
{
$permission = $this->findBy($id);
// 不能使用改属性判断,模型有该属性,使用数组方式
// $permission->hidden
$hidden = $permission['hidden'] == Permissions::ENABLE ? Permissions::DISABLE : Permissions::ENABLE;
$nextLevelIds = $this->getNextLevel([$id]);
$nextLevelIds[] = $id;
return $this->whereIn('id', $nextLevelIds)->update([
'hidden' => $hidden,
'updated_at' => time(),
]);
}
/**
* 获取 level ids
*
* @time 2020年09月06日
* @param array $id
* @param array $ids
* @return array
*/
protected function getNextLevel(array $id, &$ids = [])
{
$_ids = $this->whereIn('parent_id', $id)
->where('type', self::MENU_TYPE)
->column('id');
if (count($_ids)) {
$ids = array_merge($_ids, $this->getNextLevel($_ids, $ids));
}
return $ids;
}
}

View File

@@ -1,53 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\RolesSearch;
use catcher\base\CatchModel;
class Roles extends CatchModel
{
use HasDepartmentsTrait;
use HasPermissionsTrait;
use RolesSearch;
protected $name = 'roles';
public const ALL_DATA = 1; // 全部数据
public const SELF_CHOOSE = 2; // 自定义数据
public const SELF_DATA = 3; // 本人数据
public const DEPARTMENT_DATA = 4; // 部门数据
public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据
protected $field = [
'id', //
'role_name', // 角色名
'identify', // 身份标识
'parent_id', // 父级ID
'creator_id', // 创建者
'data_range', // 数据范围
'description', // 角色备注
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态0未删除 >0 已删除
];
public function getList()
{
return $this->catchSearch()
->order('id', 'desc')
->select()
->toTree();
}
/**
*
* @time 2019年12月08日
* @return \think\model\relation\BelongsToMany
*/
public function users(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
}
}

View File

@@ -1,111 +0,0 @@
<?php
namespace catchAdmin\permissions\model;
use catchAdmin\permissions\model\search\UserSearch;
use catcher\base\CatchModel;
use catcher\exceptions\FailedException;
use catcher\Utils;
class Users extends CatchModel
{
use HasRolesTrait;
use HasJobsTrait;
use UserSearch;
protected $name = 'users';
protected $field = [
'id', //
'username', // 用户名
'password', // 用户密码
'email', // 邮箱 登录
'avatar', // 头像
'remember_token',
'creator_id', // 创建者ID
'department_id', // 部门ID
'status', // 用户状态 1 正常 2 禁用
'last_login_ip', // 最后登录IP
'last_login_time', // 最后登录时间
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态0未删除 >0 已删除
];
/**
* set password
*
* @time 2019年12月07日
* @param $value
* @return false|string
*/
public function setPasswordAttr($value)
{
return password_hash($value, PASSWORD_DEFAULT);
}
/**
* 用户列表
*
* @time 2019年12月08日
* @throws \think\db\exception\DbException
* @return \think\Paginator
*/
public function getList(): \think\Paginator
{
return $this->withoutField(['updated_at'], true)
->catchSearch()
->catchLeftJoin(Department::class, 'id', 'department_id', ['department_name'])
->order($this->aliasField('id'), 'desc')
->paginate();
}
/**
* 获取权限
*
* @time 2019年12月12日
* @param $uid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array
*/
public function getPermissionsBy($uid = 0): array
{
// 获取超级管理配置 超级管理员全部权限
if ($uid == config('catch.permissions.super_admin_id')) {
return Permissions::select()->column('id');
}
$roles = $uid ? $this->findBy($uid)->getRoles() : $this->getRoles();
$permissionIds = [];
foreach ($roles as $role) {
$permissionIds = array_merge($permissionIds, $role->getPermissions()->column('id'));
}
return array_unique($permissionIds);
}
/**
* 后台根据权限标识判断用户是否拥有某个权限
* @param string $permission_mark
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*
* 用法 request()->user()->can('permission@create');
*/
public function can($permission_mark)
{
// 超级管理员直接返回true
if (Utils::isSuperAdmin()){
return true;
}
// 查询当前用户的权限
return in_array(
Permissions::where('permission_mark',$permission_mark)->value('id') ? : 0,
$this->getPermissionsBy()
);
}
}

View File

@@ -1,15 +0,0 @@
<?php
namespace catchAdmin\permissions\model\search;
trait DepartmentSearch
{
public function searchDepartmentNameAttr($query, $value, $data)
{
return $query->whereLike('department_name', $value);
}
public function searchStatusAttr($query, $value, $data)
{
return $query->where('status', $value);
}
}

View File

@@ -1,20 +0,0 @@
<?php
namespace catchAdmin\permissions\model\search;
trait JobsSearch
{
public function searchJobNameAttr($query, $value, $data)
{
return $query->whereLike('job_name', $value);
}
public function searchCodingAttr($query, $value, $data)
{
return $query->whereLike('coding', $value);
}
public function searchStatusAttr($query, $value, $data)
{
return $query->where('status', $value);
}
}

View File

@@ -1,31 +0,0 @@
<?php
namespace catchAdmin\permissions\model\search;
use catchAdmin\permissions\model\Roles;
trait PermissionsSearch
{
public function searchPermissionNameAttr($query, $value, $data)
{
return $query->whereLike('permission_name', $value);
}
public function searchIdAttr($query, $value, $data)
{
$query->where('parent_id', $value)->whereOr('id', $value);
}
public function searchRoleIdAttr($query, $value, $data)
{
$permissionIds = [];
$permissions = Roles::where('id', $value)->find()->getPermissions();
foreach ($permissions as $_permission) {
$permissionIds[] = $_permission->pivot->permission_id;
}
if(!empty($permissionIds)) {
$query->whereIn('id', $permissionIds);
}
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace catchAdmin\permissions\model\search;
trait RolesSearch
{
public function searchRoleNameAttr($query, $value, $data)
{
return $query->whereLike('role_name', $value);
}
public function searchIdAttr($query, $value, $data)
{
$query->where('parent_id', $value)->whereOr('id', $value);
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace catchAdmin\permissions\model\search;
use catchAdmin\permissions\model\Department;
trait UserSearch
{
public function searchUsernameAttr($query, $value, $data)
{
return $query->whereLike('username', $value);
}
public function searchEmailAttr($query, $value, $data)
{
return $query->whereLike('email', $value);
}
public function searchStatusAttr($query, $value, $data)
{
return $query->where($this->aliasField('status'), $value);
}
/**
* 查询部门下的用户
*
* @time 2020年11月04日
* @param $query
* @param $value
* @param $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return mixed
*/
public function searchDepartmentIdAttr($query, $value, $data)
{
return $query->whereIn($this->aliasField('department_id'), Department::getChildrenDepartmentIds($value));
}
}

View File

@@ -1,15 +0,0 @@
{
"name": "权限管理",
"alias": "permissions",
"description": "权限管理模块",
"version": "1.0.0",
"keywords": [],
"order": 100,
"services": [
"\\catchAdmin\\permissions\\PermissionService"
],
"aliases": {},
"files": [],
"requires": [],
"enable": true
}

View File

@@ -1,17 +0,0 @@
<?php
namespace catchAdmin\permissions\request;
use catchAdmin\permissions\model\Users;
use catcher\base\CatchRequest;
class ProfileRequest extends CatchRequest
{
protected function rules(): array
{
// TODO: Implement rules() method.
return [
'username|用户名' => 'require|max:20',
'email|邮箱' => 'require|email|unique:'.Users::class . ',email,' . $this->user()->id,
];
}
}

View File

@@ -1,27 +0,0 @@
<?php
$router->group(function () use ($router){
// 角色
$router->resource('roles', '\catchAdmin\permissions\controller\Role');
// 角色列表
$router->get('/role/get/permissions', '\catchAdmin\permissions\controller\Role@getPermissions');
// 权限
$router->resource('permissions', '\catchAdmin\permissions\controller\Permission');
$router->put('permissions/show/<id>', '\catchAdmin\permissions\controller\Permission@show');
$router->get('controller/methods/<id>', '\catchAdmin\permissions\controller\Permission@getMethods');
// 部门
$router->resource('departments', '\catchAdmin\permissions\controller\Department');
// 所有职位
$router->get('jobs/all', '\catchAdmin\permissions\controller\Job@getAll');
// 岗位
$router->resource('jobs', '\catchAdmin\permissions\controller\Job');
// 用户
$router->resource('users', '\catchAdmin\permissions\controller\User');
// 切换状态
$router->put('users/switch/status/<id>', '\catchAdmin\permissions\controller\User@switchStatus');
$router->put('users/recover/<id>', '\catchAdmin\permissions\controller\User@recover');
$router->put('user/profile', '\catchAdmin\permissions\controller\User@profile');
$router->get('users/get/roles', '\catchAdmin\permissions\controller\User@getRoles');
$router->get('user/info', '\catchAdmin\permissions\controller\User@info');
$router->get('user/export', '\catchAdmin\permissions\controller\User@export');
})->middleware('auth');

View File

@@ -1,226 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\sms;
use catchAdmin\sms\model\SmsConfig;
use catcher\exceptions\FailedException;
use Overtrue\EasySms\EasySms;
use think\helper\Str;
class Sms
{
/**
* timeout http 请求时间
*
* @var int
*/
protected $timeout = 5;
/**
* 错误日志
*
* @var string
*/
protected $errorLog;
/**
* 网关
*
* @var array
*/
protected $gateways = [];
/**
* 配置
*
* @var array
*/
protected $config = [];
/**
* 发送数据
*
* @var array
*/
protected $sendData = [];
/**
* Sms constructor.
* @param $config
*/
public function __construct(array $config)
{
$config['timeout'] = $this->timeout;
$config['gateways']['errorlog'] = runtime_path('log') . 'sms.log';
$this->config = $config;
}
/**
* 发送
*
* @time 2020年09月17日
* @param string $phone
* @param array $data
* @return mixed
* @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
* @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
*/
public function send(string $phone, array $data)
{
try {
$this->sendData['data'] = $data;
return $this->easySms()
->send($phone, $this->sendData);
} catch (\Exception $exception) {
throw new FailedException($exception->getMessage());
}
}
/**
* easy sms
*
* @time 2020年09月17日
* @return EasySms
*/
public function easySms()
{
return new EasySms($this->config);
}
/**
* 内容
*
* @time 2020年09月17日
* @param $content
* @param string $key
* @return $this
*/
public function content($content, $key = 'content')
{
$this->sendData[$key] = $content;
return $this;
}
/**
* 模版
*
* @time 2020年09月17日
* @param $template
* @param string $key
* @return $this
*/
public function template($template, $key = 'template')
{
$this->sendData[$key] = $template;
return $this;
}
/**
* 超时间时间 s
*
* @time 2020年09月17日
* @param int $timeout
* @return $this
*/
public function timeout(int $timeout)
{
$this->config['timeout'] = $timeout;
return $this;
}
/**
* 记录记录地址
*
* @time 2020年09月17日
* @param string $log
* @return $this
*/
public function errorLog(string $log)
{
$this->config['gateways']['errorlog'] = $log;
return $this;
}
/**
* gateways config
*
* @time 2020年09月17日
* @param $gateways
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array
*/
protected static function getGatewaysConfig($gateways)
{
$gatewaysConfig = [];
$smsConfig = new SmsConfig();
foreach ($gateways as $gate) {
$c = $smsConfig->findByName($gate);
if ($c) {
$c->hasConfig()
->select()
->each(function ($item) use (&$gatewaysConfig, $gate){
$gatewaysConfig[$gate][$item['key']] = $item['value'];
});
}
}
return $gatewaysConfig;
}
/**
* sms
*
* @time 2020年09月17日
* @param $method
* @param $arg
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Sms
*/
public static function __callStatic($method, $arg)
{
$gateways = Str::snake($method);
if (Str::contains($gateways, '_')) {
$gateways = explode('_', $gateways);
} else {
$gateways = [$gateways];
}
$config = [
'default' => [
'gateways' => $gateways,
],
'gateways' => static::getGatewaysConfig($gateways)
];
return new self($config);
}
}

View File

@@ -1,23 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\sms;
use catcher\ModuleService;
class SmsService extends ModuleService
{
public function loadRouteFrom()
{
// TODO: Implement loadRouteFrom() method.
return __DIR__ . DIRECTORY_SEPARATOR . 'route.php';
}
}

View File

@@ -1,89 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\sms\controller;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\sms\model\SmsConfig as SmsConfigModel;
class Config extends CatchController
{
protected $model;
public function __construct(SmsConfigModel $model)
{
$this->model = $model;
}
/**
* 列表
*
* @time 2020/09/16 17:28
*
* @return \think\Response
*/
public function index()
{
return CatchResponse::paginate($this->model->getList());
}
/**
* 保存
*
* @time 2020/09/16 17:28
* @param Request Request
* @return \think\Response
*/
public function save(Request $request)
{
return CatchResponse::success($this->model->storeBy($request->param()));
}
/**
* 读取
*
* @time 2020/09/16 17:28
* @param $id
* @return \think\Response
*/
public function read($id)
{
return CatchResponse::success($this->model->findBy($id));
}
/**
* 更新
*
* @time 2020/09/16 17:28
* @param Request $request
* @return \think\Response
*/
public function update(Request $request, $id)
{
return CatchResponse::success($this->model->updateBy($id, $request->post()));
}
/**
* 删除
*
* @time 2020/09/16 17:28
* @param $id
* @return \think\Response
*/
public function delete($id)
{
return CatchResponse::success($this->model->deleteBy($id));
}
}

View File

@@ -1,86 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\sms\controller;
use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\sms\model\SmsTemplate as SmsTemplateModel;
class Template extends CatchController
{
protected $model;
public function __construct(SmsTemplateModel $model)
{
$this->model = $model;
}
/**
* 列表
*
* @time 2020/09/16 17:28
*
* @return \think\Response
*/
public function index()
{
return CatchResponse::success($this->model->getList());
}
/**
* 保存
*
* @time 2020/09/16 17:28
* @param Request Request
* @return \think\Response
*/
public function save(Request $request)
{
return CatchResponse::success($this->model->storeBy($request->param()));
}
/**
* 读取
*
* @time 2020/09/16 17:28
* @param $id
* @return \think\Response
*/
public function read($id)
{
return CatchResponse::success($this->model->findBy($id));
}
/**
* 更新
*
* @time 2020/09/16 17:28
* @param Request $request
* @return \think\Response
*/
public function update(Request $request, $id)
{
return CatchResponse::success($this->model->updateBy($id, $request->post()));
}
/**
* 删除
*
* @time 2020/09/16 17:28
* @param $id
* @return \think\Response
*/
public function delete($id)
{
return CatchResponse::success($this->model->deleteBy($id));
}
}

View File

@@ -1,43 +0,0 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class SmsConfig extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('sms_config', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => 'sms 配置' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('name', 'string', ['limit' => 50,'null' => true,'signed' => false,'comment' => '运营商名称',])
->addColumn('pid', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => false,'default' => 0,'signed' => false,'comment' => '父级ID',])
->addColumn('key', 'string', ['limit' => 100,'null' => false,'default' => '','signed' => false,'comment' => 'key',])
->addColumn('value', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => 'value',])
->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->create();
}
}

Some files were not shown because too many files have changed in this diff Show More