webApp.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. parent::__construct();
  45. // 初始化分页参数
  46. $this->initConfig();
  47. // 登录检测中间件
  48. $this->middleware('web.login');
  49. // 初始化登录信息
  50. $this->middleware(function ($request, $next) {
  51. // 请求头信息
  52. $token = $request->headers->get('Authorization');
  53. $token = str_replace("Bearer ", null, $token);
  54. if($token == 'app123'){
  55. $userId = ConfigService::make()->getConfigByCode('test_uid');
  56. $userId = $userId? $userId : 58;
  57. }else{
  58. // JWT解密token
  59. $jwt = new Jwt('jwt_app');
  60. $userId = $jwt->verifyToken($token);
  61. }
  62. // 登录验证
  63. $userInfo = RedisService::get("stores:auths:info:{$userId}");
  64. if(empty($userInfo) && $userId){
  65. $this->initLogin($userId);
  66. }else{
  67. $this->userId = $userId;
  68. $this->userInfo = $userInfo;
  69. }
  70. return $next($request);
  71. });
  72. }
  73. /**
  74. * 初始化配置
  75. * @author laravel开发员
  76. * @since 2020/11/10
  77. */
  78. public function initConfig()
  79. {
  80. // 请求参数
  81. $this->param = \request()->input();
  82. // 分页基础默认值
  83. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  84. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  85. }
  86. /**
  87. * 登录验证
  88. * @param $userId 用户ID
  89. * @return
  90. * @author wesmiler
  91. * @since 2020/8/31
  92. */
  93. public function initLogin($userId)
  94. {
  95. // 登录用户ID
  96. $this->userId = $userId;
  97. // 登录用户信息
  98. if ($userId) {
  99. $memberModel = new MemberModel();
  100. $userInfo = $memberModel->getInfo($this->userId);
  101. $this->userInfo = $userInfo;
  102. RedisService::set("auths:info:{$userId}", $this->userInfo, 4*24*3600);
  103. }
  104. }
  105. /**
  106. * 获取数据列表
  107. * @return mixed
  108. * @since 2020/11/11
  109. * @author wesmiler
  110. */
  111. public function index()
  112. {
  113. $result = $this->service->getList();
  114. return $result;
  115. }
  116. /**
  117. * 获取数据详情
  118. * @return mixed
  119. * @since 2020/11/11
  120. * @author wesmiler
  121. */
  122. public function info()
  123. {
  124. $result = $this->service->info();
  125. return $result;
  126. }
  127. /**
  128. * 添加或编辑
  129. * @return mixed
  130. * @since 2020/11/11
  131. * @author wesmiler
  132. */
  133. public function edit()
  134. {
  135. $result = $this->service->edit();
  136. return $result;
  137. }
  138. /**
  139. * 删除数据
  140. * @return mixed
  141. * @since 2020/11/11
  142. * @author wesmiler
  143. */
  144. public function delete()
  145. {
  146. $result = $this->service->delete();
  147. return $result;
  148. }
  149. /**
  150. * 设置状态
  151. * @return mixed
  152. * @since 2020/11/21
  153. * @author wesmiler
  154. */
  155. public function status()
  156. {
  157. $result = $this->service->status();
  158. return $result;
  159. }
  160. }