Backend.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers;
  12. use App\Helpers\Jwt;
  13. use App\Models\UserModel;
  14. use Illuminate\Support\Facades\Session;
  15. /**
  16. * 后台控制器基类
  17. * @author wesmiler
  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 wesmiler
  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('jwt_yh');
  54. $userId = $jwt->verifyToken($token);
  55. // 登录验证
  56. $this->initLogin($userId);
  57. return $next($request);
  58. });
  59. }
  60. /**
  61. * 初始化配置
  62. * @author wesmiler
  63. * @since 2020/11/10
  64. */
  65. public function initConfig()
  66. {
  67. // 请求参数
  68. $this->param = \request()->input();
  69. // 分页基础默认值
  70. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  71. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  72. }
  73. /**
  74. * 登录验证
  75. * @param $userId 用户ID
  76. * @return
  77. * @author wesmiler
  78. * @since 2020/8/31
  79. */
  80. public function initLogin($userId)
  81. {
  82. // 登录用户ID
  83. $this->userId = $userId;
  84. // 登录用户信息
  85. if ($userId) {
  86. $adminModel = new UserModel();
  87. $userInfo = $adminModel->getInfo($this->userId);
  88. $this->userInfo = $userInfo;
  89. }
  90. }
  91. /**
  92. * 获取数据列表
  93. * @return mixed
  94. * @since 2020/11/11
  95. * @author wesmiler
  96. */
  97. public function index()
  98. {
  99. $result = $this->service->getList();
  100. return $result;
  101. }
  102. /**
  103. * 获取数据详情
  104. * @return mixed
  105. * @since 2020/11/11
  106. * @author wesmiler
  107. */
  108. public function info()
  109. {
  110. $result = $this->service->info();
  111. return $result;
  112. }
  113. /**
  114. * 添加或编辑
  115. * @return mixed
  116. * @since 2020/11/11
  117. * @author wesmiler
  118. */
  119. public function edit()
  120. {
  121. $result = $this->service->edit();
  122. return $result;
  123. }
  124. /**
  125. * 删除数据
  126. * @return mixed
  127. * @since 2020/11/11
  128. * @author wesmiler
  129. */
  130. public function delete()
  131. {
  132. $result = $this->service->delete();
  133. return $result;
  134. }
  135. /**
  136. * 设置状态
  137. * @return mixed
  138. * @since 2020/11/21
  139. * @author wesmiler
  140. */
  141. public function status()
  142. {
  143. $result = $this->service->status();
  144. return $result;
  145. }
  146. }