webApp.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Http\Controllers\BaseController;
  13. use App\Models\MemberModel;
  14. use App\Services\RedisService;
  15. /**
  16. * 控制器基类
  17. * @since 2020/11/10
  18. * Class BaseController
  19. * @package App\Http\Controllers
  20. */
  21. class webApp extends BaseController
  22. {
  23. // 模型
  24. protected $model;
  25. // 服务
  26. protected $service;
  27. // 校验
  28. protected $validate;
  29. // 登录ID
  30. protected $userId = 0;
  31. // 用户类型
  32. protected $userType = 1;
  33. // 仓库ID
  34. protected $stockId = 1;
  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. $userId = $request->headers->get('token_uid');
  54. $userId = $userId? $userId : 0;
  55. // 登录验证
  56. $userInfo = RedisService::get("auths:info:{$userId}");
  57. if(empty($userInfo) && $userId){
  58. $this->initLogin($userId);
  59. }else{
  60. $this->userId = $userId;
  61. $this->stockId = isset($userInfo['stock_id'])? $userInfo['stock_id'] : 0;
  62. $this->userType = isset($userInfo['user_type']) && $userInfo['user_type']? $userInfo['user_type'] : 1;
  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. $this->userType = isset($userInfo['user_type']) && $userInfo['user_type']? $userInfo['user_type'] : 1;
  99. RedisService::set("auths:info:{$userId}", $this->userInfo, 7*24*3600);
  100. }
  101. }
  102. /**
  103. * 获取数据列表
  104. * @return mixed
  105. * @since 2020/11/11
  106. * @author wesmiler
  107. */
  108. public function index()
  109. {
  110. $result = $this->service->getList();
  111. return $result;
  112. }
  113. /**
  114. * 获取数据详情
  115. * @return mixed
  116. * @since 2020/11/11
  117. * @author wesmiler
  118. */
  119. public function info()
  120. {
  121. $result = $this->service->info();
  122. return $result;
  123. }
  124. /**
  125. * 添加或编辑
  126. * @return mixed
  127. * @since 2020/11/11
  128. * @author wesmiler
  129. */
  130. public function edit()
  131. {
  132. $result = $this->service->edit();
  133. return $result;
  134. }
  135. /**
  136. * 删除数据
  137. * @return mixed
  138. * @since 2020/11/11
  139. * @author wesmiler
  140. */
  141. public function delete()
  142. {
  143. $result = $this->service->delete();
  144. return $result;
  145. }
  146. /**
  147. * 设置状态
  148. * @return mixed
  149. * @since 2020/11/21
  150. * @author wesmiler
  151. */
  152. public function status()
  153. {
  154. $result = $this->service->status();
  155. return $result;
  156. }
  157. }