Files
huangjingfen/pro_v3.5.1_副本/app/services/activity/lottery/LuckPrizeServices.php
apple 434aa8c69d feat(fsgx): 完成全部24项开发任务 Phase1-7
Phase1 后端核心:
- 新增 fsgx_v1.sql 迁移脚本(is_queue_goods/frozen_points/available_points/no_assess)
- SystemConfigServices 返佣设置扩展(周期人数/分档比例/范围/时机)
- StoreOrderCreateServices 周期循环佣金计算
- StoreOrderTakeServices 佣金发放后同步冻结积分
- StoreProductServices/StoreProduct 保存 is_queue_goods

Phase2 后端接口:
- GET /api/hjf/brokerage/progress 佣金周期进度
- GET /api/hjf/assets/overview 资产总览
- HjfPointsServices 每日 frozen_points 0.4‰ 释放定时任务
- PUT /adminapi/hjf/member/{uid}/no_assess 不考核接口
- GET /adminapi/hjf/points/release_log 积分日志接口

Phase3 前端清理:
- hjfCustom.js 路由精简(仅保留 points/log)
- hjfQueue.js/hjfMember.js API 清理/重定向至 CRMEB 原生接口
- pages.json 公排→推荐佣金/佣金记录/佣金规则

Phase4-5 前端改造:
- queue/status.vue 推荐佣金进度页整体重写
- 商品详情/订单确认/支付结果页文案与逻辑改造
- 个人中心/资产页/引导页/规则页文案改造
- HjfQueueProgress/HjfRefundNotice/HjfAssetCard 组件改造
- 推广中心嵌入佣金进度摘要
- hjfMockData.js 全量更新(公排字段→佣金字段)

Phase6 Admin 增强:
- 用户列表新增 frozen_points/available_points 列及不考核操作按钮
- hjfPoints.js USE_MOCK=false 对接真实积分日志接口

Phase7 配置文档:
- docs/fsgx-phase7-config-checklist.md 后台配置与全链路验收清单

Made-with: Cursor
2026-03-23 22:32:19 +08:00

