Backend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. use App\Models\StoreModel;
  16. /**
  17. * 后台控制器基类
  18. * @author laravel开发员
  19. * @since 2020/11/10
  20. * Class Backend
  21. * @package App\Http\Controllers
  22. */
  23. class Backend extends BaseController
  24. {
  25. // 模型
  26. protected $model;
  27. // 服务
  28. protected $service;
  29. // 校验
  30. protected $validate;
  31. // 登录ID
  32. protected $userId;
  33. // 登录信息
  34. protected $userInfo;
  35. // 请求参数
  36. protected $param;
  37. // 店铺ID
  38. protected $storeId;
  39. /**
  40. * 构造函数
  41. * @author laravel开发员
  42. * @since 2020/11/10
  43. * Backend constructor.
  44. */
  45. public function __construct()
  46. {
  47. parent::__construct();
  48. // 初始化配置
  49. $this->initConfig();
  50. // 登录检测中间件
  51. $this->middleware('user.login');
  52. // 初始化登录信息
  53. $this->middleware(function ($request, $next) {
  54. // 请求头信息
  55. $token = $request->headers->get('Authorization');
  56. $token = str_replace("Bearer ", null, $token);
  57. // JWT解密token
  58. $jwt = new Jwt();
  59. $userId = $jwt->verifyToken($token);
  60. // 登录验证
  61. $this->initLogin($userId);
  62. // 将store_id添加到request中,方便Service层使用
  63. if ($this->userInfo && isset($this->userInfo['store_id'])) {
  64. $this->storeId = $this->userInfo['store_id'];
  65. } else {
  66. $this->storeId = 0;
  67. }
  68. return $next($request);
  69. });
  70. }
  71. /**
  72. * 初始化配置
  73. * @author laravel开发员
  74. * @since 2020/11/10
  75. */
  76. public function initConfig()
  77. {
  78. // 请求参数
  79. $this->param = \request()->input();
  80. // 分页基础默认值
  81. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  82. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  83. }
  84. /**
  85. * 登录验证
  86. * @param $userId 用户ID
  87. * @return
  88. * @author laravel开发员
  89. * @since 2020/8/31
  90. */
  91. public function initLogin($userId)
  92. {
  93. // 登录用户ID
  94. $this->userId = $userId;
  95. // 登录用户信息
  96. if ($userId) {
  97. $adminModel = new UserModel();
  98. $userInfo = $adminModel->getInfo($this->userId);
  99. $this->userInfo = $userInfo;
  100. $storeModel = new StoreModel();
  101. $storeInfo = $storeModel->getInfoByUserId($userId);
  102. $this->storeId = $storeInfo ? $storeInfo['id'] : 0;
  103. }
  104. }
  105. /**
  106. * 获取数据列表
  107. * @return mixed
  108. * @since 2020/11/11
  109. * @author laravel开发员
  110. */
  111. public function index()
  112. {
  113. $result = $this->service->getList();
  114. return $result;
  115. }
  116. /**
  117. * 获取数据详情
  118. * @return mixed
  119. * @since 2020/11/11
  120. * @author laravel开发员
  121. */
  122. public function info()
  123. {
  124. $result = $this->service->info();
  125. return $result;
  126. }
  127. /**
  128. * 添加或编辑
  129. * @return mixed
  130. * @since 2020/11/11
  131. * @author laravel开发员
  132. */
  133. public function edit()
  134. {
  135. $result = $this->service->edit();
  136. return $result;
  137. }
  138. /**
  139. * 删除数据
  140. * @return mixed
  141. * @since 2020/11/11
  142. * @author laravel开发员
  143. */
  144. public function delete()
  145. {
  146. $result = $this->service->delete();
  147. return $result;
  148. }
  149. /**
  150. * 设置状态
  151. * @return mixed
  152. * @since 2020/11/21
  153. * @author laravel开发员
  154. */
  155. public function status()
  156. {
  157. $result = $this->service->status();
  158. return $result;
  159. }
  160. }