<?php


namespace App\Http\Controllers\QuickApp\Push;


use App\Consts\ErrorConst;
use App\Http\Controllers\QuickApp\BaseController;
use App\Libs\ApiResponse;
use App\Libs\Utils;
use App\Modules\Push\Services\PushMessageService;
use Illuminate\Http\Request;
use App\Exceptions\ApiException;

class PushController extends BaseController
{
    use ApiResponse;

    /**
     * @param Request $request
     * @return mixed
     * @throws ApiException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function pushToUser(Request $request)
    {
        $all     = $request->all();
        $uid     = (int)getProp($all, 'uid');
        $title   = trim(getProp($all, 'title'));
        $content = trim(getProp($all, 'content'));
        $url     = trim(getProp($all, 'url'));
        $sign    = trim(getProp($all, 'sign'));
        if (empty($uid) || empty($title) || empty($content) || empty($url)) {
            Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
        }

        // 签名校验
        if ($sign !== 'dqu7nsNZY&A8AEzwNQ*WpbjHMd6bUt@V') {
            Utils::throwError(ErrorConst::SIGN_NOT_ACCESS);
        }

        // 更新用户reg_id
        $result = PushMessageService::pushMessageToUser($uid, $title, $content, $url);

        return $this->success($result);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function vivoCallback(Request $request)
    {
        myLog('push-callback')->info('vivo', $request->all());
        return response()->success();
    }
}