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