235 lines
8.6 KiB
PHP
235 lines
8.6 KiB
PHP
<?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\Coupon as Couponmodel;
|
|
use app\common\model\CouponReceive as CouponReceivemodel;
|
|
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 think\facade\Db;
|
|
use think\facade\View;
|
|
use \think\Request;
|
|
|
|
class Coupon extends Base
|
|
{
|
|
/**
|
|
* 首页优惠券列表
|
|
* @param Request $request [description]
|
|
* @param [type] 类型 1 推荐 2 预售 3 现货 4 一番赏 5 双随机 6售罄
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$is_use_coupon = $this->getUserid1();
|
|
|
|
if (!$is_use_coupon) {
|
|
#领过,之后不再弹出
|
|
$data['status'] = 2;
|
|
return $this->renderSuccess("", $data);
|
|
}
|
|
$whe = [];
|
|
$whe[] = ['status', '=', 0];
|
|
$whe[] = ['type', '=', 1];
|
|
$field = "id,title,price,man_price,effective_day";
|
|
$order = "id desc";
|
|
$goods = Couponmodel::getList($whe, $field, $order, 1000);
|
|
return $this->renderSuccess('请求成功', compact('goods'));
|
|
}
|
|
|
|
|
|
/*
|
|
一键领取
|
|
*/
|
|
public function receive(Request $request)
|
|
{
|
|
$user = $this->getUser();
|
|
$coupon_id = $request->param('coupon_id');
|
|
if (empty($coupon_id)) {
|
|
return $this->renderError("缺少必要参数");
|
|
}
|
|
$is_find = CouponReceivemodel::where(['user_id' => $user->id])->whereIn('coupon_id', $coupon_id)->find();
|
|
if (!empty($is_find)) {
|
|
return $this->renderError("已经领取过了,无法在领取");
|
|
}
|
|
$coupon_id = explode(',', $coupon_id);
|
|
$coupon_id = array_filter($coupon_id);
|
|
|
|
$num = count($coupon_id);
|
|
for ($i = 0; $i < $num; $i++) {
|
|
$coupon_info = Couponmodel::find($coupon_id[$i]);
|
|
if (!empty($coupon_info)) {
|
|
|
|
$res[] = CouponReceivemodel::insert(
|
|
[
|
|
|
|
'user_id' => $user->id,
|
|
'title' => $coupon_info['title'],
|
|
'price' => $coupon_info['price'],
|
|
'man_price' => $coupon_info['man_price'],
|
|
'end_time' => $coupon_info['effective_day'] * 86400 + time(),
|
|
'addtime' => time(),
|
|
'coupon_id' => $coupon_info['id'],
|
|
'state' => $coupon_info['ttype']
|
|
|
|
]
|
|
|
|
);
|
|
}
|
|
|
|
}
|
|
#user表里is_use_coupon 值为1说明是领过优惠券
|
|
$res[] = User::where(['id' => $user['id'], 'is_use_coupon' => 0])->update(['is_use_coupon' => 1]);
|
|
if (resCheck($res)) {
|
|
return $this->renderSuccess("领取成功");
|
|
} else {
|
|
return $this->renderError("购买失败,请刷新重试");
|
|
}
|
|
}
|
|
|
|
//可使用的优惠券
|
|
public function used(Request $request)
|
|
{
|
|
$user = $this->getUser();
|
|
$total_price = $request->param('total_price');
|
|
$type = $request->param('type');
|
|
$type1 = $request->param('type1');
|
|
$status = $request->param('status');
|
|
$state = $request->param('state');
|
|
$page = $request->param('page');
|
|
CouponReceivemodel::where(['user_id' => $user->id, 'status' => 0])->where('end_time', '<=', time())->update(['status' => 2]);
|
|
#根据有效期时间改变一下状态
|
|
if ($type == 1) {
|
|
#从个人中心进
|
|
if ($type1 == 1) {
|
|
#可用
|
|
$whe[] = ['status', '=', $status];
|
|
} else {
|
|
#不可用
|
|
$whe[] = ['status', '>', 0];
|
|
}
|
|
$coupon_receive = CouponReceivemodel::where(['user_id' => $user->id])->where($whe)->paginate(10)->each(function ($itme) {
|
|
$itme['end_time'] = date('Y-m-d', $itme['end_time']);
|
|
if ($itme['status'] == 1) {
|
|
$itme['mark'] = "已使用";
|
|
} elseif ($itme['status'] == 2) {
|
|
$itme['mark'] = "已过期";
|
|
} elseif ($itme['status'] == 0) {
|
|
$itme['mark'] = "去使用";
|
|
|
|
}
|
|
return $itme;
|
|
});
|
|
} else {
|
|
#从计算金额里面进
|
|
if (empty($total_price)) {
|
|
return $this->renderError("缺少必要参数");
|
|
}
|
|
if ($type1 == 1) {
|
|
$whe = 'user_id=' . $user->id . ' and status=0 and man_price<=' . $total_price;
|
|
$whe1 = '';
|
|
} else {
|
|
$whe = 'user_id=' . $user->id . ' and status>0';
|
|
$whe1 = 'user_id=' . $user->id . ' and status=0 and man_price>' . $total_price;
|
|
}
|
|
|
|
#不可用(包括 已使用,未使用(金额不足,已过期))
|
|
$coupon_receive = CouponReceivemodel::where($whe)->whereOr($whe1)
|
|
->paginate(10)->each(function ($val) use ($total_price) {
|
|
$val['end_time'] = date('Y-m-d', $val['end_time']);
|
|
if ($val['status'] == 1) {
|
|
$val['mark'] = "已使用";
|
|
} elseif ($val['status'] == 2) {
|
|
$val['mark'] = "已过期";
|
|
} elseif ($val['status'] == 0) {
|
|
if ($val['man_price'] > $total_price) {
|
|
$val['mark'] = "未满足";
|
|
} else {
|
|
$val['mark'] = "去使用";
|
|
}
|
|
}
|
|
return $val;
|
|
});
|
|
|
|
|
|
|
|
}
|
|
$new_data = [
|
|
'data' => $coupon_receive->items(),
|
|
'last_page' => $coupon_receive->lastPage(),
|
|
];
|
|
|
|
return $this->renderSuccess('请求成功', $new_data);
|
|
}
|
|
|
|
public function get_coupon_list(Request $request)
|
|
{
|
|
$user = $this->getUser();
|
|
$total_price = $request->param('total_price');
|
|
$type = $request->param('type');
|
|
$status = $request->param('status');
|
|
$page = $request->param('page');
|
|
$limit = $this->request->param('limit', 10);
|
|
CouponReceivemodel::where(['user_id' => $user->id, 'status' => 0])->where('end_time', '<=', time())->update(['status' => 2]);
|
|
#根据有效期时间改变一下状态
|
|
if ($type == 1) {
|
|
#从个人中心进
|
|
$whe[] = ['status', '=', $status];
|
|
$coupon_receive = CouponReceivemodel::where(['user_id' => $user->id])->where($whe)
|
|
->field('id,title,price,man_price,status,end_time,coupon_id')
|
|
->paginate(['list_rows' => $limit, 'query' => request()->param()])->
|
|
each(function ($itme) {
|
|
$itme['end_time'] = date('Y-m-d', $itme['end_time']);
|
|
if ($itme['status'] == 1) {
|
|
$itme['mark'] = "已使用";
|
|
} elseif ($itme['status'] == 2) {
|
|
$itme['mark'] = "已过期";
|
|
} elseif ($itme['status'] == 0) {
|
|
$itme['mark'] = "去使用";
|
|
}
|
|
return $itme;
|
|
});
|
|
} else {
|
|
#从计算金额里面进
|
|
if (empty($total_price)) {
|
|
return $this->renderError("缺少必要参数");
|
|
}
|
|
$whe = 'user_id=' . $user->id . ' and status=0 and man_price<=' . $total_price;
|
|
$whe1 = '';
|
|
|
|
#不可用(包括 已使用,未使用(金额不足,已过期))
|
|
$coupon_receive = CouponReceivemodel::where($whe)->whereOr($whe1)
|
|
->paginate(10)->each(function ($val) use ($total_price) {
|
|
$val['end_time'] = date('Y-m-d', $val['end_time']);
|
|
if ($val['status'] == 1) {
|
|
$val['mark'] = "已使用";
|
|
} elseif ($val['status'] == 2) {
|
|
$val['mark'] = "已过期";
|
|
} elseif ($val['status'] == 0) {
|
|
if ($val['man_price'] > $total_price) {
|
|
$val['mark'] = "未满足";
|
|
} else {
|
|
$val['mark'] = "去使用";
|
|
}
|
|
}
|
|
return $val;
|
|
});
|
|
|
|
|
|
|
|
}
|
|
$new_data = [
|
|
'list' => $coupon_receive->items(),
|
|
'last_page' => $coupon_receive->lastPage(),
|
|
];
|
|
|
|
return $this->renderSuccess('请求成功', $new_data);
|
|
}
|
|
|
|
|
|
}
|