BaseController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exceptions\ValidateException;
  4. use App\Http\Enum\MeiTuanLinkTypeEnum;
  5. use App\Http\Utils\Meituan\MeituanLianmengUtil;
  6. use App\Models\WechatAccountModel;
  7. use Illuminate\Http\Request;
  8. /**
  9. * @SWG\Swagger(
  10. * schemes={"http"},
  11. * host="test-daogou.codedreamit.com/api",
  12. * basePath="/",
  13. * @SWG\Info(
  14. * title="导购API文档",
  15. * version="1.0.0",
  16. * )
  17. * )
  18. */
  19. class BaseController extends Controller
  20. {
  21. /**
  22. * Controller constructor.
  23. */
  24. public function __construct(Request $request)
  25. {
  26. //进行统一的参数验证
  27. //判断对应目录下的验证类是否存在
  28. $controllerName = $request->route()->getActionName();
  29. $flag = preg_match("/Controllers\\\(.*)@(.*)/",$controllerName,$match);
  30. if($flag){
  31. $controllerName = $match[1];
  32. $actionName = $match[2];
  33. $class = "\\App\\Http\\Validate\\$controllerName"."Validate";
  34. //判断验证是否存在
  35. if(class_exists($class) && method_exists($class,$actionName)){
  36. $instance = new $class();
  37. $validateFlag = $instance->$actionName($request->all());
  38. if(!$validateFlag){
  39. throw new ValidateException($instance->getError(),501);
  40. }
  41. }
  42. }
  43. }
  44. /**
  45. * 成功返回
  46. * @param array $data
  47. * @param int $code
  48. * @param string $msg
  49. * @param string $url
  50. * @return \Illuminate\Http\JsonResponse
  51. */
  52. public function success($data = [], $code = 200, $msg = "", $url = "")
  53. {
  54. return response()->json(
  55. [
  56. "status"=>true,
  57. "data"=>$data,
  58. "code"=>$code,
  59. "msg"=>$msg,
  60. "url"=>$url
  61. ]
  62. );
  63. }
  64. /**
  65. * 错误返回
  66. * @param $msg
  67. * @param string $code
  68. * @param string $url
  69. */
  70. public function error($msg,$code="500",$url="")
  71. {
  72. return response()->json(
  73. [
  74. "status"=>false,
  75. "data"=>"",
  76. "code"=>$code,
  77. "msg"=>$msg,
  78. "url"=>$url
  79. ]
  80. );
  81. }
  82. /**
  83. * 生成swagger.json文件
  84. */
  85. public function swagger()
  86. {
  87. $swagger=\Swagger\scan(__DIR__."/../");
  88. $swagger->saveAs('./swagger.json');
  89. return $this->success();
  90. }
  91. /**
  92. * 读取swagger内容
  93. */
  94. public function loadSwagger()
  95. {
  96. $data = json_decode(file_get_contents("./swagger.json"),true);
  97. return $data;
  98. }
  99. public function test()
  100. {
  101. $data = MeituanLianmengUtil::generateLink(2,"ceshi",MeiTuanLinkTypeEnum::H5,env("MEITUAN_LIANMENG_KEY"));
  102. // $data = WechatAccountModel::findByWechatAppCode("fanbuting");
  103. return $data;
  104. }
  105. }