Init.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/6/11
  6. * Time: 下午5:06
  7. */
  8. namespace app\api\controller\v1;
  9. use app\common\helper\ArrayHelper;
  10. use app\main\model\object\ReturnObject;
  11. use app\main\service\LogService;
  12. use think\exception\HttpResponseException;
  13. use think\Request;
  14. use app\main\constants\ErrorCodeConstants;
  15. use think\Response;
  16. class Init
  17. {
  18. protected $format = 'json';
  19. protected $limited_formats = ['json', 'jsonp', 'xml'];
  20. /**
  21. * @var ReturnObject
  22. */
  23. protected $oReturn;
  24. public function __construct()
  25. {
  26. if ($format = Request::instance()->param('format')) {
  27. if (in_array($format, $this->limited_formats)) {
  28. $this->format = $format;
  29. }
  30. }
  31. $this->oReturn = new ReturnObject();
  32. }
  33. /**
  34. * 返回结果
  35. */
  36. public function getReturn()
  37. {
  38. $result = [
  39. 'code' => $this->oReturn->code,
  40. 'msg' => $this->oReturn->msg,
  41. 'time' => Request::instance()->server('REQUEST_TIME'),
  42. 'data' => $this->oReturn->data,
  43. ];
  44. $response = Response::create($result, $this->format);
  45. throw new HttpResponseException($response);
  46. }
  47. /**
  48. * @param $data
  49. * @return $this
  50. */
  51. public function setData($data)
  52. {
  53. $this->oReturn->data = $data;
  54. return $this;
  55. }
  56. /**
  57. * @param $msg
  58. * @return $this
  59. */
  60. public function setMsg($msg)
  61. {
  62. $this->oReturn->msg = $msg;
  63. return $this;
  64. }
  65. /**
  66. * @param $code
  67. * @return $this
  68. */
  69. public function setCode($code)
  70. {
  71. $this->oReturn->code = $code;
  72. return $this;
  73. }
  74. }