webApp.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Api\MemberService;
  16. use App\Services\ConfigService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 控制器基类
  21. * @since 2020/11/10
  22. * Class BaseController
  23. * @package App\Http\Controllers
  24. */
  25. class webApp extends BaseController
  26. {
  27. // 模型
  28. protected $model;
  29. // 服务
  30. protected $service;
  31. // 校验
  32. protected $validate;
  33. // 登录ID
  34. protected $userId = 0;
  35. // 登录信息
  36. protected $userInfo = [];
  37. /**
  38. * 构造函数
  39. * @author wesmiler
  40. * @since 2020/11/10
  41. * Backend constructor.
  42. */
  43. public function __construct()
  44. {
  45. parent::__construct();
  46. // 初始化分页参数
  47. $this->initConfig();
  48. // 登录检测中间件
  49. $this->middleware('web.login');
  50. // 初始化登录信息
  51. $this->middleware(function ($request, $next) {
  52. // 请求头信息
  53. $token = $request->headers->get('Authorization');
  54. $token = str_replace("Bearer ", null, $token);
  55. var_dump($token);
  56. if($token == 'app123'){
  57. $userId = ConfigService::make()->getConfigByCode('test_uid');
  58. $userId = $userId? $userId : 10001;
  59. }else{
  60. // JWT解密token
  61. $jwt = new Jwt('jwt_app');
  62. $userId = $jwt->verifyToken($token);
  63. }
  64. var_dump($userId);
  65. // 登录验证
  66. $userInfo = RedisService::get("stores:auths:info:{$userId}");
  67. if(empty($userInfo) && $userId){
  68. $this->initLogin($userId);
  69. }else{
  70. $this->userId = $userId;
  71. $this->userInfo = $userInfo;
  72. }
  73. var_dump($this->userInfo);
  74. var_dump($this->userId);
  75. return $next($request);
  76. });
  77. }
  78. /**
  79. * 初始化配置
  80. * @author laravel开发员
  81. * @since 2020/11/10
  82. */
  83. public function initConfig()
  84. {
  85. // 请求参数
  86. $this->param = \request()->input();
  87. // 分页基础默认值
  88. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  89. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  90. }
  91. /**
  92. * 登录验证
  93. * @param $userId 用户ID
  94. * @return
  95. * @author wesmiler
  96. * @since 2020/8/31
  97. */
  98. public function initLogin($userId)
  99. {
  100. // 登录用户ID
  101. $this->userId = $userId;
  102. // 登录用户信息
  103. if ($userId) {
  104. $userInfo = MemberService::make()->getInfo($this->userId);
  105. $this->userInfo = $userInfo;
  106. RedisService::set("stores:auths:info:{$userId}", $this->userInfo, 4*24*3600);
  107. }
  108. }
  109. /**
  110. * 获取数据列表
  111. * @return mixed
  112. * @since 2020/11/11
  113. * @author wesmiler
  114. */
  115. public function index()
  116. {
  117. $result = $this->service->getList();
  118. return $result;
  119. }
  120. /**
  121. * 获取数据详情
  122. * @return mixed
  123. * @since 2020/11/11
  124. * @author wesmiler
  125. */
  126. public function info()
  127. {
  128. $result = $this->service->info();
  129. return $result;
  130. }
  131. /**
  132. * 添加或编辑
  133. * @return mixed
  134. * @since 2020/11/11
  135. * @author wesmiler
  136. */
  137. public function edit()
  138. {
  139. $result = $this->service->edit();
  140. return $result;
  141. }
  142. /**
  143. * 删除数据
  144. * @return mixed
  145. * @since 2020/11/11
  146. * @author wesmiler
  147. */
  148. public function delete()
  149. {
  150. $result = $this->service->delete();
  151. return $result;
  152. }
  153. /**
  154. * 设置状态
  155. * @return mixed
  156. * @since 2020/11/21
  157. * @author wesmiler
  158. */
  159. public function status()
  160. {
  161. $result = $this->service->status();
  162. return $result;
  163. }
  164. }