<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Miniprogram\Auth;
use Illuminate\Http\Request;

class LogController extends Controller
{
    //
    public function accessLog(Request $request)
    {
        try {
            $uid = Auth::auth();
        } catch (\Exception $e) {
            $uid = $request->input('uid', 0);
        }
        $data = $request->only(['app_id', 'uuid']);
        $url = urldecode($request->input('url'));
        $data['start_time'] = time();
        $data['uid'] = $uid;
        $data["url"] = stripos($url, "?") ? substr($url, 0, stripos($url, "?")) : $url;
        $data['ip'] = ip2long($_SERVER['REMOTE_ADDR']);
        $data['browser'] = $_SERVER['HTTP_USER_AGENT'];
        $data['query'] = substr(json_encode($request->input('query')), 0, 256);
        \DB::connection('mysql_datalog')->table('page_access_logs_' . date('Ym'))->insert($data);
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }


    public function groupLog(Request $request)
    {
        $group_name = \DB::connection('mysql_datalog')->table("group_logs")->where(
            'group_id',
            $request->post('group_id')
        )->value('group_name') ?: '';
        $data = [
            'uid' => $request->post('uid'),
            'group_id' => $request->post('group_id'),
            'group_name' => $group_name,
            'created_at' => time()
        ];
        \DB::connection('mysql_datalog')->table("group_logs")->insert($data);
        return response([
            'code' => 200,
            'message' => 'OK'
        ]);
    }
}
