HaniBlindBox/server/php/app/api/controller/Mall.php
2026-01-01 20:46:07 +08:00

1014 lines
39 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
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 app\common\model\DiamondProducts;
use app\common\model\DiamondOrder;
use think\facade\Db;
use app\common\model\CouponReceive as CouponReceiveModel;
use \think\Request;
use app\common\model\GoodsExtend;
class Mall extends Base
{
static $shang_prize_id = [34, 38];#抽奖赏品id
/**
* 下单计算金额
*/
public function mall_ordermoney()
{
$user = $this->getUser();
$prize_num = request()->param('prize_num/d', 0); #抽几发
$goods_id = request()->param('goods_id/d', 0); #盒子ID
#盒子信息
$goods = Goodsmodel::field('title,imgurl_detail,type,price,status,is_shou_zhe')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
$goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']);
$price = $goods['price'];
#抽奖数量
$goods['prize_num'] = $prize_num;
$goods_type = $goods['type'];
$goodsExtend = GoodsExtend::getGoodsExtendByGoodsId($goods_id, $goods_type);
$data = [
'goods' => $goods,
'price' => round($price, 2),
'integral' => round($price * 100, 2),
'use_integral' => round($user['money2'], 2),
'money' => round($user['money'], 2),
'goodsExtend' => $goodsExtend,
];
return $this->renderSuccess("请求成功", $data);
}
/**
* 下单
*/
public function mall_orderbuy()
{
$user = $this->getUser();
if (empty($user['mobile'])) {
return $this->renderError('请先绑定手机号', [], -9);
}
$ad_id = request()->header('adid');
$goods_id = request()->param('goods_id/d', 0); #盒子ID
$prize_num = request()->param('prize_num/d', 0); #抽几发
#盒子信息
$goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,choujiang_xianzhi,daily_xiangou')->where(['id' => $goods_id])
->find();
if (!$goods) {
return $this->renderError("盒子不存在");
}
if ($goods['status'] != 1) {
return $this->renderError("盒子已下架");
}
if (!in_array($goods['type'], [2, 8, 9, 10])) {
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');
$user_use_money = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('use_money');
if($user_use_money>0){
$user_price = $user_price + $user_use_money;
}
if ($user_price < $choujiang_xianzhi) {
return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "");
}
}
// 检查每日限购次数
if (isset($goods['daily_xiangou']) && $goods['daily_xiangou'] > 0) {
// 计算今天的开始和结束时间戳
$today_start = strtotime(date('Y-m-d 00:00:00', time()));
$today_end = strtotime(date('Y-m-d 23:59:59', time()));
// 查询用户今天对该商品的购买次数
$today_buy_count = Order::where([
['user_id', '=', $user_id],
['goods_id', '=', $goods_id],
['status', '=', 1],
['addtime', '>=', $today_start],
['addtime', '<=', $today_end]
])->count();
// 如果今日购买次数已达到限制,返回错误
if ($today_buy_count >= $goods['daily_xiangou']) {
return $this->renderError("该商品每日限购" . $goods['daily_xiangou'] . "次,今日已购买" . $today_buy_count . "");
}
}
$app_setting = getConfig('app_setting');
if ($app_setting) {
$exchange_times = $app_setting["exchange_times"];
if ($exchange_times && $app_setting["exchange_times"] > 0) {
// 计算今天的开始和结束时间戳
$today_start = strtotime(date('Y-m-d 00:00:00', time()));
$today_end = strtotime(date('Y-m-d 23:59:59', time()));
// 查询用户今天对该商品的购买次数
$today_buy_count = Order::where([
['user_id', '=', $user_id],
['goods_id', '=', $goods_id],
['status', '=', 1],
['addtime', '>=', $today_start],
['addtime', '<=', $today_end]
])->count();
// 如果今日购买次数已达到限制,返回错误
if ($today_buy_count >= $exchange_times) {
return $this->renderError("商城每日限购" . $exchange_times . "次,今日已购买" . $today_buy_count . "");
}
}
}
$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];
}
$is_goodslist = GoodsList::field('id')
->where($where)
->find();
if (!$is_goodslist) {
return $this->renderError('暂无奖品信息');
}
if ($prize_num != 1 && $prize_num != 3 && $prize_num != 5 && $prize_num != 10 && $prize_num != 50) {
return $this->renderError("请求参数错误!!!");
}
#盒子单价
$box_price = $goods['price'];
$order_total = $box_price * 100;
$integral = $user['money2'];
if ($order_total > $integral) {
return $this->renderError("余额不足!!!");
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_mall_orderbuy" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 3);
}
Db::startTrans();
$num = 0;
if ($goods['type'] == 10) {
$num = 1;
}
$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' => $box_price,#订单金额
'order_zhe_total' => 0,#订单折扣金额
'price' => 0,#微信支付
'use_money' => 0,#余额抵扣
'use_integral' => 0,#吧唧币抵扣
'use_score' => 0,#积分抵扣
'use_money2' => $order_total,#积分抵扣
'zhe' => 0,#会员折扣
'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' => 0,
'use_coupon' => 0, #优惠券抵扣
'is_mibao' => 0,
'is_shou_zhe' => 0,
'ad_id' => $ad_id,
'click_id' => $user['click_id']
]);
#开盒子
$res[] = (new Notify($this->app))->infinite_shangchengshang_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,daily_xiangou')->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');
$user_use_money = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('use_money');
if($user_use_money>0){
$user_price = $user_price + $user_use_money;
}
if ($user_price < $choujiang_xianzhi) {
return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "");
}
}
// 检查每日限购次数
if (isset($goods['daily_xiangou']) && $goods['daily_xiangou'] > 0) {
// 计算今天的开始和结束时间戳
$today_start = strtotime(date('Y-m-d 00:00:00', time()));
$today_end = strtotime(date('Y-m-d 23:59:59', time()));
// 查询用户今天对该商品的购买次数
$today_buy_count = Order::where([
['user_id', '=', $user_id],
['goods_id', '=', $goods_id],
['status', '=', 1],
['addtime', '>=', $today_start],
['addtime', '<=', $today_end]
])->count();
// 如果今日购买次数已达到限制,返回错误
if ($today_buy_count >= $goods['daily_xiangou']) {
return $this->renderError("该商品每日限购" . $goods['daily_xiangou'] . "次,今日已购买" . $today_buy_count . "");
}
}
//秘宝池
$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')
->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('order_list_id')->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['jiang_img'] = $jiang_img ? imageUrl($jiang_img['goodslist_imgurl']) : '';
} 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']);
}
$data = [];
$data['goods'] = $goods;
$data['king_user'] = $king_user;
$data['list'] = $list;
$data['ling_goods_list'] = $ling_goods_list;
return $this->renderSuccess('请求成功', $data);
}
public function get_diamond_list()
{
$diamond_list = Db::name('diamond_products')->field('products_id,name,normal_image,normal_select_image,price,status,is_first,first_charge_image,first_select_charge_image')
->where(['status' => 1])
->where('products_type', '=', 1)
->order('sort_order asc')
->select()->toArray();
$user_id = $this->getuserid();
$diamond_order_count = 0;
if ($user_id > 0) {
$diamond_order_count = DiamondOrder::where('user_id', $user_id)
->where('status', '=', 'success')->count();
}
foreach ($diamond_list as &$value) {
//商品有首充,并且用户没有购买记录,才显示首充
if ($value['is_first'] == 1 && $diamond_order_count == 0) {
$value['image'] = $value['first_charge_image'];
$value['select_image'] = $value['first_select_charge_image'];
} else {
$value['image'] = $value['normal_image'];
$value['select_image'] = $value['normal_select_image'];
}
unset($value['normal_image']);
unset($value['normal_select_image']);
unset($value['first_charge_image']);
unset($value['first_select_charge_image']);
unset($value['is_first']);
}
return $this->renderSuccess('请求成功', $diamond_list);
}
public function createOrderProducts()
{
$user_id = $this->getuserid();
if ($user_id == 0) {
return $this->renderError("未登录");
}
$product_id = request()->param('product_id', '');
$pay_type = request()->param('pay_type/d', 2); //1.小程序微信支付2.h5支付宝3.app微信4.app支付宝
$diamond_product = DiamondProducts::where('products_id', '=', $product_id)->find();
if (!$diamond_product) {
return $this->renderError("商品不存在");
}
if ($diamond_product['status'] != 1) {
return $this->renderError("商品已下架");
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_products" . '_' . $user_id;
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
// 查询用户钻石订单数,判断是否首充
$diamond_order_count = DiamondOrder::where('user_id', $user_id)
->where('status', '=', DiamondOrder::STATUS_SUCCESS)->count();
Db::startTrans();
try {
$user = $this->getUser();
$attach = 'order_product';
$title = '购买钻石' . $diamond_product['name'];
$price = $diamond_product['price'];
$payRes = \app\common\server\platform\PlatformFactory::createPay($user, $diamond_product['price'], $title, $attach, "ZS_");
if ($payRes['status'] !== 1) {
throw new \Exception(isset($payRes['msg']) ? $payRes['msg'] : "购买失败,请刷新重试");
}
$order_num = $payRes['data']['order_no'];
// 创建钻石订单
$order_id = DiamondOrder::insertGetId([
'order_no' => $order_num,
'user_id' => $user['id'],
'diamond_id' => $diamond_product['id'],
'product_id' => $product_id,
'product_name' => $diamond_product['name'],
'amount_paid' => $price,
'pay_method' => $pay_type == 1 || $pay_type == 3 ? DiamondOrder::PAY_METHOD_WECHAT : DiamondOrder::PAY_METHOD_ALIPAY,
'is_first_charge' => $diamond_order_count == 0 ? 1 : 0,
'status' => DiamondOrder::STATUS_PENDING,
'created_at' => date('Y-m-d H:i:s')
]);
if (!$order_id) {
throw new \Exception("订单创建失败");
}
Db::commit();
// 删除redis锁
$redis->del($redis_key);
return $this->renderSuccess("下单成功", [
'status' => 1,
'order_num' => $order_num,
'res' => $payRes['data']['res'],
]);
} catch (\Exception $e) {
Db::rollback();
// 删除redis锁
$redis->del($redis_key);
return $this->renderError($e->getMessage());
}
}
public function get_diamond_order_log()
{
$user_id = $this->getuserid();
if ($user_id == 0) {
return $this->renderError("未登录");
}
$order_num = request()->param('order_num', '');
$diamond_order = DiamondOrder::where('order_no', '=', $order_num)->find();
if (!$diamond_order) {
return $this->renderError("订单不存在");
}
if ($diamond_order['user_id'] != $user_id) {
return $this->renderError("无权限查看");
}
if ($diamond_order['status'] != DiamondOrder::STATUS_SUCCESS) {
return $this->renderError("订单未成功");
}
return $this->renderSuccess($diamond_order['reward_log']);
}
}