$inputs = $app->sfunc->getContentTypeFromPost();
 //make sure what gifts are we going to receive by id
 if (!isset($inputs["targets"])) {
     $app->sfunc->badRequest400($app, "targetMissing");
 }
 $targets = $app->sfunc->convertStringToIntArray($inputs["targets"]);
 try {
     // Check that an access token is present and is valid
     $app->oauth->resource->isValidRequest();
     //get and check the user id by AccessToken
     $app->sfunc->isValidUUID($app, $uuid);
     $thisTime = "'" . $app->sfunc->getGMT() . "'";
     // use ' to quote the time string
     $outputString = "";
     $outputString2 = "";
     $thsUser = NiuUsrInfo::findFirst("id = " . $uuid);
     foreach ($targets as $val) {
         //GiftBox::findFirst("id = " . $val . " AND targetid = " . $uuid. " AND expired_at > " . $thisTime);
         $gBoxEntry = GiftBox::findFirst(array("conditions" => "id = {$val} AND targetid = {$uuid} AND expired_at > {$thisTime}"));
         if (!$gBoxEntry) {
             $outputString2 = $outputString2 . "," . $val;
             continue;
         }
         $giftContent = $app->sfunc->convertStringToIntArray($gBoxEntry->json);
         //now we get int[,]
         //if giftContent[0] is not array
         //$giftContent[0] is the type based on public enum NiuPurchaseType,
         //$giftContent[1] is the index of the item, used in cashcard/ eq
         //$giftContent[2] is the amount in general, used in diamond/ cash
         //if giftContent[0] is array
         //$giftContent[0][$i] is the type based on public enum NiuPurchaseType,
        $app->response->redirect($redirectUri, true)->sendHeaders();
    } else {
        $app->sfunc->forbidden403($app);
    }
});
//special autheroize for game server
$app->get('/{uuid:[0-9]+}/{gasecret:[A-Z,0-9]+}/authorize', function ($uuid, $gasecret) use($app) {
    $starttime = time();
    $authParams = null;
    try {
        $codeGrant = $app->oauth->authorize->getGrantType('authorization_code');
        $authParams = $codeGrant->checkAuthorizeParams();
    } catch (\Exception $e) {
        return $app->oauth->catcher($e);
    }
    $user = NiuUsrInfo::findFirst(array("gasecret=\"{$gasecret}\"", "id={$uuid}"));
    if ($user && $authParams) {
        $redirectUri = $codeGrant->newAuthorizeRequest('niugame', $uuid, $authParams);
        $outputArray = array('code' => explode("=", $redirectUri)[1]);
        $outputArray['time'] = time() - $starttime;
        $app->sfunc->jsonOutput($app, $outputArray);
    } else {
        $app->sfunc->badRequest400($app);
    }
});
//after route wether succcess or not
$app->after(function () use($app) {
    $returned = $app->getReturnedValue();
    $app->response->sendHeaders();
    if ($returned) {
        if (is_scalar($returned)) {