| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\Supplier;
- use Yii;
- use yii\web\Response;
- use app\modules\admin\models\AdminForm;
- use app\modules\admin\models\AuthRoleForm;
- use app\modules\admin\models\ActionLogForm;
- use app\models\Md;
- use app\models\Admin;
- use Exception;
- use yii\base\InvalidConfigException;
- class AuthController extends BaseController
- {
- /**
- * 登录
- * @return Response
- */
- public function loginByCode()
- {
- $cyy_code = input_params('cyy_code', '');
- $res = [
- 'code' => 0,
- 'msg' => '登录成功',
- ];
- $cacheV = cache()->get($cyy_code);
- if($cacheV){
- $res['data'] = $cacheV;
- cache()->delete($cyy_code);
- }else{
- $res = [
- 'code' => 1,
- 'msg' => '登录失败,code获取失败',
- ];
- }
- return $this->asJson($res);
- }
- /**
- * 登录
- * @return Response
- */
- public function actionLogin()
- {
- $cyy_code = input_params('cyy_code', '');
- if($cyy_code){
- return $this->loginByCode();
- }
- $model = new AdminForm();
- $model->attributes = post_params();
- $model->validatePassword('password');
- if ($model->login()) {
- try {
- $admin = $model->getAdmin();
- if ($admin->expire_time > 0 && time() > $admin->expire_time) {
- throw new Exception('账号已过期,请联系平台处理!');
- }
- if ($admin->is_enable === Admin::ADMIN_DISABLE && $admin->username != 'admin') {
- throw new Exception('账号已被禁用,请联系平台处理!');
- }
- $store_id = null;
- if ($admin->type == Admin::ADMIN_TYPE_STORE) {
- $store_id = $admin->type_id;
- }
- if ($admin->type == Admin::ADMIN_TYPE_STAFF) {
- $store_id = $admin->type_id;
- }
- if ($admin->type == Admin::ADMIN_TYPE_MINI_ADMIN) {
- throw new Exception('不被允许登陆的账户类型!');
- }
- $token = $model->generateToken(86400, $store_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- 'type' => $admin->type,
- ],
- ]);
- } catch (\Throwable $throwable) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $throwable->getMessage(),
- ]);
- }
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => $model->getFirstErrors()['password']
- ]);
- }
- public function actionGenerateTokenBySaas()
- {
- $store_id = post_params('storeId');
- $username = post_params('username');
- $model = new AdminForm();
- $model->username = $username;
- $model->loginType = 'username';
- $token = $model->generateToken(86400, $store_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- ],
- ]);
- }
- /**
- * 管理员快速进入门店后台
- */
- public function actionGenerateTokenByMd()
- {
- $md_id = post_params('mdId');
- $md = Md::findOne(['id' => $md_id, 'is_delete' => 0]);
- if (!$md) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->getAdminByMdId($md_id);
- $token = $model->generateToken(86400, $md->store_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- ],
- ]);
- }
- /**
- * 管理员快速进入门店后台
- */
- public function actionGenerateTokenByMch()
- {
- $mch_id = input_params('mchId');
- $mch = \app\models\Mch::findOne(['id' => $mch_id, 'is_delete' => 0]);
- if (!$mch) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->getAdminByMchId($mch_id);
- $token = $model->generateToken(86400, $mch->store_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- ],
- ]);
- }
- /**
- * 管理员快速进入门店后台
- */
- public function actionGenerateTokenBySupplier()
- {
- $supplier_id = post_params('supplier_id');
- $supplier = Supplier::findOne(['cloud_supplier_id' => $supplier_id, 'is_delete' => 0]);
- if (!$supplier) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->getAdminBySupplierId($supplier_id);
- $token = $model->generateToken(86400, 1);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- ],
- ]);
- }
- /**
- * 管理员快速进入代理后台
- */
- public function actionGenerateTokenByAdmin()
- {
- $admin_id = post_params('admin_id');
- $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => 'admin']);
- if (!$agent) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->getAdminByAgent($admin_id);
- $token = $model->generateToken(86400, 1);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- ],
- ]);
- }
- /**
- * 推广代理管理员快速进入后台
- */
- public function actionGenerateTokenByBdAgent()
- {
- $admin_id = post_params('admin_id');
- $admin = get_admin();
- $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_BD_AGENT]);
- if (!$agent) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->username = $agent->username;
- $model->getAdminByFrontAgent($admin_id);
- $token = $model->generateToken(86400, 1);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- 'admin_name' => $admin->username,
- ],
- ]);
- }
- /**
- * 前置仓管理员快速进入代理后台
- */
- public function actionGenerateTokenByFrontAgent()
- {
- $admin_id = post_params('admin_id');
- $admin = get_admin();
- $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT]);
- if (!$agent) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->username = $agent->username;
- $model->getAdminByFrontAgent($admin_id);
- $token = $model->generateToken(86400, 1);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- 'admin_name' => $admin->username
- ],
- ]);
- }
- /**
- * 代理商管理员快速进入代理后台
- */
- public function actionGenerateTokenByGoodsAgent()
- {
- $admin_id = post_params('admin_id');
- $admin = get_admin();
- $agent = Admin::findOne(['id' => $admin_id, 'is_delete' => 0, 'type' => Admin::ADMIN_TYPE_GOODS_AGENT]);
- if (!$agent) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '登录失败'
- ]);
- }
- $model = new AdminForm();
- $model->username = $agent->username;
- $model->getAdminByGoodsAgent($admin_id);
- $token = $model->generateToken(86400, 1);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $token,
- 'is_saas' => Yii::$app->isSaas(),
- 'admin_name' => $admin->username
- ],
- ]);
- }
- /**
- * 修改密码
- *
- * @return Response
- */
- public function actionEditPassword()
- {
- $mobile = post_params('mobile');
- $password = post_params('password');
- $newPassword = post_params('newPassword');
- $confirmPassword = post_params('confirmPassword');
-
- // if($mobile != '' && !preg_match("/^1[34578]\d{9}$/", $mobile)){
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => '手机号格式不正确!',
- // ]);
- // }
-
- $admin = get_admin();
- if (!\Yii::$app->security->validatePassword($password, $admin->password)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '原密码不正确!',
- ]);
- }
- if (strcmp($newPassword, $confirmPassword) !== 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '两次密码不匹配!',
- ]);
- }
- $admin->password = \Yii::$app->security->generatePasswordHash($newPassword);
- // $admin->mobile = $mobile;
- if ($admin->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '修改成功!',
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '修改失败!',
- ]);
- }
- /**
- * 获取角色列表
- * @return Response
- */
- public function actionGetRoleList()
- {
- $authRole = new AuthRoleForm();
- return $this->asJson($authRole->getRoles());
- }
- /**
- * 获取权限信息
- * @return Response
- */
- public function actionGetPermission()
- {
- if (\Yii::$app->isSaas()) {
- $store = \app\models\Store::findOne(['id' => get_store_id()]);
- $params = \Yii::$app->getPermission($store->business_model, get_store_id());
- // var_dump($params);die;
- $storeAuthParams = [];
- if ($store->auth) {
- $storeAuth = json_decode($store->auth, true);
- if (!empty($storeAuth)) {
- $storeParams = AuthRoleForm::getAdminPermission($storeAuth);
- $storeArr = [];
- foreach ($storeParams as $item) {
- $storeArr[] = $item['permissionId'];
- }
- $storeAuthParams = AuthRoleForm::filterAuth($params, $storeArr);
- }
- }
- if (empty($storeAuthParams)) {
- $storeAuthParams = $params;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $storeAuthParams,
- ]);
- }
- $params = require Yii::$app->basePath . '/config/permission.php';
- $params = \Yii::$app->filterMenu($params);
- AuthRoleForm::formatPermission($params);
- if ($params[0]['key'] == 'dashboard') {
- unset($params[0]); // 去掉概况菜单,该菜单是必选的
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => array_values($params),
- ]);
- }
- /**
- * 获取权限信息
- * @return Response
- */
- public function actionGetPermission11()
- {
- if (\Yii::$app->isSaas()) {
- $params = require Yii::$app->basePath . '/config/saas_store_permission.php';
- } else {
- $params = require Yii::$app->basePath . '/config/permission.php';
- }
- $params = \Yii::$app->filterMenu($params);
- AuthRoleForm::formatPermission($params);
- if ($params[0]['key'] == 'dashboard') {
- unset($params[0]); // 去掉概况菜单,该菜单是必选的
- }
- if (is_open_platform()) {
- //设置小程序配置
- $WechatConfig = \app\models\WechatConfig::findOne(['store_id' => get_store_id()]);
- $store_mini = \app\models\StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'store_id' => get_store_id()])->select('id, appid, authorizer_refresh_token')->one();
-
- if(empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)){
- // return [
- // 'code'=>1,
- // 'msg'=>"参数配置错误"
- // ];
- \Yii::$app->removeMenu($params, ['dataStatistic']);
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => array_values($params),
- ]);
- }
- /**
- * 创建角色
- * @return Response
- */
- public function actionCreateRole()
- {
- $authRole = new AuthRoleForm();
- $authRole->attributes = input_params_except(['data', 'edit_data']);
- $data = post_params('data');
- $authRole->data = $data ? json_encode($data) : json_encode([]);
- $edit_data = post_params('edit_data');
- $authRole->edit_data = $edit_data ? json_encode($edit_data) : json_encode([]);
- return $this->asJson($authRole->create());
- }
- /**
- * 编辑角色
- * @return Response
- */
- public function actionEditRole()
- {
- $authRole = new AuthRoleForm();
- $authRole->attributes = input_params_except(['data', 'edit_data']);
- $data = post_params('data');
- $authRole->data = $data ? json_encode($data) : json_encode([]);
- $edit_data = post_params('edit_data');
- $authRole->edit_data = $edit_data ? json_encode($edit_data) : json_encode([]);
- return $this->asJson($authRole->edit());
- }
- /**
- * 删除角色
- * @return Response
- */
- public function actionDeleteRole()
- {
- $authRole = new AuthRoleForm();
- $authRole->id = post_params('id');
- return $this->asJson($authRole->deteleRole());
- }
- /**
- * 获取员工列表
- * @return Response
- */
- public function actionGetAdminList()
- {
- $adminForm = new AdminForm();
- return $this->asJson($adminForm->getAdminList());
- }
- /**
- * 获取账户列表(saas)
- * @return Response
- */
- public function actionGetAdminListBySaas()
- {
- $adminForm = new AdminForm();
- $adminForm->mobile = get_params("mobile");
- $adminForm->name = get_params("name");
- $adminForm->username = get_params("user_name");
- $adminForm->status = get_params('status', -1);
- $adminForm->area_level = get_params('area_level', -1);
- $adminForm->type = get_params('type', 0);
- return $this->asJson($adminForm->getAdminListBySaas());
- }
- /**
- * 编辑账户(saas)
- * @return Response
- */
- public function actionEditAdminBySaas()
- {
- $adminForm = new AdminForm();
- return $this->asJson($adminForm->addOrEditBySaas(post_params()));
- }
- /**
- * 获取区域代理列表(无页码限制)
- */
- public function actionGetAreaAgent()
- {
- $admin = Admin::find()->where(['is_delete' => 0, 'type' => 'admin'])->andWhere(['<>', 'username', 'admin'])
- ->select('id, name')->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'list' => $admin
- ]
- ]);
- }
- /**
- * 获取代理申请列表
- * @return Response
- */
- public function actionGetAgentApplyList()
- {
- $adminForm = new AdminForm();
- return $this->asJson($adminForm->getAgentApplyList(get_params()));
- }
- /**
- * 获取代理申请处理
- * @return Response
- */
- public function actionSetAgentApplyStatus()
- {
- $adminForm = new AdminForm();
- return $this->asJson($adminForm->setAgentApplyStatus(post_params()));
- }
- /**
- * 修改管理员状态
- * @return Response
- * @throws InvalidConfigException
- */
- public function actionEditAdminStatusBySaas()
- {
- $adminForm = new AdminForm();
- return $this->asJson($adminForm->editStatusBySaas(post_params()));
- }
- /**
- * 修改密码(saas)
- * @return Response
- */
- public function actionEditPasswordBySaas()
- {
- $adminForm = new AdminForm();
- $adminForm->id = post_params('id');
- $adminForm->password = post_params('password');
- return $this->asJson($adminForm->editPasswordBySaas());
- }
- /**
- * 创建账号
- * @return Response
- */
- public function actionCreateAdmin()
- {
- $adminForm = new AdminForm();
- $adminForm->attributes = post_params();
- return $this->asJson($adminForm->createAdmin());
- }
- /**
- * 编辑账号
- * @return Response
- */
- public function actionEditAdmin()
- {
- $adminForm = new AdminForm();
- $adminForm->attributes = post_params();
- return $this->asJson($adminForm->editAdmin());
- }
- /**
- * 删除账号
- * @return Response
- */
- public function actionDeleteAdmin()
- {
- $adminForm = new AdminForm();
- $adminForm->attributes = post_params();
- return $this->asJson($adminForm->deleteAdmin());
- }
- /**
- * 获取操作日志列表
- * @return Response
- */
- public function actionGetActionLog()
- {
- $actionLogForm = new ActionLogForm();
- return $this->asJson($actionLogForm->getActionLogList());
- }
- /**
- * 删除指定操作日志
- * @return Response
- */
- public function actionDeleteActionLog()
- {
- $actionLogForm = new ActionLogForm();
- $actionLogForm->ids = post_params('ids');
- return $this->asJson($actionLogForm->delete());
- }
- }
|