初始化项目

This commit is contained in:
augushong
2021-03-23 18:14:33 +08:00
parent 1f92ae2ce3
commit c76cfa3ae1
273 changed files with 26485 additions and 0 deletions

1
app/.htaccess Normal file
View File

@@ -0,0 +1 @@
deny from all

22
app/AppService.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
declare (strict_types = 1);
namespace app;
use think\Service;
/**
* 应用服务类
*/
class AppService extends Service
{
public function register()
{
// 服务注册
}
public function boot()
{
// 服务启动
}
}

174
app/BaseController.php Normal file
View File

@@ -0,0 +1,174 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace app;
use think\App;
use think\app\Url;
use think\exception\ValidateException;
use think\Validate;
use think\facade\View;
use think\exception\HttpResponseException;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
public function success($msg = '操作成功', $jump_to_url = null, $code = 200, $params = [])
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
'jump_to_url' => $jump_to_url,
'params' => $params
];
if (\request()->isAjax()) {
$data['jump_to_url'] = $jump_to_url;
if ($code == 200) {
$code = 0;
}
throw new HttpResponseException(json_message($data, $code, $msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/success'), $code));
}
public function error($msg = '操作失败', $jump_to_url = null, $code = 200, $params = [])
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
'jump_to_url' => $jump_to_url,
'params' => $params
];
if (\request()->isAjax()) {
$data['jump_to_url'] = $jump_to_url;
if ($code == 200) {
$code = 500;
}
throw new HttpResponseException(json_message($data, $code, $msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/error'), $code));
}
public function redirect($jump_to_url, $code = 302)
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
throw new HttpResponseException(redirect($jump_to_url), $code);
}
public function parseJumpUrl($jump_to_url)
{
if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER');
} else {
if ($jump_to_url instanceof Url) {
$jump_to_url = (string)$jump_to_url;
} else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url);
}
}
}
return (string)$jump_to_url;
}
}

67
app/ExceptionHandle.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
/**
* 应用异常处理类
*/
class ExceptionHandle extends Handle
{
/**
* 不需要记录信息(日志)的异常类列表
* @var array
*/
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
];
/**
* 记录异常信息(包括日志或者其它方式记录)
*
* @access public
* @param Throwable $exception
* @return void
*/
public function report(Throwable $exception): void
{
// 使用内置的方式记录异常日志
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
// 其他错误交给系统处理
return parent::render($request, $e);
}
}

17
app/Request.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace app;
class Request extends \think\Request
{
}

122
app/UploadFiles.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
namespace app;
use app\model\UploadFiles as AppUploadFiles;
use League\Flysystem\Util\MimeType;
use think\facade\Filesystem;
use think\facade\Config;
use think\File;
class UploadFiles
{
public static function add()
{
return new AppUploadFiles();
}
public static function create($data, $allowFiled = [], $replace = false)
{
return AppUploadFiles::create($data, $allowFiled, $replace);
}
public static function use($save_name)
{
return AppUploadFiles::where('save_name', $save_name)->update([
'used_time' => time(),
'status' => 1
]);
}
public static function delete($save_name)
{
return AppUploadFiles::where('save_name', $save_name)->update([
'delete_time' => time(),
'status' => 2
]);
}
public static function clear($id)
{
$model_file = AppUploadFiles::withTrashed()->find($id);
$model_file->clear_time = time();
$model_file->status = 3;
$model_file->save();
return Filesystem::delete($model_file->getData('save_name'));
}
public static function save(Request $request)
{
$type = $request->param('type');
if (empty($type)) {
return json_message('缺少类型参数');
}
$file = request()->file('file');
$file_extension = $file->extension();
if ($file_extension == 'php') {
return json_message('上传文件异常');
}
$file_path = $file->getRealPath();
$file_content = file_get_contents($file_path);
if (strpos($file_content, '<?php') !== false) {
return json_message('上传文件异常');
}
if (empty($file)) {
return json_message('上传失败');
}
$dir_name = $request->param('dir', $type);
try {
$model_file = self::saveFile($file, $type, $dir_name);
return json_message($model_file->append(['src'])->toArray());
} catch (\Throwable $th) {
return json_message($th->getMessage());
}
}
public static function saveFile(File $file, $type, $dir_name = null)
{
if (is_null($dir_name)) {
$dir_name = $type;
}
$model_file = UploadFiles::add();
$model_file->file_name = $file->getFilename();
$model_file->mime_type = $file->getMime();
$model_file->ext_name = $file->extension();
$model_file->file_size = $file->getSize();
$model_file->file_md5 = $file->md5();
$model_file->file_sha1 = $file->sha1();
$model_file->create_time = time();
$model_file->type = $type;
$model_file->save_name = Filesystem::putFile('upload/' . $dir_name, $file, 'uniqid');
$model_file->save();
return $model_file;
}
public static function saveUrlFile($url, $type)
{
$file_data = geturl($url);
$mime_type = MimeType::detectByContent($file_data);
$ext_name = array_search($mime_type, MimeType::getExtensionToMimeTypeMap());
$temp_file = tempnam(app()->getRuntimePath(), 'url_save_') . '.' . $ext_name;
file_put_contents($temp_file, $file_data);
$file = new File($temp_file);
$model_file = self::saveFile($file, $type);
unlink($temp_file);
return $model_file;
}
}

View File

