mirror of
https://github.com/crmeb/CRMEB.git
synced 2026-10-28 13:31:04 +00:00
208 lines
6.7 KiB
PHP
208 lines
6.7 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
// +----------------------------------------------------------------------
|
||
// | Author: CRMEB Team <admin@crmeb.com>
|
||
// +----------------------------------------------------------------------
|
||
namespace app\api\controller\v1\store;
|
||
|
||
use app\Request;
|
||
use app\services\activity\coupon\StoreCouponIssueServices;
|
||
use app\services\product\product\StoreProductCouponServices;
|
||
|
||
/**
|
||
* 优惠券类
|
||
* Class StoreCouponsController
|
||
* @package app\api\controller\store
|
||
*/
|
||
class StoreCouponsController
|
||
{
|
||
/**
|
||
* @var StoreCouponIssueServices
|
||
*/
|
||
protected $services;
|
||
|
||
public function __construct(StoreCouponIssueServices $services)
|
||
{
|
||
$this->services = $services;
|
||
}
|
||
|
||
/**
|
||
* 可领取优惠券列表
|
||
* @param Request $request
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function lst(Request $request)
|
||
{
|
||
$where = $request->getMore([
|
||
['type', 0],
|
||
['product_id', 0],
|
||
['num', 0]
|
||
]);
|
||
return app('json')->success($this->services->getIssueCouponList($request->uid(), $where)['list']);
|
||
}
|
||
|
||
/**
|
||
* 领取优惠券
|
||
* @param Request $request
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
/**
|
||
* 批量领取优惠券
|
||
* @param Request $request
|
||
* @return mixed
|
||
*/
|
||
public function receiveBatch(Request $request)
|
||
{
|
||
list($couponIds) = $request->postMore([
|
||
['couponIds', []]
|
||
], true);
|
||
if (!is_array($couponIds) || empty($couponIds)) return app('json')->fail('参数错误');
|
||
|
||
/** @var StoreCouponIssueServices $couponIssueService */
|
||
$couponIssueService = app()->make(StoreCouponIssueServices::class);
|
||
foreach ($couponIds as $couponId) {
|
||
if ($couponId && is_numeric($couponId)) {
|
||
try {
|
||
$couponIssueService->issueUserCoupon($couponId, $request->user(), true);
|
||
} catch (\Exception $e) {
|
||
// 跳过失败的领取
|
||
continue;
|
||
}
|
||
}
|
||
}
|
||
return app('json')->success('领取成功');
|
||
}
|
||
|
||
/**
|
||
* 领取优惠券
|
||
* @param Request $request
|
||
* @return mixed
|
||
*/
|
||
public function receive(Request $request)
|
||
{
|
||
list($couponId) = $request->getMore([
|
||
['couponId', 0]
|
||
], true);
|
||
if (!$couponId || !is_numeric($couponId)) return app('json')->fail('参数错误');
|
||
|
||
/** @var StoreCouponIssueServices $couponIssueService */
|
||
$couponIssueService = app()->make(StoreCouponIssueServices::class);
|
||
$couponIssueService->issueUserCoupon($couponId, $request->user(), true);
|
||
return app('json')->success('领取成功');
|
||
}
|
||
|
||
/**
|
||
* 用户已领取优惠券
|
||
* @param Request $request
|
||
* @param $types
|
||
* @return mixed
|
||
*/
|
||
public function user(Request $request, $types)
|
||
{
|
||
$uid = (int)$request->uid();
|
||
return app('json')->success($this->services->getUserCouponList($uid, $types));
|
||
}
|
||
|
||
/**
|
||
* 优惠券 订单获取
|
||
* @param Request $request
|
||
* @param StoreCouponIssueServices $service
|
||
* @param $cartId
|
||
* @param $new
|
||
* @param $shippingType
|
||
* @return mixed
|
||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function order(Request $request, StoreCouponIssueServices $service, $cartId, $new, $shippingType)
|
||
{
|
||
return app('json')->success($service->beUsableCouponList((int)$request->uid(), $cartId, !!$new, (int)$shippingType));
|
||
}
|
||
|
||
/**
|
||
* 可领取优惠券列表(v2)
|
||
* @param Request $request
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function lstV2(Request $request)
|
||
{
|
||
$where = $request->getMore([
|
||
['type', 0],
|
||
['product_id', 0]
|
||
]);
|
||
return app('json')->success($this->services->getIssueCouponList($request->uid(), $where));
|
||
}
|
||
|
||
/**
|
||
* 获取新人券(v2)
|
||
* @param Request $request
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getNewCoupon(Request $request)
|
||
{
|
||
$userInfo = $request->user();
|
||
$data = [];
|
||
/** @var StoreCouponIssueServices $couponService */
|
||
$couponService = app()->make(StoreCouponIssueServices::class);
|
||
$data['list'] = $couponService->getNewCoupon();
|
||
$data['image'] = sys_config('coupon_img');
|
||
if ($userInfo->add_time === $userInfo->last_time) {
|
||
$data['show'] = 1;
|
||
} else {
|
||
$data['show'] = 0;
|
||
}
|
||
return app('json')->success($data);
|
||
}
|
||
|
||
/**
|
||
* 赠送下单之后订单中 关联优惠劵(v2)
|
||
* @param Request $request
|
||
* @param $orderId
|
||
* @return mixed
|
||
*/
|
||
public function getOrderProductCoupon(Request $request, $orderId)
|
||
{
|
||
$uid = (int)$request->uid() ?? 0;
|
||
if (!$orderId) {
|
||
return app('json')->fail('参数错误');
|
||
}
|
||
/** @var StoreProductCouponServices $storeProductCoupon */
|
||
$storeProductCoupon = app()->make(StoreProductCouponServices::class);
|
||
$list = $storeProductCoupon->getOrderProductCoupon($uid, $orderId);
|
||
return app('json')->success($list);
|
||
}
|
||
|
||
/**
|
||
* 获取每日新增的优惠券(v2)
|
||
* @return mixed
|
||
*/
|
||
public function getTodayCoupon(Request $request)
|
||
{
|
||
$uid = $request->uid() ?? 0;
|
||
/** @var StoreCouponIssueServices $couponService */
|
||
$couponService = app()->make(StoreCouponIssueServices::class);
|
||
$data['list'] = $couponService->getTodayCoupon($uid);
|
||
$data['image'] = sys_config('coupon_img');
|
||
return app('json')->success($data);
|
||
}
|
||
}
|