Controller.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Routing\Controller as BaseController;
  4. class Controller extends BaseController
  5. {
  6. use Helpers;
  7. public function jsonSuccess($data = [], $msg = "成功")
  8. {
  9. $response = array(
  10. 'code' => 0,
  11. 'msg' => $msg,
  12. 'data' => []
  13. );
  14. if ($data) {
  15. if (is_array($data)) {
  16. //带有分页格式转换
  17. if (isset($data['meta'])) {
  18. // 更改元数据格式,全部包含在data下
  19. $temp = array(
  20. 'data' => array(
  21. 'data' => $data['data'],
  22. 'pagination' => $data['meta']['pagination']
  23. )
  24. );
  25. $response = array_merge($response, $temp);
  26. } elseif (isset($data['data'])) {
  27. $response = array_merge($response, $data);
  28. } else {
  29. $temp = array(
  30. 'data' => $data
  31. );
  32. $response = array_merge($response, $temp);
  33. }
  34. } else {
  35. $response['data'] = $data;
  36. }
  37. }
  38. return $response;
  39. }
  40. public function jsonError($msg)
  41. {
  42. $response = array(
  43. 'code' => 1,
  44. 'msg' => $msg,
  45. 'data' => ""
  46. );
  47. return $response;
  48. }
  49. }