webApp.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\BaseController;
  14. use App\Models\MemberModel;
  15. use App\Services\ConfigService;
  16. use App\Services\RedisService;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 控制器基类
  20. * @since 2020/11/10
  21. * Class BaseController
  22. * @package App\Http\Controllers
  23. */
  24. class webApp extends BaseController
  25. {
  26. // 模型
  27. protected $model;
  28. // 服务
  29. protected $service;
  30. // 校验
  31. protected $validate;
  32. // 登录ID
  33. protected $userId = 0;
  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->initConfig();
  46. // 登录检测中间件
  47. $this->middleware('web.login');
  48. // 初始化登录信息
  49. $this->middleware(function ($request, $next) {
  50. // 请求头信息
  51. $token = $request->headers->get('Authorization');
  52. $token = str_replace("Bearer ", null, $token);
  53. if($token == 'app123'){
  54. $userId = ConfigService::make()->getConfigByCode('test_uid');
  55. $userId = $userId? $userId : 58;
  56. }else{
  57. // JWT解密token
  58. $jwt = new Jwt('jwt_app');
  59. $userId = $jwt->verifyToken($token);
  60. }
  61. // 登录验证
  62. $userInfo = RedisService::get("stores:auths:info:{$userId}");
  63. if(empty($userInfo) && $userId){
  64. $this->initLogin($userId);
  65. }else{
  66. $this->userId = $userId;
  67. $this->userInfo = $userInfo;
  68. }
  69. return $next($request);
  70. });
  71. }
  72. /**
  73. * 初始化配置
  74. * @author laravel开发员
  75. * @since 2020/11/10
  76. */
  77. public function initConfig()
  78. {
  79. // 请求参数
  80. $this->param = \request()->input();
  81. // 分页基础默认值
  82. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  83. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  84. }
  85. /**
  86. * 登录验证
  87. * @param $userId 用户ID
  88. * @return
  89. * @author wesmiler
  90. * @since 2020/8/31
  91. */
  92. public function initLogin($userId)
  93. {
  94. // 登录用户ID
  95. $this->userId = $userId;
  96. // 登录用户信息
  97. if ($userId) {
  98. $memberModel = new MemberModel();
  99. $userInfo = $memberModel->getInfo($this->userId);
  100. $this->userInfo = $userInfo;
  101. RedisService::set("auths:info:{$userId}", $this->userInfo, 4*24*3600);
  102. }
  103. }
  104. /**
  105. * 获取数据列表
  106. * @return mixed
  107. * @since 2020/11/11
  108. * @author wesmiler
  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 wesmiler
  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 wesmiler
  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 wesmiler
  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 wesmiler
  153. */
  154. public function status()
  155. {
  156. $result = $this->service->status();
  157. return $result;
  158. }
  159. }