<?php


namespace Heanup\App\Api\Controller;


use Heanup\App\Api\RestfulController;
use Heanup\Frame\Logger;

class ReceivedSms extends RestfulController
{
    protected function main()
    {

        $this->showSuccess();
//        file_put_contents(APP_DIR . "/test_msg.txt", file_get_contents("php://input").PHP_EOL,FILE_APPEND);
    }

    protected function getData()
    {
        $data = file(APP_DIR . "/test_msg.txt");
        $result = [];
        foreach ($data as $datum) {
            $pat = "#\[(.*)\]-\[IP.*\[操作:(.*)\]#iUs";
            preg_match($pat, $datum, $arr);
            if ($arr) {
                $temp = explode("$$$", $arr[2]);
                $result[] = [
                    'from' => $temp[1],
                    'to' => $temp[2],
                    'msg' => $temp[0],
                    'time' => $arr[1]
                ];
            }
        }
        $result=array_reverse($result);
        $this->showSuccess($result);
    }

    protected function postData()
    {
        $msg = file_get_contents("php://input");
        $temp = explode("$$$", $msg);
        $data = [
            "token" => '1234',
            'api' => 'SendTextMsg',
            'msg' => $temp[1] . ":" . $temp[0],
            'to_wxid' => 'heanup',
            'robot_wxid' => 'wxid_dt0ww4moe0qj22'
        ];
        $curl = curl_init();
        $data = json_encode($data);
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://myhome.97admin.com:8808',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        curl_exec($curl);
        curl_close($curl);
        Logger::setLog($msg, APP_DIR . "/test_msg.txt");
    }

    protected function putData()
    {
        // TODO: Implement putData() method.
    }

    protected function deleteData()
    {
        // TODO: Implement deleteData() method.
    }
}