MallController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\controllers\AdminController;
  4. use backend\modules\common\services\MallService;
  5. use common\enums\RabbitMqEnum;
  6. use common\enums\StatusEnum;
  7. use common\logic\common\MallCreateSync;
  8. use common\models\addons\AddonsCombo;
  9. use common\models\addons\MallAddonsComboRelation;
  10. use common\models\backend\Admin;
  11. use common\models\common\PlatformSetting;
  12. use common\models\mall\Mall;
  13. use common\models\mall\MallAddons;
  14. use common\models\mall\MallHost;
  15. use Yii;
  16. /**
  17. * 商城控制器
  18. *
  19. * @Author bing
  20. * @DateTime 2021-08-13 15:29:41
  21. * @copyright: Copyright (c) 2020 广东七件事集团
  22. */
  23. class MallController extends AdminController
  24. {
  25. public function actionIndex($keyword = null, $is_recycle = 0)
  26. {
  27. $request = Yii::$app->request;
  28. if ($request->isAjax) {
  29. $admin = Yii::$app->user->identity;
  30. $admin_type = $admin->admin_type;
  31. $params = $request->get();
  32. if (!in_array($admin_type, [Admin::ADMIN_TYPE_SUPER, Admin::ADMIN_TYPE_AGENT])) {
  33. return $this->error('管理员类型不符合条件');
  34. }
  35. // 查询参数
  36. $where[] = ['status' => StatusEnum::ENABLED, 'is_recycle' => $is_recycle];
  37. // ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $where[] = ['admin_id' => $admin->id];
  38. (!empty(trim($keyword))) && $where[] = ['LIKE', 'name', $keyword];
  39. if(!empty($params['mobile'])){
  40. $admin_list = Admin::lists([
  41. 'where'=>[
  42. ['like','username',trim($params['mobile'])],
  43. ['is_default'=>1],
  44. ['status'=>StatusEnum::ENABLED]
  45. ]
  46. ]);
  47. $mall_ids = array_column($admin_list,'mall_id');
  48. $where[] = ['id'=>$mall_ids];
  49. }
  50. if(!empty($params['date'])){
  51. $where[] = ['<=', 'expired_at', strtotime($params['date']) + 86400];
  52. $where[] = ['>', 'expired_at', 0];
  53. }
  54. $with = [
  55. 'admin' => function ($query) {
  56. $query->select('id,username');
  57. },
  58. 'adonsComboRelation',
  59. 'mallHost'
  60. ];
  61. $params = array('where' => $where, 'with' => $with, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
  62. $page_data = Mall::listPage($params, false);
  63. // 获取总页数
  64. $page_data['page_count'] = Mall::getMallTotalPage($params);
  65. $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
  66. $system = PlatformSetting::getConfByGroup('system', true);
  67. $h5_url = $system['h5_url']['value']??'';
  68. if(!empty($page_data['list']))$mall_ids = array_column($page_data['list'],'id');
  69. if(!empty($mall_ids)){
  70. $admin_list = Admin::findAll(['mall_id'=>$mall_ids,'is_default'=>1]);
  71. $admin_arr = array_column($admin_list,'username','mall_id');
  72. }
  73. $mall_ids = [];
  74. foreach ($page_data['list'] as &$mall) {
  75. $mall['logo'] = Yii::$app->services->setting->mall->get($mall['id'], 'basic.logo');
  76. if(empty($mall['logo'])) $mall['logo'] = \Yii::$app->request->hostInfo.'/resources/img/admin/mall-logo.png';
  77. $mall_sign = $mall['mall_sign'];
  78. $account = $admin_arr[$mall['id']]??'';
  79. $password=$account?substr($account,-6,6):'';
  80. $mall_info = "
  81. 后台网址:".$login_url."&mall_sign={$mall_sign}
  82. 账号:{$account}
  83. H5网址:{$h5_url}/#/?sign={$mall_sign}";
  84. $mall['mall_info'] = $mall_info;
  85. $mall_ids[] = $mall['id'];
  86. }
  87. $page_data['admin_type'] = $admin_type;
  88. // 获取付费套餐
  89. $page_data['addons_combo'] = AddonsCombo::getAddonsCombo([],[],true,"id,title");
  90. $page_data['addons_combo_data'] = array_column($page_data['addons_combo'],'title','id');
  91. return $this->success('操作成功', $page_data);
  92. }
  93. $has_subdomain = (isset(Yii::$app->params['has_subdomain']) && Yii::$app->params['has_subdomain']) ? 1 : 0;
  94. return $this->render($this->action->id, ['has_subdomain' => $has_subdomain]);
  95. }
  96. public function actionTest(){
  97. dd($_SERVER);
  98. $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
  99. var_dump($login_url);
  100. }
  101. /**
  102. * 管理员后台进入商城
  103. * @Author bing
  104. * @DateTime 2021-08-19 09:48:32
  105. * @return void
  106. * @copyright: Copyright (c) 2020 广东七件事集团
  107. */
  108. public function actionEntry()
  109. {
  110. $mall_id = Yii::$app->request->post('mall_id');
  111. $admin = \Yii::$app->user->identity;
  112. $cond = ['id' => $mall_id, 'is_recycle' => 0, 'status' => StatusEnum::ENABLED];
  113. // ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $cond['admin_id'] = $admin->id;
  114. $mall = Mall::find()->where($cond)->asArray()->one();
  115. if(!empty($mall['expired_at'])&&time()>$mall['expired_at']) return $this->error('商城已过期');
  116. if (empty($mall)) return $this->error('商城不存在');
  117. Yii::$app->session->set('mall', $mall);
  118. $redirect_url = Yii::$app->urlManager->createUrl(['mall/overview/index']);
  119. $this->success('ok', compact('redirect_url'));
  120. }
  121. public function actionCreate()
  122. {
  123. try {
  124. $request = Yii::$app->request;
  125. if ($request->isAjax) {
  126. if ($request->isGet) {
  127. }
  128. }
  129. } catch (\Exception $e) {
  130. return $this->error($e->getMessage(), []);
  131. }
  132. try {
  133. $request = Yii::$app->request;
  134. if ($request->isAjax) {
  135. if ($request->isPost) {
  136. $params = Yii::$app->request->post();
  137. $res = Yii::$app->services->validate->validate($params, [
  138. [['name'], 'required'],
  139. [['id', 'logo', 'expired_at', 'password', 'mobile', 'addons_combo_id','is_show_copyright','install_addons','is_sync_mall_data',
  140. 'contact_tel','is_compliance'], 'safe'],
  141. ]);
  142. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  143. if(empty($params['id'])){
  144. $res = Admin::findAll(['mobile'=>$params['mobile'],'status' => StatusEnum::ENABLED]);
  145. if($res)return $this->error('手机号码已存在');
  146. }
  147. Yii::$app->user->identity->getId();
  148. $admin = Yii::$app->user->identity;
  149. if($admin['mall_num']!=-1 && empty($params['id'])){
  150. $counts = Mall::find()->where(['admin_id'=>$admin['id'],'status' => StatusEnum::ENABLED])->count();
  151. if($counts>=$admin['mall_num']) return $this->error('您只能创建'.$admin['mall_num'].'个商城');
  152. }
  153. if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']);
  154. // 获取操作用户
  155. $params['operator_id'] = $this->admin->id;
  156. $params['operator_username'] = $this->admin->account;
  157. if(empty($params['is_show_copyright'])) $params['is_show_copyright'] = 0;
  158. if(empty($params['is_sync_mall_data'])) $params['is_sync_mall_data'] = 0;
  159. if(empty($params['contact_tel'])) $params['contact_tel'] = '';
  160. $res = Mall::setData($params);
  161. if ($res === false) return $this->error(Mall::getStaticError());
  162. $mall_id=$res['id'];
  163. // 批量安装插件
  164. if (!empty($params['id']) && $admin->admin_type == Admin::ADMIN_TYPE_SUPER) {
  165. $install_addons = $params['install_addons'] ?? [];
  166. if($install_addons){
  167. $res = MallAddons::batchInstall($params['id'], $install_addons);
  168. if ($res === false) return $this->error(MallAddons::getStaticError());
  169. }
  170. }
  171. if(!empty($params['is_sync_mall_data'])){
  172. \Yii::$app->services->rabbitMq->delay(10, ['self_mall_id'=>$mall_id], MallCreateSync::class, 'syncMallData');
  173. }
  174. return $this->success('操作成功');
  175. }
  176. }
  177. } catch (\Exception $e) {
  178. return $this->error($e->getMessage(), []);
  179. }
  180. }
  181. /**
  182. * @desc:删除/恢复商城
  183. * @Author: hua
  184. * @Date: 2021/12/13
  185. * @Time: 9:31
  186. * @Copyright: copyright (c) 2021 广东七件事集团
  187. * @return mixed|void
  188. */
  189. public function actionUpdate()
  190. {
  191. try {
  192. $request = Yii::$app->request;
  193. if ($request->isAjax) {
  194. if ($request->isPost) {
  195. $params = Yii::$app->request->post();
  196. $res = Yii::$app->services->validate->validate($params, [
  197. [['is_recycle'], 'required'],
  198. [['id', ], 'safe'],
  199. ]);
  200. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  201. $post['id'] = $params['id'];
  202. $post['is_recycle'] = $params['is_recycle'];
  203. $post['status'] = $params['is_recycle'] == 1 ? StatusEnum::DELETE : StatusEnum::ENABLED;
  204. $post['is_delete'] = $params['is_recycle'] == 1 ? time() : 0;
  205. $post['is_disable'] = $params['is_recycle'] == 1 ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  206. $res = Mall::setData($post);
  207. if ($res === false) return $this->error(Mall::getStaticError());
  208. return $this->success('操作成功');
  209. }
  210. }
  211. } catch (\Exception $e) {
  212. return $this->error($e->getMessage(), []);
  213. }
  214. }
  215. /**
  216. * 回收站
  217. * @Author: lun
  218. * @DateTime: 2023/4/27 0027 17:40
  219. * @Copyright: copyright (c) 2021 广东七件事集团
  220. * @param null $keyword
  221. * @return mixed|string|void
  222. */
  223. public function actionRecovery($keyword = null)
  224. {
  225. $request = Yii::$app->request;
  226. if ($request->isAjax) {
  227. $admin = Yii::$app->user->identity;
  228. $admin_type = $admin->admin_type;
  229. $params = $request->get();
  230. if (!in_array($admin_type, [Admin::ADMIN_TYPE_SUPER, Admin::ADMIN_TYPE_AGENT])) {
  231. return $this->error('管理员类型不符合条件');
  232. }
  233. // 查询参数
  234. $where[] = ['status' => StatusEnum::DELETE, 'is_recycle' => StatusEnum::ENABLED, 'is_delete' => StatusEnum::DISABLED];
  235. ($admin->admin_type != Admin::ADMIN_TYPE_SUPER) && $where[] = ['admin_id' => $admin->id];
  236. (!empty(trim($keyword))) && $where[] = ['LIKE', 'name', $keyword];
  237. if(!empty($params['mobile'])){
  238. $admin_list = Admin::lists([
  239. 'where'=>[
  240. ['like','username',trim($params['mobile'])],
  241. ['is_default'=>1],
  242. ['status'=>StatusEnum::ENABLED]
  243. ]
  244. ]);
  245. $mall_ids = array_column($admin_list,'mall_id');
  246. $where[] = ['id'=>$mall_ids];
  247. }
  248. $with = [
  249. 'admin' => function ($query) {
  250. $query->select('id,username');
  251. },
  252. 'adonsComboRelation',
  253. ];
  254. $params = array('where' => $where, 'with' => $with, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
  255. $page_data = Mall::listPage($params, false);
  256. // 获取总页数
  257. $page_data['page_count'] = Mall::getMallTotalPage($params);
  258. foreach ($page_data['list'] as &$mall) {
  259. $mall['logo'] = Yii::$app->services->setting->mall->get($mall['id'], 'basic.logo');
  260. if(empty($mall['logo'])) $mall['logo'] = Yii::$app->request->hostInfo.'/resources/img/admin/mall-logo.png';
  261. }
  262. $page_data['admin_type'] = $admin_type;
  263. // 获取付费套餐
  264. $page_data['addons_combo'] = AddonsCombo::getAddonsCombo([],[],true,"id,title");
  265. $page_data['addons_combo_data'] = array_column($page_data['addons_combo'],'title','id');
  266. return $this->success('操作成功', $page_data);
  267. }
  268. return $this->render($this->action->id);
  269. }
  270. /**
  271. * 设置域名
  272. *
  273. * @return string
  274. */
  275. public function actionCustomHost(int $mall_id)
  276. {
  277. if ($this->request->isPost) {
  278. $params = Yii::$app->request->post();
  279. $res = Yii::$app->services->validate->validate($params, [
  280. [['backend_url', 'h5_url', 'api_url', 'static_url', 'is_custom_host', 'ssl_path'], 'required'],
  281. ]);
  282. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  283. $params['mall_id'] = $mall_id;
  284. $res = MallHost::setData($params);
  285. if ($res === false) return $this->error(Mall::getStaticError());
  286. // 执行
  287. if ($params['is_custom_host']) {
  288. $service = new MallService([]);
  289. $service->setDomain(
  290. $mall_id, $params['backend_url'], $params['h5_url'], $params['api_url'], $params['static_url'], $params['ssl_path']
  291. );
  292. }
  293. return $this->success('操作成功');
  294. }
  295. }
  296. public function actionWraparound()
  297. {
  298. return $this->render($this->action->id);
  299. }
  300. }