| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace addons\ElectronCoupon\api\modules\v1\controllers;
- use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
- use addons\ElectronCoupon\common\listeners\OrderFinishListener;
- use addons\ElectronCoupon\common\listeners\OrderPaidListener;
- use addons\ElectronCoupon\common\models\ElectronCoupon;
- use addons\ElectronCoupon\common\models\ElectronCouponCode;
- use addons\ElectronCoupon\common\models\ElectronCouponUserCoupon;
- use common\models\user\User;
- use Yii;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- class UserCouponController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- * @var array
- */
- protected $authOptional = ['index'];
- public function actionIndex()
- {
- return 'Hello world';
- }
- /**
- * 获取用户电子券列表
- * @return mixed|void
- */
- public function actionGetList()
- {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['coupon_status'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['=', 'mall_id', $this->mall_id],
- ['>=', 'status', StatusEnum::DISABLED],
- ['=', 'user_id', $this->user_id]
- ];
- // 未激活 0
- if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_RECEIVE) {
- $where[] = ['>=', 'use_end_time', time()];
- $where[] = ['>', 'receive_amount', 0];
- $order = 'last_receive_time desc';
- }
- // 已激活 1
- if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_ACTIVE) {
- $where[] = ['>=', 'use_end_time', time()];
- $where[] = ['>', 'active_amount', 0];
- $order = 'last_active_time desc';
- }
- // 已兑换 2
- if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_USED) {
- $where[] = ['>', 'used_amount', 0];
- $order = 'last_used_time desc';
- }
- // 已过期 3
- if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_EXPIRED) {
- $where[] = ['<', 'use_end_time', time()];
- $order = 'use_end_time desc';
- }
- $with = ['user', 'properUser'];
- $order = isset($order) ? $order . ',id desc' : 'id desc';
- $condition = compact('where', 'with', 'order');
- $page_data = ElectronCouponUserCoupon::getListPage($condition, $this->mall_id);
- return $this->success('ok', compact('page_data'));
- }
- }
- /**
- * 获取用户信息,用于判断用户是否存在
- */
- public function actionUserInfo()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if ($params['user_id'] <= 0) {
- return $this->error('请输入正确的用户ID');
- }
- $user_info = User::getOne([
- ['id' => $params['user_id']],
- ['mall_id' => $this->mall_id]
- ], true, '', '', 'nickname');
- if (!$user_info) {
- return $this->error('用户不存在');
- }
- return $this->success('获取用户信息成功', $user_info);
- }
- }
- /**
- * 赠送电子券
- */
- public function actionGiveCoupon()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_coupon_id', 'e_coupon_id', 'receiver_user_id', 'status'], 'integer'],
- [['amount'], 'number']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $result = ElectronCouponUserCoupon::userGiveCoupon($params);
- if ($result == false) {
- return $this->error(ElectronCouponUserCoupon::getStaticError());
- }
- return $this->success('赠送成功', []);
- }
- }
- /**
- * 创建核销码
- */
- public function actionCodeCreate()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_e_coupon_id'], 'integer'],
- [['amount'], 'number']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $code = ElectronCouponCode::getCodeContent($params);
- if ($code == false) {
- return $this->error(ElectronCouponCode::getStaticError());
- }
- // $url = ElectronCouponEnums::APPROVAL_URL . '?' . http_build_query([
- // 'code' => $code,
- // 'mall_id' => $this->mall_id
- // ]);
- return $this->success('success', compact('code'));
- }
- }
- /**
- * 领券中心的轮播图
- */
- public function actionCouponConf()
- {
- $config = Yii::$app->services->setting->addons->get($this->mall_id, ElectronCouponEnums::ADDONS_NAME, 'basic');
- $rotation = $config['images'] ? json_decode($config['images'], true) : [];
- $customize_name = $config['customize_name'] ?? '电子券';
- return $this->success('success', compact('rotation', 'customize_name'));
- }
- /**
- * 领券中心 电子券列表
- */
- public function actionCenterList()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['page'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['mall_id' => $this->mall_id],
- ['is_show_in_center' => ElectronCouponEnums::IS_SHOW_IN_CENTER],
- ['status' => StatusEnum::ENABLED],
- ];
- $order = 'sort desc,id desc';
- $list = ElectronCoupon::listPage(compact('where', 'order'), false, true);
- $list['list'] = $list['list'] ?? [];
- foreach ($list['list'] as $key => $val) {
- $val['receive_percent'] = $val['amount'] > 0 ? bcdiv($val['receive_amount'] * 100, $val['amount'], 2) : 0;
- $list['list'][$key] = $val;
- }
- return $this->success('领券中心', $list);
- }
- }
- /*
- * 领券电子券操作
- */
- public function actionReceiveCoupon()
- {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['e_coupon_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $params['get_type'] = ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE;
- $result = ElectronCouponUserCoupon::userReceiveCoupon($params);
- if ($result == false) {
- return $this->error(ElectronCouponUserCoupon::getStaticError());
- }
- return $this->success('领取成功', $result);
- }
- }
- }
|