BaseRepository copy.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Exceptions\HOError;
  4. use Illuminate\Support\Facades\Log;
  5. class BaseRepository
  6. {
  7. protected $startTime;
  8. protected $error;
  9. const SUCCESS_CODE = 1000;
  10. public function startTime()
  11. {
  12. Log::debug('获取程序开始时间');
  13. $this->startTime = microtime(TRUE);
  14. return $this->startTime;
  15. }
  16. /**
  17. * 成功返回
  18. */
  19. public function response($data=null, $msg = '成功')
  20. {
  21. return response()->horesp(1000, $data, $msg);
  22. }
  23. public function fail($data=null, $msg = '失败')
  24. {
  25. return response()->horesp(9999, $data, $msg);
  26. }
  27. /**
  28. * 异常错误
  29. *
  30. * @return \App\Exceptions\HOError
  31. */
  32. public function error()
  33. {
  34. $this->error = new HOError();
  35. return $this->error;
  36. }
  37. /**
  38. * 统一格式输出日志
  39. *
  40. * @param string $typeName 类别名
  41. * @param string $startTime 开始时间
  42. * @param array $log 日志详情
  43. * @param string $ac 指定动作
  44. */
  45. public function setLog(string $typeName, $startTime = 0.00, $log = [], $ac = 'info')
  46. {
  47. //传入的时间
  48. $sTime = $startTime;
  49. //当前时间,相当于结束时间
  50. $eTime = microtime(TRUE);
  51. //计算时间差
  52. $diffTime = (($eTime - $sTime) * 1000);
  53. $msg = '执行时间:'.round($diffTime, 3).'ms'.' 执行内容:'.$typeName;
  54. switch ($ac){
  55. case 'debug':
  56. Log::debug($msg, $log);
  57. break;
  58. case 'info':
  59. Log::info($msg, $log);
  60. break;
  61. case 'error':
  62. Log::error($msg, $log);
  63. break;
  64. }
  65. }
  66. }