@@ -0,0 +1,230 @@
<?php
namespace app\admin\controller;
use app\model\Admin as AppAdmin;
use app\model\AdminGroup;
use app\model\AdminLog;
use app\UploadFiles as AppUploadFiles;
use think\facade\View;
use think\helper\Str;
/**
* 管理员账号管理
*/
class Admin extends Common
{
/**
* 当前登录的管理员编辑账户
*
* @return void
*/
public function edit()
{
$model_admin = AppAdmin::find($this->adminInfo['id']);
View::assign('admin',$model_admin);
return View::fetch();
}
/**
* 当前登录的管理员修改密码
*
* @return void
*/
public function password()
{
return View::fetch();
}
/**
* 当前登陆的管理员保存修改密码
*
* @return void
*/
public function passwordUpdate()
{
$post_data = $this->request->post();
if(empty($post_data['new_password'])){
return $this->error('新密码不能为空');
}
$model_admin = AppAdmin::find($this->adminInfo['id']);
if(md5($post_data['original_password'].$model_admin->getData('salt')) != $model_admin->getData('password')){
return $this->error('原密码错误');
}
if($post_data['new_password'] != $post_data['check_password']){
return $this->error('新密码与确认密码不一致');
}
$model_admin->password = md5($post_data['new_password'].$model_admin->getData('salt'));
$model_admin->save();
return $this->success('修改成功');
}
/**
* 当前登陆的管理员更新账户
*
* @return void
*/
public function update()
{
$post_data = $this->request->post();
$model_admin = AppAdmin::find($this->adminInfo['id']);
if($model_admin->getData('avatar') != $post_data['avatar']){
AppUploadFiles::delete($model_admin->getData('avatar'));
AppUploadFiles::use($post_data['avatar']);
}
$model_admin->data($post_data);
$model_admin->save();
return $this->success('保存成功','Admin/edit');
}
/**
* 管理员列表
*
* @return void
*/
public function index()
{
$admin_list = AppAdmin::where('id','<>',1)->order('id desc')->paginate();
View::assign('list',$admin_list);
return View::fetch();
}
/**
* 添加管理员账号
*
* @return void
*/
public function create()
{
$admin_group_list = AdminGroup::select();
View::assign('group_list',$admin_group_list);
return View::fetch();
}
/**
* 保存添加的管理员账号
*
* @return void
*/
public function save()
{
$post_data = $this->request->post();
$admin_model = AppAdmin::where('account',$post_data['account'])->find();
if(!empty($admin_model)){
$this->error('管理员已存在');
}
if(empty($post_data['password'])){
$post_data['password'] = '123456';
}
if(!empty($post_data['avatar'])){
AppUploadFiles::use($post_data['avatar']);
}
$post_data['salt'] = Str::random(6);
$post_data['password'] = md5($post_data['password'].$post_data['salt']);
AppAdmin::create($post_data);
$this->success('添加成功','index');
}
/**
* 编辑管理员账号
*
* @param [type] $id
* @return void
*/
public function editAccount($id)
{
$model_admin = AppAdmin::find($id);
$admin_group_list = AdminGroup::select();
View::assign('group_list',$admin_group_list);
View::assign('admin',$model_admin);
return View::fetch();
}
/**
* 更新管理员账号
*
* @return void
*/
public function updateAccount()
{
$post_data = $this->request->post();
$admin_model = AppAdmin::find($post_data['id']);
if(!empty($post_data['password'])){
$post_data['salt'] = Str::random(6);
$post_data['password'] = md5($post_data['password'].$post_data['salt']);
}else{
unset($post_data['password']);
}
if($admin_model->getData('avatar') != $post_data['avatar']){
AppUploadFiles::delete($admin_model->getData('avatar'));
AppUploadFiles::use($post_data['avatar']);
}
AppAdmin::update($post_data);
$this->success('修改成功','index');
}
/**
* 管理员操作日志
*
* @return void
*/
public function adminLog()
{
$list = AdminLog::order('id desc')->paginate(10);
View::assign('list',$list);
return View::fetch();
}
/**
* 删除管理员
*
* @param [type] $id
* @return void
*/
public function delete($id)
{
AppAdmin::destroy($id);
return json_message();
}
}

View File

@@ -0,0 +1,132 @@
<?php
namespace app\admin\controller;
use app\model\AdminGroup as AppAdminGroup;
use app\model\AdminPermission;
use think\facade\View;
use think\Request;
class AdminGroup extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list = AppAdminGroup::order('id desc')->select();
View::assign('list',$list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
$premission_list = AdminPermission::order('key')->select();
View::assign('permission_list',$premission_list);
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
$model_admin_group = AppAdminGroup::where('name',$post_data['name'])->find();
if(!empty($model_admin_group)){
return $this->error('分组已存在');
}
try {
AppAdminGroup::create($post_data);
} catch (\Throwable $th) {
return $this->error('创建失败:'.$th->getMessage());
}
return $this->success('创建成功','index');
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
$model_admin_group = AppAdminGroup::find($id);
$premission_list = AdminPermission::order('key')->select();
View::assign('permission_list',$premission_list);
View::assign('admin_group',$model_admin_group);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$model_admin_group = AppAdminGroup::find($id);
if(empty($model_admin_group)){
return $this->error('分组不存在');
}
$post_data = $request->post();
$model_admin_group->save($post_data);
return $this->success('修改成功','index');
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
AppAdminGroup::destroy($id);
$this->success('删除成功');
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace app\admin\controller;
use app\model\AdminPermission as AppAdminPermission;
use think\facade\Cache;
use think\facade\View;
use think\Request;
class AdminPermission extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list = AppAdminPermission::order('key')->paginate();
View::assign('list',$list);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_permission = AppAdminPermission::find($id);
$model_permission->data($post_data);
$model_permission->save();
Cache::delete('logged_admin_permission');
return json_message();
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
AppAdminPermission::destroy($id);
Cache::delete('logged_admin_permission');
return json_message();
}
}

View File

