webApp.php 4.2 KB

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