IndexController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace addons\Seed\api\modules\v1\controllers;
  3. use addons\Seed\common\enums\SeedEnum;
  4. use addons\Seed\common\models\SeedLog;
  5. use addons\Seed\common\models\SeedOrder;
  6. use addons\Seed\common\models\SeedUser;
  7. use common\enums\RedisKeyEnum;
  8. use common\enums\StatusEnum;
  9. use common\helpers\LockHelper;
  10. use PHPUnit\Util\Exception;
  11. use Yii;
  12. use api\controllers\OnAuthController;
  13. /**
  14. * 默认控制器
  15. *
  16. * Class IndexController
  17. * @package addons\Seed\api\modules\v1\controllers
  18. */
  19. class IndexController extends OnAuthController
  20. {
  21. public $modelClass = '';
  22. /**
  23. * 不用进行登录验证的方法
  24. *
  25. * 例如: ['index', 'update', 'create', 'view', 'delete']
  26. * 默认全部需要验证
  27. *
  28. * @var array
  29. */
  30. protected $authOptional = [];
  31. /**
  32. * 用户种子金
  33. * @return string
  34. */
  35. public function actionUser()
  36. {
  37. if (Yii::$app->request->isGet) {
  38. $get = Yii::$app->request->get();
  39. // 检查提交的参数
  40. $res = Yii::$app->services->validate->validate($post,[
  41. [['page','limit'], 'integer'],
  42. [['start_at','end_at'], 'string'],
  43. ]);
  44. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  45. // 获取用户当前种子金数量跟累计转换金额
  46. $info_select = "total_seed_num,collect_seed_num,refund_seed_num,total_change_money";
  47. $info = SeedUser::find()->select($info_select)->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  48. $data['seed_num'] = empty($info) ? 0 : $info['total_seed_num'] - $info['collect_seed_num'] - $info['refund_seed_num'];// 待收取种子金
  49. $data['change_money'] = empty($info) ? 0 : $info['total_change_money'];// 已转换金额
  50. // 查询推荐内容
  51. $where[] = ['=', 'mall_id', $this->mall_id];
  52. $where[] = ['=', 'user_id', $this->user_id];
  53. // 根据时间筛选
  54. if (!empty($get['start_at']) && !empty($get['end_at'])) {
  55. $where[] = ['between', 'created_at', strtotime($get['start_at'].' 00:00:00'), strtotime($get['end_at'].' 23:59:59')];
  56. }
  57. $where[] = ['=', 'status', StatusEnum::ENABLED];
  58. $order = 'id desc,created_at desc';
  59. $select = 'id,user_id,order_id,order_no,seed_num,increase_num,unit_price,seed_cost,seed_status';
  60. $params = array('where' => $where, 'order' => $order, 'select' => $select, 'limit' => $post['limit']);
  61. $data['list'] = SeedOrder::listPage($params);
  62. $data['seed_status_arr'] = SeedEnum::getSeedMap();// 获取种子金状态
  63. return $this->success('获取成功', $data);
  64. }
  65. }
  66. /**
  67. * 用户种子金变动记录
  68. * @return string
  69. */
  70. public function actionChangeLog()
  71. {
  72. try {
  73. if (Yii::$app->request->isGet) {
  74. $get = Yii::$app->request->get();
  75. // 检查提交的参数
  76. $res = Yii::$app->services->validate->validate($get,[
  77. [['id','page','limit'], 'integer'],
  78. ]);
  79. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  80. // 种子金订单信息
  81. $select = "id,order_id,order_no,event_pay,seed_num,seed_status";
  82. $data['info'] = SeedOrder::find()->select($select)->where(['id' => $get['id'], 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  83. $data['capital_type_arr'] = SeedEnum::getChangeMap();// 获取种子金状态
  84. // 查询推荐内容
  85. $where[] = ['=', 'seed_order_id', $data['info']['id']];
  86. $where[] = ['=', 'mall_id', $this->mall_id];
  87. $where[] = ['=', 'user_id', $this->user_id];
  88. $where[] = ['=', 'status', StatusEnum::ENABLED];
  89. $order = 'id desc,created_at desc';
  90. $select = 'id,user_id,order_id,after_unit_price,seed_num,seed_cost,capital_type,status,created_at';
  91. $params = array('where' => $where, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);
  92. $data['list'] = SeedLog::listPage($params);
  93. return $this->success('获取成功', $data);
  94. }
  95. } catch (\Exception $e) {
  96. return $this->error('获取出错:'.$e->getMessage());
  97. }
  98. }
  99. /**
  100. * 种子金收取
  101. * @Author: lun
  102. * @DateTime: 2022/8/1 0001 10:02
  103. * @Copyright: copyright (c) 2021 广东七件事集团
  104. * @return mixed|void
  105. */
  106. public function actionCollect()
  107. {
  108. if (Yii::$app->request->isPost) {
  109. $post = Yii::$app->request->post();
  110. // 检查提交的参数
  111. $res = Yii::$app->services->validate->validate($post,[
  112. [['id'], 'required'],
  113. [['id'], 'integer'],
  114. ]);
  115. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  116. // 检查能量池释放锁是否开启中
  117. $lock_key = RedisKeyEnum::suffix(SeedEnum::LOCK_POOL , $this->mall_id);
  118. if(!LockHelper::readLock($lock_key, 0)) {
  119. return $this->error('能量池正在释放,请稍后再进行收取');
  120. }
  121. // 收取种子金
  122. $res = SeedOrder::collect($this->user_id, $post['id']);
  123. if ($res === false) return $this->error('收取失败:' . SeedOrder::getStaticError());
  124. return $this->success('收取成功');
  125. }
  126. }
  127. }