123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
-
- namespace App\Http\Services\Web;
-
- use Exception;
- use App\Http\Services\ConstDef\GeneralConst;
- use Illuminate\Support\Facades\DB;
- use App\Models\Web\Info;
- use App\Models\Web\GeneralSettings;
- use Aws\S3\S3Client;
-
- class Api2021Service
- {
-
- private $infoDb;
- private $gsDb;
- private $basedir;
- private $s3;
-
- public function __construct()
- {
- date_default_timezone_set("Asia/Taipei");
- $this->infoDb = new Info();
- $this->gsDb = new GeneralSettings();
- $this->basedir = preg_replace('/\/app\/.*/', '/public/', __DIR__);
- $this->s3 = new S3Client([
- 'credentials' => [
- 'key' => env('AWS_APP_KEY'),
- 'secret' => env('AWS_APP_SECRET'),
- ],
- 'region' => env('AWS_S3_REGION'),
- 'version' => 'latest',
- ]);
- }
-
- public function setdoc($data)
- {
- $path = env('APP_ENV') . '/' . date("Y-m-d") . '/' . time() . '.jpg';
- $this->s3->putObject([
- 'ACL' => 'public-read',
- 'Body' => $data,
- 'Bucket' => env('AWS_S3_BUCKET'),
- 'ContentType' => 'image/jpeg ',
- 'Key' => $path,
- ]);
-
- return $path;
- }
-
- public function setinfo($info)
- {
- $info['cdate'] = date("Y-m-d H:i:s");
- $info['ip'] = $this->getUserIP();
- $this->infoDb->insert($info);
- // 計數
- if ($info['image_uri'] != '') {
- // 掃臉
- $this->gsDb->where('key', 'counter_a')->update(['value' => DB::raw("value+1")]);
- } else {
- // 留言
- $this->gsDb->where('key', 'counter_b')->update(['value' => DB::raw("value+1")]);
- }
-
- return true;
- }
-
- public function counterGet($type)
- {
- $counter = $this->gsDb->select(['value'])->where('key', '=', 'counter_' . $type)->first();
- $counter = (isset($counter)) ? $counter->toArray() : [];
- $counter = (count($counter) > 0) ? (int)$counter['value'] : 0;
-
- return $counter;
- }
-
- public function counterSet($type)
- {
- $this->gsDb->where('key', 'counter_' . $type)->update(['value' => DB::raw("value+1")]);
-
- return true;
- }
-
- public function reset()
- {
- DB::select(DB::raw("truncate table infos;"));
- DB::update(DB::raw("update general_settings set value=0 where 1=1;"));
-
- return true;
- }
-
- public function getUserIP()
- {
- $ipaddress = '';
- if (isset($_SERVER['HTTP_CLIENT_IP']))
- $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
- else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
- $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
- else if (isset($_SERVER['HTTP_X_FORWARDED']))
- $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
- else if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
- $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
- else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
- $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
- else if (isset($_SERVER['HTTP_FORWARDED']))
- $ipaddress = $_SERVER['HTTP_FORWARDED'];
- else if (isset($_SERVER['REMOTE_ADDR']))
- $ipaddress = $_SERVER['REMOTE_ADDR'];
- else
- $ipaddress = 'UNKNOWN';
-
- return $ipaddress;
- }
-
- }
|