<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/2/14
 * Time: 18:09
 */

namespace app\common\service;


use file\DirHelper;
use think\Controller;

class WebService extends Controller
{
    /**
     * 安装检测
     */
    public function checkInstalled()
    {
        if ($this->request->module() != 'install' && !is_installed()) {
            // 检测是否安装过,未安装直接跳转到安装导向
            $this->redirect("/install");
        }
    }

    /**
     * 检查是否开启用户中心
     */
    public function checkOpenUserModule()
    {
        if (empty(\think\Env::get("open_user_module", false))) {
            $this->error('暂不开放此功能', '/');
        }
    }

    /**
     * 记录请求的详细数据,用来测试第三方回调数据
     * @param string $msg [描述信息]
     * @return string
     */
    public function record($msg = "")
    {
        $savePath = RUNTIME_PATH . "notify_log" . DS . date('Y-m-d', time()) . DS;//日志目录
        DirHelper::makeDir($savePath);
        $saveFile = $savePath . date('H-i-s_', time()) . '_' . rand(1000, 9999) . ".log";

        $input = "TIME:    " . date('Y-m-d H:i:s', time()) . PHP_EOL
            . "URL:     " . $this->request->url() . PHP_EOL
            . "MESSAGE: " . $msg . PHP_EOL . PHP_EOL;

        if ($this->request->isGet() && $this->request->get()) {
            $input = $input . "METHOD:  " . "GET" . PHP_EOL
                . "PARAMS:  " . json_encode($this->request->get(), JSON_UNESCAPED_UNICODE) . PHP_EOL . PHP_EOL;
        } elseif ($this->request->isPost() && $this->request->param()) {
            $input = $input . "METHOD:  " . "POST" . PHP_EOL
                . "PARAMS:  " . json_encode($this->request->param(), JSON_UNESCAPED_UNICODE) . PHP_EOL . PHP_EOL;
        }
        $callbackBody = file_get_contents('php://input');
        if ($callbackBody) {
            $input = $input . "METHOD:  " . "INPUT" . PHP_EOL
                . "PARAMS:  " . json_encode($callbackBody, JSON_UNESCAPED_UNICODE) . PHP_EOL . PHP_EOL;
        }

        if (false == file_put_contents($saveFile, $input)) {
            exit("创建文件失败，请检查目录权限");
        }
        return $msg;
    }
}