JsonTrait.php 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/30
  6. * Time: 下午2:27
  7. */
  8. namespace app\common\traits;
  9. trait JsonTrait
  10. {
  11. /**
  12. * 接口返回成功 JSON格式
  13. * @param string $message 提示信息
  14. * @param array $data 返回数据
  15. * @return \Illuminate\Http\JsonResponse
  16. */
  17. public function successJson($message = '成功', $data = [])
  18. {
  19. return response()->json([
  20. 'result' => 1,
  21. 'msg' => $message,
  22. 'data' => $data
  23. ], 200, ['charset' => 'utf-8']);
  24. }
  25. /**
  26. * 接口返回错误JSON 格式
  27. * @param string $message 提示信息
  28. * @param array $data 返回数据
  29. * @return \Illuminate\Http\JsonResponse
  30. */
  31. public function errorJson($message = '失败', $data = [])
  32. {
  33. response()->json([
  34. 'result' => 0,
  35. 'msg' => $message,
  36. 'data' => $data
  37. ], 200, ['charset' => 'utf-8'])->send();
  38. exit();
  39. }
  40. }