Backend.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers\Admin;
  12. use App\Helpers\Jwt;
  13. use App\Http\Controllers\BaseController;
  14. use App\Models\UserModel;
  15. /**
  16. * 后台控制器基类
  17. * @author laravel开发员
  18. * @since 2020/11/10
  19. * Class Backend
  20. * @package App\Http\Controllers
  21. */
  22. class Backend extends BaseController
  23. {
  24. // 模型
  25. protected $model;
  26. // 服务
  27. protected $service;
  28. // 校验
  29. protected $validate;
  30. // 登录ID
  31. protected $userId;
  32. // 登录信息
  33. protected $userInfo;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/10
  38. * Backend constructor.
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. // 初始化配置
  44. $this->initConfig();
  45. // 登录检测中间件
  46. $this->middleware('user.login');
  47. // 初始化登录信息
  48. $this->middleware(function ($request, $next) {
  49. // 请求头信息
  50. $token = $request->headers->get('Authorization');
  51. $token = str_replace("Bearer ", null, $token);
  52. // JWT解密token
  53. $jwt = new Jwt();
  54. $userId = $jwt->verifyToken($token);
  55. // 登录验证
  56. $this->initLogin($userId);
  57. app()->setLocale('zh-cn');
  58. return $next($request);
  59. });
  60. }
  61. /**
  62. * 初始化配置
  63. * @author laravel开发员
  64. * @since 2020/11/10
  65. */
  66. public function initConfig()
  67. {
  68. // 请求参数
  69. $this->param = \request()->input();
  70. // 分页基础默认值
  71. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  72. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  73. }
  74. /**
  75. * 登录验证
  76. * @param $userId 用户ID
  77. * @return
  78. * @author laravel开发员
  79. * @since 2020/8/31
  80. */
  81. public function initLogin($userId)
  82. {
  83. // 登录用户ID
  84. $this->userId = $userId;
  85. // 登录用户信息
  86. if ($userId) {
  87. $adminModel = new UserModel();
  88. $userInfo = $adminModel->getInfo($this->userId);
  89. $this->userInfo = $userInfo;
  90. }
  91. }
  92. /**
  93. * 获取数据列表
  94. * @return mixed
  95. * @since 2020/11/11
  96. * @author laravel开发员
  97. */
  98. public function index()
  99. {
  100. $result = $this->service->getList();
  101. return $result;
  102. }
  103. /**
  104. * 获取数据详情
  105. * @return mixed
  106. * @since 2020/11/11
  107. * @author laravel开发员
  108. */
  109. public function info()
  110. {
  111. $result = $this->service->info();
  112. return $result;
  113. }
  114. /**
  115. * 添加或编辑
  116. * @return mixed
  117. * @since 2020/11/11
  118. * @author laravel开发员
  119. */
  120. public function edit()
  121. {
  122. $result = $this->service->edit();
  123. return $result;
  124. }
  125. /**
  126. * 删除数据
  127. * @return mixed
  128. * @since 2020/11/11
  129. * @author laravel开发员
  130. */
  131. public function delete()
  132. {
  133. $result = $this->service->delete();
  134. return $result;
  135. }
  136. /**
  137. * 设置状态
  138. * @return mixed
  139. * @since 2020/11/21
  140. * @author laravel开发员
  141. */
  142. public function status()
  143. {
  144. $result = $this->service->status();
  145. return $result;
  146. }
  147. }