Backend.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. session(['locale'=>'zh-cn']);
  59. return $next($request);
  60. });
  61. }
  62. /**
  63. * 初始化配置
  64. * @author laravel开发员
  65. * @since 2020/11/10
  66. */
  67. public function initConfig()
  68. {
  69. // 请求参数
  70. $this->param = \request()->input();
  71. // 分页基础默认值
  72. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  73. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  74. }
  75. /**
  76. * 登录验证
  77. * @param $userId 用户ID
  78. * @return
  79. * @author laravel开发员
  80. * @since 2020/8/31
  81. */
  82. public function initLogin($userId)
  83. {
  84. // 登录用户ID
  85. $this->userId = $userId;
  86. // 登录用户信息
  87. if ($userId) {
  88. $adminModel = new UserModel();
  89. $userInfo = $adminModel->getInfo($this->userId);
  90. $this->userInfo = $userInfo;
  91. }
  92. }
  93. /**
  94. * 获取数据列表
  95. * @return mixed
  96. * @since 2020/11/11
  97. * @author laravel开发员
  98. */
  99. public function index()
  100. {
  101. $result = $this->service->getList();
  102. return $result;
  103. }
  104. /**
  105. * 获取数据详情
  106. * @return mixed
  107. * @since 2020/11/11
  108. * @author laravel开发员
  109. */
  110. public function info()
  111. {
  112. $result = $this->service->info();
  113. return $result;
  114. }
  115. /**
  116. * 添加或编辑
  117. * @return mixed
  118. * @since 2020/11/11
  119. * @author laravel开发员
  120. */
  121. public function edit()
  122. {
  123. $result = $this->service->edit();
  124. return $result;
  125. }
  126. /**
  127. * 删除数据
  128. * @return mixed
  129. * @since 2020/11/11
  130. * @author laravel开发员
  131. */
  132. public function delete()
  133. {
  134. $result = $this->service->delete();
  135. return $result;
  136. }
  137. /**
  138. * 设置状态
  139. * @return mixed
  140. * @since 2020/11/21
  141. * @author laravel开发员
  142. */
  143. public function status()
  144. {
  145. $result = $this->service->status();
  146. return $result;
  147. }
  148. }