274 lines
8.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\lottery;
use app\services\BaseServices;
use app\dao\activity\lottery\LuckPrizeDao;
use app\services\activity\coupon\StoreCouponIssueServices;
use crmeb\services\CacheService;
use think\annotation\Inject;
use think\exception\ValidateException;
/**
*
* Class LuckPrizeServices
* @package app\services\activity\lottery
* @mixin LuckPrizeDao
*/
class LuckPrizeServices extends BaseServices
{
/**
* @var array 1未中奖2积分3:余额4红包5:优惠券6站内商品7等级经验8用户等级 9svip天数
*/
public array $prize_type = [
'1' => '未中奖',
'2' => '积分',
'3' => '余额',
'4' => '红包',
'5' => '优惠券',
'6' => '站内商品',
'7' => '等级经验',
'8' => '用户等级',
'9' => 'svip天数'
];
/**
* 奖品数据字段
* @var array
*/
public array $prize = [
'id' => 0,
'type' => 1,
'lottery_id' => 0,
'name' => '',
'prompt' => '',
'image' => '',
'chance' => 0,
'percent' => 0,
'total' => 0,
'coupon_id' => 0,
'product_id' => 0,
'unique' => '',
'num' => 1,
'sort' => 0,
'status' => 1,
'is_del' => 0,
'add_time' => 0,
];
/**
* @var LuckPrizeDao
*/
#[Inject]
protected LuckPrizeDao $dao;
/**
* 奖品数据验证
* @param array $data
* @return array
*/
public function checkPrizeData(array $data)
{
$data = array_merge($this->prize, array_intersect_key($data, $this->prize));
if (!isset($data['name']) || !$data['name']) {
throw new ValidateException('请填写奖品名称');
}
if (!isset($data['image']) || !$data['image']) {
throw new ValidateException('请选择奖品图片');
}
if (!isset($data['chance'])) {
throw new ValidateException('请填写奖品中奖权重');
}
if (!isset($data['type']) || !isset($this->prize_type[$data['type']])) {
throw new ValidateException('请选择奖品类型');
}
if (in_array($data['type'], [2, 3, 4]) && (!isset($data['num']) || !$data['num'])) {
$msg = '';
switch ($data['type']) {
case 2:
$msg = '积分';
break;
case 3:
$msg = '余额';
break;
case 4:
$msg = '红包';
break;
}
throw new ValidateException('请填写奖品赠送' . $msg . '数');
}
if ($data['type'] == 2) {
$data['num'] = (int)$data['num'];
}
if ($data['type'] == 5 && (!isset($data['coupon_id']) || !$data['coupon_id'])) {
throw new ValidateException('请选择优惠券');
}
if ($data['type'] == 6 && (!isset($data['product_id']) || !$data['product_id'])) {
throw new ValidateException('请选择商品');
}
return $data;
}
/**
* 修改奖品
* @param int $id
* @param array $data
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit(int $id, array $data)
{
$this->checkPrizeData($data);
$prize = $this->dao->get($id);
if (!$prize) {
throw new ValidateException('奖品不存在');
}
if (!$this->dao->update($id, $data, 'id')) {
throw new ValidateException('修改失败');
}
return true;
}
/**
* 获取某个抽奖活动的所有奖品
* @param int $lottery_id
* @param string $field
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getLotteryPrizeList(int $lottery_id, string $field = '*')
{
return $this->dao->getPrizeList($lottery_id, $field);
}
/**
* 随机奖品
* @param array $data
* @return array|mixed
*/
public function getLuckPrize(array $data)
{
$totalPercent = array_sum(array_column($data, 'percent')) * 100;
$prize = [];
if (!$data) return $prize;
mt_srand();
$random = mt_rand(1, (int)$totalPercent);
$range = 0;
$newPrize = array_combine(array_column($data, 'type'), $data);
foreach ($data as $item) {
// 转换百分比为千分位范围
$range += $item['percent'] * 100; // 例如 12.34% -> 1234
if ($random <= $range) {
if (($item['sort'] != 1 && $item['total'] != -1 && $item['total'] <= 0)) {
$prize = $newPrize[1] ?? [];
} else {
if (CacheService::checkStock((string)$item['id'], 1, 6)) {
$prize = $item;
} else {
$prize = $newPrize[1] ?? [];
}
}
break;
}
}
return $prize;
// $prize = [];
// if (!$data) return $prize;
// $coupon = [];
// $coupon_ids = array_unique(array_column($data, 'coupon_id'));
// if ($coupon_ids) {
// /** @var StoreCouponIssueServices $couponServices */
// $couponServices = app()->make(StoreCouponIssueServices::class);
// $coupon = $couponServices->getGiveCoupon([['id', 'IN', $coupon_ids]]);
// if ($coupon) $coupon = array_combine(array_column($coupon, 'id'), $coupon);
// }
// $totalChance = 100;
// $startChance = 0;
// mt_srand();
// $prizeChance = rand(0, $totalChance - 1);
// $newPrize = array_combine(array_column($data, 'type'), $data);
// foreach ($data as $item) {
// $newStartChance = (int)bcadd((string)$item['chance'], (string)$startChance);
// //随机数在这个基数端内 且该商品数量大于0 中奖
// if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
// //奖品权重<=0 || 随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
// if ((int)$item['chance'] <= 0 ||
// (
// $item['type'] != 1 &&
// $item['total'] != -1 &&
// $item['total'] <= 0
// )
// ||
// (
// $item['coupon_id'] &&
// $coupon &&
// !isset($coupon[$item['coupon_id']])
// )
// ) {
// $prize = $newPrize[1] ?? [];
// } else {
// if (CacheService::checkStock((string)$item['id'], 1, 6)) {
// $prize = $item;
// } else {
// $prize = $newPrize[1] ?? [];
// }
// }
// break;
// }
// $startChance = $newStartChance;
// }
// return $prize;
}
/**
* 中奖后减少奖品数量
* @param int $id
* @param array $prize
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function decPrizeNum(int $id, array $prize = [])
{
if (!$id) return false;
if (!$prize) {
$prize = $this->dao->get($id);
}
if (!$prize) {
throw new ValidateException('该奖品不存在');
}
//不是未中奖奖品 减少奖品数量
if ($prize['type'] != 1 && $prize['total'] >= 1) {
$total = $prize['total'] - 1;
if (!$this->dao->update($id, ['total' => $total], 'id')) {
throw new ValidateException('抽奖减少奖品总数失败');
}
CacheService::popStock((string)$id, 1, 6);
}
return true;
}
}