Backend.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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('jwt_admin');
  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. // 企业数据
  96. $type = isset($userInfo['type'])? $userInfo['type'] : 1;
  97. $memberId = $userInfo['user_id'] ?? 0;
  98. if($type==2){
  99. $this->storeId = $this->userId;
  100. $this->memberId = $memberId;
  101. }
  102. }
  103. }
  104. /**
  105. * 获取数据列表
  106. * @return mixed
  107. * @since 2020/11/11
  108. * @author laravel开发员
  109. */
  110. public function index()
  111. {
  112. $result = $this->service->getList();
  113. return $result;
  114. }
  115. /**
  116. * 获取数据详情
  117. * @return mixed
  118. * @since 2020/11/11
  119. * @author laravel开发员
  120. */
  121. public function info()
  122. {
  123. $result = $this->service->info();
  124. return $result;
  125. }
  126. /**
  127. * 添加或编辑
  128. * @return mixed
  129. * @since 2020/11/11
  130. * @author laravel开发员
  131. */
  132. public function edit()
  133. {
  134. $result = $this->service->edit();
  135. return $result;
  136. }
  137. /**
  138. * 删除数据
  139. * @return mixed
  140. * @since 2020/11/11
  141. * @author laravel开发员
  142. */
  143. public function delete()
  144. {
  145. $result = $this->service->delete();
  146. return $result;
  147. }
  148. /**
  149. * 设置状态
  150. * @return mixed
  151. * @since 2020/11/21
  152. * @author laravel开发员
  153. */
  154. public function status()
  155. {
  156. $result = $this->service->status();
  157. return $result;
  158. }
  159. /**
  160. * 设置推荐
  161. * @return mixed
  162. * @since 2020/11/21
  163. * @author laravel开发员
  164. */
  165. public function recommend()
  166. {
  167. $result = $this->service->recommend();
  168. return $result;
  169. }
  170. }