<?php

namespace app\api\controller;

use think\Request;
use app\api\validate\ArticleValidate;
use app\BaseController;
use app\model\ArticleModel;

class Wecaht extends BaseController
{


    public function index()
    {
        $get = input();
        exit($get['echostr']);

//        // 配置 Token（需与微信公众号后台设置一致）
//        define("WX_TOKEN", "YOUR_TOKEN_HERE");
//        // 处理微信服务器发送的 GET 验证请求
//        if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['echostr'])) {
//            $signature = $_GET['signature'] ?? '';
//            $timestamp = $_GET['timestamp'] ?? '';
//            $nonce = $_GET['nonce'] ?? '';
//            $echostr = $_GET['echostr'] ?? '';
//
//            // 参数缺失检测
//            if (empty($signature) || empty($timestamp) || empty($nonce) || empty($echostr)) {
//                exit('Invalid Request: Missing Parameters');
//            }
//
//            // 1. 生成本地签名
//            $tmpArr = [WX_TOKEN, $timestamp, $nonce];
//            sort($tmpArr, SORT_STRING); // 字典序排序
//            $tmpStr = implode('', $tmpArr); // 拼接字符串
//            $localSign = sha1($tmpStr); // SHA1 加密
//
//            // 2. 验证签名并返回 echostr
//            if ($localSign == $signature) {
//                header('Content-Type: text/plain; charset=utf-8');
//                exit($echostr);
//            } else {
//                exit('Signature Verification Failed');
//            }
//            return true;
//        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = 'VtUEf2MvC0dMMaDGwMT8';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

}