<?php
namespace App\Controller;
use App\Entity\OGoodsImage;
use App\Entity\OIndexHeadImage;
use App\Entity\OIndexHighlight;
use App\Entity\OGoods;
use App\Entity\OGoodsIntro;
use App\Entity\OOrder;
use App\Entity\OSubscribe;
use App\Service\jsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class IndexController extends AbstractController
{
/**
* @todo
* @Route("/test/{str}", name="test")
*/
public function test($str)
{
$str = str_replace(',','","',$str);
$re = '["'.$str.'"]';
echo $re;exit;
}
/**
* @Route("/", name="index")
*/
public function index(): Response
{
$D = $this->getDoctrine();
$firstImage = $D->getRepository(OIndexHeadImage::class)->firstImage();
$highlights = $D->getRepository(OIndexHighlight::class)->highlights();
$indexGoods = $D->getRepository(OGoods::class)->indexGoods();
return $this->render('index/index.html.twig', [
'firstImage' => $firstImage,
'highlights' => $highlights,
'indexGoods' => $indexGoods,
]);
}
/**
* @Route("/subscribe", name="subscribe")
*/
public function subscribe(Request $request, ValidatorInterface $validator, jsonResponse $jsonResponse)
{
$email = $request->query->get('email');
$type = $request->query->get('type');
$em = $this->getDoctrine()->getManager();
$s = new OSubscribe();
$s->setOEmail($email);
$s->setOGender($type);
$eor = $validator->validate($s);
if (count($eor) > 0) {
$jsonResponse->setEor('error');
}else{
$em->persist($s);
$em->flush();
}
return $this->json($jsonResponse->return());
}
/**
* @Route("/order", name="order")
*/
public function order(Request $request, ValidatorInterface $validator, jsonResponse $jsonResponse)
{
$id = $request->query->get('id');
$quantity = $request->query->get('quantity');
$color = $request->query->get('color');
$size = $request->query->get('size');
$phone = $request->query->get('phone');
$email = $request->query->get('email');
$content = $request->query->get('content');
$price = $request->query->get('price');
$em = $this->getDoctrine()->getManager();
$goods = $em->getRepository(OGoods::class)->find($id);
if (!$goods) {
$jsonResponse->setEor('Goods does not exist.');
return $this->json($jsonResponse->return());
}
$order = new OOrder();
$order->setOQuantity($quantity);
$order->setOColor($color);
$order->setOSize($size);
$order->setOPhone($phone);
$order->setOEmail($email);
$order->setOContent($content);
$order->setOGoods($goods);
$order->setOPirce($price);
$user = $this->getUser();
if($user){
$order->setOUser($user);
}
$eor = $validator->validate($order);
if (count($eor) > 0) {
$jsonResponse->setEor($eor);
}else{
$em->persist($order);
$em->flush();
}
return $this->json($jsonResponse->return());
}
/**
* @Route(
* "/list/{category}/{sub}/{sort}",
* name="list",
* defaults={"sort":"recommend"},
* requirements={
* "sort": "price|price_desc|newest",
* }
* )
*/
public function list($category, $sub, $sort)
{
$D = $this->getDoctrine();
$goodses = $D->getRepository(OGoods::class)->getGoodsList($category, $sub, $sort);
return $this->render('index/list.html.twig', [
'goodses' => $goodses,
'sort' => $sort,
'category' => $category,
'sub' => $sub
]);
}
/**
* TODO
* @Route("/aaa", name="aaa")
*/
public function aaa(Request $request)
{
$hex1 = $request->query->get('hex1');
$hex2 = $request->query->get('hex2','');
$id = $request->query->get('id');
$type = $request->query->get('t');
$D = $this->getDoctrine();
$im = $D->getRepository(OGoodsImage::class);
$obj = $im->find($id);
$hex = $im->getImageHex($type, $hex1, $hex2);
$obj->setOHex($hex);
$em = $D->getManager();
$em->persist($obj);
$em->flush();
echo 1;exit;
}
/**
* @Route("/search", name="search")
*/
public function search(Request $request)
{
$key = $request->query->get('key');
$D = $this->getDoctrine();
$goodses = $D->getRepository(OGoods::class)->getGoodsListByKeyword($key);
return $this->render('index/search.html.twig', [
'goodses' => $goodses,
'key' => $key,
]);
}
/**
* @Route("/detail/{id}", name="detail")
*/
public function detail($id)
{
$D = $this->getDoctrine();
$goods = $D->getRepository(OGoods::class)->getGoods($id);
$intro = $D->getRepository(OGoodsIntro::class)->findBy(['oGoods'=>$goods],['oSort'=>'ASC']);
return $this->render('index/detail.html.twig', [
'goods' => $goods,
'intro' => $intro
]);
}
/**
* TODO
* @Route("/image/{id}", name="image")
*/
public function image($id)
{
$D = $this->getDoctrine();
$goods = $D->getRepository(OGoodsImage::class)->find($id);
return $this->render('index/image.html.twig', [
'image' => $goods
]);
}
/**
* TODO
* @Route("/hex/{id}/{x}/{y}", name="hex")
*/
public function imageHex($id,$x,$y)
{
$DRe = $this->getDoctrine()->getRepository(OGoodsImage::class);
$goods = $DRe->find($id);
$image = $this->getParameter('cdn_url').$goods->getOUrl();
list($width, $height, $type, $attr) = getimagesize($image);
$x = intval($x/500*$width);
$y = intval($y/500*$width);
$hex = $DRe->point_color($image,$x,$y);
echo $hex;exit;
}
/**
* @Route("/reset-password", name="reset-password")
*/
public function resetPassword()
{
return $this->render('index/resetPassword.html.twig', []);
}
/**
* @Route("/filter", name="filter")
*/
public function getFilterData()
{
}
}