stockApp.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\UserModel;
  14. use App\Services\RedisService;
  15. /**
  16. * 仓管手机端控制器基类
  17. * @since 2020/11/10
  18. * @package App\Http\Controllers
  19. */
  20. class stockApp extends BaseController
  21. {
  22. // 模型
  23. protected $model;
  24. // 服务
  25. protected $service;
  26. // 校验
  27. protected $validate;
  28. // 登录ID
  29. protected $userId = 0;
  30. // 仓管用户ID
  31. protected $stockId = 0;
  32. // 登录信息
  33. protected $userInfo = [];
  34. /**
  35. * 构造函数
  36. * @author wesmiler
  37. * @since 2020/11/10
  38. * Backend constructor.
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. // 初始化分页参数
  44. $this->initConfig();
  45. // 初始化登录信息
  46. $this->middleware(function ($request, $next) {
  47. // 授权用户数据
  48. $id = $request->headers->get('token_sid');
  49. $id = $id? $id : 0;
  50. // 登录验证
  51. $info = RedisService::get("auths:stockInfo:{$id}");
  52. if(empty($info) && $id){
  53. $this->initLogin($id);
  54. }else{
  55. $this->stockId = $id;
  56. $this->userId = isset($info['id'])? $info['id'] : 0;
  57. $this->userInfo = $info;
  58. }
  59. return $next($request);
  60. });
  61. }
  62. /**
  63. * 初始化配置
  64. * @author laravel开发员
  65. * @since 2020/11/10
  66. */
  67. public function initConfig()
  68. {
  69. // 请求参数
  70. $this->param = \request()->input();
  71. // 分页基础默认值
  72. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  73. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  74. }
  75. /**
  76. * 登录验证
  77. * @param $userId 用户ID
  78. * @return
  79. * @author wesmiler
  80. * @since 2020/8/31
  81. */
  82. public function initLogin($id)
  83. {
  84. // 登录用户ID
  85. $this->stockId = $id;
  86. // 登录用户信息
  87. if ($id) {
  88. $info = UserModel::where(['id'=> $this->stockId,'mark'=>1])
  89. ->select(['id','username','nickname','mobile','realname','status'])
  90. ->first();
  91. $this->stockId = isset($info['id'])? $info['id'] : 0;
  92. $this->userInfo = $info? $info->toArray() : [];
  93. RedisService::set("auths:stockInfo:{$id}", $this->userInfo, 3*24*3600);
  94. }
  95. }
  96. /**
  97. * 获取数据列表
  98. * @return mixed
  99. * @since 2020/11/11
  100. * @author wesmiler
  101. */
  102. public function index()
  103. {
  104. $result = $this->service->getList();
  105. return $result;
  106. }
  107. /**
  108. * 获取数据详情
  109. * @return mixed
  110. * @since 2020/11/11
  111. * @author wesmiler
  112. */
  113. public function info()
  114. {
  115. $result = $this->service->info();
  116. return $result;
  117. }
  118. /**
  119. * 添加或编辑
  120. * @return mixed
  121. * @since 2020/11/11
  122. * @author wesmiler
  123. */
  124. public function edit()
  125. {
  126. $result = $this->service->edit();
  127. return $result;
  128. }
  129. /**
  130. * 删除数据
  131. * @return mixed
  132. * @since 2020/11/11
  133. * @author wesmiler
  134. */
  135. public function delete()
  136. {
  137. $result = $this->service->delete();
  138. return $result;
  139. }
  140. /**
  141. * 设置状态
  142. * @return mixed
  143. * @since 2020/11/21
  144. * @author wesmiler
  145. */
  146. public function status()
  147. {
  148. $result = $this->service->status();
  149. return $result;
  150. }
  151. }