DefaultController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace addons\CircleDistribution\backend\controllers;
  3. use addons\CircleDistribution\common\models\Distribution;
  4. use common\enums\DownloadEnum;
  5. use common\enums\StatusEnum;
  6. use common\helpers\csv\CsvExportHelper;
  7. use common\models\user\User;
  8. use Yii;
  9. /**
  10. * 默认控制器
  11. *
  12. * Class DefaultController
  13. * @package addons\CircleDistribution\backend\controllers
  14. */
  15. class DefaultController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. public $from_type = ['Distribution'];
  19. /**
  20. * @desc:首页
  21. * @Author: hua
  22. * @Date: 2021/10/23
  23. * @Time: 18:17
  24. * @Copyright: copyright (c) 2021 广东七件事集团
  25. * @return mixed|string|void
  26. * @throws \Exception
  27. */
  28. public function actionIndex()
  29. {
  30. if (\Yii::$app->request->isAjax) {
  31. if (\Yii::$app->request->isGet) {
  32. $get = \Yii::$app->request->get();
  33. $get['mall_id'] = $this->mall_id;
  34. $params = [];
  35. $user_id_arr = [];
  36. if (isset($get['level']) && !empty($get['level'])) $params['where'][] = ['=', 'level', $get['level']];
  37. $is_search = 0;
  38. if (!empty($get['user_id'])) {
  39. $is_search = 1;
  40. $user_id_arr[] = $get['user_id'];
  41. }
  42. if (!empty($get['keyword'])) {
  43. $is_search = 1;
  44. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  45. 'or',
  46. ['like', 'username', $get['keyword']],
  47. ['like', 'nickname', $get['keyword']],
  48. ['like', 'mobile', $get['keyword']]
  49. ]], 'select' => 'id']);
  50. $user_ids = array_column($user_list, 'id');
  51. if (!empty($user_id_arr)) {
  52. $user_id_arr = array_intersect($user_id_arr, $user_ids);
  53. } else {
  54. $user_id_arr = $user_ids;
  55. }
  56. }
  57. if ($is_search) $params['where'][] = ['in', 'user_id', $user_id_arr];
  58. $params['where'][] = ['=', 'mall_id', $this->mall_id];
  59. $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  60. $params['order'] = 'id desc';
  61. $params['limit'] = 10;
  62. $pageData = Distribution::listPage($params, false);
  63. if ($pageData === false) return $this->error(Distribution::getStaticError());
  64. if (!empty($pageData['list'])) {
  65. $user_id_arr = array_column($pageData['list'], 'user_id');
  66. $user_list = $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'select' => 'id,nickname,avatar_url,mobile,parent_id']);
  67. $user_arr = $parent_arr = [];
  68. $parent_id_arr = [];
  69. foreach ($user_list as $item) {
  70. $parent_id_arr[] = $item['parent_id'];
  71. $user_arr[$item['id']] = $item;
  72. }
  73. $user_parent_list = $user_list = User::lists(['where' => [['id' => $parent_id_arr]], 'select' => 'id,nickname,avatar_url']);
  74. foreach ($user_parent_list as $item) {
  75. $parent_arr[$item['id']] = $item;
  76. }
  77. foreach ($pageData['list'] as &$v) {
  78. $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = '';
  79. if (isset($user_arr[$v['user_id']])) {
  80. $v['mobile'] = $user_arr[$v['user_id']]['mobile'];
  81. $v['nickname'] = $user_arr[$v['user_id']]['nickname'];
  82. $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url'];
  83. $v['parent_id'] = $user_arr[$v['user_id']]['parent_id'];
  84. if (isset($parent_arr[$v['parent_id']])) $v['parent_name'] = $parent_arr[$v['parent_id']]['nickname'];
  85. }
  86. }
  87. }
  88. $wheres = [];
  89. $wheres[] = ['mall_id' => $this->mall_id];
  90. $wheres[] = ['status' => StatusEnum::ENABLED];
  91. $pageData['export_list'] = Distribution::exportFieldsList();
  92. return $this->success('操作成功', $pageData);
  93. }
  94. }
  95. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  96. $post = \Yii::$app->request->post();
  97. $post['mall_id'] = $this->mall_id;
  98. $filename = '分销商列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  99. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_DISTRIBUTION, Distribution::class, 'exportHandle', $post);
  100. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  101. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  102. }
  103. return $this->render('index', []);
  104. }
  105. /**
  106. * @desc:编辑
  107. * @Author: hua
  108. * @Date: 2021/10/23
  109. * @Time: 18:17
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @return mixed|void
  112. */
  113. public function actionEdit()
  114. {
  115. if (\Yii::$app->request->isAjax) {
  116. if (\Yii::$app->request->isPost) {
  117. $params = \Yii::$app->request->post();
  118. $res = Yii::$app->services->validate->validate($params, [
  119. [['user_id'], 'required'],
  120. [['id'], 'safe'],
  121. ]);
  122. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  123. $wheres = [
  124. 'user_id' => $params['user_id'],
  125. 'mall_id' => $this->mall_id,
  126. 'status' => StatusEnum::ENABLED,
  127. ];
  128. $model = Distribution::findOne($wheres);
  129. if (!empty($model)) return $this->error('该会员已经是分销商,不能重复添加');
  130. $params['mall_id'] = $this->mall_id;
  131. $params['status'] = StatusEnum::ENABLED;
  132. $res = Distribution::setData($params);
  133. if ($res === false) return $this->error(Distribution::getStaticError());
  134. return $this->success('操作成功');
  135. }
  136. }
  137. return $this->error();
  138. }
  139. /**
  140. * @desc:修改备注
  141. * @Author: hua
  142. * @Date: 2021/10/29
  143. * @Time: 10:34
  144. * @Copyright: copyright (c) 2021 广东七件事集团
  145. * @return mixed|void
  146. */
  147. public function actionRemarksEdit()
  148. {
  149. if (\Yii::$app->request->isAjax) {
  150. if (\Yii::$app->request->isPost) {
  151. $params = \Yii::$app->request->post();
  152. $res = Yii::$app->services->validate->validate($params, [
  153. [['id'], 'required'],
  154. [['remarks'], 'safe']
  155. ]);
  156. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  157. $params['mall_id'] = $this->mall_id;
  158. $res = Distribution::setData($params);
  159. if ($res === false) return $this->error(Distribution::getStaticError());
  160. return $this->success('操作成功');
  161. }
  162. }
  163. return $this->error();
  164. }
  165. /**
  166. * @desc:
  167. * @Author: hua
  168. * @Date: 2021/10/29
  169. * @Time: 10:37
  170. * @Copyright: copyright (c) 2021 广东七件事集团
  171. * @return mixed|void
  172. */
  173. public function actionSearchUser()
  174. {
  175. if (\Yii::$app->request->isAjax) {
  176. if (!\Yii::$app->request->isGet) return $this->error('请求方式错误');
  177. $keyword = \Yii::$app->request->get('keyword');
  178. $params = [];
  179. $where[] = ['mall_id' => $this->mall_id];
  180. $where[] = ['status' => StatusEnum::ENABLED];
  181. if (!empty($keyword)) $where[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword], ['like', 'id', $keyword]];
  182. $params['where'] = $where;
  183. $params['select'] = 'id,nickname,mobile,username,avatar_url';
  184. $params['limit'] = 20;
  185. $list = User::lists($params);
  186. if (!empty($list)) {
  187. foreach ($list as &$item) {
  188. if (empty($item['nickname'])) $item['nickname'] = $item['mobile'] . "(id:{$item['id']})";
  189. }
  190. }
  191. return $this->success('操作成功', ['list' => $list]);
  192. }
  193. }
  194. /**
  195. * @desc:
  196. * @Author: hua
  197. * @Date: 2021/11/12
  198. * @Time: 12:19
  199. * @Copyright: copyright (c) 2021 广东七件事集团
  200. * @return mixed|void
  201. */
  202. public function actionLevelChange()
  203. {
  204. try {
  205. if (\Yii::$app->request->isAjax) {
  206. // $post = \Yii::$app->request->post();
  207. // $post['mall_id'] = $this->mall_id;
  208. // $post['upgrade_status'] = Distribution::UPGRADE_STATUS_MANUAL;
  209. // $res = Distribution::setData($post);
  210. // if ($res == false) throw new \Exception(Distribution::getStaticError());
  211. // return $this->success('修改成功');
  212. }
  213. } catch (\Exception $exception) {
  214. return $this->error($exception->getMessage() . $exception->getLine() . $exception->getFile());
  215. }
  216. }
  217. /**
  218. * @desc:
  219. * @Author: hua
  220. * @Date: 2021/10/29
  221. * @Time: 10:31
  222. * @Copyright: copyright (c) 2021 广东七件事集团
  223. * @return mixed|void
  224. */
  225. public function actionDelete()
  226. {
  227. if (\Yii::$app->request->isAjax) {
  228. if (!\Yii::$app->request->isPost) return $this->error('请求方式错误');
  229. $params = \Yii::$app->request->post();
  230. $res = Yii::$app->services->validate->validate($params, [
  231. [['id'], 'required'],
  232. [['id'], 'integer']
  233. ]);
  234. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  235. $params['mall_id'] = $this->mall_id;
  236. $params['status'] = StatusEnum::DELETE;
  237. $res = Distribution::setData($params);
  238. if ($res) return $this->success('删除成功');
  239. return $this->error(Distribution::getStaticError());
  240. }
  241. }
  242. }