[ '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('发送成功'); } }