UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace addons\ShortVideo\api\modules\v1\controllers;
  3. use addons\ShortVideo\common\models\ShortVideo;
  4. use addons\ShortVideo\common\models\ShortVideoBrowseAward;
  5. use addons\ShortVideo\common\models\ShortVideoFollow;
  6. use addons\ShortVideo\common\models\ShortVideoOrderAward;
  7. use common\enums\StatusEnum;
  8. use common\helpers\DateHelper;
  9. use common\models\goods\Goods;
  10. use common\models\order\Order;
  11. use common\models\user\User;
  12. /**
  13. * 会员控制器
  14. *
  15. * Class DefaultController
  16. * @package addons\ShortVideo\api\modules\v1\controllers
  17. */
  18. class UserController extends BaseController
  19. {
  20. public $modelClass = '';
  21. /**
  22. * 不用进行登录验证的方法
  23. *
  24. * 例如: ['index', 'update', 'create', 'view', 'delete']
  25. * 默认全部需要验证
  26. *
  27. * @var array
  28. */
  29. protected $authOptional = [''];
  30. /**
  31. * @name:
  32. * @msg: 关注/取消关注会员(废弃,该功能使用会员模块公共接口)
  33. * @param {*}
  34. * @return {*}
  35. */
  36. public function actionFollow()
  37. {
  38. if (!\Yii::$app->request->isGet) {
  39. return $this->error('请求方式错误');
  40. }
  41. $user_id = \Yii::$app->request->get('user_id');
  42. $type = \Yii::$app->request->get('type');
  43. if (empty($user_id) || (empty($type) || !in_array($type, ['follow', 'unfollow']))) return $this->error('参数错误');
  44. if ($user_id == $this->user_id) return $this->error('不可以关注自己');
  45. $follow_user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
  46. if (empty($follow_user)) return $this->error('关注的会员不存在');
  47. $op = '关注';
  48. $params = [
  49. 'user_id' => $this->user_id,
  50. 'follow_id' => $user_id,
  51. 'status' => StatusEnum::ENABLED,
  52. ];
  53. if ('unfollow' == $type) {
  54. $params['status'] = StatusEnum::DELETE;
  55. $op = '取消关注';
  56. }
  57. $res = ShortVideoFollow::setData($this->mall_id, $params);
  58. if (false === $res) return $this->error($op . '失败:' . ShortVideoFollow::getStaticError());
  59. return $this->success($op . '成功');
  60. }
  61. /**
  62. * @name:
  63. * @msg: 关注列表,包括我关注别人的列表和别人关注我的列表(废弃,该功能使用会员模块公共接口)
  64. * @param {integer} $user_id 会员id
  65. * @param {string} $type 列表类型,follow:我关注别人的列表;fans:别人关注我的列表
  66. * @return {*}
  67. */
  68. public function actionFollowList()
  69. {
  70. if (!\Yii::$app->request->isGet) {
  71. return $this->error('请求方式错误');
  72. }
  73. $user_id = \Yii::$app->request->get('user_id');
  74. $type = \Yii::$app->request->get('type');
  75. if (empty($user_id) || (empty($type) || !in_array($type, ['follow', 'fans']))) return $this->error('参数错误');
  76. $page = \Yii::$app->request->get('page', 1);
  77. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  78. $list = ShortVideoFollow::getList($this->mall_id, $user_id, $type, $page, $limit);
  79. return $this->success('success', $list);
  80. }
  81. /**
  82. * @name:
  83. * @msg: 会员带货奖励
  84. * @param {integer} $user_id 会员id
  85. * @return {*}
  86. */
  87. public function actionBoughtRewardList()
  88. {
  89. if (!\Yii::$app->request->isGet) {
  90. return $this->error('请求方式错误');
  91. }
  92. $user_id = \Yii::$app->request->get('user_id');
  93. $page = \Yii::$app->request->get('page', 1);
  94. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  95. if (empty($user_id)) return $this->error('参数错误');
  96. $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
  97. if (empty($user)) return $this->error('会员不存在');
  98. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'is_settlement' => 1, 'is_award' => 1];
  99. $params = [
  100. 'select' => 'user_id,order_id,video_id,goods_id,award_money,created_at',
  101. 'where' => $wheres,
  102. 'order' => 'settlement_time desc,created_at desc',
  103. 'page' => $page,
  104. 'limit' => $limit,
  105. ];
  106. $order_rewards = ShortVideoOrderAward::listPage($params);
  107. if (false === $order_rewards['list']) {
  108. return $this->error(ShortVideoOrderAward::getStaticError());
  109. }
  110. $user_ids = array_column($order_rewards['list'], 'user_id');
  111. $users = User::find()
  112. ->select('id,nickname,avatar_url')
  113. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
  114. ->asArray()
  115. ->indexBy('id')
  116. ->all();
  117. $order_ids = array_column($order_rewards['list'], 'order_id');
  118. $orders = Order::find()
  119. ->select('id,order_no,pay_money')
  120. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids])
  121. ->asArray()
  122. ->indexBy('id')
  123. ->all();
  124. $good_ids = array_column($order_rewards['list'], 'goods_id');
  125. $goods = Goods::find()
  126. ->select('id,goods_name,cover_pic,price')
  127. ->where(['mall_id' => $this->mall_id, 'id' => $good_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1])
  128. ->asArray()
  129. ->indexBy('id')
  130. ->all();
  131. foreach ($order_rewards['list'] as &$reward) {
  132. $user = $users[$reward['user_id']] ?? [];
  133. $order = $orders[$reward['order_id']] ?? [];
  134. $good = $goods[$reward['goods_id']] ?? [];
  135. $reward['nickname'] = $user['nickanme'] ?? '默认会员';
  136. $reward['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  137. $reward['order_no'] = $order['order_no'] ?? '';
  138. $reward['pay_money'] = $order['pay_money'] ?? 0;
  139. unset($good['id']);
  140. $reward = array_merge($reward, $good);
  141. }
  142. return $this->success('success', $order_rewards);
  143. }
  144. /**
  145. * @name:
  146. * @msg: 观看视频奖励列表
  147. * @param {integer} $user_id 会员id
  148. * @return {*}
  149. */
  150. public function actionBrowseRewardList()
  151. {
  152. if (!\Yii::$app->request->isGet) {
  153. return $this->error('请求方式错误');
  154. }
  155. $user_id = \Yii::$app->request->get('user_id');
  156. $page = \Yii::$app->request->get('page', 1);
  157. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  158. if (empty($user_id)) return $this->error('参数错误');
  159. $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
  160. if (empty($user)) return $this->error('会员不存在');
  161. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'award_type' => 1];
  162. $params = [
  163. 'select' => 'user_id,video_id,award_money,created_at',
  164. 'where' => $wheres,
  165. 'order' => 'created_at desc',
  166. 'page' => $page,
  167. 'limit' => $limit,
  168. ];
  169. $browse_rewards = ShortVideoBrowseAward::listPage($params);
  170. if (empty($browse_rewards['list'])) {
  171. if (is_array($browse_rewards['list'])) {
  172. return $this->success('success', $browse_rewards);
  173. } else {
  174. return $this->error(ShortVideoBrowseAward::getStaticError());
  175. }
  176. }
  177. $user_ids = array_column($browse_rewards['list'], 'user_id');
  178. $users = User::find()
  179. ->select('id,nickname,avatar_url')
  180. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
  181. ->asArray()
  182. ->indexBy('id')
  183. ->all();
  184. foreach ($browse_rewards['list'] as &$reward) {
  185. $user = $users[$reward['user_id']] ?? [];
  186. $reward['nickname'] = $user['nickanme'] ?? '默认会员';
  187. $reward['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  188. }
  189. return $this->success('success', $browse_rewards);
  190. }
  191. public function actionIncomeSummary()
  192. {
  193. if (!\Yii::$app->request->isGet) {
  194. return $this->error('请求方式错误');
  195. }
  196. $user_id = \Yii::$app->request->get('user_id');
  197. if (empty($user_id)) return $this->error('参数错误');
  198. $user = User::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id]);
  199. if (empty($user)) return $this->error('会员不存在');
  200. // 观看收益 start
  201. $browse_incomes_query = ShortVideoBrowseAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'award_type' => 1]);
  202. $browse_incomes_query_all = clone $browse_incomes_query;
  203. $browse_incomes_query_today = clone $browse_incomes_query;
  204. $browse_incomes_all = $browse_incomes_query_all->sum('award_money');
  205. $today = DateHelper::today();
  206. $browse_incomes_today = $browse_incomes_query_today
  207. ->andWhere(['>=', 'created_at', $today['start']])
  208. ->andWhere(['<=', 'created_at', $today['end']])
  209. ->sum('award_money');
  210. // 观看收益 end
  211. // 带货奖励 start
  212. $bought_incomes_query = ShortVideoOrderAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'is_award' => 1]);
  213. $bought_incomes_query_all = clone $bought_incomes_query;
  214. $bought_incomes_query_today = clone $bought_incomes_query;
  215. $bought_incomes_query_not_settlement = clone $bought_incomes_query;
  216. $bought_incomes_query_settlement = clone $bought_incomes_query;
  217. $bought_incomes_all = $bought_incomes_query_all->sum('award_money');
  218. $bought_incomes_today = $bought_incomes_query_today
  219. ->andWhere(['is_settlement' => 1])
  220. ->andWhere(['>=', 'created_at', $today['start']])
  221. ->andWhere(['<=', 'created_at', $today['end']])
  222. ->sum('award_money');
  223. $bought_incomes_not_settlement = $bought_incomes_query_not_settlement
  224. ->andWhere(['is_settlement' => 0])
  225. ->sum('award_money');
  226. $bought_incomes_settlement = $bought_incomes_query_settlement
  227. ->andWhere(['is_settlement' => 1])
  228. ->sum('award_money');
  229. // 带货奖励 end
  230. $income_all = bcadd($browse_incomes_all, $bought_incomes_all, 2);
  231. $income_today = bcadd($browse_incomes_today, $bought_incomes_today, 2);
  232. $income_not_settlement = round($bought_incomes_not_settlement, 2);
  233. $income_settlement = bcadd($browse_incomes_all, $bought_incomes_settlement, 2);
  234. $user_info = [
  235. 'user_id' => $user_id,
  236. 'nickname' => $user->nickname ?? '默认会员',
  237. 'level' => $user->level ?? -1,
  238. 'level_name' => $user->userLevel->name,
  239. 'avatar_url' => $user->avatar_url ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png',
  240. ];
  241. // dd($browse_incomes_all, $bought_incomes_all, $bought_incomes_not_settlement, $browse_incomes_all, $bought_incomes_settlement);
  242. return $this->success('success', compact('income_all', 'income_today', 'income_not_settlement', 'income_settlement', 'user_info'));
  243. }
  244. }