UserCouponController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace addons\ElectronCoupon\api\modules\v1\controllers;
  3. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  4. use addons\ElectronCoupon\common\listeners\OrderFinishListener;
  5. use addons\ElectronCoupon\common\listeners\OrderPaidListener;
  6. use addons\ElectronCoupon\common\models\ElectronCoupon;
  7. use addons\ElectronCoupon\common\models\ElectronCouponCode;
  8. use addons\ElectronCoupon\common\models\ElectronCouponUserCoupon;
  9. use common\models\user\User;
  10. use Yii;
  11. use api\controllers\OnAuthController;
  12. use common\enums\StatusEnum;
  13. class UserCouponController extends OnAuthController
  14. {
  15. public $modelClass = '';
  16. /**
  17. * 不用进行登录验证的方法
  18. * @var array
  19. */
  20. protected $authOptional = ['index'];
  21. public function actionIndex()
  22. {
  23. return 'Hello world';
  24. }
  25. /**
  26. * 获取用户电子券列表
  27. * @return mixed|void
  28. */
  29. public function actionGetList()
  30. {
  31. if (Yii::$app->request->isGet) {
  32. $params = Yii::$app->request->get();
  33. $res = Yii::$app->services->validate->validate($params, [
  34. [['coupon_status'], 'integer'],
  35. ]);
  36. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  37. $where = [
  38. ['=', 'mall_id', $this->mall_id],
  39. ['>=', 'status', StatusEnum::DISABLED],
  40. ['=', 'user_id', $this->user_id]
  41. ];
  42. // 未激活 0
  43. if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_RECEIVE) {
  44. $where[] = ['>=', 'use_end_time', time()];
  45. $where[] = ['>', 'receive_amount', 0];
  46. $order = 'last_receive_time desc';
  47. }
  48. // 已激活 1
  49. if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_ACTIVE) {
  50. $where[] = ['>=', 'use_end_time', time()];
  51. $where[] = ['>', 'active_amount', 0];
  52. $order = 'last_active_time desc';
  53. }
  54. // 已兑换 2
  55. if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_USED) {
  56. $where[] = ['>', 'used_amount', 0];
  57. $order = 'last_used_time desc';
  58. }
  59. // 已过期 3
  60. if (isset($params['coupon_status']) && $params['coupon_status'] == ElectronCouponEnums::COUPON_STATUS_EXPIRED) {
  61. $where[] = ['<', 'use_end_time', time()];
  62. $order = 'use_end_time desc';
  63. }
  64. $with = ['user', 'properUser'];
  65. $order = isset($order) ? $order . ',id desc' : 'id desc';
  66. $condition = compact('where', 'with', 'order');
  67. $page_data = ElectronCouponUserCoupon::getListPage($condition, $this->mall_id);
  68. return $this->success('ok', compact('page_data'));
  69. }
  70. }
  71. /**
  72. * 获取用户信息,用于判断用户是否存在
  73. */
  74. public function actionUserInfo()
  75. {
  76. if (Yii::$app->request->isPost) {
  77. $params = Yii::$app->request->post();
  78. $res = Yii::$app->services->validate->validate($params, [
  79. [['user_id'], 'integer'],
  80. ]);
  81. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  82. if ($params['user_id'] <= 0) {
  83. return $this->error('请输入正确的用户ID');
  84. }
  85. $user_info = User::getOne([
  86. ['id' => $params['user_id']],
  87. ['mall_id' => $this->mall_id]
  88. ], true, '', '', 'nickname');
  89. if (!$user_info) {
  90. return $this->error('用户不存在');
  91. }
  92. return $this->success('获取用户信息成功', $user_info);
  93. }
  94. }
  95. /**
  96. * 赠送电子券
  97. */
  98. public function actionGiveCoupon()
  99. {
  100. if (Yii::$app->request->isPost) {
  101. $params = Yii::$app->request->post();
  102. $res = Yii::$app->services->validate->validate($params, [
  103. [['user_coupon_id', 'e_coupon_id', 'receiver_user_id', 'status'], 'integer'],
  104. [['amount'], 'number']
  105. ]);
  106. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  107. $params['mall_id'] = $this->mall_id;
  108. $params['user_id'] = $this->user_id;
  109. $result = ElectronCouponUserCoupon::userGiveCoupon($params);
  110. if ($result == false) {
  111. return $this->error(ElectronCouponUserCoupon::getStaticError());
  112. }
  113. return $this->success('赠送成功', []);
  114. }
  115. }
  116. /**
  117. * 创建核销码
  118. */
  119. public function actionCodeCreate()
  120. {
  121. if (Yii::$app->request->isPost) {
  122. $params = Yii::$app->request->post();
  123. $res = Yii::$app->services->validate->validate($params, [
  124. [['user_e_coupon_id'], 'integer'],
  125. [['amount'], 'number']
  126. ]);
  127. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  128. $params['mall_id'] = $this->mall_id;
  129. $params['user_id'] = $this->user_id;
  130. $code = ElectronCouponCode::getCodeContent($params);
  131. if ($code == false) {
  132. return $this->error(ElectronCouponCode::getStaticError());
  133. }
  134. // $url = ElectronCouponEnums::APPROVAL_URL . '?' . http_build_query([
  135. // 'code' => $code,
  136. // 'mall_id' => $this->mall_id
  137. // ]);
  138. return $this->success('success', compact('code'));
  139. }
  140. }
  141. /**
  142. * 领券中心的轮播图
  143. */
  144. public function actionCouponConf()
  145. {
  146. $config = Yii::$app->services->setting->addons->get($this->mall_id, ElectronCouponEnums::ADDONS_NAME, 'basic');
  147. $rotation = $config['images'] ? json_decode($config['images'], true) : [];
  148. $customize_name = $config['customize_name'] ?? '电子券';
  149. return $this->success('success', compact('rotation', 'customize_name'));
  150. }
  151. /**
  152. * 领券中心 电子券列表
  153. */
  154. public function actionCenterList()
  155. {
  156. if (Yii::$app->request->isPost) {
  157. $params = Yii::$app->request->post();
  158. $res = Yii::$app->services->validate->validate($params, [
  159. [['page'], 'integer'],
  160. ]);
  161. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  162. $where = [
  163. ['mall_id' => $this->mall_id],
  164. ['is_show_in_center' => ElectronCouponEnums::IS_SHOW_IN_CENTER],
  165. ['status' => StatusEnum::ENABLED],
  166. ];
  167. $order = 'sort desc,id desc';
  168. $list = ElectronCoupon::listPage(compact('where', 'order'), false, true);
  169. $list['list'] = $list['list'] ?? [];
  170. foreach ($list['list'] as $key => $val) {
  171. $val['receive_percent'] = $val['amount'] > 0 ? bcdiv($val['receive_amount'] * 100, $val['amount'], 2) : 0;
  172. $list['list'][$key] = $val;
  173. }
  174. return $this->success('领券中心', $list);
  175. }
  176. }
  177. /*
  178. * 领券电子券操作
  179. */
  180. public function actionReceiveCoupon()
  181. {
  182. if (Yii::$app->request->isPost) {
  183. $params = Yii::$app->request->post();
  184. $res = Yii::$app->services->validate->validate($params, [
  185. [['e_coupon_id'], 'integer'],
  186. ]);
  187. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  188. $params['mall_id'] = $this->mall_id;
  189. $params['user_id'] = $this->user_id;
  190. $params['get_type'] = ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE;
  191. $result = ElectronCouponUserCoupon::userReceiveCoupon($params);
  192. if ($result == false) {
  193. return $this->error(ElectronCouponUserCoupon::getStaticError());
  194. }
  195. return $this->success('领取成功', $result);
  196. }
  197. }
  198. }