JsonService.php 4.6 KB

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