| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace addons\ShortVideo\api\modules\v1\controllers;
- use addons\ShortVideo\common\models\ShortVideo;
- use addons\ShortVideo\common\models\ShortVideoBrowseAward;
- use addons\ShortVideo\common\models\ShortVideoFollow;
- use addons\ShortVideo\common\models\ShortVideoOrderAward;
- use common\enums\StatusEnum;
- use common\helpers\DateHelper;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- /**
- * 会员控制器
- *
- * Class DefaultController
- * @package addons\ShortVideo\api\modules\v1\controllers
- */
- class UserController extends BaseController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [''];
- /**
- * @name:
- * @msg: 关注/取消关注会员(废弃,该功能使用会员模块公共接口)
- * @param {*}
- * @return {*}
- */
- public function actionFollow()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $type = \Yii::$app->request->get('type');
- if (empty($user_id) || (empty($type) || !in_array($type, ['follow', 'unfollow']))) return $this->error('参数错误');
- if ($user_id == $this->user_id) return $this->error('不可以关注自己');
- $follow_user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
- if (empty($follow_user)) return $this->error('关注的会员不存在');
- $op = '关注';
- $params = [
- 'user_id' => $this->user_id,
- 'follow_id' => $user_id,
- 'status' => StatusEnum::ENABLED,
- ];
- if ('unfollow' == $type) {
- $params['status'] = StatusEnum::DELETE;
- $op = '取消关注';
- }
- $res = ShortVideoFollow::setData($this->mall_id, $params);
- if (false === $res) return $this->error($op . '失败:' . ShortVideoFollow::getStaticError());
- return $this->success($op . '成功');
- }
- /**
- * @name:
- * @msg: 关注列表,包括我关注别人的列表和别人关注我的列表(废弃,该功能使用会员模块公共接口)
- * @param {integer} $user_id 会员id
- * @param {string} $type 列表类型,follow:我关注别人的列表;fans:别人关注我的列表
- * @return {*}
- */
- public function actionFollowList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $type = \Yii::$app->request->get('type');
- if (empty($user_id) || (empty($type) || !in_array($type, ['follow', 'fans']))) return $this->error('参数错误');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $list = ShortVideoFollow::getList($this->mall_id, $user_id, $type, $page, $limit);
- return $this->success('success', $list);
- }
- /**
- * @name:
- * @msg: 会员带货奖励
- * @param {integer} $user_id 会员id
- * @return {*}
- */
- public function actionBoughtRewardList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- if (empty($user_id)) return $this->error('参数错误');
- $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
- if (empty($user)) return $this->error('会员不存在');
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'is_settlement' => 1, 'is_award' => 1];
- $params = [
- 'select' => 'user_id,order_id,video_id,goods_id,award_money,created_at',
- 'where' => $wheres,
- 'order' => 'settlement_time desc,created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $order_rewards = ShortVideoOrderAward::listPage($params);
- if (false === $order_rewards['list']) {
- return $this->error(ShortVideoOrderAward::getStaticError());
- }
- $user_ids = array_column($order_rewards['list'], 'user_id');
- $users = User::find()
- ->select('id,nickname,avatar_url')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $order_ids = array_column($order_rewards['list'], 'order_id');
- $orders = Order::find()
- ->select('id,order_no,pay_money')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- $good_ids = array_column($order_rewards['list'], 'goods_id');
- $goods = Goods::find()
- ->select('id,goods_name,cover_pic,price')
- ->where(['mall_id' => $this->mall_id, 'id' => $good_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($order_rewards['list'] as &$reward) {
- $user = $users[$reward['user_id']] ?? [];
- $order = $orders[$reward['order_id']] ?? [];
- $good = $goods[$reward['goods_id']] ?? [];
- $reward['nickname'] = $user['nickanme'] ?? '默认会员';
- $reward['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- $reward['order_no'] = $order['order_no'] ?? '';
- $reward['pay_money'] = $order['pay_money'] ?? 0;
- unset($good['id']);
- $reward = array_merge($reward, $good);
- }
- return $this->success('success', $order_rewards);
- }
- /**
- * @name:
- * @msg: 观看视频奖励列表
- * @param {integer} $user_id 会员id
- * @return {*}
- */
- public function actionBrowseRewardList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- if (empty($user_id)) return $this->error('参数错误');
- $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
- if (empty($user)) return $this->error('会员不存在');
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'award_type' => 1];
- $params = [
- 'select' => 'user_id,video_id,award_money,created_at',
- 'where' => $wheres,
- 'order' => 'created_at desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $browse_rewards = ShortVideoBrowseAward::listPage($params);
- if (empty($browse_rewards['list'])) {
- if (is_array($browse_rewards['list'])) {
- return $this->success('success', $browse_rewards);
- } else {
- return $this->error(ShortVideoBrowseAward::getStaticError());
- }
- }
- $user_ids = array_column($browse_rewards['list'], 'user_id');
- $users = User::find()
- ->select('id,nickname,avatar_url')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($browse_rewards['list'] as &$reward) {
- $user = $users[$reward['user_id']] ?? [];
- $reward['nickname'] = $user['nickanme'] ?? '默认会员';
- $reward['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- }
- return $this->success('success', $browse_rewards);
- }
- public function actionIncomeSummary()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- if (empty($user_id)) return $this->error('参数错误');
- $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
- if (empty($user)) return $this->error('会员不存在');
- // 观看收益 start
- $browse_incomes_query = ShortVideoBrowseAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'award_type' => 1]);
- $browse_incomes_query_all = clone $browse_incomes_query;
- $browse_incomes_query_today = clone $browse_incomes_query;
- $browse_incomes_all = $browse_incomes_query_all->sum('award_money');
- $today = DateHelper::today();
- $browse_incomes_today = $browse_incomes_query_today
- ->andWhere(['>=', 'created_at', $today['start']])
- ->andWhere(['<=', 'created_at', $today['end']])
- ->sum('award_money');
- // 观看收益 end
- // 带货奖励 start
- $bought_incomes_query = ShortVideoOrderAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'is_award' => 1]);
- $bought_incomes_query_all = clone $bought_incomes_query;
- $bought_incomes_query_today = clone $bought_incomes_query;
- $bought_incomes_query_not_settlement = clone $bought_incomes_query;
- $bought_incomes_query_settlement = clone $bought_incomes_query;
- $bought_incomes_all = $bought_incomes_query_all->sum('award_money');
- $bought_incomes_today = $bought_incomes_query_today
- ->andWhere(['is_settlement' => 1])
- ->andWhere(['>=', 'created_at', $today['start']])
- ->andWhere(['<=', 'created_at', $today['end']])
- ->sum('award_money');
- $bought_incomes_not_settlement = $bought_incomes_query_not_settlement
- ->andWhere(['is_settlement' => 0])
- ->sum('award_money');
- $bought_incomes_settlement = $bought_incomes_query_settlement
- ->andWhere(['is_settlement' => 1])
- ->sum('award_money');
- // 带货奖励 end
- $income_all = bcadd($browse_incomes_all, $bought_incomes_all, 2);
- $income_today = bcadd($browse_incomes_today, $bought_incomes_today, 2);
- $income_not_settlement = round($bought_incomes_not_settlement, 2);
- $income_settlement = bcadd($browse_incomes_all, $bought_incomes_settlement, 2);
- $user_info = [
- 'user_id' => $user_id,
- 'nickname' => $user->nickname ?? '默认会员',
- 'level' => $user->level ?? -1,
- 'level_name' => $user->userLevel->name,
- 'avatar_url' => $user->avatar_url ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png',
- ];
- // dd($browse_incomes_all, $bought_incomes_all, $bought_incomes_not_settlement, $browse_incomes_all, $bought_incomes_settlement);
- return $this->success('success', compact('income_all', 'income_today', 'income_not_settlement', 'income_settlement', 'user_info'));
- }
- }
|