chore(src): 所有代码上传
This commit is contained in:
@@ -1,351 +1,343 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\api\controller;
|
||||
|
||||
use app\api\controller\BaseApi;
|
||||
use addon\coupon\model\Coupon as CouponModel;
|
||||
use addon\coupon\model\CouponType as CouponTypeModel;
|
||||
use addon\coupon\model\MemberCoupon;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class Coupon extends BaseApi
|
||||
{
|
||||
|
||||
/**
|
||||
* 优惠券类型信息
|
||||
*/
|
||||
public function typeinfo()
|
||||
{
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
if (empty($coupon_type_id)) {
|
||||
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
|
||||
}
|
||||
|
||||
$app_type = $this->params['app_type'] ?? 'h5';
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'coupon_type_id', '=', $coupon_type_id ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
$coupon_type_model = new CouponTypeModel();
|
||||
$qrcode = $coupon_type_model->qrcode($coupon_type_id, $app_type, $this->site_id)[ 'data' ];
|
||||
|
||||
$info = $coupon_model->getCouponTypeInfo($condition);
|
||||
if (!empty($info[ 'data' ]) && !empty($qrcode)) {
|
||||
$info[ 'data' ][ 'qrcode' ] = $qrcode[ 'path' ];
|
||||
}
|
||||
return $this->response($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表信息
|
||||
*/
|
||||
public function memberpage()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$state = $this->params['state'] ?? 1;//优惠券状态 1已领用(未使用) 2已使用 3已过期
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'npc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
|
||||
[ 'npc.state', '=', $state ]
|
||||
];
|
||||
|
||||
//按类型筛选
|
||||
$type = $this->params['type'] ?? '';
|
||||
$related_id = $this->params['related_id'] ?? 0;
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['npc.type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['npc.type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['npc.at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
if (!empty($related_id)) {
|
||||
$condition[] = [ 'related_id', '=', $related_id ];
|
||||
}
|
||||
|
||||
$list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券类型列表
|
||||
*/
|
||||
public function typelists()
|
||||
{
|
||||
$num = $this->params['num'] ?? 0;
|
||||
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
|
||||
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
|
||||
|
||||
$token = $this->checkToken();
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
//按类型查询
|
||||
$type = $this->params['type'] ?? '';
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($coupon_type_id_arr)) {
|
||||
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
|
||||
}
|
||||
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
|
||||
if ($can_receive == 1) {
|
||||
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
|
||||
}
|
||||
|
||||
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
|
||||
|
||||
$list = $coupon_model->getCouponTypeList($condition, $field, $order, $num);
|
||||
if (!empty($list[ 'data' ]) && $this->member_id) {
|
||||
foreach ($list[ 'data' ] as $k => $v) {
|
||||
$list[ 'data' ][ $k ][ 'member_coupon_num' ] = $coupon_model->getCouponCount([
|
||||
[ 'get_type', '=', 2 ],
|
||||
[ 'member_id', '=', $this->member_id ],
|
||||
[ 'coupon_type_id', '=', $v[ 'coupon_type_id' ] ]
|
||||
])[ 'data' ];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券类型分页列表
|
||||
*/
|
||||
public function typepagelists()
|
||||
{
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
|
||||
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
|
||||
|
||||
$token = $this->checkToken();
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
//按类型查询
|
||||
$type = $this->params['type'] ?? '';
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($coupon_type_id_arr)) {
|
||||
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
|
||||
}
|
||||
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
|
||||
if ($can_receive == 1) {
|
||||
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
|
||||
}
|
||||
|
||||
if ($this->member_id) {
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$field .= ', (select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = ct.coupon_type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
|
||||
}
|
||||
|
||||
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
|
||||
|
||||
$list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, $order, $field, 'ct');
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券
|
||||
*/
|
||||
public function receive()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$site_id = $this->site_id;
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
$get_type = $this->params['get_type'] ?? 2;//获取方式:1订单2.直接领取3.活动领取
|
||||
|
||||
if (empty($coupon_type_id)) {
|
||||
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
|
||||
}
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $token[ 'data' ][ 'member_id' ], $get_type);
|
||||
$res[ 'data' ] = [];
|
||||
|
||||
//判断一下用户是否拥有当前优惠券
|
||||
$coupon = $coupon_model->getCouponInfo([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ], [ 'member_id', '=', $token[ 'data' ][ 'member_id' ] ] ], 'coupon_id')[ 'data' ];
|
||||
$res[ 'data' ][ 'is_exist' ] = empty($coupon) ? 0 : 1;
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员优惠券数量
|
||||
* @return string
|
||||
*/
|
||||
public function num()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$state = $this->params[ 'state' ] ?? 1;
|
||||
$coupon_model = new MemberCoupon();
|
||||
|
||||
$count = $coupon_model->getMemberCouponNum($token[ 'data' ][ 'member_id' ], $state);
|
||||
return $this->response($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可以领取
|
||||
*/
|
||||
public function receivedNum()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
|
||||
$coupon_model = new MemberCoupon();
|
||||
$res = $coupon_model->receivedNum($coupon_type_id, $this->member_id);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询商品可用的优惠券
|
||||
* @param int $id
|
||||
* @return false|string
|
||||
*/
|
||||
public function goodsCoupon($id = 0)
|
||||
{
|
||||
$this->checkToken();
|
||||
$coupon_model = new CouponModel();
|
||||
$goods_id = $this->params[ 'goods_id' ] ?? 0;
|
||||
if (!empty($id)) {
|
||||
$goods_id = $id;
|
||||
}
|
||||
|
||||
// 查询全部商品参与
|
||||
$condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 1 ]
|
||||
];
|
||||
|
||||
$field = 'count,lead_count,coupon_type_id,coupon_type_id as type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit';
|
||||
|
||||
if ($this->member_id) {
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$field .= ',(select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
|
||||
}
|
||||
|
||||
$list = $coupon_model->getCouponTypeList($condition, $field, 'money desc', null, 'ct');
|
||||
|
||||
// 查询指定商品参与
|
||||
$goods_condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 2 ],
|
||||
[ 'goods_ids', 'like', "%,$goods_id,%" ]
|
||||
];
|
||||
|
||||
$goods_coupon = $coupon_model->getCouponTypeList($goods_condition, $field, 'money desc', null, 'ct');
|
||||
|
||||
if (!empty($goods_coupon[ 'data' ])) {
|
||||
$list[ 'data' ] = array_merge($list[ 'data' ], $goods_coupon[ 'data' ]);
|
||||
}
|
||||
|
||||
// 查询指定商品不参与
|
||||
$not_goods_condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 3 ],
|
||||
[ 'goods_ids', 'not like', "%,$goods_id,%" ]
|
||||
];
|
||||
|
||||
$not_goods_coupon = $coupon_model->getCouponTypeList($not_goods_condition, $field, 'money desc', null, 'ct');
|
||||
if (!empty($not_goods_coupon[ 'data' ])) {
|
||||
$list[ 'data' ] = array_merge($list[ 'data' ], $not_goods_coupon[ 'data' ]);
|
||||
}
|
||||
|
||||
if ($list[ 'data' ] && $this->member_id) {
|
||||
foreach ($list[ 'data' ] as $k => $v) {
|
||||
// 已抢光
|
||||
if ($v[ 'count' ] == $v[ 'lead_count' ]) {
|
||||
unset($list[ 'data' ][ $k ]);
|
||||
} elseif ($v[ 'max_fetch' ] != 0 && $v[ 'member_coupon_num' ] > 0 && $v[ 'member_coupon_num' ] >= $v[ 'max_fetch' ]) {
|
||||
// 已领取
|
||||
unset($list[ 'data' ][ $k ]);
|
||||
}
|
||||
}
|
||||
$list[ 'data' ] = array_values($list[ 'data' ]);
|
||||
}
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询优惠券通过优惠券类型id
|
||||
*/
|
||||
public function couponById()
|
||||
{
|
||||
$id = $this->params[ 'id' ] ?? 0;
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'coupon_type_id', 'in', $id ]
|
||||
];
|
||||
$list = $coupon_model->getCouponTypeList($condition, 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit', 'money desc', '');
|
||||
return $this->response($list);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\api\controller;
|
||||
|
||||
use app\api\controller\BaseApi;
|
||||
use addon\coupon\model\Coupon as CouponModel;
|
||||
use addon\coupon\model\CouponType as CouponTypeModel;
|
||||
use addon\coupon\model\MemberCoupon;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class Coupon extends BaseApi
|
||||
{
|
||||
|
||||
/**
|
||||
* 优惠券类型信息
|
||||
*/
|
||||
public function typeinfo()
|
||||
{
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
if (empty($coupon_type_id)) {
|
||||
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
|
||||
}
|
||||
|
||||
$app_type = $this->params['app_type'] ?? 'h5';
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'coupon_type_id', '=', $coupon_type_id ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
$coupon_type_model = new CouponTypeModel();
|
||||
$qrcode = $coupon_type_model->qrcode($coupon_type_id, $app_type, $this->site_id)[ 'data' ];
|
||||
|
||||
$info = $coupon_model->getCouponTypeInfo($condition);
|
||||
if (!empty($info[ 'data' ]) && !empty($qrcode)) {
|
||||
$info[ 'data' ][ 'qrcode' ] = $qrcode[ 'path' ];
|
||||
}
|
||||
return $this->response($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表信息
|
||||
*/
|
||||
public function memberpage()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$state = $this->params['state'] ?? 1;//优惠券状态 1已领用(未使用) 2已使用 3已过期
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'npc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
|
||||
[ 'npc.state', '=', $state ]
|
||||
];
|
||||
|
||||
//按类型筛选
|
||||
$type = $this->params['type'] ?? '';
|
||||
$related_id = $this->params['related_id'] ?? 0;
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['npc.type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['npc.type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['npc.at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
if (!empty($related_id)) {
|
||||
$condition[] = [ 'related_id', '=', $related_id ];
|
||||
}
|
||||
|
||||
$list = $coupon_model->getMemberCouponPageList($condition, $page, $page_size);
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券类型列表
|
||||
*/
|
||||
public function typelists()
|
||||
{
|
||||
$num = $this->params['num'] ?? 0;
|
||||
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
|
||||
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
|
||||
|
||||
$token = $this->checkToken();
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
//按类型查询
|
||||
$type = $this->params['type'] ?? '';
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($coupon_type_id_arr)) {
|
||||
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
|
||||
}
|
||||
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
|
||||
if ($can_receive == 1) {
|
||||
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
|
||||
}
|
||||
|
||||
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
|
||||
|
||||
$list = $coupon_model->getCouponTypeList($condition, $field, $order, $num);
|
||||
if (!empty($list[ 'data' ]) && $this->member_id) {
|
||||
foreach ($list[ 'data' ] as $k => $v) {
|
||||
$list[ 'data' ][ $k ][ 'member_coupon_num' ] = $coupon_model->getCouponCount([
|
||||
[ 'get_type', '=', 2 ],
|
||||
[ 'member_id', '=', $this->member_id ],
|
||||
[ 'coupon_type_id', '=', $v[ 'coupon_type_id' ] ]
|
||||
])[ 'data' ];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券类型分页列表
|
||||
*/
|
||||
public function typepagelists()
|
||||
{
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$coupon_type_id_arr = $this->params['coupon_type_id_arr'] ?? '';//coupon_type_id数组
|
||||
$can_receive = $this->params['can_receive'] ?? 0;// 是否只查询可领取的
|
||||
|
||||
$token = $this->checkToken();
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'site_id', '=', $this->site_id ]
|
||||
];
|
||||
|
||||
//按类型查询
|
||||
$type = $this->params['type'] ?? '';
|
||||
switch ( $type ) {
|
||||
case 'reward'://满减
|
||||
$condition[] = ['type', '=', 'reward'];
|
||||
break;
|
||||
case 'discount'://折扣
|
||||
$condition[] = ['type', '=', 'discount'];
|
||||
break;
|
||||
case 'no_threshold'://无门槛
|
||||
$condition[] = ['at_least', '=', 0 ];
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($coupon_type_id_arr)) {
|
||||
$condition[] = [ 'coupon_type_id', 'in', $coupon_type_id_arr ];
|
||||
}
|
||||
$field = 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,image,validity_type,fixed_term,status,is_show,goods_type,discount_limit,count,lead_count,IF(count < 0 or count - lead_count > 0, 1, 0) as is_remain';
|
||||
if ($can_receive == 1) {
|
||||
$condition[] = [ [ 'count', '<>', Db::raw('lead_count') ] ];
|
||||
}
|
||||
|
||||
if ($this->member_id) {
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$field .= ', (select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = ct.coupon_type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
|
||||
}
|
||||
|
||||
$order = Db::raw('IF(count < 0 or count - lead_count > 0, 1, 0) DESC,sort ASC');
|
||||
|
||||
$list = $coupon_model->getCouponTypePageList($condition, $page, $page_size, $order, $field, 'ct');
|
||||
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券
|
||||
*/
|
||||
public function receive()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$site_id = $this->site_id;
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
$get_type = $this->params['get_type'] ?? 2;//获取方式:1订单2.直接领取3.活动领取
|
||||
|
||||
if (empty($coupon_type_id)) {
|
||||
return $this->response($this->error('', 'REQUEST_COUPON_TYPE_ID'));
|
||||
}
|
||||
|
||||
$coupon_model = new CouponModel();
|
||||
$res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $token[ 'data' ][ 'member_id' ], $get_type);
|
||||
$res[ 'data' ] = [];
|
||||
|
||||
//判断一下用户是否拥有当前优惠券
|
||||
$coupon = $coupon_model->getCouponInfo([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ], [ 'member_id', '=', $token[ 'data' ][ 'member_id' ] ] ], 'coupon_id')[ 'data' ];
|
||||
$res[ 'data' ][ 'is_exist' ] = empty($coupon) ? 0 : 1;
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员优惠券数量
|
||||
* @return string
|
||||
*/
|
||||
public function num()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$state = $this->params[ 'state' ] ?? 1;
|
||||
$coupon_model = new MemberCoupon();
|
||||
|
||||
$count = $coupon_model->getMemberCouponNum($token[ 'data' ][ 'member_id' ], $state);
|
||||
return $this->response($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可以领取
|
||||
*/
|
||||
public function receivedNum()
|
||||
{
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$coupon_type_id = $this->params['coupon_type_id'] ?? 0;
|
||||
|
||||
$coupon_model = new MemberCoupon();
|
||||
$res = $coupon_model->receivedNum($coupon_type_id, $this->member_id);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询商品可用的优惠券
|
||||
* @param int $id
|
||||
* @return false|string
|
||||
*/
|
||||
public function goodsCoupon($id = 0)
|
||||
{
|
||||
$this->checkToken();
|
||||
$coupon_model = new CouponModel();
|
||||
$goods_id = $this->params[ 'goods_id' ] ?? 0;
|
||||
if (!empty($id)) {
|
||||
$goods_id = $id;
|
||||
}
|
||||
|
||||
// 查询全部商品参与
|
||||
$condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 1 ]
|
||||
];
|
||||
|
||||
$field = 'count,lead_count,coupon_type_id,coupon_type_id as type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit';
|
||||
|
||||
if ($this->member_id) {
|
||||
$prefix = config('database.connections.mysql.prefix');
|
||||
$field .= ',(select count(coupon_id) from ' . $prefix . 'promotion_coupon pc where pc.coupon_type_id = type_id and pc.get_type=2 and pc.member_id=' . $this->member_id . ') as member_coupon_num';
|
||||
}
|
||||
|
||||
$list = $coupon_model->getCouponTypeList($condition, $field, 'money desc', null, 'ct');
|
||||
|
||||
// 查询指定商品参与
|
||||
$goods_condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 2 ],
|
||||
[ 'goods_ids', 'like', "%,$goods_id,%" ]
|
||||
];
|
||||
|
||||
$goods_coupon = $coupon_model->getCouponTypeList($goods_condition, $field, 'money desc', null, 'ct');
|
||||
|
||||
if (!empty($goods_coupon[ 'data' ])) {
|
||||
$list[ 'data' ] = array_merge($list[ 'data' ], $goods_coupon[ 'data' ]);
|
||||
}
|
||||
|
||||
// 查询指定商品不参与
|
||||
$not_goods_condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'is_show', '=', 1 ],
|
||||
[ 'goods_type', '=', 3 ],
|
||||
[ 'goods_ids', 'not like', "%,$goods_id,%" ]
|
||||
];
|
||||
|
||||
$not_goods_coupon = $coupon_model->getCouponTypeList($not_goods_condition, $field, 'money desc', null, 'ct');
|
||||
if (!empty($not_goods_coupon[ 'data' ])) {
|
||||
$list[ 'data' ] = array_merge($list[ 'data' ], $not_goods_coupon[ 'data' ]);
|
||||
}
|
||||
|
||||
if ($list[ 'data' ] && $this->member_id) {
|
||||
foreach ($list[ 'data' ] as $k => $v) {
|
||||
// 已抢光
|
||||
if ($v[ 'count' ] == $v[ 'lead_count' ]) {
|
||||
unset($list[ 'data' ][ $k ]);
|
||||
} elseif ($v[ 'max_fetch' ] != 0 && $v[ 'member_coupon_num' ] > 0 && $v[ 'member_coupon_num' ] >= $v[ 'max_fetch' ]) {
|
||||
// 已领取
|
||||
unset($list[ 'data' ][ $k ]);
|
||||
}
|
||||
}
|
||||
$list[ 'data' ] = array_values($list[ 'data' ]);
|
||||
}
|
||||
return $this->response($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询优惠券通过优惠券类型id
|
||||
*/
|
||||
public function couponById()
|
||||
{
|
||||
$id = $this->params[ 'id' ] ?? 0;
|
||||
$coupon_model = new CouponModel();
|
||||
$condition = [
|
||||
[ 'site_id', '=', $this->site_id ],
|
||||
[ 'status', '=', 1 ],
|
||||
[ 'coupon_type_id', 'in', $id ]
|
||||
];
|
||||
$list = $coupon_model->getCouponTypeList($condition, 'coupon_type_id,type,site_id,coupon_name,money,discount,max_fetch,at_least,end_time,validity_type,fixed_term,goods_type,discount_limit', 'money desc', '');
|
||||
return $this->response($list);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
return [
|
||||
|
||||
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据,json格式' ]
|
||||
'template' => [],
|
||||
|
||||
// 后台自定义组件——装修
|
||||
'util' => [
|
||||
[
|
||||
'name' => 'Coupon',
|
||||
'title' => '优惠券',
|
||||
'type' => 'PROMOTION',
|
||||
'value' => '{"style":1,"sources":"initial","styleName":"风格一","couponIds":[],"count":6,"previewList":[],"nameColor":"","moneyColor":"#FFFFFF","limitColor":"#FFFFFF","btnStyle":{"maxLen": 4,"textColor":"#FFFFFF","bgColor":"","text":"立即领取","aroundRadius":0,"isBgColor":false,"isAroundRadius":false},"isName":false,"couponBgColor":"","couponBgUrl":"","couponType":"img","ifNeedBg":true}',
|
||||
'sort' => '30000',
|
||||
'support_diy_view' => '',
|
||||
'max_count' => 0,
|
||||
'icon' => 'iconfont iconyouhuiquan',
|
||||
],
|
||||
],
|
||||
|
||||
// 自定义页面路径
|
||||
'link' => [
|
||||
[
|
||||
'name' => 'COUPON_LIST',
|
||||
'title' => '优惠券',
|
||||
'parent' => 'MARKETING_LINK',
|
||||
'wap_url' => '',
|
||||
'web_url' => '',
|
||||
'sort' => 0,
|
||||
'child_list' => [
|
||||
[
|
||||
'name' => 'COUPON_PREFECTURE',
|
||||
'title' => '优惠券专区',
|
||||
'wap_url' => '/pages_tool/goods/coupon',
|
||||
'web_url' => '',
|
||||
'sort' => 0
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
|
||||
// 自定义图标库
|
||||
'icon_library' => [],
|
||||
|
||||
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ],多个逗号隔开,自定义组件名称前缀必须是diy-,也可以引用第三方组件
|
||||
'component' => [],
|
||||
|
||||
// uni-app 页面,多个逗号隔开
|
||||
'pages' => [],
|
||||
|
||||
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
|
||||
'info' => [],
|
||||
|
||||
// 主题风格配色,格式可以自由定义扩展,【在uni-app中通过:this.themeStyle... 获取定义的颜色字段,例如:this.themeStyle.main_color】
|
||||
'theme' => [],
|
||||
|
||||
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据,json格式] ]
|
||||
'data' => []
|
||||
<?php
|
||||
return [
|
||||
|
||||
// 自定义模板页面类型,格式:[ 'title' => '页面类型名称', 'name' => '页面标识', 'path' => '页面路径', 'value' => '页面数据,json格式' ]
|
||||
'template' => [],
|
||||
|
||||
// 后台自定义组件——装修
|
||||
'util' => [
|
||||
[
|
||||
'name' => 'Coupon',
|
||||
'title' => '优惠券',
|
||||
'type' => 'PROMOTION',
|
||||
'value' => '{"style":1,"sources":"initial","styleName":"风格一","couponIds":[],"count":6,"previewList":[],"nameColor":"","moneyColor":"#FFFFFF","limitColor":"#FFFFFF","btnStyle":{"maxLen": 4,"textColor":"#FFFFFF","bgColor":"","text":"立即领取","aroundRadius":0,"isBgColor":false,"isAroundRadius":false},"isName":false,"couponBgColor":"","couponBgUrl":"","couponType":"img","ifNeedBg":true}',
|
||||
'sort' => '30000',
|
||||
'support_diy_view' => '',
|
||||
'max_count' => 0,
|
||||
'icon' => 'iconfont iconyouhuiquan',
|
||||
],
|
||||
],
|
||||
|
||||
// 自定义页面路径
|
||||
'link' => [
|
||||
[
|
||||
'name' => 'COUPON_LIST',
|
||||
'title' => '优惠券',
|
||||
'parent' => 'MARKETING_LINK',
|
||||
'wap_url' => '',
|
||||
'web_url' => '',
|
||||
'sort' => 0,
|
||||
'child_list' => [
|
||||
[
|
||||
'name' => 'COUPON_PREFECTURE',
|
||||
'title' => '优惠券专区',
|
||||
'wap_url' => '/pages_tool/goods/coupon',
|
||||
'web_url' => '',
|
||||
'sort' => 0
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
|
||||
// 自定义图标库
|
||||
'icon_library' => [],
|
||||
|
||||
// uni-app 组件,格式:[ 'name' => '组件名称/文件夹名称', 'path' => '文件路径/目录路径' ],多个逗号隔开,自定义组件名称前缀必须是diy-,也可以引用第三方组件
|
||||
'component' => [],
|
||||
|
||||
// uni-app 页面,多个逗号隔开
|
||||
'pages' => [],
|
||||
|
||||
// 模板信息,格式:'title' => '模板名称', 'name' => '模板标识', 'cover' => '模板封面图', 'preview' => '模板预览图', 'desc' => '模板描述'
|
||||
'info' => [],
|
||||
|
||||
// 主题风格配色,格式可以自由定义扩展,【在uni-app中通过:this.themeStyle... 获取定义的颜色字段,例如:this.themeStyle.main_color】
|
||||
'theme' => [],
|
||||
|
||||
// 自定义页面数据,格式:[ 'title' => '页面名称', 'name' => "页面标识", 'value' => [页面数据,json格式] ]
|
||||
'data' => []
|
||||
];
|
||||
@@ -1,21 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
return [
|
||||
'name' => 'coupon',
|
||||
'title' => '优惠券',
|
||||
'description' => '会员优惠券功能',
|
||||
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
|
||||
'status' => 1,
|
||||
'author' => '',
|
||||
'version' => '5.3.1',
|
||||
'version_no' => '525231212001',
|
||||
'content' => '',
|
||||
<?php
|
||||
return [
|
||||
'name' => 'coupon',
|
||||
'title' => '优惠券',
|
||||
'description' => '会员优惠券功能',
|
||||
'type' => 'system', //插件类型 system :系统插件(自动安装), promotion:扩展营销插件 tool:工具插件
|
||||
'status' => 1,
|
||||
'author' => '',
|
||||
'version' => '5.3.1',
|
||||
'version_no' => '525231212001',
|
||||
'content' => '',
|
||||
];
|
||||
@@ -1,55 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\dict;
|
||||
|
||||
|
||||
/**
|
||||
* 订单公共属性
|
||||
*/
|
||||
class CouponDict
|
||||
{
|
||||
const normal = 1;
|
||||
const used = 2;
|
||||
const expire = 3;
|
||||
const close = 4;
|
||||
|
||||
/**
|
||||
* 优惠券状态
|
||||
* @param $status
|
||||
* @return string|string[]
|
||||
*/
|
||||
public static function getStatus($status = ''){
|
||||
$list = [
|
||||
self::normal => '待使用',
|
||||
self::used => '已使用',
|
||||
self::expire => '已过期',
|
||||
self::close => '已关闭',
|
||||
];
|
||||
|
||||
if($status) return $list[$status] ?? '';
|
||||
return $list;
|
||||
}
|
||||
|
||||
const all = 1;
|
||||
const selected = 2;
|
||||
const selected_out = 3;
|
||||
public static function getGoodsType($type = ''){
|
||||
$list = [
|
||||
self::all => '全部商品参与',
|
||||
self::selected => '指定商品参与',
|
||||
self::selected_out => '指定不参与商品'
|
||||
];
|
||||
|
||||
if($type) return $list[$type] ?? '';
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\dict;
|
||||
|
||||
|
||||
/**
|
||||
* 订单公共属性
|
||||
*/
|
||||
class CouponDict
|
||||
{
|
||||
const normal = 1;
|
||||
const used = 2;
|
||||
const expire = 3;
|
||||
const close = 4;
|
||||
|
||||
/**
|
||||
* 优惠券状态
|
||||
* @param $status
|
||||
* @return string|string[]
|
||||
*/
|
||||
public static function getStatus($status = ''){
|
||||
$list = [
|
||||
self::normal => '待使用',
|
||||
self::used => '已使用',
|
||||
self::expire => '已过期',
|
||||
self::close => '已关闭',
|
||||
];
|
||||
|
||||
if($status) return $list[$status] ?? '';
|
||||
return $list;
|
||||
}
|
||||
|
||||
const all = 1;
|
||||
const selected = 2;
|
||||
const selected_out = 3;
|
||||
public static function getGoodsType($type = ''){
|
||||
$list = [
|
||||
self::all => '全部商品参与',
|
||||
self::selected => '指定商品参与',
|
||||
self::selected_out => '指定不参与商品'
|
||||
];
|
||||
|
||||
if($type) return $list[$type] ?? '';
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use addon\coupon\model\Coupon;
|
||||
|
||||
/**
|
||||
* 启动活动
|
||||
*/
|
||||
class CronCouponEnd
|
||||
{
|
||||
|
||||
public function handle($params = [])
|
||||
{
|
||||
$coupon = new Coupon();
|
||||
$res = $coupon->cronCouponEnd();
|
||||
return $res;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use addon\coupon\model\Coupon;
|
||||
|
||||
/**
|
||||
* 启动活动
|
||||
*/
|
||||
class CronCouponEnd
|
||||
{
|
||||
|
||||
public function handle($params = [])
|
||||
{
|
||||
$coupon = new Coupon();
|
||||
$res = $coupon->cronCouponEnd();
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use addon\coupon\model\CouponType;
|
||||
|
||||
/**
|
||||
* 优惠券定时结束
|
||||
*/
|
||||
class CronCouponTypeEnd
|
||||
{
|
||||
|
||||
public function handle($params = [])
|
||||
{
|
||||
$coupon = new CouponType();
|
||||
$res = $coupon->couponCronEnd($params[ 'relate_id' ]);
|
||||
return $res;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use addon\coupon\model\CouponType;
|
||||
|
||||
/**
|
||||
* 优惠券定时结束
|
||||
*/
|
||||
class CronCouponTypeEnd
|
||||
{
|
||||
|
||||
public function handle($params = [])
|
||||
{
|
||||
$coupon = new CouponType();
|
||||
$res = $coupon->couponCronEnd($params[ 'relate_id' ]);
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use app\model\system\Cron;
|
||||
|
||||
/**
|
||||
* 应用安装
|
||||
*/
|
||||
class Install
|
||||
{
|
||||
/**
|
||||
* 执行安装
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
execute_sql('addon/coupon/data/install.sql');
|
||||
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ ['event', '=', 'CronCouponEnd'] ]);
|
||||
$cron->addCron(2, 1, '优惠券过期自动关闭', 'CronCouponEnd', time(), 0);
|
||||
|
||||
return success();
|
||||
} catch (\Exception $e) {
|
||||
return error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
use app\model\system\Cron;
|
||||
|
||||
/**
|
||||
* 应用安装
|
||||
*/
|
||||
class Install
|
||||
{
|
||||
/**
|
||||
* 执行安装
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
execute_sql('addon/coupon/data/install.sql');
|
||||
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ ['event', '=', 'CronCouponEnd'] ]);
|
||||
$cron->addCron(2, 1, '优惠券过期自动关闭', 'CronCouponEnd', time(), 0);
|
||||
|
||||
return success();
|
||||
} catch (\Exception $e) {
|
||||
return error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
/**
|
||||
* 店铺活动
|
||||
*/
|
||||
class ShowPromotion
|
||||
{
|
||||
|
||||
/**
|
||||
* 活动展示
|
||||
* @return array
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$data = [
|
||||
'shop' => [
|
||||
[
|
||||
//插件名称
|
||||
'name' => 'coupon',
|
||||
//展示分类(根据平台端设置,admin(平台营销),shop:店铺营销,member:会员营销, tool:应用工具)
|
||||
'show_type' => 'shop',
|
||||
//展示主题
|
||||
'title' => '优惠券',
|
||||
//展示介绍
|
||||
'description' => '设置商家优惠券',
|
||||
//展示图标
|
||||
'icon' => 'addon/coupon/icon.png',
|
||||
//跳转链接
|
||||
'url' => 'coupon://shop/coupon/lists',
|
||||
]
|
||||
]
|
||||
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
/**
|
||||
* 店铺活动
|
||||
*/
|
||||
class ShowPromotion
|
||||
{
|
||||
|
||||
/**
|
||||
* 活动展示
|
||||
* @return array
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$data = [
|
||||
'shop' => [
|
||||
[
|
||||
//插件名称
|
||||
'name' => 'coupon',
|
||||
//展示分类(根据平台端设置,admin(平台营销),shop:店铺营销,member:会员营销, tool:应用工具)
|
||||
'show_type' => 'shop',
|
||||
//展示主题
|
||||
'title' => '优惠券',
|
||||
//展示介绍
|
||||
'description' => '设置商家优惠券',
|
||||
//展示图标
|
||||
'icon' => 'addon/coupon/icon.png',
|
||||
//跳转链接
|
||||
'url' => 'coupon://shop/coupon/lists',
|
||||
]
|
||||
]
|
||||
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
/**
|
||||
* 应用卸载
|
||||
*/
|
||||
class UnInstall
|
||||
{
|
||||
/**
|
||||
* 执行卸载
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
return error('', "系统插件不允许删除");
|
||||
//execute_sql('addon/coupon/data/uninstall.sql');
|
||||
//return success();
|
||||
} catch (\Exception $e) {
|
||||
return error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\event;
|
||||
|
||||
/**
|
||||
* 应用卸载
|
||||
*/
|
||||
class UnInstall
|
||||
{
|
||||
/**
|
||||
* 执行卸载
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
return error('', "系统插件不允许删除");
|
||||
//execute_sql('addon/coupon/data/uninstall.sql');
|
||||
//return success();
|
||||
} catch (\Exception $e) {
|
||||
return error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,64 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Stat;
|
||||
|
||||
/**
|
||||
* 优惠券统计
|
||||
*/
|
||||
class CouponStat extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 领取优惠券统计
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function addReceiveCouponStat($params)
|
||||
{
|
||||
$coupon_id = $params[ 'coupon_id' ];
|
||||
$site_id = $params[ 'site_id' ] ?? 0;
|
||||
$order_condition = array (
|
||||
[ 'coupon_id', '=', $coupon_id ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
);
|
||||
$info = model('promotion_coupon')->getInfo($order_condition);
|
||||
if (empty($info))
|
||||
return $this->error();
|
||||
|
||||
$stat_data = array (
|
||||
'site_id' => $site_id,
|
||||
'coupon_count' => 1
|
||||
);
|
||||
$member_id = $info[ 'member_id' ];
|
||||
//如果是第一笔订单才能累加下单会员数
|
||||
|
||||
$time_region = getDayStartAndEndTime();
|
||||
$today_start_time = $time_region[ 'start_time' ];
|
||||
$today_end_time = $time_region[ 'end_time' ];
|
||||
$today_order_condition = array (
|
||||
[ 'member_id', '=', $member_id ],
|
||||
[ 'fetch_time', 'between', [ $today_start_time, $today_end_time ] ],
|
||||
[ 'coupon_id', '<>', $coupon_id ]
|
||||
);
|
||||
$count = model('promotion_coupon')->getCount($today_order_condition);
|
||||
if ($count == 0) {
|
||||
$stat_data[ 'coupon_member_count' ] = 1;
|
||||
}
|
||||
|
||||
//发布统计
|
||||
$stat_model = new Stat();
|
||||
$result = $stat_model->addShopStat($stat_data);
|
||||
return $result;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Stat;
|
||||
|
||||
/**
|
||||
* 优惠券统计
|
||||
*/
|
||||
class CouponStat extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 领取优惠券统计
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function addReceiveCouponStat($params)
|
||||
{
|
||||
$coupon_id = $params[ 'coupon_id' ];
|
||||
$site_id = $params[ 'site_id' ] ?? 0;
|
||||
$order_condition = array (
|
||||
[ 'coupon_id', '=', $coupon_id ],
|
||||
[ 'site_id', '=', $site_id ]
|
||||
);
|
||||
$info = model('promotion_coupon')->getInfo($order_condition);
|
||||
if (empty($info))
|
||||
return $this->error();
|
||||
|
||||
$stat_data = array (
|
||||
'site_id' => $site_id,
|
||||
'coupon_count' => 1
|
||||
);
|
||||
$member_id = $info[ 'member_id' ];
|
||||
//如果是第一笔订单才能累加下单会员数
|
||||
|
||||
$time_region = getDayStartAndEndTime();
|
||||
$today_start_time = $time_region[ 'start_time' ];
|
||||
$today_end_time = $time_region[ 'end_time' ];
|
||||
$today_order_condition = array (
|
||||
[ 'member_id', '=', $member_id ],
|
||||
[ 'fetch_time', 'between', [ $today_start_time, $today_end_time ] ],
|
||||
[ 'coupon_id', '<>', $coupon_id ]
|
||||
);
|
||||
$count = model('promotion_coupon')->getCount($today_order_condition);
|
||||
if ($count == 0) {
|
||||
$stat_data[ 'coupon_member_count' ] = 1;
|
||||
}
|
||||
|
||||
//发布统计
|
||||
$stat_model = new Stat();
|
||||
$result = $stat_model->addShopStat($stat_data);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -1,336 +1,328 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Cron;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
/**
|
||||
* 优惠券活动
|
||||
*/
|
||||
class CouponType extends BaseModel
|
||||
{
|
||||
//优惠券类型状态
|
||||
private $coupon_type_status = [
|
||||
1 => '进行中',
|
||||
2 => '已结束',
|
||||
-1 => '已关闭',
|
||||
];
|
||||
|
||||
public function getCouponTypeStatus()
|
||||
{
|
||||
return $this->coupon_type_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加优惠券活动
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addCouponType($data)
|
||||
{
|
||||
//只要创建了就是进行中
|
||||
$data[ 'status' ] = 1;
|
||||
$data[ 'create_time' ] = time();
|
||||
//获取商品id
|
||||
if ($data[ 'goods_type' ] == 1) {//全部商品参与
|
||||
$data[ 'goods_ids' ] = '';
|
||||
}
|
||||
|
||||
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
|
||||
$res = model('promotion_coupon_type')->add($data);
|
||||
if ($data[ 'validity_type' ] == 0) {
|
||||
$cron = new Cron();
|
||||
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $res);
|
||||
}
|
||||
|
||||
$this->qrcode($res, 'all', $data[ 'site_id' ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑优惠券活动
|
||||
* @param $data
|
||||
* @param $coupon_type_id
|
||||
* @return array
|
||||
*/
|
||||
public function editCouponType($data, $coupon_type_id)
|
||||
{
|
||||
$data[ 'update_time' ] = time();
|
||||
|
||||
//获取商品id
|
||||
if ($data[ 'goods_type' ] == 1) {//全部商品参与
|
||||
$data[ 'goods_ids' ] = '';
|
||||
}
|
||||
|
||||
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
if (!empty($coupon_info[ 'image' ]) && !empty($data[ 'image' ]) && $coupon_info[ 'image' ] != $data[ 'image' ]) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
|
||||
}
|
||||
|
||||
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
|
||||
$res = model('promotion_coupon_type')->update($data, [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
model('promotion_coupon')->update([ 'goods_ids' => $data[ 'goods_ids' ], 'goods_type' => $data[ 'goods_type' ] ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'state', '=', 1 ] ]);
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
if ($data[ 'validity_type' ] == 0) {
|
||||
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $coupon_type_id);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭优惠券
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function closeCouponType($coupon_type_id, $site_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'status' => -1 ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
// if ($res) {
|
||||
// model("promotion_coupon")->update(['state' => 3], [['coupon_type_id', '=', $coupon_type_id], ['site_id', '=', $site_id]]);
|
||||
// }
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除优惠券活动
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteCouponType($coupon_type_id, $site_id)
|
||||
{
|
||||
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
|
||||
if ($coupon_info['status'] == 1) return $this->error('', '进行中的优惠卷无法删除,请先关闭');
|
||||
|
||||
if (!empty($coupon_info[ 'image' ])) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
|
||||
}
|
||||
|
||||
$res = model('promotion_coupon_type')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($res) {
|
||||
model('promotion_coupon')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
}
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动详情
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypeInfo($coupon_type_id, $site_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if (!empty($res)) {
|
||||
foreach ($res as $k => $v) {
|
||||
if ($v[ 'goods_type' ] == 2 || $v[ 'goods_type' ] == 3) {
|
||||
$field[ $k ] = 'goods_id,goods_name,FLOOR(goods_stock) as goods_stock,goods_image,price,sort';
|
||||
$goods_ids[ $k ] = substr($v[ 'goods_ids' ], '1', '-1');
|
||||
$goods_list[ $k ] = model('goods')->getList([ [ 'goods_id', 'in', $goods_ids[ $k ] ] ], $field[ $k ]);
|
||||
}
|
||||
$res[ $k ][ 'goods_list' ] = $goods_list[$k] ?? [];
|
||||
$res[ $k ][ 'goods_list_count' ] = count($res[ $k ][ 'goods_list' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动信息
|
||||
* @param array $where
|
||||
* @param bool $field
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @param null $data
|
||||
* @return array
|
||||
*/
|
||||
public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getInfo($where, $field, $alias, $join, $data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券类型列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypeList($condition = [], $field = '*', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$condition[] = [ 'promotion_type', '=', 0 ];
|
||||
$list = model('promotion_coupon_type')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序
|
||||
* @param $coupon_type_id
|
||||
* @param $sort
|
||||
* @return array
|
||||
*/
|
||||
public function couponSort($coupon_type_id, $sort)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'sort' => $sort ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成优惠券二维码
|
||||
* @param $coupon_type_id
|
||||
* @param string $app_type all为全部
|
||||
* @param string $type 类型 create创建 get获取
|
||||
* @return mixed|array
|
||||
*/
|
||||
public function qrcode($coupon_type_id, $app_type, $site_id, $type = 'create')
|
||||
{
|
||||
$res = event('Qrcode', [
|
||||
'site_id' => $site_id,
|
||||
'app_type' => $app_type,
|
||||
'type' => $type,
|
||||
'data' => [
|
||||
'coupon_type_id' => $coupon_type_id
|
||||
],
|
||||
'page' => '/pages_tool/goods/coupon_receive',
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
|
||||
], true);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券定时结束
|
||||
* @param $coupon_type_id
|
||||
* @return array
|
||||
*/
|
||||
public function couponCronEnd($coupon_type_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'status' => 2 ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
public function spread($coupon_type_id, $name, $site_id, $type = 'create')
|
||||
{
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'app_type' => 'all', // all为全部
|
||||
'type' => $type, // 类型 create创建 get获取
|
||||
'data' => [
|
||||
'coupon_type_id' => $coupon_type_id
|
||||
],
|
||||
'page' => '/pages_tool/goods/coupon_receive',
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
|
||||
];
|
||||
event('Qrcode', $data, true);
|
||||
$app_type_list = config('app_type');
|
||||
$path = [];
|
||||
foreach ($app_type_list as $k => $v) {
|
||||
switch ( $k ) {
|
||||
case 'h5':
|
||||
$wap_domain = getH5Domain();
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?coupon_type_id=' . $coupon_type_id;
|
||||
$path[ $k ][ 'img' ] = 'upload/qrcode/coupon/coupon_type_code_' . $coupon_type_id . '_' . $site_id . '_' . $k . '.png';
|
||||
break;
|
||||
case 'weapp' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'wechat' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$return = [
|
||||
'path' => $path,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
return $this->success($return);
|
||||
}
|
||||
|
||||
public function urlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
|
||||
{
|
||||
$params = [
|
||||
'site_id' => $site_id,
|
||||
'data' => $qrcode_param,
|
||||
'page' => $page,
|
||||
'promotion_type' => $promotion_type,
|
||||
'app_type' => $app_type,
|
||||
'h5_path' => $page . '?coupon_type_id=' . $qrcode_param[ 'coupon_type_id' ],
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $promotion_type . '_' . $qrcode_param[ 'coupon_type_id' ] . '_' . $site_id,
|
||||
];
|
||||
|
||||
$solitaire = event('PromotionQrcode', $params, true);
|
||||
return $this->success($solitaire);
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Cron;
|
||||
use app\model\upload\Upload;
|
||||
|
||||
/**
|
||||
* 优惠券活动
|
||||
*/
|
||||
class CouponType extends BaseModel
|
||||
{
|
||||
//优惠券类型状态
|
||||
private $coupon_type_status = [
|
||||
1 => '进行中',
|
||||
2 => '已结束',
|
||||
-1 => '已关闭',
|
||||
];
|
||||
|
||||
public function getCouponTypeStatus()
|
||||
{
|
||||
return $this->coupon_type_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加优惠券活动
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
public function addCouponType($data)
|
||||
{
|
||||
//只要创建了就是进行中
|
||||
$data[ 'status' ] = 1;
|
||||
$data[ 'create_time' ] = time();
|
||||
//获取商品id
|
||||
if ($data[ 'goods_type' ] == 1) {//全部商品参与
|
||||
$data[ 'goods_ids' ] = '';
|
||||
}
|
||||
|
||||
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
|
||||
$res = model('promotion_coupon_type')->add($data);
|
||||
if ($data[ 'validity_type' ] == 0) {
|
||||
$cron = new Cron();
|
||||
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $res);
|
||||
}
|
||||
|
||||
$this->qrcode($res, 'all', $data[ 'site_id' ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑优惠券活动
|
||||
* @param $data
|
||||
* @param $coupon_type_id
|
||||
* @return array
|
||||
*/
|
||||
public function editCouponType($data, $coupon_type_id)
|
||||
{
|
||||
$data[ 'update_time' ] = time();
|
||||
|
||||
//获取商品id
|
||||
if ($data[ 'goods_type' ] == 1) {//全部商品参与
|
||||
$data[ 'goods_ids' ] = '';
|
||||
}
|
||||
|
||||
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
if (!empty($coupon_info[ 'image' ]) && !empty($data[ 'image' ]) && $coupon_info[ 'image' ] != $data[ 'image' ]) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
|
||||
}
|
||||
|
||||
$data[ 'goods_ids' ] = ',' . $data[ 'goods_ids' ] . ',';
|
||||
$res = model('promotion_coupon_type')->update($data, [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
model('promotion_coupon')->update([ 'goods_ids' => $data[ 'goods_ids' ], 'goods_type' => $data[ 'goods_type' ] ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'state', '=', 1 ] ]);
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
if ($data[ 'validity_type' ] == 0) {
|
||||
$cron->addCron(1, 1, '优惠券活动定时结束', 'CronCouponTypeEnd', $data[ 'end_time' ], $coupon_type_id);
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭优惠券
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function closeCouponType($coupon_type_id, $site_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'status' => -1 ], [ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
// if ($res) {
|
||||
// model("promotion_coupon")->update(['state' => 3], [['coupon_type_id', '=', $coupon_type_id], ['site_id', '=', $site_id]]);
|
||||
// }
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除优惠券活动
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function deleteCouponType($coupon_type_id, $site_id)
|
||||
{
|
||||
$coupon_info = model('promotion_coupon_type')->getInfo([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
|
||||
if ($coupon_info['status'] == 1) return $this->error('', '进行中的优惠卷无法删除,请先关闭');
|
||||
|
||||
if (!empty($coupon_info[ 'image' ])) {
|
||||
$upload_model = new Upload();
|
||||
$upload_model->deletePic($coupon_info[ 'image' ], $coupon_info[ 'site_id' ]);
|
||||
}
|
||||
|
||||
$res = model('promotion_coupon_type')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if ($res) {
|
||||
model('promotion_coupon')->delete([ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
}
|
||||
$cron = new Cron();
|
||||
$cron->deleteCron([ [ 'event', '=', 'CronCouponTypeEnd' ], [ 'relate_id', '=', $coupon_type_id ] ]);
|
||||
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动详情
|
||||
* @param $coupon_type_id
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypeInfo($coupon_type_id, $site_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $coupon_type_id ], [ 'site_id', '=', $site_id ] ]);
|
||||
if (!empty($res)) {
|
||||
foreach ($res as $k => $v) {
|
||||
if ($v[ 'goods_type' ] == 2 || $v[ 'goods_type' ] == 3) {
|
||||
$field[ $k ] = 'goods_id,goods_name,FLOOR(goods_stock) as goods_stock,goods_image,price,sort';
|
||||
$goods_ids[ $k ] = substr($v[ 'goods_ids' ], '1', '-1');
|
||||
$goods_list[ $k ] = model('goods')->getList([ [ 'goods_id', 'in', $goods_ids[ $k ] ] ], $field[ $k ]);
|
||||
}
|
||||
$res[ $k ][ 'goods_list' ] = $goods_list[$k] ?? [];
|
||||
$res[ $k ][ 'goods_list_count' ] = count($res[ $k ][ 'goods_list' ]);
|
||||
}
|
||||
}
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动信息
|
||||
* @param array $where
|
||||
* @param bool $field
|
||||
* @param string $alias
|
||||
* @param null $join
|
||||
* @param null $data
|
||||
* @return array
|
||||
*/
|
||||
public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getInfo($where, $field, $alias, $join, $data);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券类型列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypeList($condition = [], $field = '*', $order = 'create_time desc', $limit = null)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券活动分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getCouponTypePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$condition[] = [ 'promotion_type', '=', 0 ];
|
||||
$list = model('promotion_coupon_type')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序
|
||||
* @param $coupon_type_id
|
||||
* @param $sort
|
||||
* @return array
|
||||
*/
|
||||
public function couponSort($coupon_type_id, $sort)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'sort' => $sort ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成优惠券二维码
|
||||
* @param $coupon_type_id
|
||||
* @param string $app_type all为全部
|
||||
* @param string $type 类型 create创建 get获取
|
||||
* @return mixed|array
|
||||
*/
|
||||
public function qrcode($coupon_type_id, $app_type, $site_id, $type = 'create')
|
||||
{
|
||||
$res = event('Qrcode', [
|
||||
'site_id' => $site_id,
|
||||
'app_type' => $app_type,
|
||||
'type' => $type,
|
||||
'data' => [
|
||||
'coupon_type_id' => $coupon_type_id
|
||||
],
|
||||
'page' => '/pages_tool/goods/coupon_receive',
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
|
||||
], true);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券定时结束
|
||||
* @param $coupon_type_id
|
||||
* @return array
|
||||
*/
|
||||
public function couponCronEnd($coupon_type_id)
|
||||
{
|
||||
$res = model('promotion_coupon_type')->update([ 'status' => 2 ], [ [ 'coupon_type_id', '=', $coupon_type_id ] ]);
|
||||
return $this->success($res);
|
||||
}
|
||||
|
||||
public function spread($coupon_type_id, $name, $site_id, $type = 'create')
|
||||
{
|
||||
$data = [
|
||||
'site_id' => $site_id,
|
||||
'app_type' => 'all', // all为全部
|
||||
'type' => $type, // 类型 create创建 get获取
|
||||
'data' => [
|
||||
'coupon_type_id' => $coupon_type_id
|
||||
],
|
||||
'page' => '/pages_tool/goods/coupon_receive',
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $coupon_type_id . '_' . $site_id,
|
||||
];
|
||||
event('Qrcode', $data, true);
|
||||
$app_type_list = config('app_type');
|
||||
$path = [];
|
||||
foreach ($app_type_list as $k => $v) {
|
||||
switch ( $k ) {
|
||||
case 'h5':
|
||||
$wap_domain = getH5Domain();
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?coupon_type_id=' . $coupon_type_id;
|
||||
$path[ $k ][ 'img' ] = 'upload/qrcode/coupon/coupon_type_code_' . $coupon_type_id . '_' . $site_id . '_' . $k . '.png';
|
||||
break;
|
||||
case 'weapp' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信小程序';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'wechat' :
|
||||
$config = new ConfigModel();
|
||||
$res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
|
||||
if (!empty($res[ 'data' ])) {
|
||||
if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 1;
|
||||
$path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
|
||||
}
|
||||
} else {
|
||||
$path[ $k ][ 'status' ] = 2;
|
||||
$path[ $k ][ 'message' ] = '未配置微信公众号';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$return = [
|
||||
'path' => $path,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
return $this->success($return);
|
||||
}
|
||||
|
||||
public function urlQrcode($page, $qrcode_param, $promotion_type, $app_type, $site_id)
|
||||
{
|
||||
$params = [
|
||||
'site_id' => $site_id,
|
||||
'data' => $qrcode_param,
|
||||
'page' => $page,
|
||||
'promotion_type' => $promotion_type,
|
||||
'app_type' => $app_type,
|
||||
'h5_path' => $page . '?coupon_type_id=' . $qrcode_param[ 'coupon_type_id' ],
|
||||
'qrcode_path' => 'upload/qrcode/coupon',
|
||||
'qrcode_name' => 'coupon_type_code_' . $promotion_type . '_' . $qrcode_param[ 'coupon_type_id' ] . '_' . $site_id,
|
||||
];
|
||||
|
||||
$solitaire = event('PromotionQrcode', $params, true);
|
||||
return $this->success($solitaire);
|
||||
}
|
||||
}
|
||||
@@ -1,243 +1,235 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use addon\coupon\dict\CouponDict;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class MemberCoupon extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取会员已领取优惠券
|
||||
* @param $member_id
|
||||
* @param $state
|
||||
* @param int $site_id
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCouponList($member_id, $state, $site_id = 0, $order = "fetch_time desc")
|
||||
{
|
||||
$condition = array (
|
||||
[ "member_id", "=", $member_id ],
|
||||
[ "state", "=", $state ],
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ "site_id", "=", $site_id ];
|
||||
}
|
||||
$list = model("promotion_coupon")->getList($condition, "*", $order, '', '', '', 0);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用优惠券
|
||||
* @param $coupon_id
|
||||
* @param $member_id
|
||||
* @param int $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function useMemberCoupon($coupon_id, $member_id, $order_id = 0)
|
||||
{
|
||||
//优惠券处理方案
|
||||
$result = model('promotion_coupon')->update([ 'use_order_id' => $order_id, 'state' => 2, 'use_time' => time() ], [ [ 'coupon_id', '=', $coupon_id ], [ "member_id", "=", $member_id ], [ 'state', '=', 1 ] ]);
|
||||
if ($result === false) {
|
||||
return $this->error();
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员已领取优惠券数量
|
||||
* @param $member_id
|
||||
* @param $state
|
||||
* @param int $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCouponNum($member_id, $state, $site_id = 0)
|
||||
{
|
||||
$condition = array (
|
||||
[ "member_id", "=", $member_id ],
|
||||
[ "state", "=", $state ],
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ "site_id", "=", $site_id ];
|
||||
}
|
||||
$num = model("promotion_coupon")->getCount($condition);
|
||||
return $this->success($num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员是否可领取该优惠券
|
||||
* @param $coupon_type_id
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function receivedNum($coupon_type_id, $member_id)
|
||||
{
|
||||
$received_num = model('promotion_coupon')->getCount([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'member_id', '=', $member_id ] ]);
|
||||
return $this->success($received_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取编码
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return random_keys(8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员批量发放优惠券
|
||||
* @param $coupon_type_ids
|
||||
* @param $site_id
|
||||
* @param $member_id
|
||||
* @param int $get_type
|
||||
* @param int $is_stock
|
||||
* @param int $related_id
|
||||
* @return array
|
||||
*/
|
||||
public function sendCoupon($coupon_type_ids, $site_id, $member_id, $get_type = 4, $is_stock = 0, $related_id = 0)
|
||||
{
|
||||
//已选优惠券提交数组
|
||||
if (!empty($coupon_type_ids)) {
|
||||
$res = 0;
|
||||
foreach ($coupon_type_ids as $coupon_type_id) {
|
||||
$coupon_type_info = model('promotion_coupon_type')->getInfo([ 'coupon_type_id' => $coupon_type_id, 'site_id' => $site_id, 'status' => 1 ]);
|
||||
if (!empty($coupon_type_info)) {
|
||||
|
||||
if ($coupon_type_info[ 'count' ] != -1 || $is_stock == 0) {
|
||||
if ($coupon_type_info[ 'count' ] == $coupon_type_info[ 'lead_count' ]) {
|
||||
return $this->error('', '来迟了该优惠券已被领取完了');
|
||||
}
|
||||
}
|
||||
if ($coupon_type_info[ 'max_fetch' ] != 0 && $get_type == 2) {
|
||||
//限制领取
|
||||
$member_receive_num = model('promotion_coupon')->getCount([
|
||||
'coupon_type_id' => $coupon_type_id,
|
||||
'member_id' => $member_id,
|
||||
'get_type' => 2
|
||||
]);
|
||||
if ($member_receive_num >= $coupon_type_info[ 'max_fetch' ] ) {
|
||||
return $this->error('', '该优惠券领取已达到上限');
|
||||
}
|
||||
}
|
||||
|
||||
$data = [
|
||||
'coupon_type_id' => $coupon_type_id,
|
||||
'site_id' => $site_id,
|
||||
'coupon_code' => $this->getCode(),
|
||||
'member_id' => $member_id,
|
||||
'money' => $coupon_type_info[ 'money' ],
|
||||
'state' => 1,
|
||||
'get_type' => $get_type,
|
||||
'goods_type' => $coupon_type_info[ 'goods_type' ],
|
||||
'fetch_time' => time(),
|
||||
'coupon_name' => $coupon_type_info[ 'coupon_name' ],
|
||||
'at_least' => $coupon_type_info[ 'at_least' ],
|
||||
'type' => $coupon_type_info[ 'type' ],
|
||||
'discount' => $coupon_type_info[ 'discount' ],
|
||||
'discount_limit' => $coupon_type_info[ 'discount_limit' ],
|
||||
'goods_ids' => $coupon_type_info[ 'goods_ids' ],
|
||||
'related_id' => $related_id
|
||||
];
|
||||
|
||||
if ($coupon_type_info[ 'validity_type' ] == 0) {
|
||||
$data[ 'end_time' ] = $coupon_type_info[ 'end_time' ];
|
||||
} elseif ($coupon_type_info[ 'validity_type' ] == 1) {
|
||||
$data[ 'end_time' ] = ( time() + $coupon_type_info[ 'fixed_term' ] * 86400 );
|
||||
}
|
||||
|
||||
$res = model('promotion_coupon')->add($data);
|
||||
if ($is_stock == 0) {
|
||||
model('promotion_coupon_type')->setInc([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count');
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回收优惠券
|
||||
* @param array $coupon_list
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function recoveryCoupon(array $coupon_list, $site_id)
|
||||
{
|
||||
$coupon = [];
|
||||
foreach ($coupon_list as $coupon_item) {
|
||||
if (isset($coupon[ $coupon_item[ 'coupon_type_id' ] ])) {
|
||||
$coupon[$coupon_item['coupon_type_id']][] = $coupon_item['coupon_id'];
|
||||
} else {
|
||||
$coupon[ $coupon_item[ 'coupon_type_id' ] ] = [ $coupon_item[ 'coupon_id' ] ];
|
||||
}
|
||||
}
|
||||
if (!count($coupon)) return $this->error();
|
||||
|
||||
model('promotion_coupon')->startTrans();
|
||||
try {
|
||||
foreach ($coupon as $coupon_type_id => $coupon_ids) {
|
||||
$num = model('promotion_coupon')->delete([ [ 'coupon_id', 'in', $coupon_ids ], [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ]);
|
||||
if ($num) model('promotion_coupon_type')->setDec([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count', $num);
|
||||
}
|
||||
model('promotion_coupon')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('promotion_coupon')->rollback();
|
||||
return $this->error('', '回收失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 专用于撤回活动赠送的优惠券
|
||||
* @return void
|
||||
*/
|
||||
public function cancelByPromotion($data){
|
||||
$member_id = $data['member_id'];
|
||||
$coupon_data = $data['coupon_data'];//优惠券id相关项
|
||||
$coupon_ids = array_column($coupon_data, 'coupon_type_id');
|
||||
$member_coupon_list = model('promotion_coupon')->getList([
|
||||
['member_id', '=', $member_id],
|
||||
['coupon_type_id', 'in', $coupon_ids],
|
||||
['state', '=', CouponDict::normal]
|
||||
], '*');
|
||||
$member_coupon_type_group_list = [];
|
||||
foreach($member_coupon_list as $v){
|
||||
$member_coupon_type_group_list[$v['coupon_type_id']][] = $v['coupon_id'];
|
||||
}
|
||||
$cancel_ids = [];
|
||||
foreach ($coupon_data as $item) {
|
||||
$coupon_type_id = $item['coupon_type_id'];
|
||||
$num = $item['num'];
|
||||
$item_coupon_type_group = $member_coupon_type_group_list[$coupon_type_id] ?? [];
|
||||
if($item_coupon_type_group){
|
||||
if(count($item_coupon_type_group) > $num){
|
||||
$cancel_ids = array_merge($cancel_ids, array_slice($item_coupon_type_group, 0, $num));
|
||||
}else{
|
||||
$cancel_ids = array_merge($cancel_ids, $item_coupon_type_group);
|
||||
}
|
||||
}
|
||||
}
|
||||
model('promotion_coupon')->update(['state' => CouponDict::close], [['coupon_id', 'in', $cancel_ids]]);
|
||||
return $this->success();
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\model;
|
||||
|
||||
use addon\coupon\dict\CouponDict;
|
||||
use app\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class MemberCoupon extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取会员已领取优惠券
|
||||
* @param $member_id
|
||||
* @param $state
|
||||
* @param int $site_id
|
||||
* @param string $order
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCouponList($member_id, $state, $site_id = 0, $order = "fetch_time desc")
|
||||
{
|
||||
$condition = array (
|
||||
[ "member_id", "=", $member_id ],
|
||||
[ "state", "=", $state ],
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ "site_id", "=", $site_id ];
|
||||
}
|
||||
$list = model("promotion_coupon")->getList($condition, "*", $order, '', '', '', 0);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用优惠券
|
||||
* @param $coupon_id
|
||||
* @param $member_id
|
||||
* @param int $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function useMemberCoupon($coupon_id, $member_id, $order_id = 0)
|
||||
{
|
||||
//优惠券处理方案
|
||||
$result = model('promotion_coupon')->update([ 'use_order_id' => $order_id, 'state' => 2, 'use_time' => time() ], [ [ 'coupon_id', '=', $coupon_id ], [ "member_id", "=", $member_id ], [ 'state', '=', 1 ] ]);
|
||||
if ($result === false) {
|
||||
return $this->error();
|
||||
}
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员已领取优惠券数量
|
||||
* @param $member_id
|
||||
* @param $state
|
||||
* @param int $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function getMemberCouponNum($member_id, $state, $site_id = 0)
|
||||
{
|
||||
$condition = array (
|
||||
[ "member_id", "=", $member_id ],
|
||||
[ "state", "=", $state ],
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ "site_id", "=", $site_id ];
|
||||
}
|
||||
$num = model("promotion_coupon")->getCount($condition);
|
||||
return $this->success($num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员是否可领取该优惠券
|
||||
* @param $coupon_type_id
|
||||
* @param $member_id
|
||||
* @return array
|
||||
*/
|
||||
public function receivedNum($coupon_type_id, $member_id)
|
||||
{
|
||||
$received_num = model('promotion_coupon')->getCount([ [ 'coupon_type_id', '=', $coupon_type_id ], [ 'member_id', '=', $member_id ] ]);
|
||||
return $this->success($received_num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取编码
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return random_keys(8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员批量发放优惠券
|
||||
* @param $coupon_type_ids
|
||||
* @param $site_id
|
||||
* @param $member_id
|
||||
* @param int $get_type
|
||||
* @param int $is_stock
|
||||
* @param int $related_id
|
||||
* @return array
|
||||
*/
|
||||
public function sendCoupon($coupon_type_ids, $site_id, $member_id, $get_type = 4, $is_stock = 0, $related_id = 0)
|
||||
{
|
||||
//已选优惠券提交数组
|
||||
if (!empty($coupon_type_ids)) {
|
||||
$res = 0;
|
||||
foreach ($coupon_type_ids as $coupon_type_id) {
|
||||
$coupon_type_info = model('promotion_coupon_type')->getInfo([ 'coupon_type_id' => $coupon_type_id, 'site_id' => $site_id, 'status' => 1 ]);
|
||||
if (!empty($coupon_type_info)) {
|
||||
|
||||
if ($coupon_type_info[ 'count' ] != -1 || $is_stock == 0) {
|
||||
if ($coupon_type_info[ 'count' ] == $coupon_type_info[ 'lead_count' ]) {
|
||||
return $this->error('', '来迟了该优惠券已被领取完了');
|
||||
}
|
||||
}
|
||||
if ($coupon_type_info[ 'max_fetch' ] != 0 && $get_type == 2) {
|
||||
//限制领取
|
||||
$member_receive_num = model('promotion_coupon')->getCount([
|
||||
'coupon_type_id' => $coupon_type_id,
|
||||
'member_id' => $member_id,
|
||||
'get_type' => 2
|
||||
]);
|
||||
if ($member_receive_num >= $coupon_type_info[ 'max_fetch' ] ) {
|
||||
return $this->error('', '该优惠券领取已达到上限');
|
||||
}
|
||||
}
|
||||
|
||||
$data = [
|
||||
'coupon_type_id' => $coupon_type_id,
|
||||
'site_id' => $site_id,
|
||||
'coupon_code' => $this->getCode(),
|
||||
'member_id' => $member_id,
|
||||
'money' => $coupon_type_info[ 'money' ],
|
||||
'state' => 1,
|
||||
'get_type' => $get_type,
|
||||
'goods_type' => $coupon_type_info[ 'goods_type' ],
|
||||
'fetch_time' => time(),
|
||||
'coupon_name' => $coupon_type_info[ 'coupon_name' ],
|
||||
'at_least' => $coupon_type_info[ 'at_least' ],
|
||||
'type' => $coupon_type_info[ 'type' ],
|
||||
'discount' => $coupon_type_info[ 'discount' ],
|
||||
'discount_limit' => $coupon_type_info[ 'discount_limit' ],
|
||||
'goods_ids' => $coupon_type_info[ 'goods_ids' ],
|
||||
'related_id' => $related_id
|
||||
];
|
||||
|
||||
if ($coupon_type_info[ 'validity_type' ] == 0) {
|
||||
$data[ 'end_time' ] = $coupon_type_info[ 'end_time' ];
|
||||
} elseif ($coupon_type_info[ 'validity_type' ] == 1) {
|
||||
$data[ 'end_time' ] = ( time() + $coupon_type_info[ 'fixed_term' ] * 86400 );
|
||||
}
|
||||
|
||||
$res = model('promotion_coupon')->add($data);
|
||||
if ($is_stock == 0) {
|
||||
model('promotion_coupon_type')->setInc([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count');
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($res) {
|
||||
return $this->success($res);
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
} else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回收优惠券
|
||||
* @param array $coupon_list
|
||||
* @param $site_id
|
||||
* @return array
|
||||
*/
|
||||
public function recoveryCoupon(array $coupon_list, $site_id)
|
||||
{
|
||||
$coupon = [];
|
||||
foreach ($coupon_list as $coupon_item) {
|
||||
if (isset($coupon[ $coupon_item[ 'coupon_type_id' ] ])) {
|
||||
$coupon[$coupon_item['coupon_type_id']][] = $coupon_item['coupon_id'];
|
||||
} else {
|
||||
$coupon[ $coupon_item[ 'coupon_type_id' ] ] = [ $coupon_item[ 'coupon_id' ] ];
|
||||
}
|
||||
}
|
||||
if (!count($coupon)) return $this->error();
|
||||
|
||||
model('promotion_coupon')->startTrans();
|
||||
try {
|
||||
foreach ($coupon as $coupon_type_id => $coupon_ids) {
|
||||
$num = model('promotion_coupon')->delete([ [ 'coupon_id', 'in', $coupon_ids ], [ 'site_id', '=', $site_id ], [ 'state', '=', 1 ] ]);
|
||||
if ($num) model('promotion_coupon_type')->setDec([ [ 'coupon_type_id', '=', $coupon_type_id ] ], 'lead_count', $num);
|
||||
}
|
||||
model('promotion_coupon')->commit();
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('promotion_coupon')->rollback();
|
||||
return $this->error('', '回收失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 专用于撤回活动赠送的优惠券
|
||||
* @return void
|
||||
*/
|
||||
public function cancelByPromotion($data){
|
||||
$member_id = $data['member_id'];
|
||||
$coupon_data = $data['coupon_data'];//优惠券id相关项
|
||||
$coupon_ids = array_column($coupon_data, 'coupon_type_id');
|
||||
$member_coupon_list = model('promotion_coupon')->getList([
|
||||
['member_id', '=', $member_id],
|
||||
['coupon_type_id', 'in', $coupon_ids],
|
||||
['state', '=', CouponDict::normal]
|
||||
], '*');
|
||||
$member_coupon_type_group_list = [];
|
||||
foreach($member_coupon_list as $v){
|
||||
$member_coupon_type_group_list[$v['coupon_type_id']][] = $v['coupon_id'];
|
||||
}
|
||||
$cancel_ids = [];
|
||||
foreach ($coupon_data as $item) {
|
||||
$coupon_type_id = $item['coupon_type_id'];
|
||||
$num = $item['num'];
|
||||
$item_coupon_type_group = $member_coupon_type_group_list[$coupon_type_id] ?? [];
|
||||
if($item_coupon_type_group){
|
||||
if(count($item_coupon_type_group) > $num){
|
||||
$cancel_ids = array_merge($cancel_ids, array_slice($item_coupon_type_group, 0, $num));
|
||||
}else{
|
||||
$cancel_ids = array_merge($cancel_ids, $item_coupon_type_group);
|
||||
}
|
||||
}
|
||||
}
|
||||
model('promotion_coupon')->update(['state' => CouponDict::close], [['coupon_id', 'in', $cancel_ids]]);
|
||||
return $this->success();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,103 +1,94 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\model\share;
|
||||
|
||||
use app\model\share\WchatShareBase as BaseModel;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Site as SiteModel;
|
||||
|
||||
/**
|
||||
* 分享
|
||||
*/
|
||||
class WchatShare extends BaseModel
|
||||
{
|
||||
protected $config = [
|
||||
[
|
||||
'title' => '领券中心',
|
||||
'config_key' => 'WCHAT_SHARE_CONFIG_COUPON_LIST',
|
||||
'path' => [ '/pages_tool/goods/coupon' ],
|
||||
'method_prefix' => 'couponList',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 商品列表分享数据
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareData($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ] ?? 0;
|
||||
|
||||
//站点设置
|
||||
$site_model = new SiteModel();
|
||||
$site_info = $site_model->getSiteInfo([ [ 'site_id', '=', $site_id ] ])[ 'data' ];
|
||||
|
||||
//跳转路径
|
||||
$link = $this->getShareLink($param);
|
||||
|
||||
//获取和替换配置数据
|
||||
$config_method = preg_replace('/Data$/', 'Config', __FUNCTION__);
|
||||
$config_data = $this->$config_method($param);
|
||||
$title = $config_data[ 'value' ][ 'title' ];
|
||||
$desc = $config_data[ 'value' ][ 'desc' ];
|
||||
$image_url = $config_data[ 'value' ][ 'imgUrl' ] ?: $site_info[ 'logo_square' ];
|
||||
|
||||
$data = [
|
||||
'title' => $title,
|
||||
'desc' => $desc,
|
||||
'link' => $link,
|
||||
'imgUrl' => $image_url,
|
||||
];
|
||||
return [
|
||||
'permission' => [
|
||||
'hideOptionMenu' => false,
|
||||
'hideMenuItems' => [],
|
||||
],
|
||||
'data' => $data,//分享内容
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品列表分享配置
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareConfig($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ];
|
||||
$config = $param[ 'config' ];
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
$data = $config_model->getConfig([
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'app_module', '=', 'shop' ],
|
||||
[ 'config_key', '=', $config[ 'config_key' ] ],
|
||||
])[ 'data' ];
|
||||
if (empty($data[ 'value' ])) {
|
||||
$data[ 'value' ] = [
|
||||
'title' => '送你一张优惠券',
|
||||
'desc' => "优惠多多\n好物多多",
|
||||
'imgUrl' => '',
|
||||
];
|
||||
}
|
||||
if (empty($data[ 'value' ][ 'imgUrl' ])) {
|
||||
$data[ 'value' ][ 'imgUrl' ] = img('addon/coupon/icon.png');
|
||||
}
|
||||
$variable = [];
|
||||
|
||||
return [
|
||||
'value' => $data[ 'value' ],
|
||||
'variable' => $variable,
|
||||
];
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\model\share;
|
||||
|
||||
use app\model\share\WchatShareBase as BaseModel;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
use app\model\system\Site as SiteModel;
|
||||
|
||||
/**
|
||||
* 分享
|
||||
*/
|
||||
class WchatShare extends BaseModel
|
||||
{
|
||||
protected $config = [
|
||||
[
|
||||
'title' => '领券中心',
|
||||
'config_key' => 'WCHAT_SHARE_CONFIG_COUPON_LIST',
|
||||
'path' => [ '/pages_tool/goods/coupon' ],
|
||||
'method_prefix' => 'couponList',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* 商品列表分享数据
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareData($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ] ?? 0;
|
||||
|
||||
//站点设置
|
||||
$site_model = new SiteModel();
|
||||
$site_info = $site_model->getSiteInfo([ [ 'site_id', '=', $site_id ] ])[ 'data' ];
|
||||
|
||||
//跳转路径
|
||||
$link = $this->getShareLink($param);
|
||||
|
||||
//获取和替换配置数据
|
||||
$config_method = preg_replace('/Data$/', 'Config', __FUNCTION__);
|
||||
$config_data = $this->$config_method($param);
|
||||
$title = $config_data[ 'value' ][ 'title' ];
|
||||
$desc = $config_data[ 'value' ][ 'desc' ];
|
||||
$image_url = $config_data[ 'value' ][ 'imgUrl' ] ?: $site_info[ 'logo_square' ];
|
||||
|
||||
$data = [
|
||||
'title' => $title,
|
||||
'desc' => $desc,
|
||||
'link' => $link,
|
||||
'imgUrl' => $image_url,
|
||||
];
|
||||
return [
|
||||
'permission' => [
|
||||
'hideOptionMenu' => false,
|
||||
'hideMenuItems' => [],
|
||||
],
|
||||
'data' => $data,//分享内容
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品列表分享配置
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareConfig($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ];
|
||||
$config = $param[ 'config' ];
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
$data = $config_model->getConfig([
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'app_module', '=', 'shop' ],
|
||||
[ 'config_key', '=', $config[ 'config_key' ] ],
|
||||
])[ 'data' ];
|
||||
if (empty($data[ 'value' ])) {
|
||||
$data[ 'value' ] = [
|
||||
'title' => '送你一张优惠券',
|
||||
'desc' => "优惠多多\n好物多多",
|
||||
'imgUrl' => '',
|
||||
];
|
||||
}
|
||||
if (empty($data[ 'value' ][ 'imgUrl' ])) {
|
||||
$data[ 'value' ][ 'imgUrl' ] = img('addon/coupon/icon.png');
|
||||
}
|
||||
$variable = [];
|
||||
|
||||
return [
|
||||
'value' => $data[ 'value' ],
|
||||
'variable' => $variable,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,91 +1,82 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\model\share;
|
||||
|
||||
use app\model\share\WeappShareBase;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
|
||||
/**
|
||||
* 分享
|
||||
*/
|
||||
class WeappShare extends WeappShareBase
|
||||
{
|
||||
protected $config = [
|
||||
[
|
||||
'title' => '领券中心',
|
||||
'config_key' => 'WEAPP_SHARE_CONFIG_COUPON_LIST',
|
||||
'path' => [ '/pages_tool/goods/coupon' ],
|
||||
'method_prefix' => 'couponList',
|
||||
],
|
||||
];
|
||||
|
||||
protected $sort = 2;
|
||||
|
||||
/***************************** 领券中心 ***************************/
|
||||
|
||||
/**
|
||||
* 首页分享数据
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareData($param)
|
||||
{
|
||||
//获取和替换配置数据
|
||||
$config_data = $this->couponListShareConfig($param);
|
||||
$title = $config_data[ 'value' ][ 'title' ];
|
||||
$image_url = $config_data[ 'value' ][ 'imageUrl' ] ? img($config_data[ 'value' ][ 'imageUrl' ]) : '';
|
||||
$path = $this->getSharePath($param);
|
||||
|
||||
$data = [
|
||||
'title' => $title,
|
||||
'path' => $path,
|
||||
'imageUrl' => $image_url,
|
||||
];
|
||||
return [
|
||||
'permission' => [
|
||||
'onShareAppMessage' => true,
|
||||
'onShareTimeline' => true,
|
||||
],
|
||||
'data' => $data,//分享内容
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页分享配置
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareConfig($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ];
|
||||
$config = $param[ 'config' ];
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
$data = $config_model->getConfig([
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'app_module', '=', 'shop' ],
|
||||
[ 'config_key', '=', $config[ 'config_key' ] ],
|
||||
])[ 'data' ];
|
||||
if (empty($data[ 'value' ])) {
|
||||
$data[ 'value' ] = [
|
||||
'title' => '送你一张优惠券,快来领取吧',
|
||||
'imageUrl' => '',
|
||||
];
|
||||
}
|
||||
$variable = [];
|
||||
|
||||
return [
|
||||
'value' => $data[ 'value' ],
|
||||
'variable' => $variable,
|
||||
];
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\model\share;
|
||||
|
||||
use app\model\share\WeappShareBase;
|
||||
use app\model\system\Config as ConfigModel;
|
||||
|
||||
/**
|
||||
* 分享
|
||||
*/
|
||||
class WeappShare extends WeappShareBase
|
||||
{
|
||||
protected $config = [
|
||||
[
|
||||
'title' => '领券中心',
|
||||
'config_key' => 'WEAPP_SHARE_CONFIG_COUPON_LIST',
|
||||
'path' => [ '/pages_tool/goods/coupon' ],
|
||||
'method_prefix' => 'couponList',
|
||||
],
|
||||
];
|
||||
|
||||
protected $sort = 2;
|
||||
|
||||
/***************************** 领券中心 ***************************/
|
||||
|
||||
/**
|
||||
* 首页分享数据
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareData($param)
|
||||
{
|
||||
//获取和替换配置数据
|
||||
$config_data = $this->couponListShareConfig($param);
|
||||
$title = $config_data[ 'value' ][ 'title' ];
|
||||
$image_url = $config_data[ 'value' ][ 'imageUrl' ] ? img($config_data[ 'value' ][ 'imageUrl' ]) : '';
|
||||
$path = $this->getSharePath($param);
|
||||
|
||||
$data = [
|
||||
'title' => $title,
|
||||
'path' => $path,
|
||||
'imageUrl' => $image_url,
|
||||
];
|
||||
return [
|
||||
'permission' => [
|
||||
'onShareAppMessage' => true,
|
||||
'onShareTimeline' => true,
|
||||
],
|
||||
'data' => $data,//分享内容
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页分享配置
|
||||
* @param $param
|
||||
* @return array
|
||||
*/
|
||||
protected function couponListShareConfig($param)
|
||||
{
|
||||
$site_id = $param[ 'site_id' ];
|
||||
$config = $param[ 'config' ];
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
$data = $config_model->getConfig([
|
||||
[ 'site_id', '=', $site_id ],
|
||||
[ 'app_module', '=', 'shop' ],
|
||||
[ 'config_key', '=', $config[ 'config_key' ] ],
|
||||
])[ 'data' ];
|
||||
if (empty($data[ 'value' ])) {
|
||||
$data[ 'value' ] = [
|
||||
'title' => '送你一张优惠券,快来领取吧',
|
||||
'imageUrl' => '',
|
||||
];
|
||||
}
|
||||
$variable = [];
|
||||
|
||||
return [
|
||||
'value' => $data[ 'value' ],
|
||||
'variable' => $variable,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,110 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
namespace addon\coupon\shopapi\controller;
|
||||
|
||||
use addon\coupon\model\Coupon as CouponModel;
|
||||
use addon\coupon\model\CouponType as CouponTypeModel;
|
||||
use addon\coupon\model\MemberCoupon;
|
||||
use app\shopapi\controller\BaseApi;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class Coupon extends BaseApi
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//执行父类构造函数
|
||||
parent::__construct();
|
||||
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) {
|
||||
echo $this->response($token);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动列表
|
||||
*/
|
||||
public function lists()
|
||||
{
|
||||
$coupon_type_model = new CouponTypeModel();
|
||||
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$coupon_name = $this->params['coupon_name'] ?? '';
|
||||
$status = $this->params['status'] ?? '';
|
||||
|
||||
$condition = [];
|
||||
if ($status !== '') {
|
||||
$condition[] = [ 'status', '=', $status ];
|
||||
}
|
||||
$type = $this->params['type'] ?? '';
|
||||
if ($type) {
|
||||
$condition[] = [ 'type', '=', $type ];
|
||||
}
|
||||
//类型
|
||||
$validity_type = $this->params['validity_type'] ?? '';
|
||||
if ($validity_type) {
|
||||
$start_time = $this->params['start_time'] ?? '';
|
||||
$end_time = $this->params['end_time'] ?? '';
|
||||
switch ( $validity_type ) {
|
||||
case 1: //固定
|
||||
$condition[] = [ 'end_time', 'between', [ $start_time, $end_time ] ];
|
||||
break;
|
||||
case 2:
|
||||
$condition[] = [ 'fixed_term', 'between', [ $start_time, $end_time ] ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$condition[] = [ 'site_id', '=', $this->site_id ];
|
||||
$condition[] = [ 'coupon_name', 'like', '%' . $coupon_name . '%' ];
|
||||
$order = 'create_time desc';
|
||||
$field = '*';
|
||||
|
||||
$res = $coupon_type_model->getCouponTypePageList($condition, $page, $page_size, $order, $field);
|
||||
//获取优惠券状态
|
||||
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
|
||||
foreach ($res[ 'data' ][ 'list' ] as $key => $val) {
|
||||
$res[ 'data' ][ 'list' ][ $key ][ 'status_name' ] = $coupon_type_status_arr[ $val[ 'status' ] ];
|
||||
}
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$member_id = $this->params['member_id'] ?? 0;
|
||||
$coupon_type_ids = $this->params['parent'] ?? '';
|
||||
$get_type = $this->params['get_type'] ?? 4;
|
||||
$site_id = $this->site_id;
|
||||
$parent = $coupon_type_ids;
|
||||
if (empty($parent)) {
|
||||
return $this->error('', 'REQUEST_COUPON_TYPE_ID');
|
||||
}
|
||||
$parent = explode(',', $parent);
|
||||
if (count($parent) == 1) {
|
||||
$coupon_model = new CouponModel();
|
||||
$res = $coupon_model->receiveCoupon($parent[ 0 ], $site_id, $member_id, $get_type);
|
||||
} else {
|
||||
$member_coupon_model = new MemberCoupon();
|
||||
$res = $member_coupon_model->sendCoupon(explode(',', $coupon_type_ids), $site_id, $member_id, $get_type);
|
||||
}
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
namespace addon\coupon\shopapi\controller;
|
||||
|
||||
use addon\coupon\model\Coupon as CouponModel;
|
||||
use addon\coupon\model\CouponType as CouponTypeModel;
|
||||
use addon\coupon\model\MemberCoupon;
|
||||
use app\shopapi\controller\BaseApi;
|
||||
|
||||
/**
|
||||
* 优惠券
|
||||
*/
|
||||
class Coupon extends BaseApi
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//执行父类构造函数
|
||||
parent::__construct();
|
||||
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) {
|
||||
echo $this->response($token);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动列表
|
||||
*/
|
||||
public function lists()
|
||||
{
|
||||
$coupon_type_model = new CouponTypeModel();
|
||||
|
||||
$page = $this->params['page'] ?? 1;
|
||||
$page_size = $this->params['page_size'] ?? PAGE_LIST_ROWS;
|
||||
$coupon_name = $this->params['coupon_name'] ?? '';
|
||||
$status = $this->params['status'] ?? '';
|
||||
|
||||
$condition = [];
|
||||
if ($status !== '') {
|
||||
$condition[] = [ 'status', '=', $status ];
|
||||
}
|
||||
$type = $this->params['type'] ?? '';
|
||||
if ($type) {
|
||||
$condition[] = [ 'type', '=', $type ];
|
||||
}
|
||||
//类型
|
||||
$validity_type = $this->params['validity_type'] ?? '';
|
||||
if ($validity_type) {
|
||||
$start_time = $this->params['start_time'] ?? '';
|
||||
$end_time = $this->params['end_time'] ?? '';
|
||||
switch ( $validity_type ) {
|
||||
case 1: //固定
|
||||
$condition[] = [ 'end_time', 'between', [ $start_time, $end_time ] ];
|
||||
break;
|
||||
case 2:
|
||||
$condition[] = [ 'fixed_term', 'between', [ $start_time, $end_time ] ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$condition[] = [ 'site_id', '=', $this->site_id ];
|
||||
$condition[] = [ 'coupon_name', 'like', '%' . $coupon_name . '%' ];
|
||||
$order = 'create_time desc';
|
||||
$field = '*';
|
||||
|
||||
$res = $coupon_type_model->getCouponTypePageList($condition, $page, $page_size, $order, $field);
|
||||
//获取优惠券状态
|
||||
$coupon_type_status_arr = $coupon_type_model->getCouponTypeStatus();
|
||||
foreach ($res[ 'data' ][ 'list' ] as $key => $val) {
|
||||
$res[ 'data' ][ 'list' ][ $key ][ 'status_name' ] = $coupon_type_status_arr[ $val[ 'status' ] ];
|
||||
}
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$member_id = $this->params['member_id'] ?? 0;
|
||||
$coupon_type_ids = $this->params['parent'] ?? '';
|
||||
$get_type = $this->params['get_type'] ?? 4;
|
||||
$site_id = $this->site_id;
|
||||
$parent = $coupon_type_ids;
|
||||
if (empty($parent)) {
|
||||
return $this->error('', 'REQUEST_COUPON_TYPE_ID');
|
||||
}
|
||||
$parent = explode(',', $parent);
|
||||
if (count($parent) == 1) {
|
||||
$coupon_model = new CouponModel();
|
||||
$res = $coupon_model->receiveCoupon($parent[ 0 ], $site_id, $member_id, $get_type);
|
||||
} else {
|
||||
$member_coupon_model = new MemberCoupon();
|
||||
$res = $member_coupon_model->sendCoupon(explode(',', $coupon_type_ids), $site_id, $member_id, $get_type);
|
||||
}
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user