webApp.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\AccountLogModel;
  15. use App\Models\MemberModel;
  16. use App\Services\Api\MemberService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 控制器基类
  22. * @since 2020/11/10
  23. * Class BaseController
  24. * @package App\Http\Controllers
  25. */
  26. class webApp extends BaseController
  27. {
  28. // 模型
  29. protected $model;
  30. // 服务
  31. protected $service;
  32. // 校验
  33. protected $validate;
  34. // 登录ID
  35. protected $userId = 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. // 初始化分页参数
  47. $this->initConfig();
  48. // 登录检测中间件
  49. $this->middleware('web.login');
  50. // 初始化登录信息
  51. $this->middleware(function ($request, $next) {
  52. // 请求头信息
  53. $token = $request->headers->get('Authorization');
  54. $token = str_replace("Bearer ", null, $token);
  55. if($token == 'app123'){
  56. $userId = ConfigService::make()->getConfigByCode('test_uid');
  57. $userId = $userId? $userId : 58;
  58. }else{
  59. // JWT解密token
  60. $jwt = new Jwt('jwt_app');
  61. $userId = $jwt->verifyToken($token);
  62. }
  63. // 登录验证
  64. $userInfo = RedisService::get("caches:auths:info:{$userId}");
  65. if(empty($userInfo) && $userId){
  66. $this->initLogin($userId);
  67. }else{
  68. $this->userId = $userId;
  69. $this->userInfo = $userInfo;
  70. }
  71. // 更新消息推送CID
  72. $clientCid = request()->post('push_cid','');
  73. $pushCid = isset($userInfo['push_cid'])? $userInfo['push_cid'] : 0;
  74. if($clientCid && $clientCid != $pushCid && $this->userId){
  75. MemberModel::where(['id'=> $this->userId])->update(['push_cid'=> $clientCid,'update_time'=>time()]);
  76. }
  77. return $next($request);
  78. });
  79. }
  80. /**
  81. * 初始化配置
  82. * @author laravel开发员
  83. * @since 2020/11/10
  84. */
  85. public function initConfig()
  86. {
  87. // 请求参数
  88. $this->param = \request()->input();
  89. // 项目根目录
  90. defined('ROOT_PATH') or define('ROOT_PATH', base_path());
  91. // 定义普通图片域名
  92. defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));
  93. // 分页基础默认值
  94. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  95. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  96. // 文件上传目录
  97. defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));
  98. // 图片上传目录
  99. defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));
  100. // 临时存放目录
  101. defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/images");
  102. // 临时存放目录
  103. defined('UPLOAD_VIDEO_PATH') or define('UPLOAD_VIDEO_PATH', ATTACHMENT_PATH . "/video");
  104. // 文件存放目录
  105. defined('UPLOAD_FILE_PATH') or define('UPLOAD_FILE_PATH', ATTACHMENT_PATH . "/file");
  106. // cert目录
  107. defined('WECHAT_PAY_CERT_PATH') or define('WECHAT_PAY_CERT_PATH', base_path('public/certs'));
  108. // 数据表前缀
  109. defined('DB_PREFIX') or define('DB_PREFIX', env('DB_PREFIX','lev_'));
  110. // 分页基础默认值
  111. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  112. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  113. }
  114. /**
  115. * 登录验证
  116. * @param $userId 用户ID
  117. * @return
  118. * @author wesmiler
  119. * @since 2020/8/31
  120. */
  121. public function initLogin($userId)
  122. {
  123. // 登录用户ID
  124. $this->userId = $userId;
  125. // 登录用户信息
  126. if ($userId) {
  127. $userInfo = MemberService::make()->getInfo($this->userId,'login');
  128. $this->userInfo = $userInfo;
  129. RedisService::set("caches:auths:info:{$userId}", $this->userInfo, 7*24*3600);
  130. }
  131. }
  132. /**
  133. * 获取数据列表
  134. * @return mixed
  135. * @since 2020/11/11
  136. * @author wesmiler
  137. */
  138. public function index()
  139. {
  140. $result = $this->service->getList();
  141. return $result;
  142. }
  143. /**
  144. * 获取数据详情
  145. * @return mixed
  146. * @since 2020/11/11
  147. * @author wesmiler
  148. */
  149. public function info()
  150. {
  151. $result = $this->service->info();
  152. return $result;
  153. }
  154. /**
  155. * 添加或编辑
  156. * @return mixed
  157. * @since 2020/11/11
  158. * @author wesmiler
  159. */
  160. public function edit()
  161. {
  162. $result = $this->service->edit();
  163. return $result;
  164. }
  165. /**
  166. * 删除数据
  167. * @return mixed
  168. * @since 2020/11/11
  169. * @author wesmiler
  170. */
  171. public function delete()
  172. {
  173. $result = $this->service->delete();
  174. return $result;
  175. }
  176. /**
  177. * 设置状态
  178. * @return mixed
  179. * @since 2020/11/21
  180. * @author wesmiler
  181. */
  182. public function status()
  183. {
  184. $result = $this->service->status();
  185. return $result;
  186. }
  187. }