Backend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // 通过 user.user_id(member ID)查询关联的商家
  97. $memberId = $userInfo['user_id'] ?? 0;
  98. $storeInfo = $memberId > 0 ? $storeModel->getInfoByUserId($memberId) : null;
  99. $this->storeId = $storeInfo ? $storeInfo['id'] : 0;
  100. $this->memberId = $storeInfo ? $storeInfo['user_id'] : 0;
  101. }
  102. }
  103. /**
  104. * 获取数据列表
  105. * @return mixed
  106. * @since 2020/11/11
  107. * @author laravel开发员
  108. */
  109. public function index()
  110. {
  111. $result = $this->service->getList();
  112. return $result;
  113. }
  114. /**
  115. * 获取数据详情
  116. * @return mixed
  117. * @since 2020/11/11
  118. * @author laravel开发员
  119. */
  120. public function info()
  121. {
  122. $result = $this->service->info();
  123. return $result;
  124. }
  125. /**
  126. * 添加或编辑
  127. * @return mixed
  128. * @since 2020/11/11
  129. * @author laravel开发员
  130. */
  131. public function edit()
  132. {
  133. $result = $this->service->edit();
  134. return $result;
  135. }
  136. /**
  137. * 删除数据
  138. * @return mixed
  139. * @since 2020/11/11
  140. * @author laravel开发员
  141. */
  142. public function delete()
  143. {
  144. $result = $this->service->delete();
  145. return $result;
  146. }
  147. /**
  148. * 设置状态
  149. * @return mixed
  150. * @since 2020/11/21
  151. * @author laravel开发员
  152. */
  153. public function status()
  154. {
  155. $result = $this->service->status();
  156. return $result;
  157. }
  158. }