JsonService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. declare(strict_types=1);
  15. namespace app\common\service;
  16. use app\common\enum\ExportEnum;
  17. use app\common\lists\BaseDataLists;
  18. use app\common\lists\ListsExcelInterface;
  19. use app\common\lists\ListsExtendInterface;
  20. use think\Response;
  21. use think\response\Json;
  22. use think\exception\HttpResponseException;
  23. class JsonService
  24. {
  25. /**
  26. * @notes 接口操作成功,返回信息
  27. * @param string $msg
  28. * @param array $data
  29. * @param int $code
  30. * @param int $show
  31. * @return Json
  32. * @author 段誉
  33. * @date 2021/12/24 18:28
  34. */
  35. public static function success(string $msg = 'success', array $data = [], int $code = 1, int $show = 1): Json
  36. {
  37. return self::result($code, $show, $msg, $data);
  38. }
  39. /**
  40. * @notes 接口操作失败,返回信息
  41. * @param string $msg
  42. * @param array $data
  43. * @param int $code
  44. * @param int $show
  45. * @return Json
  46. * @author 段誉
  47. * @date 2021/12/24 18:28
  48. */
  49. public static function fail(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1): Json
  50. {
  51. return self::result($code, $show, $msg, $data);
  52. }
  53. /**
  54. * @notes 接口返回数据
  55. * @param $data
  56. * @return Json
  57. * @author 段誉
  58. * @date 2021/12/24 18:29
  59. */
  60. public static function data($data): Json
  61. {
  62. return self::success('', $data, 1, 0);
  63. }
  64. /**
  65. * @notes 接口返回信息
  66. * @param int $code
  67. * @param int $show
  68. * @param string $msg
  69. * @param array $data
  70. * @param int $httpStatus
  71. * @return Json
  72. * @author 段誉
  73. * @date 2021/12/24 18:29
  74. */
  75. private static function result(int $code, int $show, string $msg = 'OK', array $data = [], int $httpStatus = 200): Json
  76. {
  77. $result = compact('code', 'show', 'msg', 'data');
  78. return json($result, $httpStatus);
  79. }
  80. /**
  81. * @notes 抛出异常json
  82. * @param string $msg
  83. * @param array $data
  84. * @param int $code
  85. * @param int $show
  86. * @return Json
  87. * @author 段誉
  88. * @date 2021/12/24 18:29
  89. */
  90. public static function throw(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1): Json
  91. {
  92. $data = compact('code', 'show', 'msg', 'data');
  93. $response = Response::create($data, 'json', 200);
  94. throw new HttpResponseException($response);
  95. }
  96. /**
  97. * @notes 数据列表
  98. * @param \app\common\lists\BaseDataLists $lists
  99. * @return \think\response\Json
  100. * @author 令狐冲
  101. * @date 2021/7/28 11:15
  102. */
  103. public static function dataLists(BaseDataLists $lists): Json
  104. {
  105. //获取导出信息
  106. if ($lists->export == ExportEnum::INFO && $lists instanceof ListsExcelInterface) {
  107. return self::data($lists->excelInfo());
  108. }
  109. //获取导出文件的下载链接
  110. if ($lists->export == ExportEnum::EXPORT && $lists instanceof ListsExcelInterface) {
  111. $exportDownloadUrl = $lists->createExcel($lists->setExcelFields(), $lists->lists());
  112. return self::success('', ['url' => $exportDownloadUrl], 2);
  113. }
  114. $data = [
  115. 'lists' => $lists->lists(),
  116. 'count' => $lists->count(),
  117. 'page_no' => $lists->pageNo,
  118. 'page_size' => $lists->pageSize,
  119. ];
  120. $data['extend'] = [];
  121. if ($lists instanceof ListsExtendInterface) {
  122. $data['extend'] = $lists->extend();
  123. }
  124. return self::success('', $data, 1, 0);
  125. }
  126. }