BaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Api;
  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 BaseController extends \Illuminate\Routing\Controller
  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. // 登录检测中间件
  43. $this->middleware('member.login');
  44. // 初始化登录信息
  45. $this->middleware(function ($request, $next) {
  46. // 请求头信息
  47. $token = $request->headers->get('Authorization');
  48. $token = str_replace("Bearer ", null, $token);
  49. // JWT解密token
  50. $jwt = new Jwt();
  51. $userId = $jwt->verifyToken($token);
  52. // 登录验证
  53. $this->initLogin($userId);
  54. return $next($request);
  55. });
  56. }
  57. /**
  58. * 登录验证
  59. * @param $userId 用户ID
  60. * @return
  61. * @author wesmiler
  62. * @since 2020/8/31
  63. */
  64. public function initLogin($userId)
  65. {
  66. // 登录用户ID
  67. $this->userId = $userId;
  68. // 登录用户信息
  69. if ($userId) {
  70. $adminModel = new UserModel();
  71. $userInfo = $adminModel->getInfo($this->userId);
  72. $this->userInfo = $userInfo;
  73. }
  74. }
  75. /**
  76. * 获取数据列表
  77. * @return mixed
  78. * @since 2020/11/11
  79. * @author wesmiler
  80. */
  81. public function index()
  82. {
  83. $result = $this->service->getList();
  84. return $result;
  85. }
  86. /**
  87. * 获取数据详情
  88. * @return mixed
  89. * @since 2020/11/11
  90. * @author wesmiler
  91. */
  92. public function info()
  93. {
  94. $result = $this->service->info();
  95. return $result;
  96. }
  97. /**
  98. * 添加或编辑
  99. * @return mixed
  100. * @since 2020/11/11
  101. * @author wesmiler
  102. */
  103. public function edit()
  104. {
  105. $result = $this->service->edit();
  106. return $result;
  107. }
  108. /**
  109. * 删除数据
  110. * @return mixed
  111. * @since 2020/11/11
  112. * @author wesmiler
  113. */
  114. public function delete()
  115. {
  116. $result = $this->service->delete();
  117. return $result;
  118. }
  119. /**
  120. * 设置状态
  121. * @return mixed
  122. * @since 2020/11/21
  123. * @author wesmiler
  124. */
  125. public function status()
  126. {
  127. $result = $this->service->status();
  128. return $result;
  129. }
  130. }