AuthController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Supplier;
  9. use Yii;
  10. use yii\web\Response;
  11. use app\modules\admin\models\AdminForm;
  12. use app\modules\admin\models\AuthRoleForm;
  13. use app\modules\admin\models\ActionLogForm;
  14. use app\models\Md;
  15. use app\models\Admin;
  16. use Exception;
  17. use yii\base\InvalidConfigException;
  18. class AuthController extends BaseController
  19. {
  20. /**
  21. * 登录
  22. * @return Response
  23. */
  24. public function loginByCode()
  25. {
  26. $cyy_code = input_params('cyy_code', '');
  27. $res = [
  28. 'code' => 0,
  29. 'msg' => '登录成功',
  30. ];
  31. $cacheV = cache()->get($cyy_code);
  32. if($cacheV){
  33. $res['data'] = $cacheV;
  34. cache()->delete($cyy_code);
  35. }else{
  36. $res = [
  37. 'code' => 1,
  38. 'msg' => '登录失败,code获取失败',
  39. ];
  40. }
  41. return $this->asJson($res);
  42. }
  43. /**
  44. * 登录
  45. * @return Response
  46. */
  47. public function actionLogin()
  48. {
  49. $cyy_code = input_params('cyy_code', '');
  50. if($cyy_code){
  51. return $this->loginByCode();
  52. }
  53. $model = new AdminForm();
  54. $model->attributes = post_params();
  55. $model->validatePassword('password');
  56. if ($model->login()) {
  57. try {
  58. $admin = $model->getAdmin();
  59. if ($admin->expire_time > 0 && time() > $admin->expire_time) {
  60. throw new Exception('账号已过期,请联系平台处理!');
  61. }
  62. if ($admin->is_enable === Admin::ADMIN_DISABLE && $admin->username != 'admin') {
  63. throw new Exception('账号已被禁用,请联系平台处理!');
  64. }
  65. $store_id = null;
  66. if ($admin->type == Admin::ADMIN_TYPE_STORE) {
  67. $store_id = $admin->type_id;
  68. }
  69. if ($admin->type == Admin::ADMIN_TYPE_STAFF) {
  70. $store_id = $admin->type_id;
  71. }
  72. if ($admin->type == Admin::ADMIN_TYPE_MINI_ADMIN) {
  73. throw new Exception('不被允许登陆的账户类型!');
  74. }
  75. $token = $model->generateToken(86400, $store_id);
  76. return $this->asJson([
  77. 'code' => 0,
  78. 'msg' => '登录成功',
  79. 'data' => [
  80. 'access_token' => $token,
  81. 'is_saas' => Yii::$app->isSaas(),
  82. 'type' => $admin->type,
  83. ],
  84. ]);
  85. } catch (\Throwable $throwable) {
  86. return $this->asJson([
  87. 'code' => 1,
  88. 'msg' => $throwable->getMessage(),
  89. ]);
  90. }
  91. }
  92. return $this->asJson([
  93. 'code' => 1,
  94. 'msg' => $model->getFirstErrors()['password']
  95. ]);
  96. }
  97. public function actionGenerateTokenBySaas()
  98. {
  99. $store_id = post_params('storeId');
  100. $username = post_params('username');
  101. $model = new AdminForm();
  102. $model->username = $username;
  103. $model->loginType = 'username';
  104. $token = $model->generateToken(86400, $store_id);
  105. return $this->asJson([
  106. 'code' => 0,
  107. 'msg' => '登录成功',
  108. 'data' => [
  109. 'access_token' => $token,
  110. 'is_saas' => Yii::$app->isSaas(),
  111. ],
  112. ]);
  113. }
  114. /**
  115. * 管理员快速进入门店后台
  116. */
  117. public function actionGenerateTokenByMd()
  118. {
  119. $md_id = post_params('mdId');
  120. $md = Md::findOne(['id' => $md_id, 'is_delete' => 0]);
  121. if (!$md) {
  122. return $this->asJson([
  123. 'code' => 1,
  124. 'msg' => '登录失败'
  125. ]);
  126. }
  127. $model = new AdminForm();
  128. $model->getAdminByMdId($md_id);
  129. $token = $model->generateToken(86400, $md->store_id);
  130. return $this->asJson([
  131. 'code' => 0,
  132. 'msg' => '登录成功',
  133. 'data' => [
  134. 'access_token' => $token,
  135. 'is_saas' => Yii::$app->isSaas(),
  136. ],
  137. ]);
  138. }
  139. /**
  140. * 管理员快速进入门店后台
  141. */
  142. public function actionGenerateTokenByMch()
  143. {
  144. $mch_id = input_params('mchId');
  145. $mch = \app\models\Mch::findOne(['id' => $mch_id, 'is_delete' => 0]);
  146. if (!$mch) {
  147. return $this->asJson([
  148. 'code' => 1,
  149. 'msg' => '登录失败'
  150. ]);
  151. }
  152. $model = new AdminForm();
  153. $model->getAdminByMchId($mch_id);
  154. $token = $model->generateToken(86400, $mch->store_id);
  155. return $this->asJson([
  156. 'code' => 0,
  157. 'msg' => '登录成功',
  158. 'data' => [
  159. 'access_token' => $token,
  160. 'is_saas' => Yii::$app->isSaas(),
  161. ],
  162. ]);
  163. }
  164. /**
  165. * 管理员快速进入门店后台
  166. */
  167. public function actionGenerateTokenBySupplier()
  168. {
  169. $supplier_id = post_params('supplier_id');
  170. $supplier = Supplier::findOne(['cloud_supplier_id' => $supplier_id, 'is_delete' => 0]);
  171. if (!$supplier) {
  172. return $this->asJson([
  173. 'code' => 1,
  174. 'msg' => '登录失败'
  175. ]);
  176. }
  177. $model = new AdminForm();
  178. $model->getAdminBySupplierId($supplier_id);
  179. $token = $model->generateToken(86400, 1);
  180. return $this->asJson([
  181. 'code' => 0,
  182. 'msg' => '登录成功',
  183. 'data' => [
  184. 'access_token' => $token,
  185. 'is_saas' => Yii::$app->isSaas(),
  186. ],
  187. ]);
  188. }
  189. /**
  190. * 管理员快速进入代理后台
  191. */
  192. public function actionGenerateTokenByAdmin()
  193. {
  194. $admin_id = post_params('admin_id');
  195. $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => 'admin']);
  196. if (!$agent) {
  197. return $this->asJson([
  198. 'code' => 1,
  199. 'msg' => '登录失败'
  200. ]);
  201. }
  202. $model = new AdminForm();
  203. $model->getAdminByAgent($admin_id);
  204. $token = $model->generateToken(86400, 1);
  205. return $this->asJson([
  206. 'code' => 0,
  207. 'msg' => '登录成功',
  208. 'data' => [
  209. 'access_token' => $token,
  210. 'is_saas' => Yii::$app->isSaas(),
  211. ],
  212. ]);
  213. }
  214. /**
  215. * 推广代理管理员快速进入后台
  216. */
  217. public function actionGenerateTokenByBdAgent()
  218. {
  219. $admin_id = post_params('admin_id');
  220. $admin = get_admin();
  221. $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_BD_AGENT]);
  222. if (!$agent) {
  223. return $this->asJson([
  224. 'code' => 1,
  225. 'msg' => '登录失败'
  226. ]);
  227. }
  228. $model = new AdminForm();
  229. $model->username = $agent->username;
  230. $model->getAdminByFrontAgent($admin_id);
  231. $token = $model->generateToken(86400, 1);
  232. return $this->asJson([
  233. 'code' => 0,
  234. 'msg' => '登录成功',
  235. 'data' => [
  236. 'access_token' => $token,
  237. 'is_saas' => Yii::$app->isSaas(),
  238. 'admin_name' => $admin->username,
  239. ],
  240. ]);
  241. }
  242. /**
  243. * 前置仓管理员快速进入代理后台
  244. */
  245. public function actionGenerateTokenByFrontAgent()
  246. {
  247. $admin_id = post_params('admin_id');
  248. $admin = get_admin();
  249. $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT]);
  250. if (!$agent) {
  251. return $this->asJson([
  252. 'code' => 1,
  253. 'msg' => '登录失败'
  254. ]);
  255. }
  256. $model = new AdminForm();
  257. $model->username = $agent->username;
  258. $model->getAdminByFrontAgent($admin_id);
  259. $token = $model->generateToken(86400, 1);
  260. return $this->asJson([
  261. 'code' => 0,
  262. 'msg' => '登录成功',
  263. 'data' => [
  264. 'access_token' => $token,
  265. 'is_saas' => Yii::$app->isSaas(),
  266. 'admin_name' => $admin->username
  267. ],
  268. ]);
  269. }
  270. /**
  271. * 代理商管理员快速进入代理后台
  272. */
  273. public function actionGenerateTokenByGoodsAgent()
  274. {
  275. $admin_id = post_params('admin_id');
  276. $admin = get_admin();
  277. $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_GOODS_AGENT]);
  278. if (!$agent) {
  279. return $this->asJson([
  280. 'code' => 1,
  281. 'msg' => '登录失败'
  282. ]);
  283. }
  284. $model = new AdminForm();
  285. $model->username = $agent->username;
  286. $model->getAdminByGoodsAgent($admin_id);
  287. $token = $model->generateToken(86400, 1);
  288. return $this->asJson([
  289. 'code' => 0,
  290. 'msg' => '登录成功',
  291. 'data' => [
  292. 'access_token' => $token,
  293. 'is_saas' => Yii::$app->isSaas(),
  294. 'admin_name' => $admin->username
  295. ],
  296. ]);
  297. }
  298. /**
  299. * 修改密码
  300. *
  301. * @return Response
  302. */
  303. public function actionEditPassword()
  304. {
  305. $mobile = post_params('mobile');
  306. $password = post_params('password');
  307. $newPassword = post_params('newPassword');
  308. $confirmPassword = post_params('confirmPassword');
  309. // if($mobile != '' && !preg_match("/^1[34578]\d{9}$/", $mobile)){
  310. // return $this->asJson([
  311. // 'code' => 1,
  312. // 'msg' => '手机号格式不正确!',
  313. // ]);
  314. // }
  315. $admin = get_admin();
  316. if (!\Yii::$app->security->validatePassword($password, $admin->password)) {
  317. return $this->asJson([
  318. 'code' => 1,
  319. 'msg' => '原密码不正确!',
  320. ]);
  321. }
  322. if (strcmp($newPassword, $confirmPassword) !== 0) {
  323. return $this->asJson([
  324. 'code' => 1,
  325. 'msg' => '两次密码不匹配!',
  326. ]);
  327. }
  328. $admin->password = \Yii::$app->security->generatePasswordHash($newPassword);
  329. // $admin->mobile = $mobile;
  330. if ($admin->save()) {
  331. return $this->asJson([
  332. 'code' => 0,
  333. 'msg' => '修改成功!',
  334. ]);
  335. }
  336. return $this->asJson([
  337. 'code' => 0,
  338. 'msg' => '修改失败!',
  339. ]);
  340. }
  341. /**
  342. * 获取角色列表
  343. * @return Response
  344. */
  345. public function actionGetRoleList()
  346. {
  347. $authRole = new AuthRoleForm();
  348. return $this->asJson($authRole->getRoles());
  349. }
  350. /**
  351. * 获取权限信息
  352. * @return Response
  353. */
  354. public function actionGetPermission()
  355. {
  356. if (\Yii::$app->isSaas()) {
  357. $store = \app\models\Store::findOne(['id' => get_store_id()]);
  358. $params = \Yii::$app->getPermission($store->business_model, get_store_id());
  359. // var_dump($params);die;
  360. $storeAuthParams = [];
  361. if ($store->auth) {
  362. $storeAuth = json_decode($store->auth, true);
  363. if (!empty($storeAuth)) {
  364. $storeParams = AuthRoleForm::getAdminPermission($storeAuth);
  365. $storeArr = [];
  366. foreach ($storeParams as $item) {
  367. $storeArr[] = $item['permissionId'];
  368. }
  369. $storeAuthParams = AuthRoleForm::filterAuth($params, $storeArr);
  370. }
  371. }
  372. if (empty($storeAuthParams)) {
  373. $storeAuthParams = $params;
  374. }
  375. return $this->asJson([
  376. 'code' => 0,
  377. 'msg' => 'success',
  378. 'data' => $storeAuthParams,
  379. ]);
  380. }
  381. $params = require Yii::$app->basePath . '/config/permission.php';
  382. $params = \Yii::$app->filterMenu($params);
  383. AuthRoleForm::formatPermission($params);
  384. if ($params[0]['key'] == 'dashboard') {
  385. unset($params[0]); // 去掉概况菜单,该菜单是必选的
  386. }
  387. return $this->asJson([
  388. 'code' => 0,
  389. 'msg' => 'success',
  390. 'data' => array_values($params),
  391. ]);
  392. }
  393. /**
  394. * 获取权限信息
  395. * @return Response
  396. */
  397. public function actionGetPermission11()
  398. {
  399. if (\Yii::$app->isSaas()) {
  400. $params = require Yii::$app->basePath . '/config/saas_store_permission.php';
  401. } else {
  402. $params = require Yii::$app->basePath . '/config/permission.php';
  403. }
  404. $params = \Yii::$app->filterMenu($params);
  405. AuthRoleForm::formatPermission($params);
  406. if ($params[0]['key'] == 'dashboard') {
  407. unset($params[0]); // 去掉概况菜单,该菜单是必选的
  408. }
  409. if (is_open_platform()) {
  410. //设置小程序配置
  411. $WechatConfig = \app\models\WechatConfig::findOne(['store_id' => get_store_id()]);
  412. $store_mini = \app\models\StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'store_id' => get_store_id()])->select('id, appid, authorizer_refresh_token')->one();
  413. if(empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)){
  414. // return [
  415. // 'code'=>1,
  416. // 'msg'=>"参数配置错误"
  417. // ];
  418. \Yii::$app->removeMenu($params, ['dataStatistic']);
  419. }
  420. }
  421. return $this->asJson([
  422. 'code' => 0,
  423. 'msg' => 'success',
  424. 'data' => array_values($params),
  425. ]);
  426. }
  427. /**
  428. * 创建角色
  429. * @return Response
  430. */
  431. public function actionCreateRole()
  432. {
  433. $authRole = new AuthRoleForm();
  434. $authRole->attributes = input_params_except(['data', 'edit_data']);
  435. $data = post_params('data');
  436. $authRole->data = $data ? json_encode($data) : json_encode([]);
  437. $edit_data = post_params('edit_data');
  438. $authRole->edit_data = $edit_data ? json_encode($edit_data) : json_encode([]);
  439. return $this->asJson($authRole->create());
  440. }
  441. /**
  442. * 编辑角色
  443. * @return Response
  444. */
  445. public function actionEditRole()
  446. {
  447. $authRole = new AuthRoleForm();
  448. $authRole->attributes = input_params_except(['data', 'edit_data']);
  449. $data = post_params('data');
  450. $authRole->data = $data ? json_encode($data) : json_encode([]);
  451. $edit_data = post_params('edit_data');
  452. $authRole->edit_data = $edit_data ? json_encode($edit_data) : json_encode([]);
  453. return $this->asJson($authRole->edit());
  454. }
  455. /**
  456. * 删除角色
  457. * @return Response
  458. */
  459. public function actionDeleteRole()
  460. {
  461. $authRole = new AuthRoleForm();
  462. $authRole->id = post_params('id');
  463. return $this->asJson($authRole->deteleRole());
  464. }
  465. /**
  466. * 获取员工列表
  467. * @return Response
  468. */
  469. public function actionGetAdminList()
  470. {
  471. $adminForm = new AdminForm();
  472. return $this->asJson($adminForm->getAdminList());
  473. }
  474. /**
  475. * 获取账户列表(saas)
  476. * @return Response
  477. */
  478. public function actionGetAdminListBySaas()
  479. {
  480. $adminForm = new AdminForm();
  481. $adminForm->mobile = get_params("mobile");
  482. $adminForm->name = get_params("name");
  483. $adminForm->username = get_params("user_name");
  484. $adminForm->status = get_params('status', -1);
  485. $adminForm->area_level = get_params('area_level', -1);
  486. $adminForm->type = get_params('type', 0);
  487. return $this->asJson($adminForm->getAdminListBySaas());
  488. }
  489. /**
  490. * 编辑账户(saas)
  491. * @return Response
  492. */
  493. public function actionEditAdminBySaas()
  494. {
  495. $adminForm = new AdminForm();
  496. return $this->asJson($adminForm->addOrEditBySaas(post_params()));
  497. }
  498. /**
  499. * 获取区域代理列表(无页码限制)
  500. */
  501. public function actionGetAreaAgent()
  502. {
  503. $admin = Admin::find()->where(['is_delete' => 0, 'type' => 'admin'])->andWhere(['<>', 'username', 'admin'])
  504. ->select('id, name')->asArray()->all();
  505. return $this->asJson([
  506. 'code' => 0,
  507. 'msg' => '获取成功',
  508. 'data' => [
  509. 'list' => $admin
  510. ]
  511. ]);
  512. }
  513. /**
  514. * 获取代理申请列表
  515. * @return Response
  516. */
  517. public function actionGetAgentApplyList()
  518. {
  519. $adminForm = new AdminForm();
  520. return $this->asJson($adminForm->getAgentApplyList(get_params()));
  521. }
  522. /**
  523. * 获取代理申请处理
  524. * @return Response
  525. */
  526. public function actionSetAgentApplyStatus()
  527. {
  528. $adminForm = new AdminForm();
  529. return $this->asJson($adminForm->setAgentApplyStatus(post_params()));
  530. }
  531. /**
  532. * 修改管理员状态
  533. * @return Response
  534. * @throws InvalidConfigException
  535. */
  536. public function actionEditAdminStatusBySaas()
  537. {
  538. $adminForm = new AdminForm();
  539. return $this->asJson($adminForm->editStatusBySaas(post_params()));
  540. }
  541. /**
  542. * 修改密码(saas)
  543. * @return Response
  544. */
  545. public function actionEditPasswordBySaas()
  546. {
  547. $adminForm = new AdminForm();
  548. $adminForm->id = post_params('id');
  549. $adminForm->password = post_params('password');
  550. return $this->asJson($adminForm->editPasswordBySaas());
  551. }
  552. /**
  553. * 创建账号
  554. * @return Response
  555. */
  556. public function actionCreateAdmin()
  557. {
  558. $adminForm = new AdminForm();
  559. $adminForm->attributes = post_params();
  560. return $this->asJson($adminForm->createAdmin());
  561. }
  562. /**
  563. * 编辑账号
  564. * @return Response
  565. */
  566. public function actionEditAdmin()
  567. {
  568. $adminForm = new AdminForm();
  569. $adminForm->attributes = post_params();
  570. return $this->asJson($adminForm->editAdmin());
  571. }
  572. /**
  573. * 删除账号
  574. * @return Response
  575. */
  576. public function actionDeleteAdmin()
  577. {
  578. $adminForm = new AdminForm();
  579. $adminForm->attributes = post_params();
  580. return $this->asJson($adminForm->deleteAdmin());
  581. }
  582. /**
  583. * 获取操作日志列表
  584. * @return Response
  585. */
  586. public function actionGetActionLog()
  587. {
  588. $actionLogForm = new ActionLogForm();
  589. return $this->asJson($actionLogForm->getActionLogList());
  590. }
  591. /**
  592. * 删除指定操作日志
  593. * @return Response
  594. */
  595. public function actionDeleteActionLog()
  596. {
  597. $actionLogForm = new ActionLogForm();
  598. $actionLogForm->ids = post_params('ids');
  599. return $this->asJson($actionLogForm->delete());
  600. }
  601. }