Backend.php 4.2 KB

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