Controller.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Dingo\Api\Routing\Helpers;
  5. use Laravel\Lumen\Routing\Controller as BaseController;
  6. class Controller extends BaseController
  7. {
  8. use Helpers;
  9. public function success($data=[],$msg="ok"){
  10. $this->parseNull($data);
  11. $result = [
  12. "code"=>200,
  13. "msg"=>$msg,
  14. "data"=>$data,
  15. ];
  16. return response()->json($result,200);
  17. }
  18. public function success_list($data,$msg="ok" ,$total=0,$re=[],$res="all"){
  19. $this->parseNull($data);
  20. $result = [
  21. "code"=>200,
  22. "message"=>$msg,
  23. "data"=>[
  24. 'size'=>15,
  25. 'total'=>$total,
  26. "$res"=>$re,
  27. 'list'=>$data
  28. ],
  29. ];
  30. return response()->json($result,200);
  31. }
  32. //失败返回
  33. public function error($code="422",$data="",$msg="fail"){
  34. $result = [
  35. "code"=>$code,
  36. "message"=>$msg,
  37. "msg"=>$msg,
  38. "data"=>$data
  39. ];
  40. return response()->json($result,200);
  41. }
  42. //失败返回
  43. public function errormsg($code="422",$data="",$msg="fail"){
  44. $result = [
  45. "code"=>$code,
  46. "msg"=>$msg,
  47. "data"=>$data
  48. ];
  49. return response()->json($result,200);
  50. }
  51. public function new_error($code="422",$msg="fail"){
  52. $result = [
  53. "code"=>$code,
  54. "message"=>$msg,
  55. ];
  56. return response()->json($result,200);
  57. }
  58. //如果返回的数据中有 null 则那其值修改为空 (安卓和IOS 对null型的数据不友好,会报错)
  59. private function parseNull(&$data){
  60. if(is_array($data)){
  61. foreach($data as &$v){
  62. $this->parseNull($v);
  63. }
  64. }else{
  65. if(is_null($data)){
  66. $data = "";
  67. }
  68. }
  69. }
  70. }