helpers.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. use Carbon\Carbon;
  3. use Illuminate\Database\Eloquent\Model;
  4. const DAY_TO_MILLI_SECONDS = 1000*60*60*24;
  5. function formatExcelDate(int $timestamp)
  6. {
  7. $diff = intval($timestamp);
  8. $today=new Carbon('1900-01-01');
  9. $day = $today->addDays($diff-2);
  10. return $day->toDateString();
  11. }
  12. function formatExcelDateTime(float $timestamp): string
  13. {
  14. $today = new Carbon('1900-01-01');
  15. $day = $today->addMilliseconds(intval($timestamp*DAY_TO_MILLI_SECONDS))->subDays(2);
  16. return $day->toDateTimeString();
  17. }
  18. function diff($array1,$array2,string $identification,array $mapping,bool $intactDetached = false):array
  19. {
  20. $changes = [
  21. 'attached' => [], 'detached' => [], 'updated' => [],
  22. ];
  23. $map = [];
  24. foreach ($array2 as $item){
  25. if (is_array($item))$map[$item[$identification]] = $item;
  26. else $map[$item->$identification] = json_decode($item,true);
  27. }
  28. foreach ($array1 as $item){
  29. /** @var \stdClass|array|Model $item */
  30. if (!is_array($item) && !is_subclass_of($item,Model::class))$item = (array)$item;
  31. if (!isset($map[$item[$mapping[$identification]]])){
  32. $obj = [];
  33. foreach ($mapping as $column2=>$column1)$obj[$column2] = $item[$column1];
  34. $changes['attached'][] = $obj;continue;
  35. }
  36. $sign = false;
  37. $obj = [];
  38. foreach ($mapping as $column2=>$column1){
  39. $obj[$column2] = $item[$column1];
  40. if ($map[$item[$mapping[$identification]]][$column2] != $item[$column1])$sign = true;
  41. }
  42. if ($sign)$changes['updated'][] = $obj;
  43. unset($map[$item[$mapping[$identification]]]);
  44. }
  45. if ($map){
  46. if ($intactDetached){
  47. foreach ($map as $item){
  48. $obj = [];
  49. foreach ($mapping as $column2=>$column1)$obj[$column2] = $item[$column2];
  50. $changes['detached'][] = $obj;
  51. }
  52. }else $changes['detached'][] = array_keys($map);
  53. }
  54. return $changes;
  55. }
  56. //**官方的时间戳是13位的而php一般都是11位的所以需要进行处理一下**
  57. function getMillisecond():string
  58. {
  59. list($t1, $t2) = explode(' ', microtime());
  60. return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
  61. }
  62. function httpPost($uri = '', $body = '', $header = ''):array
  63. {
  64. $client = new \GuzzleHttp\Client([
  65. 'base_uri' => $uri
  66. ]);
  67. $res = $client->post($uri, [
  68. 'header' => $header,
  69. 'form_params' => $body
  70. ]);
  71. $data = $res->getBody()->getContents();
  72. return json_decode($data, true);
  73. }