manghe/app/api/controller/Infinite.php
2025-04-25 02:31:33 +08:00

1375 lines
53 KiB
PHP
Executable File
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
declare(strict_types=1);
namespace app\api\controller;
use app\api\controller\Base;
use app\common\model\Collect;
use app\common\model\Give;
use app\common\model\Goods as Goodsmodel;
use app\common\model\GoodsList;
use app\common\model\ItemCard;
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 app\common\model\UserCoupon;
use think\facade\Db;
use app\common\model\CouponReceive as CouponReceiveModel;
use app\common\model\GoodsExtend;
use \think\Request;
use app\common\service\GoodsService;
use app\common\model\GoodsType;
/**
* 无限赏
*/
class Infinite extends Base
{
static $shang_prize_id = [34, 38];#抽奖赏品id
/**
* 商品详情
*/
public function infinite_goodsdetail(Request $request)
{
$user_id = $this->getUserId();
$goods_id = request()->param('goods_id/d', 0);
// 获取Redis实例并检查缓存
$redis = (new \app\common\server\RedisHelper())->getRedis();
$cache_key = "infinite_goodsdetail_{$goods_id}_{$user_id}";
$cache_data = $redis->get($cache_key);
// 如果缓存存在,直接返回缓存数据
if ($cache_data) {
$cached_data = json_decode($cache_data, true);
// 检查是否有最新的热度值,并更新缓存中的数据
$heat_cache_key = "order_goods_count:{$goods_id}";
$latest_heat = $redis->get($heat_cache_key);
if ($latest_heat !== false) {
// 更新缓存数据中的热度值
$cached_data['goods']['join_count'] = intval($latest_heat);
}
return $this->renderSuccess("请求成功", $cached_data);
}
$goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,addtime,rage_is,rage,item_card_id,lingzhu_is,lingzhu_shang_id,is_shou_zhe,daily_xiangou,quanju_xiangou')
->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
$goods['addtime'] = date('Y-m-d H:i:s', $goods['addtime']);
$goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']);
//重抽卡
$goods['item_card'] = (object) [];
$goods['user_rage_schedule'] = 0;
$goods['user_rage'] = 0;
$goods['item_card_info'] = '';
if ($goods['rage_is'] == 1 && $goods['item_card_id'] > 0) {
$item_card = ItemCard::field('id,type,title')->where(['id' => $goods['item_card_id'], 'status' => 1])->find();
if ($item_card) {
$goods['item_card'] = $item_card;
$goods['item_card_info'] = '重抽卡说明: 怒气值达到' . $goods['rage'] . '时, 获得重抽卡';
//当前盒子的怒气值
if ($user_id > 0) {
$user_rage = Db::name('user_rage')->field('id,rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->find();
$goods['user_rage'] = $user_rage ? $user_rage['rage'] : 0;
//怒气值进度
if (!$user_rage || $user_rage['rage'] <= 0) {
$goods['user_rage_schedule'] = 0;
} else {
$goods['user_rage_schedule'] = round($user_rage['rage'] / $goods['rage'] * 100);
}
}
}
}
if ($goods['type'] == 7) {
$goods['need_draw_num'] = 1;
}
$goods_type = GoodsType::field('corner_text')->where('value', '=', $goods['type'])->find();
if ($goods_type) {
$goods['type_text'] = $goods_type['corner_text'];
}
#当前热度
$join_user = Order::field('user_id')
->append(['userinfo'])
->where('goods_id', '=', $goods_id)
// ->where('order_type', '=', $goods['type'])
->order('id desc')
->group('user_id')
->limit(5)
->select();
$new_join_user = [];
foreach ($join_user as $join_user_value) {
$new_join_user[] = imageUrl($join_user_value['userinfo']['headimg']);
}
$goods['join_user'] = $new_join_user;
#参与次数
$redis = (new \app\common\server\RedisHelper())->getRedis();
$cacheKey = "order_goods_count:{$goods_id}";
$join_count1 = $redis->get($cacheKey);
// 缓存未命中,从数据库获取并缓存
if ($join_count1 === false) {
$join_count1 = OrderList::field('id')
->where('goods_id', '=', $goods_id)
->where('shang_id', 'between', [34, 38])
// ->where('order_type', '=', $goods['type'])
// ->where('source', '=', 1)
->count();
// 设置缓存5分钟过期
$redis->set($cacheKey, $join_count1, 300);
} else {
// 转为整数
$join_count1 = intval($join_count1);
}
$goods['join_count'] = $join_count1;
#赏池分类
$goodslist = GoodsList::field('goods_id,shang_id')
->append(['shang_info', 'goods_list'])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->where('goods_list_id', '=', 0)
->order('sort desc,shang_id asc,id asc')
->group('shang_id')
->select()->toArray();
foreach ($goodslist as &$value) {
$value['pro'] = '概率:' . array_sum(array_column($value['goods_list'], 'real_pro')) . '%';
$value['shang_title'] = $value['shang_info']['title'];
$value['shang_imgurl'] = $value['shang_info']['imgurl'];
$value['shang_color'] = $value['shang_info']['color'];
$value['lingzhu_shang_id'] = 0;
if ($goods['lingzhu_is'] == 1) {
if ($value['shang_id'] == $goods['lingzhu_shang_id']) {
$value['lingzhu_shang_id'] = 1;
foreach ($value['goods_list'] as &$value1) {
$value1['is_lingzhu'] = 1;
}
}
}
unset($value['shang_info']);
}
$goods_list = GoodsList::field('id,goods_id,shang_id,title,imgurl,price,real_pro,sc_money,goods_type,sale_time,doubling')
->append(['shang_info', 'goods_list'])
->withAttr('imgurl', function ($value, $data) {
return imageUrl($value);
})
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->where('goods_list_id', '=', 0)
->order('sort desc,shang_id asc,id asc')
->paginate(1000);
foreach ($goods_list as &$value) {
#预售时间
if ($value['sale_time']) {
$value['sale_time'] = date('Y-m-d', $value['sale_time']);
}
$value['shang_title'] = $value['shang_info']['title'];
$value['shang_imgurl'] = $value['shang_info']['imgurl'];
$value['shang_color'] = $value['shang_info']['color'];
unset($value['shang_info']);
}
$type1 = 8;
if ($goods['type'] == 2) {
$type1 = 8;
} elseif ($goods['type'] == 8) {
$type1 = 14;
} elseif ($goods['type'] == 16) {
$type1 = 21;
} elseif ($goods['type'] == 17) {
$type1 = 8;
}
$draw_num = 0;
$goodsService = new GoodsService();
$limitInfo = $goodsService->getPurchaseLimitInfo($user_id, 0, $goods, self::$shang_prize_id);
if ($user_id > 0) {
//是否收藏
$collection_is = Collect::field('id')
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->find();
$goods['collection_is'] = $collection_is ? 1 : 0;
$user_info = $this->getUser();
$draw_num = $user_info['draw_num'];
$limitInfo['user_test'] = $user_info['istest'];
}
$new_data = [
'goods' => $goods,
'goodslist' => $goodslist,
'goods_list' => $goods_list,
'draw_num' => $draw_num, #优惠券的数量
'danye_id' => $type1,
'limitInfo' => $limitInfo
];
// 将原始数据缓存到Redis设置过期时间为60秒3分钟
$redis->set($cache_key, json_encode($new_data), 180);
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 连击赏详情
*/
public function infinite_goodsdetail2(Request $request)
{
$user_info = $this->getUser();
$goods_id = request()->param('goods_id/d', 0);
$type = request()->param('type/d', 1);
$goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,addtime,lian_ji_num,lian_ji_shang_id,is_shou_zhe')
->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
if (!in_array($type, [0, 1])) {
return $this->renderError("参数错误");
}
$goods['addtime'] = date('Y-m-d H:i:s', $goods['addtime']);
$goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']);
$base = getConfig('base');
#最大发数
$goods['max_fa'] = $base['lianji_max_num'];
//用户秘宝次数
$goods['user_mb_number'] = $user_info['mb_number'];
//用户连击次数
$lian_ji_number = Db::name('user_goods_lian_ji')->where([['user_id', '=', $user_info['id']], ['goods_id', '=', $goods_id]])->whereNull('deltime')->order('id', 'desc')->limit(1)->value('number');
$goods['user_lian_ji_number'] = $lian_ji_number ? $lian_ji_number : 0;
#是否收藏
$collection_is = Collect::field('id')
->where(['user_id' => $user_info['id']])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->find();
$goods['collection_is'] = $collection_is ? 1 : 0;
#当前热度
$join_user = OrderList::field('user_id')
->append(['userinfo'])
->where('goods_id', '=', $goods_id)
->where('order_type', '=', $goods['type'])
->where('source', '=', 1)
->order('id desc')
->group('user_id')
->limit(5)
->select();
$new_join_user = [];
foreach ($join_user as $join_user_value) {
$new_join_user[] = $join_user_value['userinfo']['headimg'];
}
$goods['join_user'] = $new_join_user;
#参与次数
$redis = (new \app\common\server\RedisHelper())->getRedis();
$cacheKey = "order_goods_count:{$goods_id}";
$join_count1 = $redis->get($cacheKey);
// 缓存未命中,从数据库获取并缓存
if ($join_count1 === false) {
$join_count1 = OrderList::field('id')->where('goods_id', '=', $goods_id)
->where('shang_id', 'between', [34, 38])
->where('order_type', '=', $goods['type'])
->where('source', '=', 1)
->count();
// 设置缓存5分钟过期
$redis->set($cacheKey, $join_count1, 300);
} else {
// 转为整数
$join_count1 = intval($join_count1);
}
$goods['join_count'] = $join_count1;
#赏池分类
$goodslist = GoodsList::field('goods_id,shang_id')
->append(['shang_info', 'goods_list'])
->where(['goods_id' => $goods_id])
->where(['lian_ji_type' => $type])
->where(['num' => 0])
->order('sort desc,shang_id asc,id asc')
->group('shang_id')
->select()->toArray();
foreach ($goodslist as &$value) {
$value['pro'] = '概率:' . array_sum(array_column($value['goods_list'], 'real_pro')) . '%';
$value['shang_title'] = $value['shang_info']['title'];
$value['shang_imgurl'] = $value['shang_info']['imgurl'];
$value['shang_color'] = $value['shang_info']['color'];
unset($value['shang_info']);
}
$goods_list = GoodsList::field('goods_id,shang_id,title,imgurl,price,real_pro,sc_money')
->append(['shang_info', 'goods_list'])
->withAttr('imgurl', function ($value, $data) {
return imageUrl($value);
})
->where(['goods_id' => $goods_id])
->where(['lian_ji_type' => $type])
->where(['num' => 0])
->order('sort desc,shang_id asc,id asc')
->paginate(1000);
foreach ($goods_list as &$value) {
$value['shang_title'] = $value['shang_info']['title'];
$value['shang_imgurl'] = $value['shang_info']['imgurl'];
$value['shang_color'] = $value['shang_info']['color'];
unset($value['shang_info']);
}
$new_data = [
'goods' => $goods,
'goodslist' => $goodslist,
'goods_list' => $goods_list,
'draw_num' => $user_info['draw_num'] #优惠券的数量
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 中奖记录
*/
public function infinite_shang_log()
{
$shang_id = request()->param('shang_id/d', 0);
$goods_id = request()->param('goods_id/d', 0);
$is_mibao = request()->param('is_mibao/d', 0);
// 生成缓存键
$redis = (new \app\common\server\RedisHelper())->getRedis();
$cache_key = "infinite_shang_log:{$goods_id}:{$shang_id}:{$is_mibao}";
// 尝试从缓存获取数据
$cached_data = $redis->get($cache_key);
if ($cached_data) {
// 直接返回缓存的数据,这个方法不涉及热度值显示,无需更新
return $this->renderSuccess("请求成功", json_decode($cached_data, true));
}
#盒子信息
$goods = Goodsmodel::field('id,stock,status,type')
->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
#中奖记录分类
$where = [];
$where[] = ['goods_id', '=', $goods_id];
$where[] = ['num', '=', 0];
if ($is_mibao == 1) {
$where[] = ['lian_ji_type', '=', 1];
}
$category = GoodsList::field('shang_id')
->append(['shang_title'])
->where($where)
->group('shang_id')
->order('sort desc,shang_id asc,id asc')
->select()->toArray();
array_unshift($category, ['shang_id' => 0, 'shang_title' => '全部']);
$where2 = [];
$where2[] = ['goods_id', '=', $goods_id];
$where2[] = ['num', '=', 0];
$where2[] = ['order_type', '=', $goods['type']];
if ($shang_id) {
$where2[] = ['shang_id', '=', $shang_id];
}
$data = OrderList::field('user_id,goodslist_title,goodslist_imgurl,shang_id,addtime,luck_no,doubling,is_lingzhu')
->append(['shang_title', 'user_info', 'shang_color'])
->where($where2)
->where('source', '=', 1)
->order('id desc')
->paginate(100)->each(function ($item) {
$item['user_info']['headimg'] = imageUrl($item['user_info']['headimg']);
$item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
$item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']);
return $item;
});
$new_data = [
'category' => $category,
'data' => $data->items(),
'last_page' => $data->lastPage(),
];
// 将数据存入缓存设置1分钟过期时间
$redis->set($cache_key, json_encode($new_data), 60);
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 盒子抽奖记录
* @param \think\Request $request
* @return \think\response\Json
*/
public function infinite_prizerecords(Request $request)
{
$goods_id = request()->param('goods_id/d', 0);
$user_id = $this->getUserId();
// 查询中奖记录
$where = [];
$where[] = ['goods_id', '=', $goods_id];
$where[] = ['num', '=', 0];
$where[] = ['user_id', '=', $user_id];
$data = OrderList::field('user_id,goodslist_title,goodslist_imgurl,addtime')
->where($where)
->where('source', '=', 1)
->order('id desc')
->paginate(100)->each(function ($item) {
$item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
$item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']);
return $item;
});
return $this->renderSuccess("请求成功", $data);
}
/**
* 下单计算金额
*/
public function infinite_ordermoney()
{
$user = $this->getUser();
$prize_num = request()->param('prize_num/d', 0); #抽几发
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$use_money_is = request()->param('use_money_is/d', 0); #0不抵扣 1抵扣
$use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣
$use_money2_is = request()->param('use_money2_is/d', 0); #0不抵扣 1抵扣 货币2抵扣
$coupon_id = request()->param('coupon_id/d'); //优惠券
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,quanju_xiangou,daily_xiangou,choujiang_xianzhi')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
// 获取服务实例
$calculator = new \app\common\service\PaymentCalculator();
// 验证抽奖限制
$validationResult = $calculator->validateDrawRestrictions($user, $goods, $prize_num);
if ($validationResult['status'] == 0) {
return $this->renderError($validationResult['msg']);
}
// 计算订单金额
$result = $calculator->calculateOrderAmount(
$user,
$goods_id,
$prize_num,
$use_money_is,
$use_integral_is,
$use_money2_is,
$coupon_id,
$goods
);
if ($result['status'] == 0) {
return $this->renderError($result['msg']);
}
return $this->renderSuccess("请求成功", $result);
}
/**
* 下单
*/
public function infinite_orderbuy()
{
$user = $this->getUser();
if (empty($user['mobile'])) {
return $this->renderError('请先绑定手机号', [], -9);
}
return $this->renderError('正在维护中', []);
$ad_id = request()->header('adid');
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$prize_num = request()->param('prize_num/d', 0); #抽几发
$use_money_is = request()->param('use_money_is/d', 0); #0不抵扣 1抵扣
$use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣
$use_money2_is = request()->param('use_money2_is/d', 0); #0不抵扣 1抵扣 货币2抵扣
$coupon_id = request()->param('coupon_id'); //优惠券
$is_mibao = request()->param('is_mibao/d', 0); //连击赏下 是否是抽的秘宝池 1是 0否
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,choujiang_xianzhi,quanju_xiangou,daily_xiangou')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
# 获取盒子类型配置
$goods_type = $goods['type'];
$goods_extend = GoodsExtend::getGoodsExtendByGoodsId($goods_id, $goods_type);
if (!$goods_extend) {
return $this->renderError("盒子类型配置不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
if (!in_array($goods['type'], [2, 8, 9, 10, 16, 17])) {
return $this->renderError("非法请求");
}
// 使用PaymentCalculator验证抽奖限制限购、消费门槛等
$paymentCalculator = new \app\common\service\PaymentCalculator();
$validationResult = $paymentCalculator->validateDrawRestrictions($user, $goods, $prize_num);
if ($validationResult['status'] == 0) {
return $this->renderError($validationResult['msg']);
}
// 验证奖品信息
$where = [];
$where[] = ['goods_id', '=', $goods_id];
if ($goods_type == 10) {
$where[] = ['shang_id', 'between', [10, 33]];
$where[] = ['num', '=', 1];
} else {
$where[] = ['num', '=', 0];
$where[] = ['real_pro', '>', 0];
$where[] = ['shang_id', 'between', self::$shang_prize_id];
}
#奖品信息
if ($goods['type'] == 9 && $is_mibao == 1) {
//秘宝池
$where[] = ['lian_ji_type', '=', 1];
return $this->renderError('请求错误');
}
// $is_goodslist = GoodsList::field('id')
// ->where($where)
// ->find();
// if (!$is_goodslist) {
// return $this->renderError('暂无奖品信息');
// }
if ($goods['type'] == 9) {
$prize_num = intval($prize_num);
if ($prize_num > 20) {
return $this->renderError("一次最多购买20次");
}
$user_mb_number = User::where(['id' => $user['id']])->value('mb_number');
if ($is_mibao == 1 && $prize_num > $user_mb_number) {
return $this->renderError("秘宝池抽奖次数不足");
}
} else {
if ($prize_num != 1 && $prize_num != 3 && $prize_num != 5 && $prize_num != 10 && $prize_num != 50) {
return $this->renderError("请求参数错误!!!");
}
}
// 使用PaymentCalculator计算订单金额和支付方式
$paymentResult = $paymentCalculator->calculateOrderAmount(
$user,
$goods_id,
$prize_num,
$use_money_is,
$use_integral_is,
$use_money2_is,
$coupon_id,
$goods,
$goods_extend
);
if ($paymentResult['status'] == 0) {
return $this->renderError($paymentResult['msg']);
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_infinite_orderbuy" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
Db::startTrans();
$num = 0;
if ($goods['type'] == 10) {
$num = 1;
}
if ($goods['daily_xiangou'] > 0) {
// 查看一天内有没有未支付的订单,未支付的订单要先作废
$order_info = Order::where('goods_id', '=', $goods_id)
->where('user_id', '=', $user['id'])
->where('num', '=', $num)
->where('status', '=', 0)
->where('addtime', '>=', time() - 86400)
->find();
if ($order_info) {
$order_info->status = 2;
$order_info->save();
}
}
$res = [];
$order_num = create_order_no('MH_', 'order', 'order_num');
#创建订单
$res[] = $order_id = Order::insertGetId([
'user_id' => $user['id'],
'order_num' => $order_num,
'order_total' => $paymentResult['order_total'],#订单金额
'order_zhe_total' => $paymentResult['order_zhe_total'],#订单折扣金额
'price' => $paymentResult['price'],#微信支付
'use_money' => $paymentResult['use_money'],#余额抵扣
'use_integral' => $paymentResult['use_integral'],#吧唧币抵扣
'use_money2' => $paymentResult['use_money2'],#货币2抵扣
'use_score' => 0,#积分抵扣
'zhe' => $paymentResult['zhe'],#会员折扣
'goods_id' => $goods_id,
'num' => $num,
'goods_price' => $goods['price'],
'goods_title' => $goods['title'],
'goods_imgurl' => $goods['imgurl_detail'],
'prize_num' => $prize_num,
'status' => 0,
'pay_type' => 1,#1微信 2支付宝
'order_type' => $goods['type'],
'addtime' => time(),
'coupon_id' => $paymentResult['coupon_id'],
'use_coupon' => $paymentResult['coupon_price'], #优惠券抵扣
'is_mibao' => $is_mibao,
'is_shou_zhe' => $paymentResult['is_shou_zhe'],
'ad_id' => $ad_id,
'click_id' => $user['click_id']
]);
if ($goods['quanju_xiangou'] > 0) {
// 查看有没有未支付的订单,未支付的订单要先作废
Order::where('goods_id', '=', $goods_id)
->where('user_id', '=', $user['id'])
->where('status', '=', 0)
->where('order_num', '!=', $order_num)
->update(['status' => 2]);
}
if ($goods['daily_xiangou'] > 0) {
// 查看一天内有没有未支付的订单,未支付的订单要先作废
Order::where('goods_id', '=', $goods_id)
->where('user_id', '=', $user['id'])
->where('status', '=', 0)
->where('addtime', '>=', time() - 86400)
->where('order_num', '!=', $order_num)
->update(['status' => 2]);
}
#微信支付金额大于0
if ($paymentResult['price'] > 0) {
$body = '购买盒子' . $goods['title'];
$attach = 'order_wxs';
if ($goods['type'] == 16) {
$attach = 'order_fbs';
}
// $isTest = \app\common\helper\ConfigHelper::getSystemTestKey("disable_wechat_pay");
// if ($isTest == "1") {
// Db::rollback();
// #删除redis
// $redis->del($redis_key);
// return $this->renderError("支付未开放");
// }
$payRes = (new Pay())->wxCreateOrder($order_num, $paymentResult['price'], $user['openid'], $body, $attach);
if ($payRes['status'] == 1) {
#结果集
$new_data = [
'status' => 1,
'order_num' => $order_num,
'res' => $payRes['data'],
];
} else {
#删除redis
$redis->del($redis_key);
Db::rollback();
return $this->renderError("下单失败");
}
} else {
#开盒子
$res[] = (new Notify($this->app))->infinite_drawprize_notice($user['id'], $order_id, $goods_id, $num);
#结果集
$new_data = [
'status' => 0,
'order_num' => $order_num,
];
}
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("下单成功", $new_data);
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("购买失败,请刷新重试");
}
}
/**
* 秘宝池下单
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function infinite_mibao_orderbuy()
{
$user = $this->getUser();
if (empty($user['mobile'])) {
return $this->renderError('请先绑定手机号', [], -9);
}
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$prize_num = request()->param('prize_num/d', 0); #抽几发
$is_mibao = request()->param('is_mibao/d', 0); //连击赏下 是否是抽的秘宝池 1是 0否
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,choujiang_xianzhi')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
if ($goods['type'] != 9) {
return $this->renderError("非法请求");
}
$user_id = $this->getuserid();
$choujiang_xianzhi = $goods['choujiang_xianzhi'];
if ($choujiang_xianzhi && $choujiang_xianzhi > 0) {
// SELECT sum(price) FROM xinglanmh_shequt_test.`order` where user_id=4445 and status=1
$user_price = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('price');
if ($user_price < $choujiang_xianzhi) {
return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "");
}
}
//秘宝池
$where = [];
$where[] = ['goods_id', '=', $goods_id];
$where[] = ['num', '=', 0];
$where[] = ['real_pro', '>', 0];
$where[] = ['shang_id', 'between', self::$shang_prize_id];
$where[] = ['lian_ji_type', '=', 1];
$is_goodslist = GoodsList::field('id')
->where($where)
->find();
if (!$is_goodslist) {
return $this->renderError('暂无奖品信息');
}
$prize_num = intval($prize_num);
if ($prize_num > 20) {
return $this->renderError("一次最多购买20次");
}
$user_mb_number = User::where(['id' => $user['id']])->value('mb_number');
if ($prize_num > $user_mb_number) {
return $this->renderError("秘宝池抽奖次数不足");
}
#盒子单价
$box_price = $goods['price'];
#订单金额 微信支付金额
$order_total = $price = bcmul("$box_price", "$prize_num", 2);
if ($price <= 0) {
$price = 0;
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_infinite_orderbuy" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 2);
}
Db::startTrans();
$res = [];
$order_num = create_order_no('MH_', 'order', 'order_num');
#创建订单
$res[] = $order_id = Order::insertGetId([
'user_id' => $user['id'],
'order_num' => $order_num,
'order_total' => $order_total,#订单金额
'order_zhe_total' => 0,#订单折扣金额
'price' => 0,#微信支付
'use_money' => 0,#余额抵扣
'use_integral' => 0,#吧唧币抵扣
'use_score' => 0,#积分抵扣
'zhe' => 0,#会员折扣
'goods_id' => $goods_id,
'num' => 0,
'goods_price' => $goods['price'],
'goods_title' => $goods['title'],
'goods_imgurl' => $goods['imgurl_detail'],
'prize_num' => $prize_num,
'status' => 0,
'pay_type' => 1,#1微信 2支付宝
'order_type' => $goods['type'],
'addtime' => time(),
'coupon_id' => 0,
'use_coupon' => 0, #优惠券抵扣
'is_mibao' => $is_mibao
]);
#开盒子
$res[] = (new Notify($this->app))->infinite_drawprize_notice($user['id'], $order_id, $goods_id);
#结果集
$new_data = [
'status' => 0,
'order_num' => $order_num,
];
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("下单成功", $new_data);
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("购买失败,请刷新重试");
}
}
/**
* 抽中的奖品
*/
public function infinite_prizeorderlog()
{
$user = $this->getUser();
$order_num = request()->param('order_num', '');
$order_info = Order::field('id,goods_id,num,order_type')
->where('order_num', '=', $order_num)
->where('user_id', '=', $user['id'])
->find();
if (!$order_info) {
return $this->renderError("支付异常,请刷新重试");
}
#普通赏
$data = OrderList::field('id,user_id,shang_id,goodslist_id,goodslist_title,goodslist_imgurl,goodslist_money,doubling,is_lingzhu')
->append(['shang_title'])
->where('user_id', '=', $user['id'])
->where('order_id', '=', $order_info['id'])
->where('order_type', '=', $order_info['order_type'])
->order('goodslist_money desc, id asc')
->paginate(100)->each(function ($item) {
$item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']);
return $item;
});
$userCoupon = UserCoupon::field('id,level,title,num')
->where('user_id', '=', $user['id'])
->where('from_id', '=', $order_info['id'])
->order('level desc')
->find();
if ($userCoupon != null) {
//1特级赏券 2终极赏券 3高级赏券 4普通赏券
if ($userCoupon['level'] == 1) {
$userCoupon['level_text'] = '特级赏券';
$userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_a.png');
} elseif ($userCoupon['level'] == 2) {
$userCoupon['level_text'] = '终极赏券';
$userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_b.png');
} elseif ($userCoupon['level'] == 3) {
$userCoupon['level_text'] = '高级赏券';
$userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_c.png');
} elseif ($userCoupon['level'] == 4) {
$userCoupon['level_text'] = '普通赏券';
$userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_d.png');
}
}
//重抽卡数量
$item_card_count = Db::name('user_item_card')->where(['user_id' => $user['id'], 'status' => 1])->count();
$new_data = [
'user_info' => [
'nickname' => $user['nickname'],
'headimg' => $user['headimg'],
],
'data' => $data->items(),
'item_card_count' => $item_card_count,
'user_coupon' => $userCoupon
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 无限令奖励
*/
public function infinite_give_list()
{
$user = $this->getUser();
$thismonth = getConfig('give_time')['range_time'];
$thismontharr = explode(' - ', $thismonth);
#获取开始时间戳
$beginThismonth = strtotime($thismontharr[0]);
#获取结束时间戳
$endThismonth = strtotime($thismontharr[1]);
$consumption_total = Order::field('total')
->where('status', '=', 1)
->where('user_id', '=', $user['id'])
->where('addtime', 'BETWEEN', array($beginThismonth, $endThismonth))
->where('order_type', '=', 2)
->sum('order_total');
#时间数据 消费
$other_data = [
'time' => date('Y-m-d', $beginThismonth) . '-' . date('Y-m-d', $endThismonth),
'money' => $consumption_total,
];
#是否领取过当前时间戳
$time_int = $beginThismonth . '_' . $endThismonth;
$give_money = Give::where('time_int', '=', $time_int)->order('id desc')->value('money');
$give_money = $give_money ? $give_money : 0;
#奖品列表
$data = GoodsList::field('id,give_money,goods_id')
->append(['give_list'])
->where('goods_id', '=', 0)
->order('give_money asc,id asc')
->group('give_money')
->select()->toArray();
foreach ($data as &$value) {
if ($give_money >= $value['give_money']) {
$value['is'] = 1;
} else {
$value['is'] = 0;
}
}
$new_data = [
'other_data' => $other_data,
'data' => $data,
];
return $this->renderSuccess('请求成功', $new_data);
}
/**
* 无限令领取接口
*/
public function infinite_give_goods()
{
$user = $this->getUser();
$user_id = $user['id'];
$thismonth = getConfig('give_time')['range_time'];
$thismontharr = explode(' - ', $thismonth);
#获取开始时间戳
$beginThismonth = strtotime($thismontharr[0]);
#获取结束时间戳
$endThismonth = strtotime($thismontharr[1]);
#是否领取过当前时间戳
$time_int = $beginThismonth . '_' . $endThismonth;
$give_money = Give::where('time_int', '=', $time_int)->order('id desc')->value('money');
$give_money = $give_money ? $give_money : 0;
#总消费
$consumption_total = Order::field('total')
->where('status', '=', 1)
->where('user_id', '=', $user['id'])
->where('addtime', 'BETWEEN', array($beginThismonth, $endThismonth))
->where('order_type', '=', 2)
->sum('order_total');
if ($give_money >= $consumption_total) {
return $this->renderError("暂无可领取奖品");
}
#赠送商品
$give_goods = GoodsList::where(['goods_id' => 0])
->where("give_money", '>', $give_money)
->where("give_money", '<=', $consumption_total)
->order('give_money asc,id asc')
->select()->toArray();
if (!$give_goods) {
return $this->renderError("暂无可领取奖品");
}
Db::startTrans();
$res = [];
#符合赠送
$save_data = [];
foreach ($give_goods as $value) {
$save_data[] = [
'order_id' => 0,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => 0,
'num' => 0,
'shang_id' => $value['shang_id'],
'goodslist_id' => $value['id'],
'goodslist_title' => $value['title'],
'goodslist_imgurl' => $value['imgurl'],
'goodslist_price' => $value['price'],
'goodslist_money' => $value['money'],
'goodslist_type' => $value['goods_type'],
'goodslist_sale_time' => $value['sale_time'],
'addtime' => time(),
'prize_code' => $value['prize_code'],
'order_type' => 6,
];
}
$res[] = OrderList::insertAll($save_data);
$res[] = Give::insert([
'user_id' => $user_id,
'time_int' => $time_int,
'time_date' => date('Y-m-d H:i:s', $beginThismonth) . '_' . date('Y-m-d H:i:s', $endThismonth),
'money' => $consumption_total,
'addtime' => time(),
]);
if (resCheck($res)) {
Db::commit();
return $this->renderSuccess("领取成功");
} else {
Db::rollback();
return $this->renderError("领取失败,请稍后重试");
}
}
//进行抽奖
public function do_draw()
{
$user = $this->getUser();
if (empty($user['mobile'])) {
return $this->renderError('请先绑定手机号', [], -9);
}
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$prize_num = request()->param('prize_num/d', 1); #抽几发
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,choujiang_xianzhi')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
if ($goods['type'] != 7) {
return $this->renderError("非法请求");
}
$user_id = $this->getuserid();
$choujiang_xianzhi = $goods['choujiang_xianzhi'];
if ($choujiang_xianzhi && $choujiang_xianzhi > 0) {
// SELECT sum(price) FROM xinglanmh_shequt_test.`order` where user_id=4445 and status=1
$user_price = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('price');
if ($user_price < $choujiang_xianzhi) {
return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "");
}
}
#奖品信息
$is_goodslist = GoodsList::field('id')
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('real_pro', '>', 0)
->where('shang_id', 'between', self::$shang_prize_id)
->find();
if (!$is_goodslist) {
return $this->renderError('暂无奖品信息');
}
if ($prize_num != 1) {
return $this->renderError("请求参数错误");
}
$draw_num = $user['draw_num'];
// var_dump($draw_num);
// exit;
if ($user['draw_num'] == 0 || bccomp("$draw_num", "$prize_num") < 0) {
return $this->renderError("抽奖券不足,无法进行抽奖");
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_infinite_orderbuy_draw" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
Db::startTrans();
$res = [];
$order_num = create_order_no('MH_', 'order', 'order_num');
#创建订单
$res[] = $order_id = Order::insertGetId([
'user_id' => $user['id'],
'order_num' => $order_num,
'order_total' => 0,#订单金额
'order_zhe_total' => 0,#订单折扣金额
'price' => 0,#微信支付
'use_money' => 0,#余额抵扣
'use_integral' => 0,#吧唧币抵扣
'use_score' => 0,#积分抵扣
'zhe' => 0,#会员折扣
'goods_id' => $goods_id,
'num' => 0,
'goods_price' => $goods['price'],
'goods_title' => $goods['title'],
'goods_imgurl' => $goods['imgurl_detail'],
'prize_num' => $prize_num,
'status' => 0,
'pay_type' => 1,#1微信 2支付宝
'order_type' => $goods['type'],
'addtime' => time(),
'use_draw' => 1
]);
#开盒子
$res[] = (new Notify($this->app))->draw_drawprize_notice($user['id'], $order_id, $goods_id);
#结果集
$new_data = [
'status' => 0,
'order_num' => $order_num,
];
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("下单成功", $new_data);
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("购买失败,请刷新重试");
}
}
/**
* 重抽卡重抽
* @param Request $request
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function item_card_chou(Request $request)
{
$user = $this->getUser();
if (empty($user['mobile'])) {
return $this->renderError('请先绑定手机号', [], -9);
}
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$order_list_ids = request()->param('order_list_ids', ""); #重抽的ID
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,choujiang_xianzhi')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
$order_list_ids = explode(',', trim($order_list_ids));
if (empty($order_list_ids)) {
return $this->renderError("参数错误");
}
$user_id = $this->getuserid();
$choujiang_xianzhi = $goods['choujiang_xianzhi'];
if ($choujiang_xianzhi && $choujiang_xianzhi > 0) {
// SELECT sum(price) FROM xinglanmh_shequt_test.`order` where user_id=4445 and status=1
$user_price = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('price');
if ($user_price < $choujiang_xianzhi) {
return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "");
}
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "item_card_chou" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 5);
}
$item_card_count = Db::name('user_item_card')->where(['user_id' => $user['id'], 'status' => 1])->whereNull('deltime')->count();
if (1 > $item_card_count) {
return $this->renderError("重抽卡数量不足");
}
foreach ($order_list_ids as $k => $v) {
$order_list = OrderList::field('id')->where(['id' => $v, 'status' => 0])->find();
if (!$order_list) {
return $this->renderError("数据错误");
}
}
Db::startTrans();
$res = [];
$order_num = create_order_no('MH_', 'order', 'order_num');
#创建订单
$res[] = $order_id = Order::insertGetId([
'user_id' => $user['id'],
'order_num' => $order_num,
'order_total' => 0,#订单金额
'order_zhe_total' => 0,#订单折扣金额
'price' => 0,#微信支付
'use_money' => 0,#余额抵扣
'use_integral' => 0,#吧唧币抵扣
'use_score' => 0,#积分抵扣
'zhe' => 0,#会员折扣
'goods_id' => $goods_id,
'num' => 0,
'goods_price' => $goods['price'],
'goods_title' => $goods['title'],
'goods_imgurl' => $goods['imgurl_detail'],
'prize_num' => count($order_list_ids),
'status' => 0,
'pay_type' => 1,#1微信 2支付宝
'order_type' => $goods['type'],
'addtime' => time(),
'use_item_card' => 1
// 'use_item_card' => count($order_list_ids)
]);
#开盒子
$res[] = (new Notify($this->app))->item_card_notice($user['id'], $order_id, $goods_id);
#判断是否发积分 发券
$order = Order::where(['id' => $order_id])->find();
$res[] = User::is_integral_coupon($order);
$res[] = OrderList::where(['user_id' => $user['id']])->whereIn('id', $order_list_ids)->update(['is_chong' => 1, 'deltime' => time()]);
#结果集
$new_data = [
'status' => 0,
'order_num' => $order_num,
];
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("重抽成功", $new_data);
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("重抽失败,请刷新重试");
}
}
/**
*领主
*/
public function ling_zhu_king(Request $request)
{
$goods_id = $request->param('goods_id');
$type = $request->param('type/d', 1);
if (!isset($goods_id) && empty($goods_id)) {
return $this->renderSuccess('参数错误');
}
//查找当前领主
$goods = Db::name('goods')->field('king_user_id,lingzhu_fan,lingzhu_shang_id')->where([['id', '=', $goods_id]])->find();
$king_user = Db::name('user')->field('id,nickname,headimg')->where(['id' => $goods['king_user_id']])->find();
if ($goods && $goods['king_user_id'] && $king_user) {
$king_user['headimg'] = imageUrl($king_user['headimg']);
$goods_king_rank = Db::name('goods_king_rank')->field('id,z_nums,order_list_id,count')->where(['user_id' => $goods['king_user_id'], 'goods_id' => $goods_id])->order('id desc')->limit(1)->find();
$order_list_id = $goods_king_rank ? $goods_king_rank['order_list_id'] : 0;
$jiang_img = OrderList::field('goodslist_title,goodslist_imgurl')->where(['id' => $order_list_id])->find();
$king_user['jiang_title'] = $jiang_img ? $jiang_img['goodslist_title'] : '';
$king_user['count'] = $goods_king_rank ? $goods_king_rank['count'] : 0;
$king_user['jiang_img'] = $jiang_img ? imageUrl($jiang_img['goodslist_imgurl']) : '';
$king_user['z_nums'] = $goods_king_rank ? $goods_king_rank['z_nums'] : 0;
} else {
$king_user = null;
}
if ($type == 1) {
//挑战人数
$list = Db::name('goods_king_rank')
->alias('a')
->join('user b', 'a.user_id = b.id')
->field('a.user_id,b.nickname,b.headimg')
->where([['a.goods_id', '=', $goods_id]])
->group('a.user_id')
->order('a.id', 'desc')
->paginate(20)->each(function ($item) {
$item['headimg'] = imageUrl($item['headimg']);
return $item;
});
} elseif ($type == 2) {
//领主记录
$list = Db::name('goods_king_rank')
->alias('a')
->join('user b', 'a.user_id = b.id')
->field('a.user_id,a.addtime,a.end_time,b.nickname,b.headimg')
->where([['a.goods_id', '=', $goods_id]])
->order('a.id', 'desc')
->paginate(20)->each(function ($item) {
$item['headimg'] = imageUrl($item['headimg']);
$item['time'] = time_jian($item['addtime'], $item['end_time'] ? $item['end_time'] : time());
return $item;
});
}
$ling_goods_list = GoodsList::field('goods_id,shang_id,title,imgurl')
->append(['shang_info'])
->where(['goods_id' => $goods_id])
->where(['shang_id' => $goods['lingzhu_shang_id']])
->where(['num' => 0])
->order('sort desc,shang_id asc,id asc')
->select();
foreach ($ling_goods_list as &$value) {
$value['imgurl'] = imageUrl($value['imgurl']);
}
$ling_goods_list1 = GoodsList::field('goods_id,shang_id,title,imgurl')
->append(['shang_info'])
->where(['goods_id' => $goods_id])
->where('is_lingzhu', '=', 1)
->where(['num' => 0])
->order('sort desc,shang_id asc,id asc')
->select();
foreach ($ling_goods_list1 as &$value) {
$value['imgurl'] = imageUrl($value['imgurl']);
$ling_goods_list[] = $value;
}
$data = [];
$data['goods'] = $goods;
$data['king_user'] = $king_user;
$data['list'] = $list;
$data['ling_goods_list'] = $ling_goods_list;
return $this->renderSuccess('请求成功', $data);
}
}