add
This commit is contained in:
90
app/admin/controller/routine/AdminInfo.php
Normal file
90
app/admin/controller/routine/AdminInfo.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\routine;
|
||||
|
||||
use Throwable;
|
||||
use app\admin\model\Admin;
|
||||
use app\common\controller\Backend;
|
||||
|
||||
class AdminInfo extends Backend
|
||||
{
|
||||
/**
|
||||
* @var object
|
||||
* @phpstan-var Admin
|
||||
*/
|
||||
protected object $model;
|
||||
|
||||
protected string|array $preExcludeFields = ['username', 'last_login_time', 'password', 'salt', 'status'];
|
||||
|
||||
protected array $authAllowFields = ['id', 'username', 'nickname', 'avatar', 'email', 'mobile', 'motto', 'last_login_time'];
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
$this->auth->setAllowFields($this->authAllowFields);
|
||||
$this->model = $this->auth->getAdmin();
|
||||
}
|
||||
|
||||
public function index(): void
|
||||
{
|
||||
$info = $this->auth->getInfo();
|
||||
$this->success('', [
|
||||
'info' => $info
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(): void
|
||||
{
|
||||
$pk = $this->model->getPk();
|
||||
$id = $this->request->param($pk);
|
||||
$row = $this->model->find($id);
|
||||
if (!$row) {
|
||||
$this->error(__('Record not found'));
|
||||
}
|
||||
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if (!$data) {
|
||||
$this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
|
||||
if (!empty($data['avatar'])) {
|
||||
$row->avatar = $data['avatar'];
|
||||
if ($row->save()) {
|
||||
$this->success(__('Avatar modified successfully!'));
|
||||
}
|
||||
}
|
||||
|
||||
// 数据验证
|
||||
if ($this->modelValidate) {
|
||||
try {
|
||||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = new $validate();
|
||||
$validate->scene('info')->check($data);
|
||||
} catch (Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$this->model->resetPassword($this->auth->id, $data['password']);
|
||||
}
|
||||
|
||||
$data = $this->excludeFields($data);
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
$result = $row->save($data);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success(__('Update successful'));
|
||||
} else {
|
||||
$this->error(__('No rows updated'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
app/admin/controller/routine/Attachment.php
Normal file
59
app/admin/controller/routine/Attachment.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\routine;
|
||||
|
||||
use Throwable;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\Attachment as AttachmentModel;
|
||||
|
||||
class Attachment extends Backend
|
||||
{
|
||||
/**
|
||||
* @var object
|
||||
* @phpstan-var AttachmentModel
|
||||
*/
|
||||
protected object $model;
|
||||
|
||||
protected string|array $quickSearchField = 'name';
|
||||
|
||||
protected array $withJoinTable = ['admin', 'user'];
|
||||
|
||||
protected string|array $defaultSortField = 'last_upload_time,desc';
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
$this->model = new AttachmentModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function del(): void
|
||||
{
|
||||
$where = [];
|
||||
$dataLimitAdminIds = $this->getDataLimitAdminIds();
|
||||
if ($dataLimitAdminIds) {
|
||||
$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
|
||||
}
|
||||
|
||||
$ids = $this->request->param('ids/a', []);
|
||||
$where[] = [$this->model->getPk(), 'in', $ids];
|
||||
$data = $this->model->where($where)->select();
|
||||
|
||||
$count = 0;
|
||||
try {
|
||||
foreach ($data as $v) {
|
||||
$count += $v->delete();
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success(__('%d records and files have been deleted', [$count]));
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
}
|
||||
246
app/admin/controller/routine/Config.php
Normal file
246
app/admin/controller/routine/Config.php
Normal file
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\routine;
|
||||
|
||||
use Throwable;
|
||||
use ba\Filesystem;
|
||||
use app\common\library\Email;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use app\common\controller\Backend;
|
||||
use app\admin\model\Config as ConfigModel;
|
||||
use PHPMailer\PHPMailer\Exception as PHPMailerException;
|
||||
|
||||
class Config extends Backend
|
||||
{
|
||||
/**
|
||||
* @var object
|
||||
* @phpstan-var ConfigModel
|
||||
*/
|
||||
protected object $model;
|
||||
|
||||
protected array $filePath = [
|
||||
'appConfig' => 'config/app.php',
|
||||
'webAdminBase' => 'web/src/router/static/adminBase.ts',
|
||||
'backendEntranceStub' => 'app/admin/library/stubs/backendEntrance.stub',
|
||||
];
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
$this->model = new ConfigModel();
|
||||
}
|
||||
|
||||
public function index(): void
|
||||
{
|
||||
$configGroup = get_sys_config('config_group');
|
||||
$config = $this->model->order('weigh desc')->select()->toArray();
|
||||
|
||||
$list = [];
|
||||
$newConfigGroup = [];
|
||||
foreach ($configGroup as $item) {
|
||||
$list[$item['key']]['name'] = $item['key'];
|
||||
$list[$item['key']]['title'] = __($item['value']);
|
||||
$newConfigGroup[$item['key']] = $list[$item['key']]['title'];
|
||||
}
|
||||
foreach ($config as $item) {
|
||||
if (array_key_exists($item['group'], $newConfigGroup)) {
|
||||
$item['title'] = __($item['title']);
|
||||
$list[$item['group']]['list'][] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('', [
|
||||
'list' => $list,
|
||||
'remark' => get_route_remark(),
|
||||
'configGroup' => $newConfigGroup ?? [],
|
||||
'quickEntrance' => get_sys_config('config_quick_entrance'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function edit(): void
|
||||
{
|
||||
$all = $this->model->select();
|
||||
foreach ($all as $item) {
|
||||
if ($item['type'] == 'editor') {
|
||||
$this->request->filter('clean_xss');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->modelValidate = false;
|
||||
$data = $this->request->post();
|
||||
if (!$data) {
|
||||
$this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
|
||||
$data = $this->excludeFields($data);
|
||||
|
||||
$configValue = [];
|
||||
foreach ($all as $item) {
|
||||
if (array_key_exists($item->name, $data)) {
|
||||
$configValue[] = [
|
||||
'id' => $item->id,
|
||||
'type' => $item->getData('type'),
|
||||
'value' => $data[$item->name]
|
||||
];
|
||||
|
||||
// 自定义后台入口
|
||||
if ($item->name == 'backend_entrance') {
|
||||
$backendEntrance = get_sys_config('backend_entrance');
|
||||
if ($backendEntrance == $data[$item->name]) continue;
|
||||
|
||||
if (!preg_match("/^\/[a-zA-Z0-9]+$/", $data[$item->name])) {
|
||||
$this->error(__('Backend entrance rule'));
|
||||
}
|
||||
|
||||
// 修改 adminBaseRoutePath
|
||||
$adminBaseFilePath = Filesystem::fsFit(root_path() . $this->filePath['webAdminBase']);
|
||||
$adminBaseContent = @file_get_contents($adminBaseFilePath);
|
||||
if (!$adminBaseContent) $this->error(__('Configuration write failed: %s', [$this->filePath['webAdminBase']]));
|
||||
|
||||
$adminBaseContent = str_replace("export const adminBaseRoutePath = '$backendEntrance'", "export const adminBaseRoutePath = '{$data[$item->name]}'", $adminBaseContent);
|
||||
$result = @file_put_contents($adminBaseFilePath, $adminBaseContent);
|
||||
if (!$result) $this->error(__('Configuration write failed: %s', [$this->filePath['webAdminBase']]));
|
||||
|
||||
// 去除后台入口开头的斜杠
|
||||
$oldBackendEntrance = ltrim($backendEntrance, '/');
|
||||
$newBackendEntrance = ltrim($data[$item->name], '/');
|
||||
|
||||
// 设置应用别名映射
|
||||
$appMap = config('app.app_map');
|
||||
$adminMapKey = array_search('admin', $appMap);
|
||||
if ($adminMapKey !== false) {
|
||||
unset($appMap[$adminMapKey]);
|
||||
}
|
||||
if ($newBackendEntrance != 'admin') {
|
||||
$appMap[$newBackendEntrance] = 'admin';
|
||||
}
|
||||
$appConfigFilePath = Filesystem::fsFit(root_path() . $this->filePath['appConfig']);
|
||||
$appConfigContent = @file_get_contents($appConfigFilePath);
|
||||
if (!$appConfigContent) $this->error(__('Configuration write failed: %s', [$this->filePath['appConfig']]));
|
||||
|
||||
$appMapStr = '';
|
||||
foreach ($appMap as $newAppName => $oldAppName) {
|
||||
$appMapStr .= "'$newAppName' => '$oldAppName', ";
|
||||
}
|
||||
$appMapStr = rtrim($appMapStr, ', ');
|
||||
$appMapStr = "[$appMapStr]";
|
||||
|
||||
$appConfigContent = preg_replace("/'app_map'(\s+)=>(\s+)(.*)\/\/ 域名/s", "'app_map'\$1=>\$2$appMapStr,\n // 域名", $appConfigContent);
|
||||
$result = @file_put_contents($appConfigFilePath, $appConfigContent);
|
||||
if (!$result) $this->error(__('Configuration write failed: %s', [$this->filePath['appConfig']]));
|
||||
|
||||
// 建立API入口文件
|
||||
$oldBackendEntranceFile = Filesystem::fsFit(public_path() . $oldBackendEntrance . '.php');
|
||||
$newBackendEntranceFile = Filesystem::fsFit(public_path() . $newBackendEntrance . '.php');
|
||||
if (file_exists($oldBackendEntranceFile)) @unlink($oldBackendEntranceFile);
|
||||
|
||||
if ($newBackendEntrance != 'admin') {
|
||||
$backendEntranceStub = @file_get_contents(Filesystem::fsFit(root_path() . $this->filePath['backendEntranceStub']));
|
||||
if (!$backendEntranceStub) $this->error(__('Configuration write failed: %s', [$this->filePath['backendEntranceStub']]));
|
||||
|
||||
$result = @file_put_contents($newBackendEntranceFile, $backendEntranceStub);
|
||||
if (!$result) $this->error(__('Configuration write failed: %s', [$newBackendEntranceFile]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
// 模型验证
|
||||
if ($this->modelValidate) {
|
||||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
if (class_exists($validate)) {
|
||||
$validate = new $validate();
|
||||
if ($this->modelSceneValidate) $validate->scene('edit');
|
||||
$validate->check($data);
|
||||
}
|
||||
}
|
||||
$result = $this->model->saveAll($configValue);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success(__('The current page configuration item was updated successfully'));
|
||||
} else {
|
||||
$this->error(__('No rows updated'));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function add(): void
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->request->post();
|
||||
if (!$data) {
|
||||
$this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
|
||||
$data = $this->excludeFields($data);
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
// 模型验证
|
||||
if ($this->modelValidate) {
|
||||
$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
if (class_exists($validate)) {
|
||||
$validate = new $validate();
|
||||
if ($this->modelSceneValidate) $validate->scene('add');
|
||||
$validate->check($data);
|
||||
}
|
||||
}
|
||||
$result = $this->model->save($data);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success(__('Added successfully'));
|
||||
} else {
|
||||
$this->error(__('No rows were added'));
|
||||
}
|
||||
}
|
||||
|
||||
$this->error(__('Parameter error'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件测试
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function sendTestMail(): void
|
||||
{
|
||||
$data = $this->request->post();
|
||||
$mail = new Email();
|
||||
try {
|
||||
$mail->Host = $data['smtp_server'];
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = $data['smtp_user'];
|
||||
$mail->Password = $data['smtp_pass'];
|
||||
$mail->SMTPSecure = $data['smtp_verification'] == 'SSL' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
|
||||
$mail->Port = $data['smtp_port'];
|
||||
|
||||
$mail->setFrom($data['smtp_sender_mail'], $data['smtp_user']);
|
||||
|
||||
$mail->isSMTP();
|
||||
$mail->addAddress($data['testMail']);
|
||||
$mail->isHTML();
|
||||
$mail->setSubject(__('This is a test email') . '-' . get_sys_config('site_name'));
|
||||
$mail->Body = __('Congratulations, receiving this email means that your email service has been configured correctly');
|
||||
$mail->send();
|
||||
} catch (PHPMailerException) {
|
||||
$this->error($mail->ErrorInfo);
|
||||
}
|
||||
$this->success(__('Test mail sent successfully~'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user