chore: 添加静态资源文件和版本配置文件

添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材
添加系统版本配置文件(.version)和常量定义文件(.constant)
更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件
添加验证码配置文件(captcha.php)和.gitignore规则
This commit is contained in:
2026-03-06 20:14:23 +08:00
commit c9a98f342c
4485 changed files with 490986 additions and 0 deletions

View File

@@ -0,0 +1,947 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\activity\coupon;
use app\services\BaseServices;
use app\dao\activity\coupon\StoreCouponIssueDao;
use app\services\order\StoreCartServices;
use app\services\other\queue\QueueServices;
use app\services\product\brand\StoreBrandServices;
use app\services\product\category\StoreProductCategoryServices;
use app\services\product\product\StoreProductServices;
use app\services\user\member\MemberCardServices;
use app\services\user\member\MemberRightServices;
use app\services\user\UserServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder;
use crmeb\traits\OptionTrait;
use think\annotation\Inject;
use think\exception\ValidateException;
/**
*
* Class StoreCouponIssueServices
* @package app\services\activity\coupon
* @mixin StoreCouponIssueDao
*/
class StoreCouponIssueServices extends BaseServices
{
use OptionTrait;
public array $_couponType = [0 => "通用券", 1 => "品类券", 2 => '商品券', 3 => '品牌券'];
/**
* @var StoreCouponIssueDao
*/
#[Inject]
protected StoreCouponIssueDao $dao;
/**
* 获取已发布列表
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCouponIssueList(array $where)
{
[$page, $limit] = $this->getPageValue();
$where['is_del'] = 0;
$list = $this->dao->getList($where, $page, $limit);
foreach ($list as &$item) {
if (!$item['coupon_time']) {
$item['coupon_time'] = ceil(($item['end_use_time'] - $item['start_use_time']) / '86400');
}
$item['coupon_time'] = $item['coupon_time'] . '天';
}
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 获取会员优惠券列表
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getMemberCouponIssueList(array $where, int $page, int $limit, array $with = [])
{
return $this->dao->getValidList($where, '*', $page, $limit, $with);
}
/**
* 新增优惠券
* @param $data
* @return bool
*/
public function saveCoupon($data)
{
$data['start_use_time'] = strtotime((string)$data['start_use_time']);
$data['end_use_time'] = strtotime((string)$data['end_use_time']);
$data['start_time'] = strtotime((string)$data['start_time']);
$data['end_time'] = strtotime((string)$data['end_time']);
$data['title'] = $data['coupon_title'];
$data['remain_count'] = $data['total_count'];
$data['add_time'] = time();
if ($data['id'] != 0) {
$res = $this->dao->update($data['id'], $data);
app()->make(StoreCouponUserServices::class)->update(
['cid' => $data['id'], 'status' => 0],
['coupon_title' => $data['coupon_title'], 'coupon_price' => $data['coupon_price']],
);
} else {
$res = $this->dao->save($data);
if ($data['product_id'] !== '' && $res) {
$productIds = explode(',', $data['product_id']);
$couponData = [];
foreach ($productIds as $product_id) {
$couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
}
/** @var StoreCouponProductServices $storeCouponProductService */
$storeCouponProductService = app()->make(StoreCouponProductServices::class);
$storeCouponProductService->saveAll($couponData);
}
}
if (!$res) throw new AdminException('添加优惠券失败!');
return true;
}
/**
* 修改状态
* @param int $id
* @return array
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function createForm(int $id)
{
$issueInfo = $this->dao->get($id);
if (-1 == $issueInfo['status'] || 1 == $issueInfo['is_del']) throw new ValidateException('状态错误,无法修改');
$f = [FormBuilder::radio('status', '是否开启', $issueInfo['status'])->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])];
return create_form('状态修改', $f, $this->url('/marketing/coupon/released/status/' . $id), 'PUT');
}
/**
* 领取记录
* @param int $id
* @return array
*/
public function issueLog(int $id)
{
$coupon = $this->dao->get($id);
if (!$coupon) {
throw new ValidateException('优惠券不存在');
}
if ($coupon['category'] != 2) {
/** @var StoreCouponIssueUserServices $storeCouponIssueUserService */
$storeCouponIssueUserService = app()->make(StoreCouponIssueUserServices::class);
return $storeCouponIssueUserService->issueLog(['issue_coupon_id' => $id]);
} else {//会员券
/** @var StoreCouponUserServices $storeCouponUserService */
$storeCouponUserService = app()->make(StoreCouponUserServices::class);
return $storeCouponUserService->issueLog(['cid' => $id]);
}
}
/**
* 保存赠送给用户优惠券
* @param int $uid
* @param array $couponList
* @param string $type
* @return array
*/
public function saveGiveCoupon(int $uid, array $couponList, string $type = 'get')
{
if (!$uid || !$couponList) {
return [];
}
$couponData = [];
$issueUserData = [];
$time = time();
/** @var StoreCouponIssueUserServices $issueUser */
$issueUser = app()->make(StoreCouponIssueUserServices::class);
foreach ($couponList as $item) {
$data['cid'] = $item['id'];
$data['uid'] = $uid;
$data['coupon_title'] = $item['title'];
$data['coupon_price'] = $item['coupon_price'];
$data['use_min_price'] = $item['use_min_price'];
if ($item['coupon_time']) {
$data['add_time'] = $time;
$data['end_time'] = $data['add_time'] + $item['coupon_time'] * 86400;
} else {
$data['add_time'] = $item['start_use_time'];
$data['end_time'] = $item['end_use_time'];
}
$data['type'] = $type;
$issue['uid'] = $uid;
$issue['issue_coupon_id'] = $item['id'];
$issue['add_time'] = $time;
$issueUserData[] = $issue;
$couponData[] = $data;
if (!$item['is_permanent']) {
$remain_count = $item['remain_count'] > 0 ? $item['remain_count'] - 1 : 0;
$this->dao->update($item['id'], ['remain_count' => $remain_count]);
}
unset($data);
unset($issue);
}
if ($couponData) {
/** @var StoreCouponUserServices $storeCouponUser */
$storeCouponUser = app()->make(StoreCouponUserServices::class);
if (!$storeCouponUser->saveAll($couponData)) {
throw new AdminException('发劵失败');
}
}
if ($issueUserData) {
if (!$issueUser->saveAll($issueUserData)) {
throw new AdminException('发劵失败');
}
}
return $couponData;
}
/**
* 新人礼赠送优惠券
* @param int $uid
*/
public function newcomerGiveCoupon(int $uid)
{
if (!sys_config('newcomer_status')) {
return false;
}
$status = sys_config('register_coupon_status');
if (!$status) {//未开启
return true;
}
$couponIds = sys_config('register_give_coupon', []);
if (!$couponIds) {
return true;
}
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$userInfo = $userServices->getUserInfo($uid);
if (!$userInfo) {
return true;
}
$couponList = $this->dao->getGiveCoupon([['id', 'IN', $couponIds]]);
if ($couponList) {
$this->saveGiveCoupon($uid, $couponList, 'newcomer');
}
return true;
}
/**
* 会员卡激活赠送优惠券
* @param int $uid
*/
public function levelGiveCoupon(int $uid)
{
$status = sys_config('level_activate_status');
if (!$status) {//是否需要激活
return true;
}
$status = sys_config('level_coupon_status');
if (!$status) {//未开启
return true;
}
$couponIds = sys_config('level_give_coupon', []);
if (!$couponIds) {
return true;
}
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$userInfo = $userServices->getUserInfo($uid);
if (!$userInfo) {
return true;
}
$couponList = $this->dao->getGiveCoupon([['id', 'IN', $couponIds]]);
if ($couponList) {
$this->saveGiveCoupon($uid, $couponList, 'activate_level');
}
return true;
}
/**
* 关注送优惠券
* @param int $uid
*/
public function userFirstSubGiveCoupon(int $uid)
{
$couponList = $this->dao->getGiveCoupon(['receive_type' => 2]);
if ($couponList) {
$this->saveGiveCoupon($uid, $couponList, 'user_first');
}
return true;
}
/**
* 下单之后赠送
* @param $uid
*/
public function orderPayGiveCoupon($uid, $coupon_issue_ids)
{
if (!$coupon_issue_ids) return [];
$couponList = $this->dao->getGiveCoupon([['id', 'IN', $coupon_issue_ids]]);
$couponData = [];
if ($couponList) {
$couponData = $this->saveGiveCoupon($uid, $couponList, 'order');
}
return $couponData;
}
/**
* 获取商品关联
* @param int $uid
* @param int $product_id
* @param string $field
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getProductCouponList(int $uid, int $product_id, string $field = '*', int $limit = 0)
{
if ($limit) {
$page = 1;
} else {
[$page, $limit] = $this->getPageValue();
}
/** @var StoreProductServices $storeProductService */
$storeProductService = app()->make(StoreProductServices::class);
/** @var StoreProductCategoryServices $storeCategoryService */
$storeCategoryService = app()->make(StoreProductCategoryServices::class);
$productInfo = $storeProductService->getCacheProductInfo($product_id);
$cateId = explode(',', $productInfo['cate_id']);
$cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
$cateId = array_diff($cateId, [0]);
$brandIds = [];
if ($productInfo['brand_id']) {
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
$brandInfo = $storeBrandServices->get((int)$productInfo['brand_id'], ['id', 'pid']);
if ($brandInfo) {
$brandIds = $brandInfo->toArray();
$brandIds = array_diff($brandIds, [0]);
}
}
$where = ['product_id' => $productInfo['pid'] ?: $product_id, 'cate_id' => $cateId, 'brand_id' => $brandIds];
$list = $this->dao->getIssueCouponListNew($uid, $where, $field, $page, $limit);
foreach ($list as &$item) {
$item['coupon_price'] = floatval($item['coupon_price']);
$item['use_min_price'] = floatval($item['use_min_price']);
$item['is_use'] = $uid && isset($item['used']);
$item['use_status'] = 1;
if (isset($item['used']) && $item['used']) {
$item['use_status'] = $uid && isset($item['used'][0]['status']) && $item['used'][0]['status'] == '未使用' ? 1 : 0;
}
$item['start_time'] = $item['start_time'] ? date('Y-m-d', $item['start_time']) : 0;
$item['end_time'] = $item['end_time'] ? date('Y-m-d', $item['end_time']) : date('Y-m-d', time() + 86400);
}
return $list;
}
/**
* 获取优惠券列表
* @param int $uid
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getIssueCouponList(int $uid, array $where, string $field = '*', bool $is_product = false)
{
/** @var StoreProductServices $storeProductService */
$storeProductService = app()->make(StoreProductServices::class);
/** @var StoreProductCategoryServices $storeCategoryService */
$storeCategoryService = app()->make(StoreProductCategoryServices::class);
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
$cateId = 0;
$bandId = 0;
$product_id = 0;
$coupon_where = [];
if ((int)$where['product_id']) {//存在商品
$productInfo = $storeProductService->get((int)$where['product_id'], ['id,cate_id', 'brand_id']);
$product_id = $where['product_id'];
if ($productInfo) {
$cateId = $productInfo['cate_id'];
if ($cateId) {
$cateId = explode(',', $cateId);
$cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
$cateId = array_diff($cateId, [0]);
}
if ($productInfo['brand_id']) {
$brandInfo = $storeBrandServices->get((int)$productInfo['brand_id'], ['id', 'pid']);
if ($brandInfo) {
$bandId = $brandInfo->toArray();
$bandId = array_diff($bandId, [0]);
}
}
if ($where['type'] == 1) {
$coupon_where['cate_id'] = $cateId;
} elseif ($where['type'] == 2) {
$coupon_where['product_id'] = $where['product_id'];
} elseif ($where['type'] == 3) {
$coupon_where['brand_id'] = $where['brand_id'];
} else {
$coupon_where = ['product_id' => $product_id, 'cate_id' => $cateId, 'brand_id' => $bandId];
}
}
}
$where = array_merge($where, $coupon_where);
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getIssueCouponListNew($uid, $where, $field, $page, $limit);
if ($list) {
$limit = 1;
$field = ['id', 'image', 'store_name', 'price', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales'];
$categoryIds = array_unique(array_column($list, 'category_id'));
$brandIds = array_unique(array_column($list, 'brand_id'));
$category = $brands = [];
if ($categoryIds) {
$category = $storeCategoryService->getColumn([['id', 'IN', $categoryIds]], 'pid,cate_name', 'id');
}
if ($brandIds) {
$brands = $storeBrandServices->getColumn([['id', 'IN', $brandIds]], 'id,pid', 'id');
}
foreach ($list as &$item) {
$item['coupon_price'] = floatval($item['coupon_price']);
$item['use_min_price'] = floatval($item['use_min_price']);
$item['is_use'] = false;
if ($uid && $item['used'] != null && count($item['used']) >= $item['receive_limit']) $item['is_use'] = true;
$item['start_time'] = $item['end_time'] = '';
if ($item['coupon_time']) {//领取几天内使用
if (isset($item['used']['end_time']) && isset($item['used']['start_time'])) {
$item['start_time'] = $item['used']['start_time'] ? date('Y/m/d', $item['used']['start_time']) : '';
$item['end_time'] = $item['used']['end_time'] ? date('Y/m/d', $item['used']['end_time']) : '';
}
} else {//指定时间段使用
$item['start_time'] = $item['start_use_time'] ? date('Y/m/d', $item['start_use_time']) : '';
$item['end_time'] = $item['end_use_time'] ? date('Y/m/d', $item['end_use_time']) : '';
}
if (!$is_product) continue;
$product_where = [];
$product_where['is_show'] = 1;
$product_where['is_del'] = 0;
$product_where['is_verify'] = 1;
$product_where['use_min_price'] = $item['use_min_price'];
switch ($item['type']) {
case 0:
break;
case 1://品类券
$product_where['salesOrder'] = 'desc';
$category_type = ($category[$item['category_id'] ?? 0]['pid'] ?? 0) == 0 ? 1 : 2;
if ($category_type == 1) {
$product_where['cid'] = $item['category_id'];
} else {
$product_where['sid'] = $item['category_id'];
}
break;
case 2://商品劵
$product_where['salesOrder'] = 'desc';
$product_where['ids'] = $item['product_id'];
break;
case 3://品牌券
$product_where['salesOrder'] = 'desc';
$product_where['brand_id'] = $brands[$item['brand_id']] ?? [];
break;
default:
$item['products'] = [];
}
$item['products'] = get_thumb_water($storeProductService->getSearchList($product_where, 0, $limit, $field, $item['type'] == 0 ? 'rand' : '', []));
}
}
$data['list'] = $list;
$data['count'] = $this->dao->getIssueCouponCount($where['product_id'], $cateId, $bandId);
return $data;
}
/**
* 给用户发送优惠券
* @param int $uid
* @param int $id
* @param bool $more
* @param string $type
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function issueUserCoupon(int $uid, int $id, bool $more = true, string $type = 'get')
{
if (!$uid || !$id) {
throw new ValidateException('参数异常');
}
$issueCouponInfo = $this->dao->getInfo((int)$id);
if (!$issueCouponInfo) {
throw new ValidateException('领取的优惠劵已领完或已过期!');
}
if ($issueCouponInfo['remain_count'] <= 0 && !$issueCouponInfo['is_permanent']) {
throw new ValidateException('抱歉优惠券已经领取完了!');
}
//领取付费会员券
if ($issueCouponInfo['category'] == 2) {
/** @var MemberRightServices $memberRightService */
$memberRightService = app()->make(MemberRightServices::class);
if (!$memberRightService->getMemberRightStatus("coupon")) {
throw new ValidateException('暂时无法领取!');
}
$userServices = app()->make(UserServices::class);
if (!$userServices->checkUserIsSvip($uid)) {
throw new ValidateException('请先购买付费会员后领取!');
}
}
/** @var StoreCouponIssueUserServices $issueUserService */
$issueUserService = app()->make(StoreCouponIssueUserServices::class);
/** @var StoreCouponUserServices $couponUserService */
$couponUserService = app()->make(StoreCouponUserServices::class);
//是否多次领取
$receiveCount = $issueUserService->getCount(['uid' => $uid, 'issue_coupon_id' => $id]);
if (!$more) {
if ($receiveCount >= $issueCouponInfo['receive_limit']) {
throw new ValidateException('已领取过该优惠劵!');
}
}
return $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo, $type, $receiveCount) {
$issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
$res = $couponUserService->addUserCoupon($uid, $issueCouponInfo, $type);
if ($issueCouponInfo['total_count'] > 0) {
$issueCouponInfo['remain_count'] -= 1;
$issueCouponInfo->save();
}
$res['receive_count'] = $receiveCount + 1;
return $res;
});
}
/**
* 会员发放优惠期券
* @param $id
* @param $uid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function memberIssueUserCoupon($id, $uid)
{
$issueCouponInfo = $this->dao->getInfo((int)$id);
if ($issueCouponInfo) {
/** @var StoreCouponUserServices $couponUserService */
$couponUserService = app()->make(StoreCouponUserServices::class);
if ($issueCouponInfo->remain_count >= 0 || $issueCouponInfo->is_permanent) {
$this->transaction(function () use ($uid, $id, $couponUserService, $issueCouponInfo) {
$couponUserService->addMemberUserCoupon($uid, $issueCouponInfo, "send");
// 如果会员劵需要限制数量时打开
if ($issueCouponInfo['total_count'] > 0) {
$issueCouponInfo['remain_count'] -= 1;
$issueCouponInfo->save();
}
});
}
}
}
/**
* 后台发送优惠券
* @param $coupon
* @param $user
* @return bool
*/
public function setCoupon($coupon, $user, $redisKey, $queueId, $is_single = false)
{
if ((!$redisKey || !$queueId || !$user) && !$is_single) return false;
$data = [];
$issueData = [];
/** @var StoreCouponUserServices $storeCouponUser */
$storeCouponUser = app()->make(StoreCouponUserServices::class);
/** @var StoreCouponIssueUserServices $storeCouponIssueUser */
$storeCouponIssueUser = app()->make(StoreCouponIssueUserServices::class);
/** @var QueueServices $queueService */
$queueService = app()->make(QueueServices::class);
$i = 0;
$time = time();
$remainCount = 0;
if (!$coupon['is_permanent']) {
$count = count($user);
$remain_count = $this->dao->value(['id' => $coupon['id']], 'remain_count');
if ($remain_count < $count) {
$user = array_slice($user, 0, $remain_count);
} else {
$remainCount = $remain_count - $count;
}
}
foreach ($user as $k => $v) {
$data[$i]['cid'] = $coupon['id'];
$data[$i]['uid'] = $v;
$data[$i]['coupon_title'] = $coupon['title'];
$data[$i]['coupon_price'] = $coupon['coupon_price'];
$data[$i]['use_min_price'] = $coupon['use_min_price'];
$data[$i]['add_time'] = $time;
if ($coupon['coupon_time']) {
$data[$i]['start_time'] = $time;
$data[$i]['end_time'] = $time + $coupon['coupon_time'] * 86400;
} else {
$data[$i]['start_time'] = $coupon['start_use_time'];
$data[$i]['end_time'] = $coupon['end_use_time'];
}
$data[$i]['type'] = 'send';
$issueData[$i]['uid'] = $v;
$issueData[$i]['issue_coupon_id'] = $coupon['id'];
$issueData[$i]['add_time'] = time();
$i++;
}
$res = $this->transaction(function () use ($data, $issueData, $storeCouponUser, $storeCouponIssueUser) {
$res1 = $res2 = true;
if ($data) {
$res1 = $storeCouponUser->saveAll($data);
}
if ($issueData) {
$res2 = $storeCouponIssueUser->saveAll($issueData);
}
return $res1 && $res2;
});
if (!$res) {
//发券失败后将队列状态置为失败
$queueService->setQueueFail($queueId['id'], $redisKey);
} else {
if (!$coupon['is_permanent']) {
$this->dao->update($coupon['id'], ['remain_count' => $remainCount]);
}
if ($is_single) return true;
//发券成功的用户踢出集合
$queueService->doSuccessSremRedis($user, $redisKey, $queueId['type']);
}
return true;
}
/**
* 获取下单可使用的优惠券列表
* @param int $uid
* @param $cartId
* @param bool $new
* @param int $shipping_type
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function beUseableCouponList(int $uid, $cartId, bool $new, int $shipping_type = 1)
{
/** @var StoreCartServices $services */
$services = app()->make(StoreCartServices::class);
$cartGroup = $services->getUserProductCartListV1($uid, $cartId, $new, [], $shipping_type);
if (isset($cartGroup['deduction']['activity_id']) && !$cartGroup['deduction']['activity_id'] && isset($cartGroup['deduction']['type']) && $cartGroup['deduction']['type'] != 6) {
/** @var StoreCouponUserServices $coupServices */
$coupServices = app()->make(StoreCouponUserServices::class);
return $coupServices->getUseableCouponList($uid, $cartGroup);
} else {
return [];
}
}
/**获取单个优惠券类型
* @param array $where
* @return mixed
*/
public function getOne(array $where)
{
if (!$where) throw new AdminException('参数缺失!');
return $this->dao->getOne($where);
}
/**
* 俩时间相差月份
* @param $date1
* @param $date2
* @return float|int
*/
public function getMonthNum($date1, $date2)
{
$date1_stamp = strtotime($date1);
$date2_stamp = strtotime($date2);
[$date_1['y'], $date_1['m']] = explode("-", date('Y-m', $date1_stamp));
[$date_2['y'], $date_2['m']] = explode("-", date('Y-m', $date2_stamp));
return abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m'];
}
/**
* 给会员发放优惠券
* @param $uid
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function sendMemberCoupon($uid, $couponId = 0)
{
if (!$uid) return false;
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
/** @var StoreCouponUserServices $couponUserService */
$couponUserService = app()->make(StoreCouponUserServices::class);
/** @var MemberRightServices $memberRightService */
$memberRightService = app()->make(MemberRightServices::class);
/** @var MemberCardServices $memberCardService */
$memberCardService = app()->make(MemberCardServices::class);
//看付费会员是否开启
$isOpenMember = $memberCardService->isOpenMemberCardCache();
if (!$isOpenMember) return false;
$userInfo = $userService->getUserCacheInfo((int)$uid);
//看是否会员过期
if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
return false;
}
//看是否开启会员送券
$isSendCoupon = $memberRightService->getMemberRightStatus("coupon");
if (!$isSendCoupon) return false;
if ($userInfo && (($userInfo['is_money_level'] > 0) || $userInfo['is_ever_level'] == 1)) {
$monthNum = $this->getMonthNum(date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', $userInfo['overdue_time']));
if ($couponId) {//手动点击领取
$couponWhere['id'] = $couponId;
$couponInfo = $this->getMemberCouponIssueList($couponWhere, 0, 0);
} else {//主动批量发放
$couponWhere['status'] = 1;
$couponWhere['category'] = 2;
$couponWhere['is_del'] = 0;
$couponInfo = $this->getMemberCouponIssueList($couponWhere, 0, 0);
}
if ($couponInfo) {
$couponIds = array_column($couponInfo, 'id');
$couponUserMonth = $couponUserService->memberCouponUserGroupBymonth(['uid' => $uid, 'couponIds' => $couponIds]);
$getTime = array();
if ($couponUserMonth) {
$getTime = array_column($couponUserMonth, 'num', 'time');
}
// 判断这个月是否领取过,而且领全了
//if (in_array(date('Y-m', time()), $getTime)) return false;
$timeKey = date('Y-m', time());
if (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) return false;
//判断是否领完所有月份
if (count($getTime) >= $monthNum && (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) && $userInfo['is_ever_level'] != 1 && $monthNum > 0) return false;
foreach ($couponInfo as $cv) {
//看之前是否手动领取过某一张,领取过就不再领取。
if (!$couponUserService->getUserCounponByMonth(['uid' => $uid, 'cid' => $cv['id']])) {
$this->memberIssueUserCoupon($cv['id'], $uid);
}
}
}
}
return true;
}
/**
* 获取今日新增优惠券
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getTodayCoupon(int $uid)
{
$receive_type = [1, 4];
if ($uid) {//用户登录 && 不是付费会员
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
if (!$userService->checkUserIsSvip($uid)) {
$receive_type = [1];
}
}
$list = $this->dao->getTodayCoupon($receive_type);
foreach ($list as $key => &$item) {
$item['start_time'] = $item['start_time'] ? date('Y-m-d', $item['start_time']) : 0;
$item['end_time'] = $item['end_time'] ? date('Y-m-d', $item['end_time']) : 0;
$item['coupon_price'] = floatval($item['coupon_price']);
$item['use_min_price'] = floatval($item['use_min_price']);
}
return $list;
}
/**
* 获取新人券
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getNewCoupon()
{
$list = $this->dao->getNewCoupon();
foreach ($list as &$item) {
$item['start_time'] = $item['start_time'] ? date('Y-m-d', $item['start_time']) : 0;
$item['end_time'] = $item['end_time'] ? date('Y-m-d', $item['end_time']) : 0;
$item['coupon_price'] = floatval($item['coupon_price']);
$item['use_min_price'] = floatval($item['use_min_price']);
}
return $list;
}
/**
* 获取可以使用优惠券
* @param int $uid
* @param array $cartList
* @param array $promotions
* @param bool $isMax
* @return array
*/
public function getCanUseCoupon(int $uid, array $cartList = [], array $promotions = [], bool $isMax = true)
{
$where = ['receive_type' => 1, 'category' => 1];
//传入是付费会员 获取付费会员券
$isVip = $this->getItem('isVip', 0);
if ($uid && !$isVip) {
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
$isVip = $userService->checkUserIsSvip($uid);
}
if ($isVip) {
unset($where['category']);
}
/** @var StoreCouponUserServices $coupServices */
$coupServices = app()->make(StoreCouponUserServices::class);
//用户持有有效券
$userCoupons = $coupServices->getUserAllCoupon($uid);
if ($userCoupons) {
$where['not_id'] = array_column($userCoupons, 'cid');
}
$counpons = $this->dao->getValidList($where, '*', 0, 0, ['used' => function ($query) use ($uid) {
$query->where('uid', $uid);
}]);
//用户已经领取的 && 没有领取的
$counpons = array_merge($counpons, $userCoupons);
$result = [];
if ($counpons) {
$promotionsList = [];
if ($promotions) {
$promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
}
$isOverlay = function ($cart) use ($promotionsList) {
$productInfo = $cart['productInfo'] ?? [];
if (!$productInfo) {
return false;
}
if (isset($cart['promotions_id']) && $cart['promotions_id']) {
foreach ($cart['promotions_id'] as $key => $promotions_id) {
$promotions = $promotionsList[$promotions_id] ?? [];
if ($promotions && $promotions['promotions_type'] != 4) {
$overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
if (!in_array(5, $overlay)) {
return false;
}
}
}
}
return true;
};
foreach ($counpons as $coupon) {
if (isset($coupon['used']) && $coupon['used']) {//所有优惠券中 已经领取跳过
continue;
}
$price = 0;
$count = 0;
if (isset($coupon['applicable_type'])) {//已经领取 有效优惠券
$coupon['type'] = $coupon['applicable_type'];
$coupon['used'] = ['id' => $coupon['id'], 'cid' => $coupon['cid']];
}
//&& in_array($promotions['promotions_type'], $overlay)
switch ($coupon['type']) {
case 0:
foreach ($cartList as $cart) {
if (!$isOverlay($cart)) continue;
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
break;
case 1://品类券
/** @var StoreProductCategoryServices $storeCategoryServices */
$storeCategoryServices = app()->make(StoreProductCategoryServices::class);
$cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
if ($cateGorys) {
$cateIds = array_column($cateGorys, 'id');
foreach ($cartList as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
case 2:
foreach ($cartList as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
break;
case 3://品牌券
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
$brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
if ($brands) {
$brandIds = array_column($brands, 'id');
foreach ($cartList as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
}
if ($count && $coupon['use_min_price'] <= $price) {
//满减券
if ($coupon['coupon_type'] == 1) {
$couponPrice = $coupon['coupon_price'] > $price ? $price : $coupon['coupon_price'];
} else {
if ($coupon['coupon_price'] <= 0) {//0折
$couponPrice = $price;
} else if ($coupon['coupon_price'] >= 100) {
$couponPrice = 0;
} else {
$truePrice = (float)bcmul((string)$price, bcdiv((string)$coupon['coupon_price'], '100', 2), 2);
$couponPrice = (float)bcsub((string)$price, (string)$truePrice, 2);
}
}
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['true_coupon_price'] = $couponPrice;
$result[] = $coupon;
}
}
}
if ($result) {
$trueCouponPrice = array_column($result, 'true_coupon_price');
array_multisort($trueCouponPrice, SORT_DESC, $result);
}
//最优的一个
if ($isMax) {
$useCoupon = $result[0] ?? [];
return $useCoupon;
}
return $result;
}
}

View File

@@ -0,0 +1,44 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\activity\coupon;
use app\services\BaseServices;
use app\dao\activity\coupon\StoreCouponIssueUserDao;
use think\annotation\Inject;
/**
* Class StoreCouponIssueUserServices
* @package app\services\activity\coupon
* @mixin StoreCouponIssueUserDao
*/
class StoreCouponIssueUserServices extends BaseServices
{
/**
* @var StoreCouponIssueUserDao
*/
#[Inject]
protected StoreCouponIssueUserDao $dao;
/**
* 获取列表
* @param array $where
* @return array
*/
public function issueLog(array $where)
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getList($where, $page, $limit);
$count = $this->dao->count($where);
return compact('list', 'count');
}
}

View File

@@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\activity\coupon;
use app\services\BaseServices;
use app\dao\activity\coupon\StoreCouponProductDao;
use think\annotation\Inject;
/**
* Class StoreCouponProductServices
* @package app\services\activity\coupon
* @mixin StoreCouponProductDao
*/
class StoreCouponProductServices extends BaseServices
{
/**
* @var StoreCouponProductDao
*/
#[Inject]
protected StoreCouponProductDao $dao;
}

View File

@@ -0,0 +1,197 @@
<?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\services\activity\coupon;
use app\dao\activity\coupon\StoreCouponDao;
use app\services\BaseServices;
use app\services\product\category\StoreProductCategoryServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\annotation\Inject;
use think\facade\Route as Url;
/**
* Class StoreCouponService
* @package app\services\activity\coupon
* @mixin StoreCouponDao
*/
class StoreCouponService extends BaseServices
{
/**
* @var StoreCouponDao
*/
#[Inject]
protected StoreCouponDao $dao;
/**
* 获取列表
* @param array $where
* @return array
*/
public function getList(array $where)
{
[$page, $limit] = $this->getPageValue();
$where['is_del'] = 0;
$list = $this->dao->getList($where, $page, $limit);
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 添加优惠券表单
* @param int $type
* @return array
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function createForm(int $type)
{
$f[] = Form::input('title', '优惠券名称');
switch ($type) {
case 1://品类券
$options = function () {
/** @var StoreProductCategoryServices $storeCategoryService */
$storeCategoryService = app()->make(StoreProductCategoryServices::class);
$list = $storeCategoryService->getTierList(1, 1);
$menus = [];
foreach (sort_list_tier($list) as $menu) {
$menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => false];
}
return $menus;
};
$f[] = Form::select('category_id', '选择品类')->setOptions(Form::setOptions($options))->filterable(1)->col(12);
break;
case 2://商品券
$f[] = Form::frameImages('image', '商品', Url::buildUrl('admin/store.StoreProduct/index', array('fodder' => 'image', 'type' => 'many')))->icon('ios-add')->width('960px')->height('430px')->modal(['footer-hide' => true])->props(['srcKey' => 'image']);
$f[] = Form::hidden('product_id', '');
break;
}
$f[] = Form::number('coupon_price', '优惠券面值', 0)->min(0);
$f[] = Form::number('use_min_price', '优惠券最低消费', 0)->min(0);
$f[] = Form::number('coupon_time', '优惠券有效期限', 0)->min(0);
$f[] = Form::number('sort', '排序', 1)->min(0);
$f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
$f[] = Form::hidden('type', $type);
return create_form('添加优惠券', $f, Url::buildUrl('/marketing/coupon/save'), 'POST');
}
/**
* 优惠卷模板修改表单
* @param int $id
* @return array
* @throws \FormBuilder\Exception\FormBuilderException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function createIssue(int $id)
{
$res = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
if (!$res) throw new AdminException('发布的优惠劵已失效或不存在!');
$f = [];
$f[] = Form::input('id', '优惠劵ID', $id)->disabled(1);
$f[] = Form::input('coupon_title', '优惠劵名称', $res['title'])->disabled(1);
$f[] = Form::dateTimeRange('range_date', '领取时间')->placeholder('不填为永久有效');
$f[] = Form::radio('is_permanent', '是否限量', 1)->options([['label' => '不限量', 'value' => 1], ['label' => '限量', 'value' => 0]]);
$f[] = Form::number('count', '发布数量', 0)->min(0)->placeholder('不填或填0,为不限量');
$f[] = Form::radio('is_type', '优惠券类型', 0)->options([['label' => '普通券', 'value' => 0], ['label' => '赠送券', 'value' => 1], ['label' => '新人券', 'value' => 2]]);
$f[] = Form::number('full_reduction', '满赠金额', 0)->min(0)->placeholder('赠送优惠券的最低消费金额');
$f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
return create_form('发布优惠券', $f, $this->url('/marketing/coupon/issue/' . $id), 'POST');
}
/**
* 发布优惠券
* @param int $id
* @param int $_id
* @param string $coupon_title
* @param array $rangeTime
* @param int $count
* @param int $status
* @param int $is_permanent
* @param float $full_reduction
* @param int $is_give_subscribe
* @param int $is_full_give
* @param int $is_type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function upIssue(int $id, int $_id, string $coupon_title, array $rangeTime, int $count, int $status, int $is_permanent, float $full_reduction, int $is_give_subscribe, int $is_full_give, int $is_type)
{
if ($is_type == 1) {
$is_full_give = 1;
} elseif ($is_type == 2) {
$is_give_subscribe = 1;
}
if ($_id != $id) throw new AdminException('操作失败,信息不对称');
if (!$count) $count = 0;
$couponInfo = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
if (!$couponInfo) throw new AdminException('发布的优惠劵已失效或不存在!');
if (count($rangeTime) != 2) throw new AdminException('请选择正确的时间区间');
[$startTime, $endTime] = $rangeTime;
if (!$startTime) $startTime = 0;
if (!$endTime) $endTime = 0;
if (!$startTime && $endTime) throw new AdminException('请选择正确的开始时间');
if ($startTime && !$endTime) throw new AdminException('请选择正确的结束时间');
$data['cid'] = $id;
$data['coupon_title'] = $coupon_title;
$data['start_time'] = strtotime($startTime);
$data['end_time'] = strtotime($endTime);
$data['total_count'] = $count;
$data['remain_count'] = $count;
$data['is_permanent'] = $is_permanent;
$data['status'] = $status;
$data['is_give_subscribe'] = $is_give_subscribe;
$data['is_full_give'] = $is_full_give;
$data['full_reduction'] = $full_reduction;
$data['is_del'] = 0;
$data['add_time'] = time();
$data['title'] = $couponInfo['title'];
$data['integral'] = $couponInfo['integral'];
$data['coupon_price'] = $couponInfo['coupon_price'];
$data['use_min_price'] = $couponInfo['use_min_price'];
$data['coupon_time'] = $couponInfo['coupon_time'];
$data['product_id'] = $couponInfo['product_id'];
$data['category_id'] = $couponInfo['category_id'];
$data['type'] = $couponInfo->getData('type');
/** @var StoreCouponIssueServices $storeCouponIssueService */
$storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
$res = $storeCouponIssueService->save($data);
$productIds = explode(',', $data['product_id']);
if (count($productIds)) {
$couponData = [];
foreach ($productIds as $product_id) {
$couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
}
/** @var StoreCouponProductServices $storeCouponProductService */
$storeCouponProductService = app()->make(StoreCouponProductServices::class);
$storeCouponProductService->saveAll($couponData);
}
if (!$res) throw new AdminException('发布优惠劵失败!');
}
/**
* 优惠券失效
* @param int $id
*/
public function invalid(int $id)
{
$res = $this->dao->update($id, ['status' => 0]);
if (!$res) throw new AdminException('操作失败');
/** @var StoreCouponIssueServices $storeCouponIssueService */
$storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
$storeCouponIssueService->update($id, ['status' => -1], 'cid');
}
}

View File

@@ -0,0 +1,33 @@
<?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\services\activity\coupon;
use app\dao\activity\coupon\StoreCouponUserCouponDao;
use app\services\BaseServices;
use think\annotation\Inject;
/**
* 根据下单金额获取用户能使用的优惠卷
* Class StoreCouponUserCouponServices
* @package app\services\activity\coupon
* @mixin StoreCouponUserCouponDao
*/
class StoreCouponUserCouponServices extends BaseServices
{
/**
* @var StoreCouponUserCouponDao
*/
#[Inject]
protected StoreCouponUserCouponDao $dao;
}

View File

@@ -0,0 +1,497 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\activity\coupon;
use app\services\activity\promotions\StorePromotionsServices;
use app\services\BaseServices;
use app\services\product\brand\StoreBrandServices;
use app\services\user\UserServices;
use app\dao\activity\coupon\StoreCouponUserDao;
use app\services\product\category\StoreProductCategoryServices;
use think\annotation\Inject;
/**
* Class StoreCouponUserServices
* @package app\services\activity\coupon
* @mixin StoreCouponUserDao
*/
class StoreCouponUserServices extends BaseServices
{
/**
* @var StoreCouponUserDao
*/
#[Inject]
protected StoreCouponUserDao $dao;
/**
* 获取列表
* @param array $where
* @return array
*/
public function issueLog(array $where)
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getList($where, 'uid,add_time', ['userInfo'], $page, $limit);
foreach ($list as &$item) {
$item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
}
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 获取列表
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function systemPage(array $where)
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
$count = 0;
if ($list) {
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$userAll = $userServices->getColumn([['uid', 'IN', array_column($list, 'uid')]], 'uid,nickname', 'uid');
foreach ($list as &$item) {
$item['nickname'] = $userAll[$item['uid']]['nickname'] ?? '';
}
$count = $this->dao->count($where);
}
return compact('list', 'count');
}
/**
* 获取用户优惠券
* @param int $id
* @param int $status
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCouponList(int $id, int $status = -1)
{
[$page, $limit] = $this->getPageValue();
$where = ['uid' => $id];
if ($status != -1) $where['status'] = $status;
$list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
foreach ($list as &$item) {
$item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
$item['_end_time'] = date('Y-m-d H:i:s', $item['end_time']);
if (!$item['coupon_time']) {
$item['coupon_time'] = ceil(($item['end_use_time'] - $item['start_use_time']) / '86400');
}
}
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 恢复优惠券
* @param int $id
* @return bool|mixed
*/
public function recoverCoupon(int $id)
{
$status = $this->dao->value(['id' => $id], 'status');
if ($status) return $this->dao->update($id, ['status' => 0, 'use_time' => 0]);
else return true;
}
/**
* 过期优惠卷失效
*/
public function checkInvalidCoupon($uid = 0)
{
$this->dao->update([['uid', '=', $uid], ['end_time', '<', time()], ['status', '=', '0']], ['status' => 2]);
}
/**
* 获取用户有效优惠劵数量
* @param int $uid
* @return int
*/
public function getUserValidCouponCount(int $uid)
{
$this->checkInvalidCoupon($uid);
return $this->dao->getCount(['uid' => $uid, 'status' => 0]);
}
/**
* 下单页面显示可用优惠券
* @param $uid
* @param $cartGroup
* @param $price
* @return array
*/
public function getUseableCouponList(int $uid, array $cartGroup)
{
$userCoupons = $this->dao->getUserAllCoupon($uid);
$result = [];
if ($userCoupons) {
$cartInfo = $cartGroup['valid'];
$promotions = $cartGroup['promotions'] ?? [];
$promotionsList = [];
if ($promotions) {
$promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
}
$isOverlay = function ($cart) use ($promotionsList) {
$productInfo = $cart['productInfo'] ?? [];
if (!$productInfo) {
return false;
}
if (isset($cart['promotions_id']) && $cart['promotions_id']) {
foreach ($cart['promotions_id'] as $key => $promotions_id) {
$promotions = $promotionsList[$promotions_id] ?? [];
if ($promotions && $promotions['promotions_type'] != 4) {
$overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
if (!in_array(5, $overlay)) {
return false;
}
}
}
}
return true;
};
/** @var StoreProductCategoryServices $storeCategoryServices */
$storeCategoryServices = app()->make(StoreProductCategoryServices::class);
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
foreach ($userCoupons as $coupon) {
$price = 0;
$count = 0;
switch ($coupon['applicable_type']) {
case 0:
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
break;
case 1://品类券
$cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
if ($cateGorys) {
$cateIds = array_column($cateGorys, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
case 2:
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
break;
case 3://品牌
$brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
if ($brands) {
$brandIds = array_column($brands, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
}
if ($count && $coupon['use_min_price'] <= $price) {
$coupon_data['id'] = $coupon['id'];
$coupon_data['cid'] = $coupon['cid'];
$coupon_data['type'] = $coupon['applicable_type'];
$coupon_data['applicable_type'] = $coupon['applicable_type'];
$coupon_data['receive_type'] = $coupon['receive_type'];
$coupon_data['start_time'] = $coupon['start_time'] ? date('Y-m-d', $coupon['start_time']) : date('Y-m-d', $coupon['add_time']);
$coupon_data['add_time'] = date('Y-m-d', $coupon['add_time']);
$coupon_data['end_time'] = date('Y-m-d', $coupon['end_time']);
$coupon_data['title'] = $coupon['coupon_title'];
$coupon_data['use_min_price'] = floatval($coupon['use_min_price']);
$coupon_data['coupon_type'] = $coupon['coupon_type'];
$coupon_data['coupon_price'] = floatval($coupon['coupon_price']);
$result[] = $coupon_data;
}
}
}
return $result;
}
/**
* 用户领取优惠券
* @param $uid
* @param $issueCouponInfo
* @param string $type
* @return mixed
*/
public function addUserCoupon($uid, $issueCouponInfo, string $type = 'get')
{
$data = [];
$data['cid'] = $issueCouponInfo['id'];
$data['uid'] = $uid;
$data['coupon_title'] = $issueCouponInfo['title'];
$data['coupon_price'] = $issueCouponInfo['coupon_price'];
$data['use_min_price'] = $issueCouponInfo['use_min_price'];
$data['add_time'] = time();
if ($issueCouponInfo['coupon_time']) {
$data['start_time'] = $data['add_time'];
$data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
} else {
$data['start_time'] = $issueCouponInfo['start_use_time'];
$data['end_time'] = $issueCouponInfo['end_use_time'];
}
$data['type'] = $type;
return $this->dao->save($data);
}
/**会员领取优惠券
* @param $uid
* @param $issueCouponInfo
* @param string $type
* @return mixed
*/
public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
{
$data = [];
$data['cid'] = $issueCouponInfo['id'];
$data['uid'] = $uid;
$data['coupon_title'] = $issueCouponInfo['title'];
$data['coupon_price'] = $issueCouponInfo['coupon_price'];
$data['use_min_price'] = $issueCouponInfo['use_min_price'];
$data['add_time'] = time();
$data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
$data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
$data['type'] = $type;
return $this->dao->save($data);
}
/**
* 获取用户已领取的优惠卷
* @param int $uid
* @param $type
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCounpon(int $uid, $types, $type)
{
$where = [];
$where['uid'] = $uid;
switch ($types) {
case 1:
$where['status'] = 1;
break;
case 2:
$where['status'] = 2;
break;
default:
$where['status'] = 0;
break;
}
$where['issue_type'] = $type;
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
/** @var StoreProductCategoryServices $categoryServices */
$categoryServices = app()->make(StoreProductCategoryServices::class);
$category = $categoryServices->getColumn([], 'pid,cate_name', 'id');
foreach ($list as &$item) {
if ($item['category_id'] && isset($category[$item['category_id']])) {
$item['category_type'] = $category[$item['category_id']]['pid'] == 0 ? 1 : 2;
$item['category_name'] = $category[$item['category_id']]['cate_name'];
} else {
$item['category_type'] = '';
$item['category_name'] = '';
}
}
return $list ? $this->tidyCouponList($list) : [];
}
/**
* 我的优惠券数量
* @param int $uid
* @return array
* @throws \think\db\exception\DbException
*/
public function getUserCounponNum(int $uid): array
{
$data['not_used'] = $this->dao->count(['uid' => $uid, 'status' => 0]); // 未使用
$data['used'] = $this->dao->count(['uid' => $uid, 'status' => 1]);// 已使用
$data['expired'] = $this->dao->count(['uid' => $uid, 'status' => 2]);// 已过期
return $data;
}
/**
* 格式化优惠券
* @param $couponList
* @return mixed
*/
public function tidyCouponList($couponList)
{
$time = time();
foreach ($couponList as &$coupon) {
if ($coupon['status'] == '已使用') {
$coupon['_type'] = 0;
$coupon['_msg'] = '已使用';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已使用';
} else if ($coupon['status'] == '已过期') {
$coupon['is_fail'] = 1;
$coupon['_type'] = 0;
$coupon['_msg'] = '已过期';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已过期';
} else if ($coupon['end_time'] < $time) {
$coupon['is_fail'] = 1;
$coupon['_type'] = 0;
$coupon['_msg'] = '已过期';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已过期';
} else if ($coupon['start_time'] > $time) {
$coupon['_type'] = 0;
$coupon['_msg'] = '未开始';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '未开始';
} else {
if ($coupon['start_time'] + 3600 * 24 > $time) {
$coupon['_type'] = 2;
$coupon['_msg'] = '立即使用';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '可使用';
} else {
$coupon['_type'] = 1;
$coupon['_msg'] = '立即使用';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '可使用';
}
}
$coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
$coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
}
return $couponList;
}
/**
* 获取会员优惠券列表
* @param $uid
* @return array|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getMemberCoupon($uid)
{
if (!$uid) return [];
[$page, $limit] = $this->getPageValue();
if (!$page && !$limit) {
$page = 0;
$limit = 4;
}
/** @var StoreCouponIssueServices $couponIssueService */
$couponIssueService = app()->make(StoreCouponIssueServices::class);
$couponWhere['category'] = 2;
$couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere, $page, $limit, ['used' => function ($query) use ($uid) {
$query->where('uid', $uid);
}]);
if ($couponInfo) {
$time = time();
foreach ($couponInfo as $k => &$coupon) {
$coupon['type_name'] = $couponIssueService->_couponType[$coupon['type']];
$coupon['is_use'] = $uid ? isset($coupon['used']) : false;
$coupon['used'] = $coupon['used'][0] ?? [];
if (isset($coupon['used']) && $coupon['used']) {
if ($coupon['used']['status'] == '已使用') {
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已使用';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已使用';
} else if ($coupon['used']['status'] == '已过期') {
$coupon['used']['is_fail'] = 1;
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已过期';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已过期';
} else if ($coupon['used']['end_time'] < $time) {
$coupon['used']['is_fail'] = 1;
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已过期';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已过期';
} else if ($coupon['used']['start_time'] > $time) {
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '未开始';
$coupon['used']['pc_type'] = 1;
$coupon['used']['pc_msg'] = '未开始';
} else {
if ($coupon['used']['start_time'] + 3600 * 24 > $time) {
$coupon['used']['_type'] = 2;
$coupon['used']['_msg'] = '立即使用';
$coupon['used']['pc_type'] = 1;
$coupon['used']['pc_msg'] = '可使用';
} else {
$coupon['used']['_type'] = 1;
$coupon['used']['_msg'] = '立即使用';
$coupon['used']['pc_type'] = 1;
$coupon['used']['pc_msg'] = '可使用';
}
}
$coupon['used']['end_time'] = $coupon['used']['_end_time'] = date('Y/m/d', $coupon['used']['end_time']);
$coupon['used']['_add_time'] = $coupon['used']['start_time'] ? date('Y/m/d', $coupon['used']['start_time']) : date('Y/m/d', $coupon['add_time']);
}
$coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
$coupon['start_use_time'] = date('Y/m/d', $coupon['start_use_time']);
$coupon['end_use_time'] = date('Y/m/d', $coupon['end_use_time']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
}
}
return $couponInfo ?: [];
}
/**
* 使用优惠券验证
* @param int $couponId
* @param int $uid
* @param array $cartInfo
* @param array $promotions
* @return bool
*/
public function useCoupon(int $couponId, int $uid, array $cartInfo, array $promotions = [])
{
if (!$couponId || !$uid || !$cartInfo) {
return true;
}
/** @var StorePromotionsServices $promotionsServices */
$promotionsServices = app()->make(StorePromotionsServices::class);
[$couponInfo, $couponPrice] = $promotionsServices->useCoupon($couponId, $uid, $cartInfo, $promotions);
if ($couponInfo) {
$this->dao->useCoupon($couponId);
}
return true;
}
}