123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Http\Controllers\Api;
- use App\Helpers\Jwt;
- use App\Http\Controllers\BaseController;
- use App\Models\AccountLogModel;
- use App\Models\MemberModel;
- use App\Services\Api\MemberService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 控制器基类
- * @since 2020/11/10
- * Class BaseController
- * @package App\Http\Controllers
- */
- class webApp extends BaseController
- {
- // 模型
- protected $model;
- // 服务
- protected $service;
- // 校验
- protected $validate;
- // 登录ID
- protected $userId = 0;
- // 登录信息
- protected $userInfo = [];
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/10
- * Backend constructor.
- */
- public function __construct()
- {
- // 初始化分页参数
- $this->initConfig();
- // 登录检测中间件
- $this->middleware('web.login');
- // 初始化登录信息
- $this->middleware(function ($request, $next) {
- // 请求头信息
- $token = $request->headers->get('Authorization');
- $token = str_replace("Bearer ", null, $token);
- if($token == 'app123'){
- $userId = ConfigService::make()->getConfigByCode('test_uid');
- $userId = $userId? $userId : 58;
- }else{
- // JWT解密token
- $jwt = new Jwt('jwt_app');
- $userId = $jwt->verifyToken($token);
- }
- // 登录验证
- $userInfo = RedisService::get("caches:auths:info:{$userId}");
- if(empty($userInfo) && $userId){
- $this->initLogin($userId);
- }else{
- $this->userId = $userId;
- $this->userInfo = $userInfo;
- }
- // 更新消息推送CID
- $clientCid = request()->post('push_cid','');
- $pushCid = isset($userInfo['push_cid'])? $userInfo['push_cid'] : 0;
- if($clientCid && $clientCid != $pushCid && $this->userId){
- MemberModel::where(['id'=> $this->userId])->update(['push_cid'=> $clientCid,'update_time'=>time()]);
- }
- return $next($request);
- });
- }
- /**
- * 初始化配置
- * @author laravel开发员
- * @since 2020/11/10
- */
- public function initConfig()
- {
- // 请求参数
- $this->param = \request()->input();
- // 项目根目录
- defined('ROOT_PATH') or define('ROOT_PATH', base_path());
- // 定义普通图片域名
- defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));
- // 分页基础默认值
- defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
- defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
- // 文件上传目录
- defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));
- // 图片上传目录
- defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));
- // 临时存放目录
- defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/images");
- // 临时存放目录
- defined('UPLOAD_VIDEO_PATH') or define('UPLOAD_VIDEO_PATH', ATTACHMENT_PATH . "/video");
- // 文件存放目录
- defined('UPLOAD_FILE_PATH') or define('UPLOAD_FILE_PATH', ATTACHMENT_PATH . "/file");
- // cert目录
- defined('WECHAT_PAY_CERT_PATH') or define('WECHAT_PAY_CERT_PATH', base_path('public/certs'));
- // 数据表前缀
- defined('DB_PREFIX') or define('DB_PREFIX', env('DB_PREFIX','lev_'));
- // 分页基础默认值
- defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
- defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
- }
- /**
- * 登录验证
- * @param $userId 用户ID
- * @return
- * @author wesmiler
- * @since 2020/8/31
- */
- public function initLogin($userId)
- {
- // 登录用户ID
- $this->userId = $userId;
- // 登录用户信息
- if ($userId) {
- $userInfo = MemberService::make()->getInfo($this->userId,'login');
- $this->userInfo = $userInfo;
- RedisService::set("caches:auths:info:{$userId}", $this->userInfo, 7*24*3600);
- }
- }
- /**
- * 获取数据列表
- * @return mixed
- * @since 2020/11/11
- * @author wesmiler
- */
- public function index()
- {
- $result = $this->service->getList();
- return $result;
- }
- /**
- * 获取数据详情
- * @return mixed
- * @since 2020/11/11
- * @author wesmiler
- */
- public function info()
- {
- $result = $this->service->info();
- return $result;
- }
- /**
- * 添加或编辑
- * @return mixed
- * @since 2020/11/11
- * @author wesmiler
- */
- public function edit()
- {
- $result = $this->service->edit();
- return $result;
- }
- /**
- * 删除数据
- * @return mixed
- * @since 2020/11/11
- * @author wesmiler
- */
- public function delete()
- {
- $result = $this->service->delete();
- return $result;
- }
- /**
- * 设置状态
- * @return mixed
- * @since 2020/11/21
- * @author wesmiler
- */
- public function status()
- {
- $result = $this->service->status();
- return $result;
- }
- }
|