webApp.php 4.6 KB

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