SharerController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace addons\WechatVideoShop\backend\controllers;
  3. use addons\WechatVideoShop\common\models\WechatVideoShopLevel;
  4. use addons\WechatVideoShop\common\models\WechatVideoShopSharer;
  5. use addons\WechatVideoShop\common\service\SharerService;
  6. use addons\WechatVideoShop\common\service\ShopService;
  7. use common\enums\StatusEnum;
  8. use common\models\user\User;
  9. use common\models\user\UserLevel;
  10. use Yii;
  11. class SharerController extends BaseController
  12. {
  13. public $enableCsrfValidation = false;
  14. /**
  15. * 售后列表
  16. * @return string
  17. */
  18. public function actionIndex()
  19. {
  20. return $this->render($this->action->id);
  21. }
  22. public function actionList()
  23. {
  24. if (\Yii::$app->request->isAjax) {
  25. if (\Yii::$app->request->isGet) {
  26. // 取出所有分销员
  27. $data = Yii::$app->request->get();
  28. $valid = Yii::$app->services->validate->validate($data, [
  29. [['page', 'limit', 'sharer_type', 'shop_id', 'level', 'status'], 'integer'], // 需要取本地的数据
  30. [['member', 'invite_start_date', 'invite_end_date', 'bind_start_date', 'bind_end_date'], 'string']
  31. ]);
  32. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  33. try {
  34. $sharer_list = (new SharerService())->page($this->mall_id, $data);
  35. // 获取所有店铺键值对
  36. $ret['list'] = $sharer_list;
  37. $ret['shop'] = (new ShopService())->ShopArr($this->mall_id);
  38. $ret['levels'] = WechatVideoShopLevel::getLevelByColumn($this->mall_id);
  39. $ret['is_async'] = WechatVideoShopLevel::getLevelByColumn($this->mall_id);
  40. return $this->success('获取成功', $ret);
  41. } catch (\Exception $e) {
  42. return $this->error($e->getMessage());
  43. }
  44. }
  45. }
  46. return $this->render($this->action->id);
  47. }
  48. /**
  49. * 绑定
  50. * @return mixed|null
  51. */
  52. public function actionBind()
  53. {
  54. // 取出所有分销员
  55. $data = Yii::$app->request->post();
  56. try {
  57. $ret = (new SharerService())->bind($this->mall_id, $data);
  58. return is_string($ret) ? $this->error($ret) : $this->success('绑定成功');
  59. } catch (\Exception $e) {
  60. return $this->error($e->getMessage());
  61. }
  62. }
  63. /**
  64. * 解绑
  65. * @return mixed|null
  66. */
  67. public function actionUnbind()
  68. {
  69. // 取出所有分销员
  70. $data = Yii::$app->request->post();
  71. try {
  72. $ret = (new SharerService())->unbind($this->mall_id, $data);
  73. return is_string($ret) ? $this->error($ret) : $this->success('解绑成功');
  74. } catch (\Exception $e) {
  75. return $this->error($e->getMessage());
  76. }
  77. }
  78. public function actionManual()
  79. {
  80. return $this->render('manual');
  81. }
  82. /**
  83. * @return mixed|null
  84. */
  85. public function actionAsync()
  86. {
  87. try {
  88. (new SharerService())->async();
  89. return $this->success("同步进行中...");
  90. } catch (\Exception $e) {
  91. return $this->error($e->getMessage());
  92. }
  93. }
  94. /**
  95. * @return mixed|null {*}
  96. * @msg: 获取会员列表
  97. */
  98. public function actionGetUsers()
  99. {
  100. if (!\Yii::$app->request->isGet) {
  101. return $this->error('请求方式错误');
  102. }
  103. $user_id = \Yii::$app->request->get('user_id');
  104. $keyword = \Yii::$app->request->get('keyword');
  105. $page = \Yii::$app->request->get('page', 1);
  106. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  107. $store_user_ids = WechatVideoShopSharer::find()->select('user_id')
  108. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
  109. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  110. $wheres[] = ['not in', 'id', $store_user_ids];
  111. if (!empty($user_id)) {
  112. $wheres[] = ['id' => $user_id];
  113. }
  114. if (!empty($keyword)) {
  115. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  116. }
  117. $params = [
  118. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
  119. 'where' => $wheres,
  120. 'order' => 'created_at desc',
  121. 'page' => $page,
  122. 'limit' => $limit,
  123. ];
  124. $user_list = User::listPage($params);
  125. if (empty($user_list['list'])) {
  126. if (false === $user_list['list']) {
  127. return $this->error(User::getStaticError());
  128. }
  129. } else {
  130. $levels = UserLevel::find()
  131. ->select('id,level,name')
  132. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  133. ->asArray()
  134. ->indexBy('level')
  135. ->all();
  136. $parent_ids = array_column($user_list['list'], 'parent_id');
  137. $parent_users = User::find()
  138. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  139. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  140. ->asArray()
  141. ->indexBy('id')
  142. ->all();
  143. foreach ($user_list['list'] as &$user) {
  144. if (empty($user['avatar_url'])) {
  145. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  146. }
  147. $level = $levels[$user['level']] ?? [];
  148. $parent_user = $parent_users[$user['parent_id']] ?? [];
  149. $user['level_name'] = $level['name'] ?? '';
  150. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  151. }
  152. }
  153. return $this->success('success', $user_list);
  154. }
  155. /**
  156. * 客户列表
  157. * @return mixed|null
  158. */
  159. public function actionCustomer()
  160. {
  161. $data = Yii::$app->request->get();
  162. $valid = Yii::$app->services->validate->validate($data, [
  163. [['page', 'limit', 'user_id', 'parent_id'], 'integer'], // 需要取本地的数据
  164. [['keyword', 'start_date', 'end_date'], 'string']
  165. ]);
  166. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  167. $ret = (new SharerService())->customerList($this->mall_id, $data);
  168. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  169. }
  170. }