| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace backend\modules\admin\controllers;
- use backend\controllers\AdminController;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\backend\Admin;
- use common\models\mall\Mall;
- use Yii;
- use yii\filters\AccessControl;
- use yii\filters\VerbFilter;
- use common\models\common\PlatformSetting;
- use common\models\common\MallSetting;
- /**
- *
- * 管理员
- * @Author bing
- * @DateTime 2021-08-11 17:22:39
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class AuthController extends AdminController
- {
- public $layout = false;
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'actions' => ['login','logout', 'error', 'captcha','register', 'expired', 'register-sms-code','login-sms-code'],
- 'allow' => true,
- ],
- ],
- ],
- ];
- }
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'common\components\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- 'maxLength' => 5, // 最大显示个数
- 'minLength' => 5, // 最少显示个数
- 'padding' => 5, // 间距
- 'height' => 32, // 高度
- 'width' => 100, // 宽度
- 'offset' => 4, // 设置字符偏移量
- 'backColor' => 0xffffff, // 背景颜色
- 'foreColor' => 0x2F4F4F, // 字体颜色
- ]
- ];
- }
- /**
- * 登录
- * @Author bing
- * @DateTime 2021-08-11 18:45:46
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionLogin()
- {
- $system = \Yii::$app->services->setting->platform->get('system',true);
- $open_mobile_login = 0;
- $mall_id = 0;
- if (!Yii::$app->user->isGuest) {
- // 记录行为日志
- // Yii::$app->services->actionLog->create('login', '自动登录', false);
- return $this->goHome();
- }
- $login_type = Yii::$app->request->get('login_type');
- $mall_sign = Yii::$app->request->get('mall_sign');
- if(!empty($login_type)){
- if($login_type!='mall')$login_type = base64_decode($login_type);
- }else{
- $login_type = 'mall';
- }
- if(!empty($mall_sign)){
- $mall = Mall::find()->where(['mall_sign'=>$mall_sign,'status'=>StatusEnum::ENABLED])->one();
- if(!$mall){
- return $this->error('登录网址有误');
- }
- $mall_id = $mall['id'];
- $login_setting = Yii::$app->services->setting->mall->get($mall['id'], 'login');
- $sms = MallSetting::getConfByGroup($mall_id, ['notice_sms'], true);
- if(isset($login_setting['open_sms_login']) && $login_setting['open_sms_login'] == 1 && isset($sms['notice_sms']['status']['value']) && $sms['notice_sms']['status']['value'] == 1){
- $open_mobile_login = 1;
- }
-
- $login_type = 'mall';
- }else{
- $sms = PlatformSetting::getConfByGroup('sms', true);
- if(isset($system['open_mobile_login_verify']) && $system['open_mobile_login_verify'] == 1 && isset($sms['status']['value']) && $sms['status']['value'] == 1){
- $open_mobile_login = 1;
- }
- }
- if(Yii::$app->request->isPost){
- $post = Yii::$app->request->post();
- if($open_mobile_login == 1){
- $requireParam = ['username','admin_type','is_remember','mobile','mobile_captcha'];
- }else{
- $requireParam = ['captcha','username','admin_type','is_remember'];
- }
- $res = self::checkRequireParam($post,$requireParam);
-
- if($res === false) return $this->error(self::getStaticError());
- if (YII_ENV != 'dev'){
- if($open_mobile_login == 0){
- if (!$this->createAction('captcha')->validate($post['captcha'], false)) return $this->error('验证码错误');
- }else{
- $mobile_captcha = \Yii::$app->cache->get($post['mobile'].'_AdminLogin_'. $mall_id);
- if ($mobile_captcha != $post['mobile_captcha']) return $this->error('验证码错误');
- }
- }
- if(!empty($post['mall_id'])) $post['mall_id'] = base64_decode($post['mall_id']);
- $data = Admin::login($post);
- if($data === false) return $this->error(Admin::getStaticError());
- return $this->success('登录成功',$data);
- }
-
- if(!in_array($login_type,['admin','agent','mall'])) return $this->error('登录类型错误');
-
- $system['open_mobile_login'] = $open_mobile_login;
- $system['mall_id'] = $mall_id;
- return $this->render($this->action->id,compact('login_type','system'));
- }
- /**
- * @desc:商家注册
- * @Author: hua
- * @Date: 2022/2/22
- * @Time: 15:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionRegister()
- {
- if(Yii::$app->request->isPost){
- $open_register = \Yii::$app->services->setting->platform->get('system.open_register',true);
- if(empty($open_register)) return $this->error('注册功能未开启');
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['username'], 'required', 'message' => '请输入手机号码'],
- [['password'], 'required', 'message' => '请输入密码'],
- [['confirm_password'], 'required', 'message' => '请输入确认密码'],
- [['name'], 'string'],
- [['mobile_captcha'], 'required', 'message' => '请输入验证码'],
- ['username', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- [['reference_id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if($post['password']!=$post['confirm_password']) return $this->error('密码不一致');
- if (YII_ENV == 'prod') {
- if (!Sms::checkValidateCode($post['username'], $post['mobile_captcha'])) return $this->error('输入的手机验证码不正确');
- }
- $admin = Admin::getOne([
- ['username'=>$post['username']],
- ['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]],
- ['<>','mall_id',0]
- ]);
- if(!empty($admin)) return $this->error('该手机号码已被使用');
- $register_manage_setting = \Yii::$app->services->setting->platform->get('register_manage');
- $expired_at = 0;
- if(!empty($register_manage_setting['expire'])){
- $expired_at = time()+$register_manage_setting['expire']*24*3600;
- }
- $params['name'] = empty($post['name']) ? '商城系统' : $post['name'];
- $params['reference_id'] = !empty($post['reference_id'])?$post['reference_id']:0;
- $params['mobile'] = $post['username'];
- $params['password'] = $post['password'];
- $params['expired_at'] = $expired_at;
- $params['source'] = 1;
- $res = Mall::setData($params);
- if ($res === false) return $this->error(Mall::getStaticError());
- $login_url = \Yii::$app->urlManager->createAbsoluteUrl('admin/auth/login');
- return $this->success('注册成功',['login_url'=>$login_url]);
- }
- $is_h5 = Yii::$app->request->get('h5','');
- $system = Yii::$app->services->setting->platform->get('system',true);
- return $is_h5? $this->renderPartial('register_h5'):$this->render($this->action->id,compact('system'));
- }
- /**
- * 获取注册短信验证码
- * @return mixed|void
- * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
- */
- public function actionRegisterSmsCode()
- {
- $sms = new Sms(0, 'domestic', 'admin');
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $cache_key = 'RegisterSmsCode_'.$params['mobile'];
- if (\Yii::$app->cache->get($cache_key)) {
- return $this->error('一分钟之后再试');
- }
- try {
- if ($sms->sendCaptcha($params['mobile']) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- \Yii::$app->cache->set($cache_key, 1, 60);
- return $this->success('发送成功');
- }
- /**
- * 退出登录
- * @Author bing
- * @DateTime 2021-08-11 18:45:57
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- $admin_type = $this->admin->admin_type;
- $mall_id = $this->admin->mall_id;
- $mall = Mall::find()->where(['id'=>$mall_id])->one();
- switch ($admin_type) {
- case Admin::ADMIN_TYPE_SUPER:
- $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl(['admin/auth/login','login_type'=>Yii::$app->params['login_type']]);
- break;
- case Admin::ADMIN_TYPE_AGENT:
- $redirect_url = 'admin/auth/login';
- break;
- case Admin::ADMIN_TYPE_OPERATE:
- $mall_id = base64_encode($mall_id);
- $login_type = base64_encode('mall');
- $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl(['admin/auth/login','mall_sign'=>$mall['mall_sign']]);
- break;
- default:
- $redirect_url = 'admin/auth/login';
- }
- return $this->success('操作成功',['url'=>$redirect_url]);
- }
-
- /**
- * 站点已过期
- *
- * @return mixed
- */
- public function actionExpired()
- {
- return $this->render($this->action->id);
- }
-
- public function actionLoginSmsCode()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- [['username'], 'required', 'message' => '请输入用户名'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- [['mall_id'], 'safe'],
- ]);
- $admin = Admin::find()->where(['mobile'=>$params['mobile'],'status'=>StatusEnum::ENABLED])->one();
- if(!$admin){
- return $this->error('手机号不存在');
- }
- if($admin->username!=$params['username'])
- {
- return $this->error('手机号与用户名不匹配');
- }
-
- if($params['mall_id'] > 0){
- $sms = new Sms($params['mall_id'],'domestic');
- }else{
- $sms = new Sms($admin['mall_id'],'domestic','platform');
- $params['mall_id']=0;
- }
- Sms::$validityMinutes =1;//一分钟有效
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $str = 'AdminLogin_'.$params['mall_id'];
- $cache_key = $params['mobile'].'_'.$str;
- if (\Yii::$app->cache->get($cache_key)) {
- return $this->error('一分钟之后再试');
- }
- try {
- if ($sms->sendCaptcha($params['mobile'],'',$str) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
-
- }
|