BaseController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Http\Controllers\Controller;
  14. use App\Models\MemberModel;
  15. use App\Services\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 后台控制器基类
  19. * @author wesmiler
  20. * @since 2020/11/10
  21. * Class Backend
  22. * @package App\Http\Controllers
  23. */
  24. class BaseController extends Controller
  25. {
  26. // 模型
  27. protected $model;
  28. // 服务
  29. protected $service;
  30. // 校验
  31. protected $validate;
  32. // 登录ID
  33. protected $userId;
  34. // 登录信息
  35. protected $userInfo;
  36. /**
  37. * 构造函数
  38. * @author wesmiler
  39. * @since 2020/11/10
  40. * Backend constructor.
  41. */
  42. public function __construct()
  43. {
  44. // 初始化网络请求配置
  45. $this->initRequestConfig();
  46. // 初始化系统常量
  47. $this->initSystemConst();
  48. // 登录检测中间件
  49. $this->middleware('auth.login');
  50. // 初始化登录信息
  51. $this->middleware(function ($request, $next) {
  52. // 请求头信息
  53. $token = $request->headers->get('Authorization');
  54. $token = str_replace("Bearer ", null, $token);
  55. // JWT解密token
  56. $jwt = new Jwt('jwt_wx');
  57. $userId = $jwt->verifyToken($token);
  58. // 登录验证
  59. $userInfo = RedisService::get("auths:info:{$userId}");
  60. if(empty($userInfo)){
  61. $this->initLogin($userId);
  62. }else{
  63. $this->userInfo = $userInfo;
  64. }
  65. return $next($request);
  66. });
  67. }
  68. /**
  69. * 登录验证
  70. * @param $userId 用户ID
  71. * @return
  72. * @author wesmiler
  73. * @since 2020/8/31
  74. */
  75. public function initLogin($userId)
  76. {
  77. // 登录用户ID
  78. $this->userId = $userId;
  79. // 登录用户信息
  80. if ($userId) {
  81. $memberModel = new MemberModel();
  82. $userInfo = $memberModel->getInfo($this->userId);
  83. $this->userInfo = $userInfo;
  84. RedisService::set("auths:info:{$userId}", $this->userInfo, 4*24*3600);
  85. }
  86. }
  87. /**
  88. * 初始化请求配置
  89. * @since 2020/11/10
  90. * @author wesmiler
  91. */
  92. private function initRequestConfig()
  93. {
  94. // 定义是否GET请求
  95. defined('IS_GET') or define('IS_GET', \request()->isMethod('GET'));
  96. // 定义是否POST请求
  97. defined('IS_POST') or define('IS_POST', \request()->isMethod('POST'));
  98. // 定义是否AJAX请求
  99. defined('IS_AJAX') or define('IS_AJAX', \request()->ajax());
  100. // 定义是否PAJAX请求
  101. defined('IS_PJAX') or define('IS_PJAX', \request()->pjax());
  102. // 定义是否PUT请求
  103. defined('IS_PUT') or define('IS_PUT', \request()->isMethod('PUT'));
  104. // 定义是否DELETE请求
  105. defined('IS_DELETE') or define('IS_DELETE', \request()->isMethod('DELETE'));
  106. // 请求方式
  107. defined('REQUEST_METHOD') or define('REQUEST_METHOD', \request()->method());
  108. }
  109. /**
  110. * 初始化系统常量
  111. * @author wesmiler
  112. * @since 2020/11/10
  113. */
  114. private function initSystemConst()
  115. {
  116. // 项目根目录
  117. defined('ROOT_PATH') or define('ROOT_PATH', base_path());
  118. // 文件上传目录
  119. defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));
  120. // 图片上传目录
  121. defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));
  122. // 临时存放目录
  123. defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/img");
  124. // 文件存放目录
  125. defined('UPLOAD_FILE_PATH') or define('UPLOAD_FILE_PATH', ATTACHMENT_PATH . "/file");
  126. // cert目录
  127. defined('WECHAT_PAY_CERT_PATH') or define('WECHAT_PAY_CERT_PATH', base_path('public/certs'));
  128. // 定义普通图片域名
  129. defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));
  130. // 数据表前缀
  131. defined('DB_PREFIX') or define('DB_PREFIX', DB::connection()->getTablePrefix());
  132. // 系统全称
  133. define('SITE_NAME', env('SITE_NAME'));
  134. // 系统简称
  135. define('NICK_NAME', env('NICK_NAME'));
  136. // 系统版本号
  137. define('VERSION', env('VERSION'));
  138. }
  139. /**
  140. * 获取数据列表
  141. * @return mixed
  142. * @since 2020/11/11
  143. * @author wesmiler
  144. */
  145. public function index()
  146. {
  147. $result = $this->service->getList();
  148. return $result;
  149. }
  150. /**
  151. * 获取数据详情
  152. * @return mixed
  153. * @since 2020/11/11
  154. * @author wesmiler
  155. */
  156. public function info()
  157. {
  158. $result = $this->service->info();
  159. return $result;
  160. }
  161. /**
  162. * 添加或编辑
  163. * @return mixed
  164. * @since 2020/11/11
  165. * @author wesmiler
  166. */
  167. public function edit()
  168. {
  169. $result = $this->service->edit();
  170. return $result;
  171. }
  172. /**
  173. * 删除数据
  174. * @return mixed
  175. * @since 2020/11/11
  176. * @author wesmiler
  177. */
  178. public function delete()
  179. {
  180. $result = $this->service->delete();
  181. return $result;
  182. }
  183. /**
  184. * 设置状态
  185. * @return mixed
  186. * @since 2020/11/21
  187. * @author wesmiler
  188. */
  189. public function status()
  190. {
  191. $result = $this->service->status();
  192. return $result;
  193. }
  194. }