JsonResponse.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace Dcat\Admin\Http;
  3. use Dcat\Admin\Exception\AdminException;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Contracts\Validation\Validator;
  7. use Illuminate\Support\MessageBag;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Validation\ValidationException;
  10. /**
  11. * Class JsonResponse.
  12. *
  13. * @method $this successIf($condition, ?string $message)
  14. * @method $this errorIf($condition, ?string $message)
  15. * @method $this warningIf($condition, ?string $message)
  16. * @method $this infoIf($condition, ?string $message)
  17. * @method $this detailIf($condition, ?string $message)
  18. * @method $this statusCodeIf($condition, int $code)
  19. * @method $this redirectIf($condition, ?string $url)
  20. * @method $this locationIf($condition, ?string $url = null)
  21. * @method $this refreshIf($condition)
  22. * @method $this downloadIf($condition, ?string $url)
  23. * @method $this scriptIf($condition, ?string $script)
  24. * @method $this alertIf($condition, bool $alert = true)
  25. * @method $this htmlIf($condition, $html)
  26. * @method $this dataIf($condition, array $data)
  27. * @method $this optionsIf($condition, array $data)
  28. * @method $this withValidationIf($condition, $errors)
  29. * @method $this withExceptionIf($condition, \Throwable $e)
  30. */
  31. class JsonResponse implements Arrayable
  32. {
  33. protected $status = true;
  34. protected $statusCode = 200;
  35. protected $exception;
  36. protected $data = [];
  37. protected $html;
  38. protected $options = [];
  39. public function __construct(array $data = [])
  40. {
  41. $this->data($data);
  42. }
  43. /**
  44. * 设置请求结果是否成功.
  45. *
  46. * @param bool $status
  47. *
  48. * @return $this
  49. */
  50. public function status(bool $status)
  51. {
  52. $this->status = $status;
  53. return $this;
  54. }
  55. /**
  56. * 设置 HTTP 状态码.
  57. *
  58. * @param int $statusCode
  59. *
  60. * @return $this
  61. */
  62. public function statusCode(int $statusCode)
  63. {
  64. $this->statusCode = $statusCode;
  65. return $this;
  66. }
  67. /**
  68. * 设置提示信息.
  69. *
  70. * @param string $message
  71. *
  72. * @return $this
  73. */
  74. public function message(?string $message)
  75. {
  76. $this->data['message'] = $message;
  77. return $this;
  78. }
  79. /**
  80. * 显示 成功 提示弹窗.
  81. *
  82. * @param string $message
  83. *
  84. * @return $this
  85. */
  86. public function success(?string $message)
  87. {
  88. $this->status(true);
  89. return $this->show('success', $message);
  90. }
  91. /**
  92. * @param string $message
  93. *
  94. * @return $this
  95. */
  96. public function info(?string $message)
  97. {
  98. return $this->show('info', $message);
  99. }
  100. /**
  101. * @param string $message
  102. *
  103. * @return $this
  104. */
  105. public function warning(?string $message)
  106. {
  107. return $this->show('warning', $message);
  108. }
  109. /**
  110. * 显示 错误 信息弹窗.
  111. *
  112. * @param string $message
  113. * @param bool $alert
  114. *
  115. * @return $this
  116. */
  117. public function error(?string $message)
  118. {
  119. $this->status(false);
  120. return $this->show('error', $message);
  121. }
  122. /**
  123. * 设置 toastr 显示时长.
  124. *
  125. * @param $seconds
  126. *
  127. * @return $this
  128. */
  129. public function timeout($seconds)
  130. {
  131. return $this->data(['timeout' => $seconds]);
  132. }
  133. /**
  134. * 显示确认弹窗.
  135. *
  136. * @param bool $alert
  137. *
  138. * @return $this
  139. */
  140. public function alert(bool $alert = true)
  141. {
  142. return $this->data(['alert' => $alert]);
  143. }
  144. /**
  145. * 显示弹窗描述信息.
  146. *
  147. * @param string $detail
  148. *
  149. * @return $this
  150. */
  151. public function detail(?string $detail)
  152. {
  153. return $this->data(['detail' => $detail]);
  154. }
  155. /**
  156. * 显示弹窗信息.
  157. *
  158. * @param string $type
  159. * @param string $message
  160. *
  161. * @return $this
  162. */
  163. protected function show(?string $type, ?string $message = null)
  164. {
  165. if ($message) {
  166. $this->message($message);
  167. }
  168. return $this->data(['type' => $type]);
  169. }
  170. /**
  171. * 跳转.
  172. *
  173. * @param string $url
  174. *
  175. * @return $this
  176. */
  177. public function redirect(?string $url)
  178. {
  179. return $this->then(['action' => 'redirect', 'value' => admin_url($url)]);
  180. }
  181. /**
  182. * @param string|null $url
  183. *
  184. * @return $this
  185. */
  186. public function redirectToIntended(?string $url)
  187. {
  188. $path = session()->pull('url.intended');
  189. return $this->redirect($path ?: $url);
  190. }
  191. /**
  192. * Location 跳转.
  193. *
  194. * @param string $location 不传则刷新当前页面
  195. *
  196. * @return $this
  197. */
  198. public function location(?string $location = null)
  199. {
  200. return $this->then(['action' => 'location', 'value' => $location ? admin_url($location) : null]);
  201. }
  202. /**
  203. * @param string|null $url
  204. *
  205. * @return $this
  206. */
  207. public function locationToIntended(?string $url)
  208. {
  209. $path = session()->pull('url.intended');
  210. return $this->location($path ?: $url);
  211. }
  212. /**
  213. * 下载.
  214. *
  215. * @param string $url
  216. *
  217. * @return $this
  218. */
  219. public function download($url)
  220. {
  221. return $this->then(['action' => 'download', 'value' => admin_url($url)]);
  222. }
  223. /**
  224. * 刷新页面.
  225. *
  226. * @return $this
  227. */
  228. public function refresh()
  229. {
  230. return $this->then(['action' => 'refresh', 'value' => true]);
  231. }
  232. /**
  233. * 执行JS代码.
  234. *
  235. * @param string $script
  236. *
  237. * @return $this
  238. */
  239. public function script($script)
  240. {
  241. return $this->then(['action' => 'script', 'value' => $script]);
  242. }
  243. /**
  244. * @param array $value
  245. *
  246. * @return $this
  247. */
  248. protected function then(array $value)
  249. {
  250. $this->data['then'] = $value;
  251. return $this;
  252. }
  253. /**
  254. * 设置返回数据.
  255. *
  256. * @param array $value
  257. *
  258. * @return $this
  259. */
  260. public function data(array $value)
  261. {
  262. $this->data = array_merge($this->data, $value);
  263. return $this;
  264. }
  265. /**
  266. * 返回 HTML.
  267. *
  268. * @param string $html
  269. *
  270. * @return $this
  271. */
  272. public function html($html)
  273. {
  274. $this->html = $html;
  275. return $this;
  276. }
  277. /**
  278. * 设置其他字段.
  279. *
  280. * @param array $options
  281. *
  282. * @return $this
  283. */
  284. public function options(array $options)
  285. {
  286. $this->options = array_merge($this->options, $options);
  287. return $this;
  288. }
  289. /**
  290. * 设置字段验证错误信息.
  291. *
  292. * @param $errors
  293. *
  294. * @return $this
  295. */
  296. public function withValidation($errors)
  297. {
  298. if ($errors instanceof Validator) {
  299. $errors = $errors->errors();
  300. }
  301. if ($errors instanceof MessageBag) {
  302. $errors = $errors->getMessages();
  303. }
  304. return $this
  305. ->status(false)
  306. ->statusCode(422)
  307. ->options(['errors' => $errors]);
  308. }
  309. /**
  310. * 响应异常.
  311. *
  312. * @param \Throwable $exception
  313. *
  314. * @return $this
  315. */
  316. public function withException(\Throwable $exception)
  317. {
  318. if ($exception instanceof ValidationException) {
  319. return $this->withValidation($exception->errors());
  320. }
  321. return $this
  322. ->status(false)
  323. ->error(
  324. sprintf('[%s] %s', get_class($exception), $exception->getMessage())
  325. );
  326. }
  327. /**
  328. * Flash a piece of data to the session.
  329. *
  330. * @param string|array $key
  331. * @param mixed $value
  332. * @return $this
  333. */
  334. public function with($key, $value = null)
  335. {
  336. $key = is_array($key) ? $key : [$key => $value];
  337. foreach ($key as $k => $v) {
  338. session()->flash($k, $v);
  339. }
  340. return $this;
  341. }
  342. /**
  343. * @return array
  344. */
  345. public function toArray()
  346. {
  347. $data = ['status' => $this->status, 'data' => $this->data];
  348. if ($this->html) {
  349. $data['html'] = Helper::render($this->html);
  350. }
  351. return $data + $this->options;
  352. }
  353. /**
  354. * @return \Illuminate\Http\JsonResponse
  355. */
  356. public function send()
  357. {
  358. return response()->json($this->toArray(), $this->statusCode);
  359. }
  360. public function __call($method, $arguments)
  361. {
  362. if (Str::endsWith($method, 'If')) {
  363. if ($arguments) {
  364. $method = Str::replaceLast('If', '', $method);
  365. $condition = value(array_shift($arguments));
  366. return $condition ? $this->$method(...$arguments) : $this;
  367. }
  368. }
  369. throw new AdminException(sprintf('Call to undefined method "%s"', $method));
  370. }
  371. /**
  372. * @param mixed ...$params
  373. *
  374. * @return $this
  375. */
  376. public static function make(...$params)
  377. {
  378. return new static(...$params);
  379. }
  380. }