123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Http\Controllers;
- use App\Exceptions\ValidateException;
- use App\Models\WechatAccountModel;
- use Illuminate\Http\Request;
- /**
- * @SWG\Swagger(
- * schemes={"http"},
- * host="test-daogou.codedreamit.com/api",
- * basePath="/",
- * @SWG\Info(
- * title="导购API文档",
- * version="1.0.0",
- * )
- * )
- */
- class BaseController extends Controller
- {
- /**
- * Controller constructor.
- */
- public function __construct(Request $request)
- {
- //进行统一的参数验证
- //判断对应目录下的验证类是否存在
- $controllerName = $request->route()->getActionName();
- $flag = preg_match("/Controllers\\\(.*)@(.*)/",$controllerName,$match);
- if($flag){
- $controllerName = $match[1];
- $actionName = $match[2];
- $class = "\\App\\Http\\Validate\\$controllerName"."Validate";
- //判断验证是否存在
- if(class_exists($class) && method_exists($class,$actionName)){
- $instance = new $class();
- $validateFlag = $instance->$actionName($request->all());
- if(!$validateFlag){
- throw new ValidateException($instance->getError(),501);
- }
- }
- }
- }
- /**
- * 成功返回
- * @param array $data
- * @param int $code
- * @param string $msg
- * @param string $url
- * @return \Illuminate\Http\JsonResponse
- */
- public function success($data = [], $code = 200, $msg = "", $url = "")
- {
- return response()->json(
- [
- "status"=>true,
- "data"=>$data,
- "code"=>$code,
- "msg"=>$msg,
- "url"=>$url
- ]
- );
- }
- /**
- * 错误返回
- * @param $msg
- * @param string $code
- * @param string $url
- */
- public function error($msg,$code="500",$url="")
- {
- return response()->json(
- [
- "status"=>false,
- "data"=>"",
- "code"=>$code,
- "msg"=>$msg,
- "url"=>$url
- ]
- );
- }
- /**
- * 生成swagger.json文件
- */
- public function swagger()
- {
- $swagger=\Swagger\scan(__DIR__."/../");
- $swagger->saveAs('./swagger.json');
- return $this->success();
- }
- /**
- * 读取swagger内容
- */
- public function loadSwagger()
- {
- $data = json_decode(file_get_contents("./swagger.json"),true);
- return $data;
- }
- public function test()
- {
- $data = WechatAccountModel::findByWechatAppCode("fanbuting");
- return $data;
- }
- }
|