Api2021Service.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Services\Web;
  3. use Exception;
  4. use App\Http\Services\ConstDef\GeneralConst;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Web\Info;
  7. use App\Models\Web\GeneralSettings;
  8. use Aws\S3\S3Client;
  9. class Api2021Service
  10. {
  11. private $infoDb;
  12. private $gsDb;
  13. private $basedir;
  14. private $s3;
  15. public function __construct()
  16. {
  17. date_default_timezone_set("Asia/Taipei");
  18. $this->infoDb = new Info();
  19. $this->gsDb = new GeneralSettings();
  20. $this->basedir = preg_replace('/\/app\/.*/', '/public/', __DIR__);
  21. $this->s3 = new S3Client([
  22. 'credentials' => [
  23. 'key' => env('AWS_APP_KEY'),
  24. 'secret' => env('AWS_APP_SECRET'),
  25. ],
  26. 'region' => env('AWS_S3_REGION'),
  27. 'version' => 'latest',
  28. ]);
  29. }
  30. public function setdoc($data)
  31. {
  32. $path = env('APP_ENV') . '/' . date("Y-m-d") . '/' . time() . '.jpg';
  33. $this->s3->putObject([
  34. 'ACL' => 'public-read',
  35. 'Body' => $data,
  36. 'Bucket' => env('AWS_S3_BUCKET'),
  37. 'ContentType' => 'image/jpeg ',
  38. 'Key' => $path,
  39. ]);
  40. return $path;
  41. }
  42. public function setinfo($info)
  43. {
  44. $info['cdate'] = date("Y-m-d H:i:s");
  45. $info['ip'] = $this->getUserIP();
  46. $this->infoDb->insert($info);
  47. // 計數
  48. if ($info['image_uri'] != '') {
  49. // 掃臉
  50. $this->gsDb->where('key', 'counter_a')->update(['value' => DB::raw("value+1")]);
  51. } else {
  52. // 留言
  53. $this->gsDb->where('key', 'counter_b')->update(['value' => DB::raw("value+1")]);
  54. }
  55. return true;
  56. }
  57. public function counterGet($type)
  58. {
  59. $counter = $this->gsDb->select(['value'])->where('key', '=', 'counter_' . $type)->first();
  60. $counter = (isset($counter)) ? $counter->toArray() : [];
  61. $counter = (count($counter) > 0) ? (int)$counter['value'] : 0;
  62. return $counter;
  63. }
  64. public function counterSet($type)
  65. {
  66. $this->gsDb->where('key', 'counter_' . $type)->update(['value' => DB::raw("value+1")]);
  67. return true;
  68. }
  69. public function reset()
  70. {
  71. DB::select(DB::raw("truncate table infos;"));
  72. DB::update(DB::raw("update general_settings set value=0 where 1=1;"));
  73. return true;
  74. }
  75. public function getUserIP()
  76. {
  77. $ipaddress = '';
  78. if (isset($_SERVER['HTTP_CLIENT_IP']))
  79. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  80. else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  81. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  82. else if (isset($_SERVER['HTTP_X_FORWARDED']))
  83. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  84. else if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
  85. $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
  86. else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
  87. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  88. else if (isset($_SERVER['HTTP_FORWARDED']))
  89. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  90. else if (isset($_SERVER['REMOTE_ADDR']))
  91. $ipaddress = $_SERVER['REMOTE_ADDR'];
  92. else
  93. $ipaddress = 'UNKNOWN';
  94. return $ipaddress;
  95. }
  96. }