manghe/app/api/controller/FuLiWu.php
2025-04-11 18:05:44 +08:00

344 lines
13 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app\api\controller;
use app\api\controller\Base;
use app\common\model\Goods as Goodsmodel;
use app\common\model\GoodsLock;
use app\common\model\GoodsList;
use app\common\model\Collect;
use app\common\model\Shang;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\OrderList;
use app\common\model\UserVip;
use think\facade\Db;
use \think\Request;
use app\common\model\CouponReceive as CouponReceiveModel;
use app\common\model\UserCoupon;
use app\common\model\GoodsType;
use app\common\service\CommonService;
class FuLiWu extends Base
{
/**
* 福利屋
* @param \think\Request $request
* @return \think\response\Json
*/
public function index(Request $request)
{
$type_str = request()->param('type', 1);
if ($type_str != 1 && $type_str != 3) {
return $this->renderError('参数错误');
}
$order='sort desc,id desc';
$whe = [];
$whe[] = ['status', '=', $type_str];
$paginate = 15;
$whe[] = ['type', '=', 15];
$whe[] = ['is_open', '=', $type_str == 1 ? 0 : 1];
if($type_str!=1){
$order='open_time desc';
}
$user_id = $this->getUserId();
if ($user_id == 0) {
//充值金额
$whe[] = ['unlock_amount', '=', 0];
} else {
$order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price');
$userInfo = User::where('id', '=', $user_id)->field('istest')->find();
if ($userInfo && $userInfo['istest'] > 0) {
//推广账号,门槛计算是全部的
$order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total');
}
$whe[] = ['unlock_amount', '<=', $order_money];
}
#盒子
$goods = GoodsModel::where($whe)
->field("id,title,imgurl,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open")
->order($order)->paginate($paginate)->each(function ($itme) {
$itme['imgurl'] = imageUrl($itme['imgurl']);
$itme['flw_start_time'] = date('Y-m-d H:i:s', $itme['flw_start_time']);
$itme['flw_end_time'] = date('Y-m-d H:i:s', $itme['flw_end_time']);
$itme['open_time'] = date('Y-m-d H:i:s', $itme['open_time']);
$goodslist = GoodsList::where('goods_id', '=', $itme['id'])
->where('num', '=', 0)
->field('title,imgurl,stock,price,sc_money')
->select()
->toArray();
$itme['goodslist'] = $goodslist;
#参与次数
$join_count = OrderList::field('id')
->where('goods_id', '=', $itme['id'])
->where('order_type', '=', $itme['type'])
->count();
$itme['join_count'] = $join_count;
});
$new_data = [
'data' => $goods->items(),
'last_page' => $goods->lastPage(),
];
return $this->renderSuccess('请求成功', $new_data);
}
/**
* 福利屋详情
* @param \think\Request $request
* @return \think\response\Json
*/
public function fuliwu_detail(Request $request)
{
$goods_id = request()->param('goods_id/d', 0);
$goods = GoodsModel::where(['id' => $goods_id])->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1 && $goods['status'] != 3) {
return $this->renderError("盒子已下架");
}
// 检查类型是否为福利屋
if ($goods['type'] != 15) {
return $this->renderError("该盒子不是福利屋类型");
}
// 获取用户信息
$user_id = $this->getUserId();
// 检查用户是否可以查看该福利屋(解锁金额限制)
if ($user_id > 0) {
$order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price');
$userInfo = User::where('id', '=', $user_id)->field('istest')->find();
if ($userInfo && $userInfo['istest'] > 0) {
// 推广账号,门槛计算是全部的
$order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total');
}
if ($goods['unlock_amount'] > $order_money) {
return $this->renderError("您需要消费满" . $goods['unlock_amount'] . "元才能查看此福利屋");
}
} else if ($goods['unlock_amount'] > 0) {
return $this->renderError("您需要登录并充值满" . $goods['unlock_amount'] . "元才能查看此福利屋");
}
// 获取福利屋详细信息
$goods_detail = GoodsModel::field("id,title,imgurl,imgurl_detail,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open,unlock_amount,sort")
->where(['id' => $goods_id])
->find();
// 处理图片地址和时间格式
$goods_detail['imgurl'] = imageUrl($goods_detail['imgurl']);
if (!empty($goods_detail['imgurl_detail'])) {
$goods_detail['imgurl_detail'] = imageUrl($goods_detail['imgurl_detail']);
}
// 获取福利屋中的奖品列表
$goodslist = GoodsList::where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->field('id,title,imgurl,imgurl_detail,stock,price,sc_money,shang_id,surplus_stock,sort')
->order('shang_id desc,sort asc')
->select()
->toArray();
// 处理奖品列表,添加图片地址
foreach ($goodslist as &$item) {
$item['imgurl'] = imageUrl($item['imgurl']);
$item['imgurl_detail'] = imageUrl($item['imgurl_detail']);
// 获取奖品类型信息
$shang_info = Shang::field('title,color')->where(['id' => $item['shang_id']])->find();
$item['shang_title'] = $shang_info ? $shang_info['title'] : '';
$item['shang_color'] = $shang_info ? $shang_info['color'] : '';
}
// 统计参与人数
$join_count = OrderList::field('id')
->where('goods_id', '=', $goods_id)
->where('order_type', '=', $goods_detail['type'])
->count();
//
$user_count = OrderList::
where('goods_id', '=', $goods_id)
->where('user_id', '=', $user_id)
->where('order_type', '=', $goods_detail['type'])
->field('id')
->count();
// 获取用户在该福利屋活动期间的消费情况
$consumptionData = CommonService::getUserConsumptionByTimeRange(
$user_id,
$goods_detail['flw_start_time'],
$goods_detail['flw_end_time']
);
$goods_detail['flw_start_time'] = date('Y-m-d H:i', $goods_detail['flw_start_time']);
$goods_detail['flw_end_time'] = date('Y-m-d H:i', $goods_detail['flw_end_time']);
$goods_detail['open_time'] = date('Y-m-d H:i', $goods_detail['open_time']);
// 组装返回数据
$data = [
'goods' => $goods_detail,
'goodslist' => $goodslist,
'join_count' => $join_count,
'current_time' => date('Y-m-d H:i:s'),
'user_count' => $user_count,
'user_consumption' => $consumptionData
];
// 判断福利屋状态
$now = time();
$start_time = strtotime($goods_detail['flw_start_time']);
$end_time = strtotime($goods_detail['flw_end_time']);
$open_time = strtotime($goods_detail['open_time']);
if ($now < $start_time) {
$data['status'] = 'waiting'; // 等待开始
$data['status_text'] = '即将开始';
} else if ($now >= $start_time && $now < $end_time) {
$data['status'] = 'ongoing'; // 进行中
$data['status_text'] = '进行中';
} else if ($now >= $end_time && $now < $open_time) {
$data['status'] = 'ended'; // 已结束,等待开奖
$data['status_text'] = '已结束,等待开奖';
} else if ($now >= $open_time) {
if ($goods_detail['is_open'] == 1) {
$data['status'] = 'opened'; // 已开奖
$data['status_text'] = '已开奖';
} else {
$data['status'] = 'to_open'; // 待开奖(超过开奖时间但未开奖)
$data['status_text'] = '待开奖';
}
}
return $this->renderSuccess('请求成功', $data);
}
/**
* 福利屋参与人员
* @param Request $request
* @return \think\response\Json
*/
public function fuliwu_participants(Request $request)
{
$goods_id = request()->param('goods_id/d', 0);
$list = OrderList::where('goods_id', '=', $goods_id)
->where('order_type', '=', 15)
->field('user_id,addtime')
->order('addtime desc')
->limit(1000)
->select();
foreach ($list as $item) {
$user_id = $item['user_id'];
$user_info = User::where('id', '=', $user_id)->field('nickname,headimg')->find();
unset($item['user_id']);
if ($user_info) {
$item['nickname'] = $user_info['nickname'];
$item['avatar'] = imageUrl($user_info['headimg']);
}
$item['create_time'] = date('Y-m-d H:i', $item['addtime']);
}
$data = [
'list' => $list,
];
return $this->renderSuccess('请求成功', $data);
}
/**
* 福利屋开奖记录
* @param Request $request
* @return \think\response\Json
*/
public function fuliwu_records(Request $request)
{
$goods_id = request()->param('goods_id/d', 0);
$list = OrderList::where('goods_id', '=', $goods_id)
->where('order_type', '=', 15)
->where('shang_id', '>', 0)
->field('user_id,addtime,goodslist_title,shang_id')
->order('shang_id desc,addtime desc')
->limit(1000)
->select();
foreach ($list as $item) {
$user_id = $item['user_id'];
$user_info = User::where('id', '=', $user_id)->field('nickname,headimg')->find();
unset($item['user_id']);
if ($user_info) {
$item['nickname'] = $user_info['nickname'];
$item['avatar'] = imageUrl($user_info['headimg']);
}
$item['create_time'] = date('Y-m-d H:i', $item['addtime']);
}
$data = [
'list' => $list,
];
return $this->renderSuccess('请求成功', $data);
}
/**
* 福利屋用户参与记录
* @param \think\Request $request
* @return \think\response\Json
*/
public function fuliwu_user_records(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
// $user_id = $this->getUserId();
$list = OrderList::where('user_id', '=', $user_id)
->where('order_type', '=', 15)
->field('user_id,addtime,goodslist_title,shang_id,goods_id')
->order('addtime desc')
->limit(1000)
->select();
if ($list) {
foreach ($list as $item) {
$goods_id = $item['goods_id'];
$goods = GoodsModel::where('id', '=', $goods_id)->find();
$item['goods_title'] = $goods['title'];
$item['create_time'] = date('Y-m-d H:i', $item['addtime']);
}
}
$data = [
'list' => $list,
];
return $this->renderSuccess('请求成功', $data);
}
/**
* 福利屋用户参与记录
* @param \think\Request $request
* @return \think\response\Json
*/
public function fuliwu_user_winning_records(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
$list = OrderList::where('user_id', '=', $user_id)
->where('order_type', '=', 15)
->where('shang_id', '>', 0)
->field('user_id,addtime,goodslist_title,shang_id,goods_id')
->order('addtime desc')
->limit(1000)
->select();
if ($list) {
foreach ($list as $item) {
$goods_id = $item['goods_id'];
$goods = GoodsModel::where('id', '=', $goods_id)->find();
$item['goods_title'] = $goods['title'];
$item['create_time'] = date('Y-m-d H:i', $goods['open_time']);
}
}
$data = [
'list' => $list,
];
return $this->renderSuccess('请求成功', $data);
}
}