Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
</p>

> 这是一个基于laravel8.x和dcatadmin的模块化练习项目
> 这是一个基于laravel8.x和dcatadmin的集成快速开发框架,包含api接口、后台管理等基础通用功能。

### 初始化操作
##### 1.加载依赖 <br />
Expand Down Expand Up @@ -53,7 +53,7 @@
**[propaganistas/laravel-phone](https://github.com/Propaganistas/Laravel-Phone)** <br />
**[overtrue/easy-sms](https://github.com/overtrue/easy-sms)** <br />
**[overtrue/laravel-socialite](https://github.com/overtrue/laravel-socialite)** <br />
开发后台阿里云短信、第三方登录开关按钮和配置基础注册登录三方登录api接口
开发后台阿里云短信、第三方登录开关按钮(开关请注意config/api_switch.php权限问题)和配置基础注册登录三方登录api接口



Expand Down
52 changes: 52 additions & 0 deletions app/Admin/Actions/Grid/TextActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Grid\Displayers\Actions;

/**
* 重写行操作按钮样式
*/
class TextActions extends Actions
{

/**
* @return string
*/
protected function getViewLabel()
{
$label = trans('admin.show');
return '<button class="btn btn-sm btn-success"><i class="feather icon-eye"></i> ' . $label . '</button> &emsp;';
}

/**
* @return string
*/
protected function getEditLabel()
{
$label = trans('admin.edit');

return '<button class="btn btn-sm btn-custom"><i class="feather icon-edit-1"></i> ' . $label . '</button> &emsp;';
}

/**
* @return string
*/
protected function getQuickEditLabel()
{
$label = trans('admin.edit');
$label2 = trans('admin.quick_edit');

return '<button class="btn btn-sm btn-custom" title="' . $label2 . '"><i class="feather icon-edit-1"></i> ' . $label . '</button> &emsp;';
}

/**
* @return string
*/
protected function getDeleteLabel()
{
$label = trans('admin.delete');

return '<button class="btn btn-sm btn-danger"><i class="feather icon-alert-triangle"></i> ' . $label . '</button> &emsp;';
}
}
103 changes: 97 additions & 6 deletions app/Admin/Controllers/ApiSwitchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,80 @@

class ApiSwitchController extends AdminController
{

public function index(Content $content)
{
Admin::script($this->javascript());
return $content
->header('API 功能开关')
->description('管理 API 功能的开启和关闭')
->body($this->form());
}
protected function javascript()
{
return <<<JS
//清理缓存
$('a[data-action="clear-cache"]').on('click', function () {
$.ajax({
url: '/admin/api-switches/clear-cache',
type: 'POST',
success: function (data) {
Dcat.success('配置缓存已清除');
},
error: function (a, b, c) {
Dcat.error('清除缓存失败');
}
});
});
//保存缓存配置
$('a[data-action="cache-config"]').on('click', function () {
$.ajax({
url: '/admin/api-switches/cache-config',
type: 'POST',
success: function (data) {
Dcat.success('配置缓存已保存');
},
error: function (a, b, c) {
Dcat.error('保存缓存失败');
}
});
});
// 开关动态提交
$(document).ready(function () {
// 选择正确的开关组件,并监听其变化
$('input[type="checkbox"]').change(function () {
// 由于使用了Switchery,需要确认是否通过UI反映了改变
var isChecked = $(this).prop('checked');

// 序列化包含此开关的表单数据
var formData = $(this).closest('form').serialize();

// 发送 AJAX 请求
$.ajax({
type: 'POST',
url: $(this).closest('form').attr('action'),
data: formData,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') // 确保 CSRF token 被包含
},
success: function () {
Dcat.success('设置已成功更新');
},
error: function () {
Dcat.error('更新失败,请重试');
}
});
});
});

JS;
}

protected function form()
{
// 使用null作为Form::make的第一个参数
$form = Form::make(null, function (Form $form) {
$form->title('<span style="color: #FF0000">注:如开关状态修改失败,请点击右侧清理缓存后再次尝试!</span>');
$config = config('api_switch');

foreach ($config as $key => $enabled) {
Expand All @@ -34,7 +96,7 @@ protected function form()
$helpText = '关闭后api获取验证码为默认1234并且不会发送短信';
break;
case '微信登陆':
$helpText = '关闭后则不允许微信第三方登录';
$helpText = '关闭后则不允许微信登录';
break;
}
// 直接使用switch组件
Expand All @@ -48,25 +110,32 @@ protected function form()

// 隐藏继续创建按钮
$form->disableCreatingCheck();

// 隐藏查看按钮
$form->disableViewCheck();

$form->disableHeader();

// $form->disableHeader();
// 隐藏顶部的列表按钮(返回按钮)
$form->disableListButton();
//隐藏提交按钮
$form->disableSubmitButton();

// 设置表单提交的路由
$form->action(admin_url('api-switches'));
// 添加自定义按钮
$form->tools(function (Form\Tools $tools) {
$tools->append('<a class="btn btn-danger" data-action="clear-cache" style="margin-right:10px;color: #EEEEEE;font-weight: bold;">清除配置缓存</a>');
$tools->append('<a class="btn btn-success" data-action="cache-config" style="color: #EEEEEE;font-weight: bold;">保存配置缓存</a>');
});
});


return $form;
}




// 重命名方法以避免潜在的冲突
public function save(Request $request)
protected function save(Request $request)
{
$data = $request->except(['_token', '_method', '_previous_']);
$configData = "<?php\n\nreturn " . var_export($data, true) . ";\n";
Expand All @@ -79,4 +148,26 @@ public function save(Request $request)

return JsonResponse::make()->success('操作成功');
}

//清理缓存配置
protected function clearConfigCache()
{
try {
Artisan::call('config:clear');
return JsonResponse::make()->success('配置缓存已清除');
} catch (\Exception $e) {
return JsonResponse::make()->error('清除缓存失败: '.$e->getMessage());
}
}

//保存缓存配置
protected function cacheConfig()
{
try {
Artisan::call('config:cache');
return JsonResponse::make()->success('配置缓存已保存');
} catch (\Exception $e) {
return JsonResponse::make()->error('保存缓存失败: '.$e->getMessage());
}
}
}
2 changes: 2 additions & 0 deletions app/Admin/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
$router->get('/', 'HomeController@index');
$router->get('/api-switches', [ApiSwitchController::class,'index']);
$router->post('/api-switches', [ApiSwitchController::class,'save']);
$router->post('/api-switches/clear-cache', [ApiSwitchController::class,'clearConfigCache'])->name('api-switches.clear-cache');
$router->post('/api-switches/cache-config', [ApiSwitchController::class,'cacheConfig'])->name('api-switches.cache-config');



Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"laravel/tinker": "^2.5",
"overtrue/easy-sms": "^2.6",
"overtrue/laravel-lang": "^5.0",
"overtrue/laravel-socialite": "^2.1",
"overtrue/laravel-socialite": "3.0",
"propaganistas/laravel-phone": "^4.4",
"tymon/jwt-auth": "^1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion config/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
'grid' => [

// The global Grid action display class.
'grid_action_class' => Dcat\Admin\Grid\Displayers\DropdownActions::class,
'grid_action_class' => App\Admin\Actions\Grid\TextActions::class,

// The global Grid batch action display class.
'batch_action_class' => Dcat\Admin\Grid\Tools\BatchActions::class,
Expand Down