ActivityController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace addons\BusinessActivity\backend\controllers;
  3. use addons\BusinessActivity\common\service\ActivityService;
  4. use common\models\user\User;
  5. use common\models\user\UserLevel;
  6. use Yii;
  7. use common\enums\StatusEnum;
  8. use common\models\common\MallSetting;
  9. use common\models\goods\Goods;
  10. use Exception;
  11. /**
  12. *
  13. * Class ActivityController
  14. * @package addons\BusinessActivity\backend\controllers
  15. */
  16. class ActivityController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. /**
  20. * 首页
  21. *
  22. * @return string
  23. */
  24. public function actionIndex()
  25. {
  26. if (!\Yii::$app->request->isAjax) {
  27. return $this->render($this->action->id);
  28. }
  29. try {
  30. $params = \Yii::$app->request->get();
  31. $rules = [
  32. [['title', 'start_time', 'end_time'], 'string'],
  33. [['venue_user_id', 'merchants_user_id', 'page', 'pagesize'], 'integer'],
  34. ];
  35. $res = Yii::$app->services->validate->validate($params, $rules);
  36. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  37. $service = new ActivityService();
  38. return $this->success('操作成功', $service->page($this->mall_id, $params, $this->mall));
  39. } catch (\Exception $e) {
  40. return $this->success($e->getMessage());
  41. }
  42. }
  43. public function actionEdit()
  44. {
  45. if (!\Yii::$app->request->isAjax) {
  46. return $this->render($this->action->id);
  47. }
  48. try {
  49. if (\Yii::$app->request->isGet) {
  50. $params = \Yii::$app->request->get();
  51. $id = $params['id'];
  52. $service = new ActivityService();
  53. return $this->success('成功', $service->detail($this->mall_id, $id));
  54. }
  55. if (\Yii::$app->request->isPost) {
  56. $params = \Yii::$app->request->post();
  57. $rules = [
  58. [['name', 'start_at', 'end_at'], 'string'],
  59. [['id', 'commission_type', 'is_alone_commission'], 'integer'],
  60. [['goods_info', 'merchants', 'venue'], 'safe']
  61. ];
  62. $res = Yii::$app->services->validate->validate($params, $rules);
  63. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  64. $service = new ActivityService();
  65. $params['mall_id'] = $this->mall_id;
  66. if (empty($params['venue'])) throw new Exception("场地负责人不能为空");
  67. if (empty($params['merchants'])) throw new Exception("招商负责人不能为空");
  68. if (!$service->write($params)) throw new Exception($service->getError());
  69. return $this->success('保存成功');
  70. }
  71. } catch (\Exception $e) {
  72. return $this->error($e->getMessage());
  73. }
  74. }
  75. public function actionGoodsSearch()
  76. {
  77. if (\Yii::$app->request->isAjax) {
  78. if (\Yii::$app->request->isGet) {
  79. //商品列表筛选
  80. $get = Yii::$app->request->get();
  81. $rules = [
  82. [['keyword'], 'string'],
  83. [['activity_id'], 'integer']
  84. ];
  85. $res = Yii::$app->services->validate->validate($get, $rules);
  86. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  87. $where = [['mall_id' => $this->mall['id'], 'mch_id' => $this->admin->mch_id, 'is_on_sale' => 1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  88. // 筛选
  89. $where[] = ['!=', 'goods_name', '代客下单插件商品'];
  90. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  91. $service = new ActivityService();
  92. $goods_ids = $service->getGoodsIds(['mall_id' => $this->mall_id, 'activity_id' => $get['activity_id']]);
  93. if ($goods_ids) $where[] = ['NOT IN', 'id', $goods_ids];
  94. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  95. $params = ['where' => $where, 'with' => ['attr'], 'limit' => 10, 'order' => $sort, 'group' => 'id'];
  96. $page_data = Goods::listPage($params, false, true);
  97. if ($page_data === false) return $this->error(Goods::getStaticError());
  98. return $this->success('操作成功', $page_data);
  99. }
  100. }
  101. }
  102. public function actionSwitch()
  103. {
  104. try {
  105. if (!\Yii::$app->request->isPost) throw new Exception('不支持该请求方式');
  106. $params = \Yii::$app->request->post();
  107. $rules = [
  108. [['id', 'status'], 'integer'],
  109. [['id', 'status'], 'required'],
  110. ];
  111. $res = Yii::$app->services->validate->validate($params, $rules);
  112. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  113. $service = new ActivityService();
  114. if (!$service->switch($this->mall_id, $params['id'], $params['status'])) throw new Exception($service->getError());
  115. return $this->success('操作成功');
  116. } catch (\Exception $e) {
  117. return $this->error($e->getMessage());
  118. }
  119. }
  120. public function actionDel()
  121. {
  122. try {
  123. if (!\Yii::$app->request->isPost) throw new Exception('不支持该请求方式');
  124. $params = \Yii::$app->request->post();
  125. $rules = [
  126. [['id'], 'integer'],
  127. [['id'], 'required'],
  128. ];
  129. $res = Yii::$app->services->validate->validate($params, $rules);
  130. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  131. $service = new ActivityService();
  132. if (!$service->del($this->mall_id, $params['id'])) throw new Exception($service->getError());
  133. return $this->success('删除成功');
  134. } catch (\Exception $e) {
  135. return $this->error($e->getMessage());
  136. }
  137. }
  138. public function actionSigninList()
  139. {
  140. try {
  141. $params = \Yii::$app->request->get();
  142. $rules = [
  143. [['activity_id', 'page', 'user_id', 'limit'], 'integer'],
  144. ];
  145. $res = Yii::$app->services->validate->validate($params, $rules);
  146. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  147. $service = new ActivityService();
  148. $params['mall_id'] = $this->mall_id;
  149. $list = $service->getSigninList($params);
  150. return $this->success('请求成功', $list);
  151. } catch (\Exception $e) {
  152. return $this->error($e->getMessage());
  153. }
  154. }
  155. /**
  156. * 获取海报二维码
  157. * @return mixed|null
  158. */
  159. public function actionPosterQrcode()
  160. {
  161. try {
  162. $params = \Yii::$app->request->get();
  163. $rules = [
  164. [['id'], 'integer'],
  165. ];
  166. $res = Yii::$app->services->validate->validate($params, $rules);
  167. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  168. $ret = (new ActivityService())->getPosterQrCodeUrl($this->mall, $params['id']);
  169. return $this->success('获取成功', $ret);
  170. } catch (Exception $e) {
  171. return $this->error($e->getMessage());
  172. }
  173. }
  174. public function actionGetCanBindInviter()
  175. {
  176. if (\Yii::$app->request->isAjax) {
  177. $params = \Yii::$app->request->get();
  178. $where = [];
  179. $where[] = ['mall_id' => $this->mall_id];
  180. if (!empty($params['inviter_id'])) $where[] = ['id' => $params['inviter_id']];
  181. if (!empty($params['keyword'])) {
  182. $where[] = ['or', ['like', 'mobile', $params['keyword']], ['like', 'nickname', $params['keyword']]];
  183. }
  184. $user_list = User::listPage(['where' => $where]);
  185. if (!empty($user_list['list'])) {
  186. $user_level_arr = UserLevel::getUserLevelArr($params['mall_id']);
  187. foreach ($user_list['list'] as &$v) {
  188. $v['level_name'] = !empty($user_level_arr[$v['level']]) ? $user_level_arr[$v['level']] : MallSetting::getDefaultLevelName($this->mall_id);
  189. }
  190. }
  191. return $this->success('操作成功', ['list' => $user_list]);
  192. }
  193. }
  194. }