和蕙健康小程序后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

138 lines
4.1 KiB

<?php
namespace addons\shopro\controller;
use addons\shopro\service\commission\Agent;
use app\admin\model\shopro\commission\Level;
use app\admin\model\shopro\Coupon as CouponModel;
use addons\shopro\traits\CouponSend;
use app\admin\model\shopro\goods\Goods as GoodsModel;
use app\admin\model\shopro\user\Coupon as UserCouponModel;
use app\common\model\User;
class Coupon extends Common
{
use CouponSend;
protected $noNeedLogin = ['index', 'listByGoods', 'detail'];
protected $noNeedRight = ['*'];
public function index()
{
$ids = $this->request->param('ids', '');
$coupons = CouponModel::with(['user_coupons'])
->normal() // 正常的可以展示的优惠券
->canGet() // 在领取时间之内的
->order('id', 'desc');
if ($ids) {
$coupons = $coupons->whereIn('id', $ids);
}
$coupons = $coupons->paginate($this->request->param('list_rows', 10))->each(function ($coupon) {
$coupon->get_status = $coupon->get_status;
$coupon->get_status_text = $coupon->get_status_text;
});
$this->success('获取成功', $coupons);
}
/**
* 商品相关的优惠券列表,前端商品详情使用
*
* @param Request $request
* @param int $goods_id
* @return void
*/
public function listByGoods()
{
$user = auth_user();
$goods_id = $this->request->param('goods_id');
$goods = GoodsModel::field('id,category_ids')->where('id', $goods_id)->find();
if (!$goods) {
$this->error(__('No Results were found'));
}
$coupons = CouponModel::with(['user_coupons'])
->normal() // 正常的可以展示的优惠券
->canGet() // 在领取时间之内的
->goods($goods) // 符合指定商品,并且检测商品所属分类
->order('id', 'desc');
if ($user) {
// 关联用户优惠券
$coupons = $coupons->with(['userCoupons']);
}
$coupons = $coupons->select();
$coupons = collection($coupons)->each(function ($coupon) {
$coupon->get_status = $coupon->get_status;
$coupon->get_status_text = $coupon->get_status_text;
});
$this->success('获取成功', $coupons);
}
public function get()
{
$id = $this->request->param('id');
$this->repeatFilter(null, 2);
$userCoupon = $this->getCoupon($id);
$this->success('领取成功', $userCoupon);
}
public function detail()
{
$id = $this->request->param('id');
$coupon = CouponModel::where('id', $id)->find();
if (!$coupon) {
$this->error(__('No Results were found'));
}
$coupon->get_status = $coupon->get_status;
$coupon->get_status_text = $coupon->get_status_text;
$coupon->items_value = $coupon->items_value;
$this->success('优惠券详情', $coupon);
}
/**
* 用户分销等级优惠券
*/
public function commissionCouponList() {
$user = auth_user();
$userInfo = (new \app\admin\model\shopro\commission\Agent())->where('user_id', $user->id)->find();
$data = Level::get(['level' => $userInfo['level']])->toArray();
//添加优惠券领取标识
$is_get = 0;
$coupon_ids = [];
$user_id = $user->id;
if($data['coupon_rules']['programme_1']) {
$coupon_ids = array_merge($coupon_ids, array_column($data['coupon_rules']['programme_1'], 'id'));
}
if ($data['coupon_rules']['programme_2']) {
$coupon_ids = array_merge($coupon_ids, array_column($data['coupon_rules']['programme_2'], 'id'));
}
$coupon_ids = array_values(array_unique($coupon_ids));
$coupon_list = UserCouponModel::with('coupon')
->where('user_id', $user_id)
->whereIn('coupon_id', $coupon_ids)
->select();
if ($coupon_list) {
$is_get = 1;
}
$data['coupon_rules']['is_get'] = $is_get;
$this->success('success',$data['coupon_rules']);
}
}