和蕙健康小程序后端
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.
 
 
 
 
 
 

160 lines
4.9 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()
{
$ids = $this->request->param('id');
$this->repeatFilter(null, 2);
$ids = explode(',', $ids);
$userCoupon = [];
foreach ($ids as $id) {
$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;
$result = [
'programme_1' => [],
'programme_2' => [],
];
if(!empty($data['coupon_rules']['programme_1'])) {
//查询一下优惠券是否被删除
$coupons = CouponModel::whereIn('id', array_column($data['coupon_rules']['programme_1'], 'id'))->select();
if(!empty($coupons)) {
$coupon_ids = array_merge($coupon_ids, array_column($coupons, 'id'));
$result['programme_1'] = $coupons;
}
}
if (!empty($data['coupon_rules']['programme_2'])) {
$coupons = CouponModel::whereIn('id', array_column($data['coupon_rules']['programme_2'], 'id'))->select();
if(!empty($coupons)) {
$coupon_ids = array_merge($coupon_ids, array_column($coupons, 'id'));
$result['programme_2'] = $coupons;
}
}
$coupon_ids = array_values(array_unique($coupon_ids));
$coupon_list = UserCouponModel::with('coupon')
->where('user_id', $user_id)
->where('user_level', $userInfo['level'])
->whereIn('coupon_id', $coupon_ids)
->select();
if ($coupon_list) {
$is_get = 1;
}
$result['is_get'] = $is_get;
$result['level_name'] = $data['name'];
$result['level'] = $userInfo['level'];
$this->success('success',$result);
}
}