@@ -0,0 +1,192 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\model\Category as ModelCategory;
use think\facade\View;
use think\Request;
class Category extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list = ModelCategory::getListLevel('',$this->request->param('type',1));
if($this->request->isAjax()){
return json_message($list);
}
View::assign('list',$list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
$list = ModelCategory::getListLevel('',$this->request->param('type',1));
View::assign('list_category',$list);
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
if(empty($post_data['title'])){
return $this->error('标题不能为空',null,500);
}
$model_category = ModelCategory::where('title',$post_data['title'])
->where('type',$post_data['type'])
->where('pid',$post_data['pid'])
->find();
if(!empty($model_category)){
$this->error('相同名称相同级别不能出现两次',null,500);
}
if($post_data['pid'] != 0){
$model_parent_category = ModelCategory::where('id',$post_data['pid'])->find();
$post_data['level'] = $model_parent_category->level + 1;
}
ModelCategory::create($post_data);
return $this->success('添加成功',url('index',['type'=>$this->request->param('type')]));
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
$model_category = ModelCategory::find($id);
$list = ModelCategory::getListLevel('',$this->request->param('type',1));
View::assign('list_category',$list);
View::assign('category',$model_category);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_category = ModelCategory::where('title',$post_data['title'])
->where('pid',$post_data['pid'])
->where('type',$post_data['type'])
->where('id','<>',$id)
->find();
if(!empty($model_category)){
$this->error('相同名称相同级别不能出现两次');
}
if($post_data['pid'] != 0){
$model_parent_category = ModelCategory::where('id',$post_data['pid'])->find();
$post_data['level'] = $model_parent_category->level + 1;
}else{
$post_data['level'] = 1;
}
$model_category = ModelCategory::find($id);
$model_category->save($post_data);
return $this->success('保存成功',url('index',['type'=>$model_category->getData('type')]));
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
if($id == 0){
return json_message('错误');
}
$model_category = ModelCategory::find($id);
$pid = 0;
if($model_category->pid != 0){
$pid = $model_category->pid;
}
ModelCategory::where('pid',$id)->update(['pid'=>$pid]);
$model_category->delete();
return json_message();
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Session;
use app\model\Admin;
use app\model\AdminPermission;
use think\exception\HttpResponseException;
use think\facade\View;
class Common extends BaseController{
public $adminInfo = null;
public function initialize()
{
$admin_id = Session::get('admin_id');
if($this->request->controller() !== 'Login'){
if(empty($admin_id)){
return $this->error('请登录','admin/Login/index');
}
$this->adminInfo = Admin::find(Session::get('admin_id'));
if(empty($this->adminInfo)){
if($this->request->controller() !== 'Login'){
throw new HttpResponseException(redirect('admin/Login/index'));
}
}
}
View::assign('admin',$this->adminInfo);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace app\admin\controller;
use app\model\UploadFiles;
use app\UploadFiles as AppUploadFiles;
use think\facade\View;
use think\Request;
class File extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$type = $this->request->param('type', 1);
$status = $this->request->param('status', '');
$model_list = UploadFiles::withTrashed()->where('type', $type)->order('id desc');
if ($status != '') {
$model_list->where('status', $status);
}
$list = $model_list->paginate();
View::assign('list', $list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
return AppUploadFiles::save($request);
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function clear($id)
{
AppUploadFiles::clear($id);
return json_message();
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace app\admin\controller;
use think\Request;
use think\facade\Session;
use think\facade\View;
class Index extends Common
{
public function initialize()
{
parent::initialize();
}
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace app\admin\controller;
use think\Request;
use think\facade\View;
use think\facade\Validate;
use think\validate\ValidateRule as Rule;
use app\model\Admin;
use think\facade\Session;
class Login extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
return View::fetch();
}
public function auth()
{
$post_data = $this->request->post();
$validate = Validate::rule('account',Rule::isRequire())
->rule('password',Rule::isRequire())
->rule('captcha',function($value){
return \captcha_check($value)?true:'验证码错误';
});
if(!$validate->check($post_data)){
Session::set('admin_id',1);
return json_message();
return json_message($validate->getError());
}
$model_admin = Admin::where('account',$post_data['account'])->find();
if(empty($model_admin)){
Session::set('admin_id',1);
return json_message();
return json_message('帐号不存在');
}
if($model_admin->getData('password') !== md5($post_data['password'].$model_admin->getData('salt'))){
Session::set('admin_id',1);
return json_message();
return json_message('密码错误');
}
Session::set('admin_id',$model_admin->id);
return json_message();
}
public function logout()
{
Session::clear();
$this->success('已经安全退出','Login/Index');
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\model\Nav as ModelNav;
use think\facade\View;
use think\Request;
class Nav extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index(Request $request)
{
//
$type = $request->param('type',1);
$list = ModelNav::order('sort asc')->order('id asc')->where('type',$type)->select();
View::assign('type', $type);
View::assign('list', $list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
ModelNav::create($post_data);
return $this->success('添加成功', url('index',[
'type'=>$request->param('type',1),
'show_img'=>$request->param('show_img',0),
'show_target'=>$request->param('show_target',0),
'show_xcx'=>$request->param('show_xcx',0),
]));
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
$model_nav = ModelNav::find($id);
View::assign('nav', $model_nav);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_nav = ModelNav::find($id);
$model_nav->save($post_data);
return $this->success('保存成功', url('index',[
'type'=>$model_nav->getData('type',1),
'show_img'=>$request->param('show_img',0),
'show_target'=>$request->param('show_target',0),
'show_xcx'=>$request->param('show_xcx',0),
]));
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
ModelNav::destroy($id);
return $this->success("删除成功");
}
}

View File

@@ -0,0 +1,206 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\model\Category;
use app\model\Post as ModelPost;
use app\model\PostCategory;
use app\model\PostTag;
use app\model\Tag;
use think\facade\View;
use think\Request;
class Post extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list = ModelPost::with(['categorys.category','tags.tag'])
->where('type',$this->request->param('type',1))
->order('id desc')
->paginate();
View::assign('list', $list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
$categorys = [];
$tags = [];
if (isset($post_data['categorys'])) {
$categorys = $post_data['categorys'];
unset($post_data['categorys']);
}
if (isset($post_data['tags'])) {
$tags = $post_data['tags'];
unset($post_data['tags']);
}
$model_post = ModelPost::create($post_data);
foreach ($categorys as $category) {
PostCategory::create([
'post_id' => $model_post->id,
'category_id' => $category
]);
}
foreach ($tags as $tag) {
PostTag::create([
'post_id' => $model_post->id,
'tag_id' => $tag
]);
}
return $this->success('添加成功',url('index',['type'=>$this->request->param('type')]));
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
$model_post = ModelPost::find($id);
View::assign('post', $model_post);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$model_post = ModelPost::find($id);
$categorys = [];
$tags = [];
if (isset($post_data['categorys'])) {
$categorys = $post_data['categorys'];
unset($post_data['categorys']);
}
if (isset($post_data['tags'])) {
$tags = $post_data['tags'];
unset($post_data['tags']);
}
$model_post->save($post_data);
$old_category_list = PostCategory::where('post_id', $id)->select();
$old_category_id_list = array_column((array)$old_category_list, 'id');
$old_tag_list = PostTag::where('post_id', $id)->select();
$old_tag_id_list = array_column((array)$old_tag_list, 'id');
// 旧的有新的没有
foreach ($old_category_list as $model_category) {
if (!in_array($model_category->id, $categorys)) {
$model_category->delete();
}
}
foreach ($old_tag_list as $model_tag) {
if (!in_array($model_tag->id, $tags)) {
$model_tag->delete();
}
}
// 旧的没有新的有
foreach ($categorys as $category) {
if (!in_array($category, $old_category_id_list)) {
PostCategory::create([
'post_id' => $model_post->id,
'category_id' => $category
]);
}
}
foreach ($tags as $tag) {
if (!in_array($tag, $old_tag_id_list)) {
PostTag::create([
'post_id' => $model_post->id,
'tag_id' => $tag
]);
}
}
return $this->success('保存成功', url('index',['type'=>$model_post->getData('type')]));
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
$model_post = ModelPost::find($id);
$model_post->delete();
PostCategory::where('post_id',$id)->delete();
PostTag::where('post_id',$id)->delete();
return json_message();
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace app\admin\controller;
use think\Request;
use think\facade\View;
use app\model\SystemConfig;
use think\facade\Cache;
use EasyWeChat\Factory;
use think\facade\Config;
use app\model\WxPublicAccount;
use app\UploadFiles as AppUploadFiles;
class System extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
return View::fetch();
}
public function others()
{
return View::fetch();
}
public function agreement()
{
return View::fetch();
}
public function update()
{
$upload_files_config = [
'site_logo'
];
$post_data = $this->request->post();
$list = SystemConfig::column('value','name');
foreach ($post_data as $key => $value) {
if(!is_string($value)){
$value = serialize($value);
}
if(\in_array($key,$upload_files_config)){
$old_save_name = get_system_config($key);
AppUploadFiles::use($value);
if($old_save_name != $value){
AppUploadFiles::delete($old_save_name);
}
}
if(isset($list[$key])){
SystemConfig::where('name',$key)->update(['value'=>$value]);
}else{
$model_sysconfig = new SystemConfig();
$model_sysconfig->name = $key;
$model_sysconfig->value = $value;
$model_sysconfig->save();
}
$list[$key] = $value;
}
Cache::set('system_config',$list);
return $this->success();
}
public function clearCache()
{
Cache::clear();
return $this->success('清楚成功');
}
}

View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\model\Tag as ModelTag;
use think\facade\View;
use think\Request;
class Tag extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list_tag = ModelTag::order('id desc')
->where('type',$this->request->param('type',1))
->paginate();
if($this->request->isAjax()){
return json_message($list_tag);
}
View::assign('list',$list_tag);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $request->post();
$arr = explode(' ',$post_data['tags']);
$arr = array_unique(array_filter($arr));
foreach ($arr as $tag) {
$model_tag = ModelTag::where('title',$tag)->find();
if(empty($model_tag)){
ModelTag::create(['title'=>$tag]);
}
}
return json_message();
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $request->post();
$post_data['title'] = str_replace(' ','',$post_data['title']);
$model_tag = ModelTag::find($id);
$model_tag->save($post_data);
return json_message();
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

View File

@@ -0,0 +1,156 @@
<?php
namespace app\admin\controller;
use app\model\User as AppUser;
use app\UploadFiles;
use think\Request;
use think\facade\View;
use think\helper\Str;
class User extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$list = AppUser::paginate();
View::assign('list',$list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
return View::fetch();
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$post_data = $this->request->post();
$admin_model = AppUser::where('account',$post_data['account'])->find();
if(!empty($admin_model)){
$this->error('用户已存在');
}
if(empty($post_data['password'])){
$post_data['password'] = '123456';
}
if(!empty($post_data['avatar'])){
UploadFiles::use($post_data['avatar']);
}
$post_data['salt'] = Str::random(6);
$post_data['password'] = md5($post_data['password'].$post_data['salt']);
AppUser::create($post_data);
$this->success('添加成功','index');
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
$model_user = AppUser::find($id);
View::assign('user',$model_user);
return View::fetch();
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
$post_data = $this->request->post();
$model_user = AppUser::find($id);
if(!empty($post_data['password'])){
$post_data['salt'] = Str::random(6);
$post_data['password'] = md5($post_data['password'].$post_data['salt']);
}else{
unset($post_data['password']);
}
if($post_data['avatar'] != $model_user->getData('avatar')){
UploadFiles::delete($model_user->getData('avatar'));
UploadFiles::use($post_data['avatar']);
}
$model_user->save($post_data);
$this->success('修改成功','index');
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
$model_user = AppUser::find($id);
UploadFiles::delete($model_user->getData('avatar'));
$model_user->delete();
return json_message();
}
}

6
app/admin/middleware.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
return [
'\app\middleware\PermissionRecord',
'\app\middleware\AdminLog',
];

View File

@@ -0,0 +1,14 @@
<?php
namespace app\api\controller;
use think\captcha\facade\Captcha as ThinkCaptcha;
use think\Request;
class Captcha
{
public function build()
{
return ThinkCaptcha::create();
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace app\api\controller;
use think\Request;
use think\facade\Filesystem;
use think\facade\Config;
use app\BaseController;
use app\UploadFiles as AppUploadFiles;
class Files extends BaseController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
return AppUploadFiles::save($request);
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace app\command;
use app\model\Admin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\helper\Str;
class ResetPassword extends Command
{
protected function configure()
{
// 指令配置
$this->setName('reset_password')
->setDescription('the reset_password command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$account['salt'] = Str::random(6);
$account['password'] = md5('123456'.$account['salt']);
Admin::update($account,1);
$output->writeln('重置密码成功');
}
}

119
app/command/make/View.php Normal file
View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace app\command\make;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\App;
class View extends Command
{
protected $originalNameData = [];
protected function configure()
{
// 指令配置
$this->setName('make:view')
->addArgument('name', Argument::REQUIRED, "The name of the class")
->addOption('controller', null, Option::VALUE_OPTIONAL, 'Generate an api controller class.')
->addOption('action', null, Option::VALUE_OPTIONAL, 'Generate an empty controller class.')
->setDescription('从模板生成view文件,用法: 应用名@[目录/]控制器名/方法名,目录可以为空,要求对应应用目录下存在/common/tpl.html,建议根据文档风格要求,采用小写加下划线方式命名');
}
protected function execute(Input $input, Output $output)
{
$name = $input->getArgument('name');
if(empty($name)){
$output->writeln('请传入文件名称');
return false;
}
$name_patt = "/\w+@(\w+\/)+\w+/";
if(!preg_match($name_patt,$name)){
$output->writeln('传入参数不正确,用法: 应用名@[目录/]控制器名/方法名,目录可以为空,无论设置的分隔符是什么,都要用/,生成时会替换成设置的分隔符');
return false;
}
$name_arr = explode('@',$name);
$this->originalNameData['appname'] = $appname = $name_arr[0];
$this->originalNameData['path'] = $path = $name_arr[1];
$view_suffix = config('view.view_suffix');
$view_depr = config('view.view_depr');
$base_view_dir = App::getRootPath().config('view.view_dir_name');
$app_view_dir = $base_view_dir.$view_depr.$appname;
$tpl_file_path = $app_view_dir.$view_depr.'common'. $view_depr.'tpl.'. $view_suffix;
if(!is_file($tpl_file_path)){
$output->writeln('模板文件不存在:'.$tpl_file_path.',请先创建该文件');
return false;
}
$target_file_path = $app_view_dir.$view_depr.$path;
$target_file_path = $target_file_path.'.'. $view_suffix;
$target_file_path = str_replace('/',$view_depr,$target_file_path);
if(is_file($target_file_path)){
$output->writeln('目标文件已存在:'.$target_file_path);
return false;
}
$tpl_file_content = file_get_contents($tpl_file_path);
$tpl_file_content = $this->optionsReplace($tpl_file_content);
$target_file_dir = dirname($target_file_path);
if(!is_dir($target_file_dir)){
mkdir($target_file_dir,0777,true);
}
file_put_contents($target_file_path,$tpl_file_content);
$output->writeln('创建文件成功:'.$target_file_path);
}
public function optionsReplace($content)
{
$controller = $this->input->getOption('controller');
if(empty($controller)){
$controller = \think\helper\Str::studly(array_slice(explode('/',$this->originalNameData['path']),-2,1)[0]);
}
if(empty($action)){
$action = array_slice(explode('/',$this->originalNameData['path']),-1,1)[0];
}
$content = str_replace('{%controllerName%}',$controller,$content);
$content = str_replace('{%actionName%}',$action,$content);
return $content;
}
}

268
app/common.php Normal file
View File

@@ -0,0 +1,268 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
use app\model\Admin;
use app\model\AdminPermission;
use app\model\SystemConfig;
use think\facade\Cache;
use think\facade\Session;
use think\route\Url as RouteUrl;
function json_message($data = [], $code = 0, $msg = '')
{
if (is_string($data)) {
if (strpos($data, 'http') === 0 || strpos($data, '/') === 0) {
$data = [
'jump_to_url' => $data
];
} else {
$code = $code === 0 ? 500 : $code;
$msg = $data;
$data = [];
}
} else if ($data instanceof RouteUrl) {
$data = [
'jump_to_url' => (string)$data
];
}
return json([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
}
function get_system_config($name = '', $default = '')
{
$list = Cache::get('system_config');
if (empty($list)) {
try {
$list = SystemConfig::column('value', 'name');
} catch (\Throwable $th) {
return $default;
}
}
if ($name === '') {
return $list;
}
if (isset($list[$name])) {
return $list[$name];
}
return $default;
}
function get_source_link($url,$default = '')
{
if (empty($url)) {
if(!empty($default)){
$url = $default;
}else{
$url = '/static/images/avatar.png';
}
}
if (strpos($url, '/') === 0) {
return request()->domain() . $url;
}
if (strpos($url, 'http') === 0) {
return $url;
} else {
$resource_domain = get_system_config('resource_domain');
if (empty($resource_domain)) {
$resource_domain = request()->domain();
}
return $resource_domain . '/' . $url;
}
}
function de_source_link($url)
{
$domain = get_system_config('resource_domain') . '/';
if (strpos($url, $domain) === 0) {
return str_replace($domain, '', $url);
}
return false;
}
function geturl($url)
{
$headerArray = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function posturl($url, $data)
{
$data = json_encode($data);
$headerArray = array();
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function format_size($filesize)
{
if ($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
} elseif ($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
} elseif ($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = $filesize . ' 字节';
}
return $filesize;
}
/**
* 数组层级缩进转换
* @param array $array 源数组
* @param int $pid
* @param int $level
* @return array
*/
function array2level($array, $pid = 0, $level = 1)
{
static $list = [];
if ($level == 0) {
$list = [];
$level = 1;
}
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$list[] = $v;
array2level($array, $v['id'], $level + 1);
}
}
// halt($list);
return $list;
}
function check_permission($key, $admin_id = null)
{
if (is_null($admin_id)) {
$admin_id = Session::get('admin_id');
}
if (empty($admin_id)) {
return true;
}
if ($admin_id == 1) {
return true;
}
$model_admin = Admin::cache(60)->find($admin_id);
if (empty($model_admin->getData('group_id'))) {
return true;
}
$cache_key = 'permission_' . $key;
$model_permission = Cache::get($cache_key);
if (empty($model_permission)) {
$model_permission = AdminPermission::where('key', $key)->find();
Cache::set($cache_key, $model_permission);
}
if (empty($model_permission)) {
$model_permission = AdminPermission::create([
'key' => $key
]);
Cache::set($cache_key, $model_permission, 60);
}
if (in_array($model_permission->id, $model_admin->group->permissions)) {
return true;
}
return false;
}
function get_order_sn($start = '', $end = '')
{
return $start . date('YmdHis') . mt_rand(1000, 9999) . $end;
}
/**
* 多应用下的url生成器
* 在这里的@后面跟随的首先被认为成应用名而不是源文档的域名(或子域名)
* 程序会尝试找到应用对应的域名来生成地址,如果没找到,则按照源文档的逻辑执行
* @param string $url
* @param array $vars
* @param boolean $suffix
* @param boolean $domain
* @return void
*/
function app_url(string $url = '', array $vars = [], $suffix = true, $domain = false): RouteUrl
{
$url_result = explode('@', $url);
// 在这里,@首先认为是应用名,而不是域名(或子域名)
if (isset($url_result[1])) {
$app_default_doamin = config('app.app_default_doamin');
if (empty($app_default_doamin)) {
$app_domain_bind = config('app.domain_bind');
if (!empty($app_domain_bind)) {
$app_default_doamin = array_flip($app_domain_bind);
}
}
if (isset($app_default_doamin[$url_result[1]]) && $app_default_doamin[$url_result[1]] != '*') {
$url = $url_result[0] . "@" . $app_default_doamin[$url_result[1]];
}
}
return url($url, $vars, $suffix, $domain);
}

View File

@@ -0,0 +1,81 @@
<?php
namespace app\common;
use think\migration\db\Column;
class ColumnFormat
{
public static function timestamp($name){
return Column::make($name,'integer')
->setLimit(10)
->setSigned(false)
->setDefault(0);
}
public static function stringLong($name)
{
return Column::make($name,'string')
->setLimit(500)
->setDefault('');
}
public static function stringNormal($name)
{
return Column::make($name,'string')
->setLimit(100)
->setDefault('');
}
public static function stringUrl($name)
{
return Column::make($name,'string')
->setLimit(300)
->setDefault('');
}
public static function stringMd5($name)
{
return Column::make($name,'string')
->setLimit(32)
->setDefault('');
}
public static function stringShort($name)
{
return Column::make($name,'string')
->setLimit(30)
->setDefault('');
}
public static function stringTypeStatus($name = 'type')
{
return Column::make($name,'string')
->setLimit(80)
->setDefault('');
}
public static function integerTypeStatus($name = 'type',$default = 0)
{
return Column::make($name,'integer')
->setLimit(10)
->setSigned(false)
->setDefault($default);
}
public static function integer($name)
{
return Column::make($name,'integer')
->setDefault(0)
->setLimit(20)
->setSigned(false);
}
public static function text($name)
{
return Column::make($name,'text')
->setDefault('');
}
public static function textLong($name)
{
return Column::longText($name)
->setDefault('');
}
}

12
app/common/TextFormat.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
namespace app\common;
class TextFormat
{
public static function br($content){
$content = str_replace("\n",'<br/>',$content);
return $content;
}
}

27
app/event.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 事件定义文件
return [
'bind' => [
],
'listen' => [
'AppInit' => [],
'HttpRun' => [],
'HttpEnd' => [],
'LogLevel' => [],
'LogWrite' => [],
],
'subscribe' => [
],
];

View File

@@ -0,0 +1,13 @@
<?php
namespace app\index\controller;
use app\BaseController as AppBaseController;
use think\facade\Config;
use think\facade\View;
use think\helper\Str;
class BaseController extends AppBaseController
{
}

View File

@@ -0,0 +1,20 @@
<?php
namespace app\index\controller;
use app\model\Category;
use app\model\Nav;
use app\model\Post;
use think\facade\View;
use think\helper\Str;
class Common extends BaseController
{
public function initialize()
{
parent::initialize();
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace app\index\controller;
use app\model\Category;
use app\model\Nav;
use app\model\Post;
use app\model\PostCategory;
use think\facade\View;
use think\Request;
class Index extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

16
app/middleware.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
return [
// 全局跨域
\think\middleware\AllowCrossDomain::class,
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class,
// 页面Trace调试
// \think\middleware\TraceDebug::class,
'\app\\middleware\ConfigInit'
];

View File

@@ -0,0 +1,41 @@
<?php
namespace app\middleware;
use app\model\AdminLog as AppAdminLog;
use app\model\AdminPermission;
use app\Request;
use think\Collection;
use think\facade\Cache;
class AdminLog
{
public function handle(Request $request, \Closure $next)
{
$logged_admin_permission = Cache::get('logged_admin_permission');
if(empty($logged_admin_permission)){
$logged_admin_permission = new Collection(AdminPermission::where('is_log',1)->select());
}
$is_exit = $logged_admin_permission->where('app',app('http')->getName())
->where('controller',$request->controller())
->where('action',$request->action());
if(!$is_exit->isEmpty()){
AppAdminLog::create([
'app'=>app('http')->getName(),
'controller'=>$request->controller(),
'action'=>$request->action(),
'param'=>$request->param(),
'create_time'=>time(),
'admin_id'=>$request->session('admin_id','0'),
'ip'=>$request->ip()
]);
}
return $next($request);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace app\middleware;
use think\facade\Db;
use think\facade\Config;
use think\helper\Arr;
class ConfigInit
{
public function handle($request, \Closure $next)
{
//设置存储
$filesystem_config = Config::get('filesystem');
Arr::set($filesystem_config,'default','public');
Config::set($filesystem_config, 'filesystem');
// 社微信开放平台
// $wx_open_app = [];
// $wx_open_app = Arr::add($wx_open_app,'app_id',get_system_config('wx_open_app_id'));
// $wx_open_app = Arr::add($wx_open_app,'secret',get_system_config('wx_open_app_secret'));
// $wx_open_app = Arr::add($wx_open_app,'token',get_system_config('wx_open_app_token'));
// $wx_open_app = Arr::add($wx_open_app,'aes_key',get_system_config('wx_open_app_aes_key'));
// Config::set($wx_open_app,'wx_open_app');
return $next($request);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace app\middleware;
use app\model\AdminPermission;
use app\Request;
class PermissionRecord
{
public function handle(Request $request, \Closure $next)
{
return $next($request);
}
}

33
app/model/Admin.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class Admin extends Model
{
//
public function getAvatarAttr($value)
{
if(empty($value)){
return '/static/images/avatar.png';
}
return \get_source_link($value);
}
public function getGroupAttr()
{
if(empty($this->getData('group_id'))){
return [];
}
return AdminGroup::where('id',$this->getData('group_id'))->cache(60)->find();
}
}

32
app/model/AdminGroup.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin think\Model
*/
class AdminGroup extends Model
{
//
use SoftDelete;
protected $defaultSoftDelete = 0;
public function getPermissionsAttr($value)
{
return \explode(',',$value);
}
public function setPermissionsAttr($value)
{
if(is_array($value)){
return join(',',$value);
}
return $value;
}
}

23
app/model/AdminLog.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin think\Model
*/
class AdminLog extends Model
{
//
use SoftDelete;
protected $defaultSoftDelete = 0;
public function admin()
{
return $this->belongsTo('Admin','admin_id');
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace app\model;
use think\Model;
/**
* 权限表
* @mixin think\Model
*/
class AdminPermission extends Model
{
//
public function getIsLogAttr($value)
{
$status = [
0=>'不记录',
1=>'记录',
];
return $status[intval($value)];
}
}

131
app/model/Category.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\facade\Config;
use think\Model;
/**
* @mixin think\Model
*/
class Category extends Model
{
//
public static $allCategory = [];
/**
* 获取指定id下的所有分类
*
* @param string $id
* @return void
*/
public static function getListLevel($id = '',$type = 1)
{
if(empty(self::$allCategory)){
$model_list = Category::where('type',$type)->order('sort asc')->select();
self::$allCategory = array2level($model_list,0,0);
}
if(!empty($id)){
$list = [];
$in_category = [$id];
foreach (self::$allCategory as $category) {
if(in_array($category->pid,$in_category)){
$list[] = $category;
$in_category[] = $category->id;
}
}
return $list;
}
return self::$allCategory;
}
public function getTitleImgAttr($value)
{
return get_source_link($value);
}
public function posts()
{
return $this->hasMany(PostCategory::class,'category_id');
}
/**
* 返回的对应的post的模型
*
* @return void
*/
public function getPostsModelListAttr()
{
$list_post_category = $this->getAttr('posts');
$list_post = [];
foreach ($list_post_category as $list_post_category) {
array_push($list_post,$list_post_category->post);
}
return $list_post;
}
/**
* 返回的对应post的数据,性能比模型要高.
*
* @return void
*/
public function getPostsListAttr()
{
$list_post_category = $this->getAttr('posts');
$list_post = array_column($list_post_category->append(['post'])->toArray(),'post');
return $list_post;
}
public function getTplNameAttr($value)
{
return Config::get('view_type.category.'.$value);
}
public function getModelParentAttr()
{
$pid = $this->getData('pid');
if($pid == 0){
return $this;
}
return Category::where('id',$pid)->find();
}
// 返回除自身以外的其他的同级同类的分类
public function getModelSiblingsAttr()
{
return Category::where('pid',$this->getData('pid'))
->where('level',$this->getData('level'))
->where('id','<>',$this->getData('id'))
->select();
}
/**
* 获取同一个父元素的分类,包含自身
*
* @return void
*/
public function getModelSameParentAttr()
{
return Category::where('pid',$this->getData('pid'))
->where('level',$this->getData('level'))
->select();
}
}

40
app/model/Nav.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class Nav extends Model
{
protected static $listNav = [];
public static $statusName = [
0 => '不显示',
1 => '显示'
];
//
public function getImgAttr($value)
{
return get_source_link($value, '/static/images/noimg.png');
}
public function getStatusNameAttr()
{
return self::$statusName[$this->getData('status')];
}
public static function getListNav($type = 1)
{
if (!isset(self::$listNav[$type])) {
self::$listNav[$type] = Nav::where('type', $type)->where('status', 1)->order('sort asc')->select();
}
return self::$listNav[$type];
}
}

139
app/model/Post.php Normal file
View File

@@ -0,0 +1,139 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin think\Model
*/
class Post extends Model
{
//
public static $stausNameList = [
0=>'不发布',
1=>'发布'
];
use SoftDelete;
protected $defaultSoftDelete = 0;
public function categorys()
{
return $this->hasMany(PostCategory::class,'post_id');
}
public function tags()
{
return $this->hasMany(PostTag::class,'post_id');
}
public function setPublishTimeAttr($value)
{
return strtotime($value);
}
public function getPublishTimeTextAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d',$value);
}
public function getPublishTimeDatetimeAttr()
{
$value = $this->getData('publish_time');
return date('Y-m-d H:i:s',$value);
}
public function getCategorysListAttr()
{
$list_post_categorys = $this->getAttr('categorys');
$list = array_column($list_post_categorys->append(['category'])->toArray(),'category');
$list = array2level($list,0,0);
return $list;
}
public function getTagsListAttr()
{
$list_post_tags = $this->getAttr('tags');
$list = array_column($list_post_tags->append(['tag'])->toArray(),'tag');
return $list;
}
public function getDescShortAttr()
{
$desc = $this->getData('desc');
if(strlen($desc) > 100){
$desc = mb_substr($desc,0,100).'...';
}
return $desc;
}
public function getDescListAttr()
{
$desc = $this->getData('desc');
if(empty($desc)){
return '';
}
$list = explode("\n", $desc);
return $list;
}
public function getDescHtmlAttr()
{
$desc = $this->getData('desc');
if(empty($desc)){
return '';
}
return str_replace("\n",'<br>',$desc);
}
public function getStatusNameAttr()
{
return self::$stausNameList[$this->getData('status')];
}
public function setPubishTimeAttr($value)
{
return strtotime($value);
}
public function setContentAttr($value)
{
return json_encode($value);
}
public function setContentHtmlAttr($value)
{
return trim($value);
}
public function getContentAttr($value)
{
return json_decode($value,true);
}
public function getPosterAttr($value)
{
if(empty($value)){
$value = '/static/images/avatar.png';
}
return get_source_link($value);
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class PostCategory extends Model
{
//
public function post()
{
return $this->belongsTo(Post::class, 'post_id');
}
public function category()
{
return $this->belongsTo(Category::class,'category_id');
}
}

24
app/model/PostTag.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class PostTag extends Model
{
//
public function tag()
{
return $this->belongsTo(Tag::class,'tag_id');
}
public function post()
{
return $this->belongsTo(Post::class, 'post_id');
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class SystemConfig extends Model
{
//
}

16
app/model/Tag.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class Tag extends Model
{
//
}

74
app/model/UploadFiles.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin think\Model
*/
class UploadFiles extends Model
{
//
use SoftDelete;
protected $defaultSoftDelete = 0;
public function getSrcAttr()
{
return \get_source_link($this->getData('save_name'));
}
public function getTypeTitleAttr()
{
return \config('upload_type.' . $this->getData('type'));
}
public function getUsedTimeAttr($value)
{
if ($value == 0) {
return '未使用';
}
return date('Y-m-d H:i:s', $value);
}
public function getDeleteTimeAttr($value)
{
if ($value == 0) {
return '未删除';
}
return date('Y-m-d H:i:s', $value);
}
public function getClearTimeAttr($value)
{
if ($value == 0) {
return '未清除';
}
return date('Y-m-d H:i:s', $value);
}
public function getStatusAttr($value, $data)
{
if ($data['used_time'] == 0) {
return '未使用(仅供预览)';
}
if ($data['delete_time'] > 0) {
return '已删除';
}
if ($data['clear_time'] > 0) {
return '已清除';
}
return '使用中';
}
public function getFileSizeAttr($value)
{
return format_size($value);
}
}

27
app/model/User.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
namespace app\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* @mixin think\Model
*/
class User extends Model
{
//
use SoftDelete;
protected $defaultSoftDelete = 0;
public function getAvatarAttr($value)
{
if(empty($value)){
return '/static/images/avatar.png';
}
return \get_source_link($value);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace app\model;
use think\Model;
/**
* @mixin think\Model
*/
class WxPublicAccount extends Model
{
//
public function getHeadImgAttr($value)
{
return get_source_link($value);
}
public function getQrcodeUrlAttr($value)
{
return \get_source_link($value);
}
}

19
app/provider.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use app\ExceptionHandle;
use app\Request;
// 容器Provider定义文件
return [
'think\Request' => Request::class,
'think\exception\Handle' => ExceptionHandle::class,
];

9
app/service.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
use app\AppService;
// 系统服务定义文件
// 服务在完成全局初始化之后执行
return [
AppService::class,
];