BaseController.php 3.0 KB

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