| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace addons\Seed\api\modules\v1\controllers;
- use addons\Seed\common\enums\SeedEnum;
- use addons\Seed\common\models\SeedLog;
- use addons\Seed\common\models\SeedOrder;
- use addons\Seed\common\models\SeedUser;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- use PHPUnit\Util\Exception;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class IndexController
- * @package addons\Seed\api\modules\v1\controllers
- */
- class IndexController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- /**
- * 用户种子金
- * @return string
- */
- public function actionUser()
- {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post,[
- [['page','limit'], 'integer'],
- [['start_at','end_at'], 'string'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取用户当前种子金数量跟累计转换金额
- $info_select = "total_seed_num,collect_seed_num,refund_seed_num,total_change_money";
- $info = SeedUser::find()->select($info_select)->where(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- $data['seed_num'] = empty($info) ? 0 : $info['total_seed_num'] - $info['collect_seed_num'] - $info['refund_seed_num'];// 待收取种子金
- $data['change_money'] = empty($info) ? 0 : $info['total_change_money'];// 已转换金额
- // 查询推荐内容
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'user_id', $this->user_id];
- // 根据时间筛选
- if (!empty($get['start_at']) && !empty($get['end_at'])) {
- $where[] = ['between', 'created_at', strtotime($get['start_at'].' 00:00:00'), strtotime($get['end_at'].' 23:59:59')];
- }
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'id desc,created_at desc';
- $select = 'id,user_id,order_id,order_no,seed_num,increase_num,unit_price,seed_cost,seed_status';
- $params = array('where' => $where, 'order' => $order, 'select' => $select, 'limit' => $post['limit']);
- $data['list'] = SeedOrder::listPage($params);
- $data['seed_status_arr'] = SeedEnum::getSeedMap();// 获取种子金状态
- return $this->success('获取成功', $data);
- }
- }
- /**
- * 用户种子金变动记录
- * @return string
- */
- public function actionChangeLog()
- {
- try {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($get,[
- [['id','page','limit'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 种子金订单信息
- $select = "id,order_id,order_no,event_pay,seed_num,seed_status";
- $data['info'] = SeedOrder::find()->select($select)->where(['id' => $get['id'], 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- $data['capital_type_arr'] = SeedEnum::getChangeMap();// 获取种子金状态
- // 查询推荐内容
- $where[] = ['=', 'seed_order_id', $data['info']['id']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'user_id', $this->user_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'id desc,created_at desc';
- $select = 'id,user_id,order_id,after_unit_price,seed_num,seed_cost,capital_type,status,created_at';
- $params = array('where' => $where, 'order' => $order, 'select' => $select, 'limit' => $get['limit']);
- $data['list'] = SeedLog::listPage($params);
- return $this->success('获取成功', $data);
- }
- } catch (\Exception $e) {
- return $this->error('获取出错:'.$e->getMessage());
- }
- }
- /**
- * 种子金收取
- * @Author: lun
- * @DateTime: 2022/8/1 0001 10:02
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionCollect()
- {
- if (Yii::$app->request->isPost) {
- $post = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post,[
- [['id'], 'required'],
- [['id'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 检查能量池释放锁是否开启中
- $lock_key = RedisKeyEnum::suffix(SeedEnum::LOCK_POOL , $this->mall_id);
- if(!LockHelper::readLock($lock_key, 0)) {
- return $this->error('能量池正在释放,请稍后再进行收取');
- }
- // 收取种子金
- $res = SeedOrder::collect($this->user_id, $post['id']);
- if ($res === false) return $this->error('收取失败:' . SeedOrder::getStaticError());
- return $this->success('收取成功');
- }
- }
- }
|