webApp.php 4.7 KB

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