246 lines
7.5 KiB
PHP
Executable File
246 lines
7.5 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controllers\User;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Huodong;
|
|
use App\Models\Order;
|
|
use App\Models\Product;
|
|
use App\Models\UserCoupon;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Slim\Http\Response;
|
|
use Slim\Http\ServerRequest;
|
|
use function explode;
|
|
use function in_array;
|
|
use function json_decode;
|
|
use function time;
|
|
|
|
final class CouponController extends BaseController
|
|
{
|
|
|
|
public function check(ServerRequest $request, Response $response, array $args): ResponseInterface
|
|
{
|
|
$coupon_raw = $this->antiXss->xss_clean($request->getParam('coupon'));
|
|
$product_id = $this->antiXss->xss_clean($request->getParam('product_id'));
|
|
$invalid_coupon_msg = '优惠码无效';
|
|
|
|
if ($coupon_raw === '') {
|
|
return $response->withJson([
|
|
'ret' => 2,
|
|
'msg' => "不使用优惠码",
|
|
]);
|
|
}
|
|
|
|
$coupon = (new UserCoupon())->where('code', $coupon_raw)->first();
|
|
|
|
if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$product = (new Product())->where('id', $product_id)->first();
|
|
|
|
if ($product === null) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$limit = json_decode($coupon->limit);
|
|
|
|
if ($limit->disabled) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
if ($limit->product_id !== '' && ! in_array($product_id, explode(',', $limit->product_id))) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$user = $this->user;
|
|
$use_limit = $limit->use_time;
|
|
|
|
if ($use_limit > 0) {
|
|
$user_use_count = (new Order())->where('user_id', $user->id)->where('coupon', $coupon->code)->count();
|
|
if ($user_use_count >= $use_limit) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$total_use_limit = $limit->total_use_time;
|
|
|
|
if ($total_use_limit > 0 && $coupon->use_count >= $total_use_limit) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$content = json_decode($coupon->content);
|
|
$invalid_coupon_msg = "优惠码有效";
|
|
// $iRet = 1;
|
|
// $sMsg = "优惠码可用";
|
|
//
|
|
// $sDateNow = date("Y-m-d");
|
|
//// $sDateNow = "2025-05-02";
|
|
// $oHuodong = Huodong::where("start_date", "<=", $sDateNow)->where("end_date", ">=", $sDateNow)->first();
|
|
//
|
|
// if ($oHuodong) {
|
|
// if ($oHuodong->class == "A") {
|
|
// if ($content->type === 'percentage') {
|
|
//
|
|
// $content->value = $oHuodong->coupon_global;
|
|
// $iRet = 2;
|
|
// $sMsg = '该优惠码可用,在【'.$oHuodong->title.'】期间视为-'.$oHuodong->coupon_global.'%优惠。';
|
|
//
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// if ($content->type === 'percentage') {
|
|
// $discount = $product->price * $content->value / 100;
|
|
// } else {
|
|
// $discount = $content->value;
|
|
// }
|
|
//
|
|
// $buy_price = $product->price - $discount;
|
|
|
|
// return $response->withJson([
|
|
// 'ret' => $iRet,
|
|
// 'msg' => $sMsg,
|
|
// 'discount' => $discount,
|
|
// 'buy_price' => $buy_price,
|
|
// ]);
|
|
|
|
return $response->withJson([
|
|
'ret' => 1,
|
|
'msg' => $invalid_coupon_msg,
|
|
'coupon_value' => $content->value,
|
|
// 'name' => 8888,
|
|
// 'code' => 111,
|
|
// 'shop' => 222,
|
|
// 'credit' => $discount,
|
|
// 'total' => $buy_price,
|
|
]);
|
|
}
|
|
|
|
public function check_old(ServerRequest $request, Response $response, array $args): ResponseInterface
|
|
{
|
|
$coupon_raw = $this->antiXss->xss_clean($request->getParam('coupon'));
|
|
$product_id = $this->antiXss->xss_clean($request->getParam('product_id'));
|
|
$invalid_coupon_msg = '优惠码无效';
|
|
|
|
if ($coupon_raw === '') {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$coupon = (new UserCoupon())->where('code', $coupon_raw)->first();
|
|
|
|
if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$product = (new Product())->where('id', $product_id)->first();
|
|
|
|
if ($product === null) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$limit = json_decode($coupon->limit);
|
|
|
|
if ($limit->disabled) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
if ($limit->product_id !== '' && ! in_array($product_id, explode(',', $limit->product_id))) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$user = $this->user;
|
|
$use_limit = $limit->use_time;
|
|
|
|
if ($use_limit > 0) {
|
|
$user_use_count = (new Order())->where('user_id', $user->id)->where('coupon', $coupon->code)->count();
|
|
if ($user_use_count >= $use_limit) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$total_use_limit = $limit->total_use_time;
|
|
|
|
if ($total_use_limit > 0 && $coupon->use_count >= $total_use_limit) {
|
|
return $response->withJson([
|
|
'ret' => 0,
|
|
'msg' => $invalid_coupon_msg,
|
|
]);
|
|
}
|
|
|
|
$content = json_decode($coupon->content);
|
|
|
|
$iRet = 1;
|
|
$sMsg = "优惠码可用";
|
|
|
|
$sDateNow = date("Y-m-d");
|
|
// $sDateNow = "2025-05-02";
|
|
$oHuodong = Huodong::where("start_date", "<=", $sDateNow)->where("end_date", ">=", $sDateNow)->first();
|
|
|
|
if ($oHuodong) {
|
|
if ($oHuodong->class == "A") {
|
|
if ($content->type === 'percentage') {
|
|
|
|
$content->value = $oHuodong->coupon_global;
|
|
$iRet = 2;
|
|
$sMsg = '该优惠码可用,在【'.$oHuodong->title.'】期间视为-'.$oHuodong->coupon_global.'%优惠。';
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($content->type === 'percentage') {
|
|
$discount = $product->price * $content->value / 100;
|
|
} else {
|
|
$discount = $content->value;
|
|
}
|
|
|
|
$buy_price = $product->price - $discount;
|
|
|
|
return $response->withJson([
|
|
'ret' => $iRet,
|
|
'msg' => $sMsg,
|
|
'discount' => $discount,
|
|
'buy_price' => $buy_price,
|
|
]);
|
|
}
|
|
}
|