<?php

namespace App\Web\Controllers;


use App\Exceptions\ApiException;
use App\Http\Controllers\BaseController;
use App\Web\Services\GlobalConfigService;
use Illuminate\Http\Request;


class SysGlobalController extends BaseController
{

    protected $service = null;

    /**
     * 构造函数
     * BlogController constructor.
     */
    public function __construct(GlobalConfigService $service)
    {
        $this->service = $service;
    }

    public function notificationEmail(Request $request)
    {
        $signature = $request->input('signature');
        $timestamp = $request->input('timestamp');
        $emails = [];
        $validTime = time() - 7200;
        if (!empty($signature) && !empty($timestamp) && $timestamp > $validTime) {
            $params = ['global_key' => 'MP_SYSTEM_MESSAGES'];
            $data = $this->service->getGlobalConfigInfo($params);
            if(!empty($data['value'])) {
                $mpSystemMessages = json_decode($data['value'], true);
                if(!empty($mpSystemMessages['sys_app_key'])
                    && md5($mpSystemMessages['sys_app_key'].$timestamp) == $signature) {
                    //通过校验
                    $emails = $mpSystemMessages['to_mail'];
                }
            }
        } else {
            throw new ApiException(500);
        }
        return $this->jsonResponse(
            'ok',
            $emails
        );
    }

}
