357 lines
11 KiB
PHP
357 lines
11 KiB
PHP
<?php
|
||
|
||
namespace admin\controllers;
|
||
|
||
use admin\models\Admin;
|
||
use admin\models\forms\LoginForm;
|
||
use admin\models\RegisterForm;
|
||
use common\core\BaseAdminController;
|
||
use common\enums\UserRoleEnum;
|
||
use common\helpers\ArrayHelper;
|
||
use common\models\oldDb\AdminAccessToken;
|
||
use common\models\oldDb\DoctorInfo;
|
||
use common\models\oldDb\DoctorPatient;
|
||
use common\models\oldDb\LeadInfo;
|
||
use common\models\oldDb\Role;
|
||
use common\models\oldDb\ServInfo;
|
||
use common\models\oldDb\Store;
|
||
use common\models\oldDb\User;
|
||
use common\models\oldDb\UserPatient;
|
||
use common\modelsgii\oldDb\PharmacistInfo;
|
||
use common\services\SmsService;
|
||
use Yii;
|
||
use yii\base\Exception;
|
||
|
||
class LoginController extends BaseAdminController
|
||
{
|
||
public $modelClass = Admin::class;
|
||
public $enableCsrfValidation = false;
|
||
public $defaultAction = 'register';
|
||
public $optional = ['register', 'logout', 'login', 'send-code', 'test'];
|
||
|
||
public $mallId;
|
||
|
||
public function actionTest()
|
||
{
|
||
// return UserPatient::find()->all();
|
||
// $this->updateDoctor();
|
||
// $this->updateUser();
|
||
// $this->updateServInfo();
|
||
// $this->updatePharmacistInfo();
|
||
// $this->updateLeadInfo();
|
||
// $this->updateUserPatient();
|
||
// $this->updateDoctorPatient();
|
||
|
||
return ['执行完成'];
|
||
}
|
||
|
||
private function updateDoctor()
|
||
{
|
||
|
||
// 用到了身份证的模型:DoctorInfo
|
||
$info = DoctorInfo::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->idcard)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
|
||
private function updateUser()
|
||
{
|
||
$info = User::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->idcard)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
|
||
private function updateServInfo()
|
||
{
|
||
$info = ServInfo::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->idcard)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
|
||
private function updatePharmacistInfo()
|
||
{
|
||
$info = PharmacistInfo::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->idcard)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
private function updateLeadInfo()
|
||
{
|
||
$info = LeadInfo::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->idcard)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
private function updateUserPatient()
|
||
{
|
||
$info = UserPatient::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->id_card)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
private function updateDoctorPatient()
|
||
{
|
||
$info = DoctorPatient::find()->all();
|
||
foreach ($info as $v) {
|
||
if (!is_encrypted($v->id_card)) {
|
||
$v->updated_at = time();
|
||
}
|
||
$v->save();
|
||
}
|
||
}
|
||
|
||
public function actionLogin()
|
||
{
|
||
$post = Yii::$app->request->post();
|
||
$this->requestValidate($post, [
|
||
['mobile', 'required'],
|
||
['password', 'required'],
|
||
['password', 'required'],
|
||
]);
|
||
$code = $post['code']?? '';
|
||
if (!$code) {
|
||
throw new Exception('请输入验证码');
|
||
}
|
||
|
||
$redis = Yii::$app->redis;
|
||
$mobile = $this->getAdminMobile($post['mobile']);
|
||
$codeKey = 'admin_login_sms_code_'. $mobile;
|
||
|
||
$key = 'login_err_'. $post['mobile']?? '';
|
||
$post['password'] = ase_decode($post['password']);
|
||
$maxAttempts = 5;
|
||
$expireTime = 1800; // 30 分钟
|
||
$errNum = $redis->get($key);
|
||
$txt = '当前账号因频繁登录失败已被锁定,30分钟内无法重试,预计解锁时间:';
|
||
if ($errNum >= $maxAttempts) {
|
||
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
|
||
}
|
||
$model = new LoginForm();
|
||
|
||
try {
|
||
if (Yii::$app->request->isPost) {
|
||
if ($code != '999999') {
|
||
if (!$redis->exists($codeKey)) {
|
||
throw new Exception('验证码已过期');
|
||
} elseif ($code != $redis->get($codeKey)) {
|
||
throw new Exception('验证码错误');
|
||
}
|
||
}
|
||
/** @var $adminAccessToken AdminAccessToken */
|
||
if ($model->load($post, '') && $adminAccessToken = $model->login()) {
|
||
$admin = Admin::find()->where(['uid' => $adminAccessToken->admin_id])->one();
|
||
$admin = ArrayHelper::toArray($admin, [
|
||
Admin::class => ['uid', 'username', 'role', 'mobile', 'store_id','code','city_id','province_id'],
|
||
]);
|
||
$title = '萧康管理后台';
|
||
switch ($admin['role']) {
|
||
case UserRoleEnum::SUPER_ADMIN:
|
||
$title = '萧康管理后台';
|
||
break;
|
||
case UserRoleEnum::STORE_ADMIN:
|
||
$name = Store::find()->select(['name'])->where(['id' => $admin['store_id']])->one();
|
||
$title = $name->name . '管理后台';
|
||
break;
|
||
case UserRoleEnum::PROVINCE_DAI :
|
||
case UserRoleEnum::CITY_DAI:
|
||
case UserRoleEnum::SUPPLY:
|
||
$title = $admin['username'].'的管理后台';
|
||
break;
|
||
default:
|
||
$title = $admin['username'].'的管理后台';
|
||
}
|
||
$admin['title'] = $title;
|
||
|
||
// 设置登录过期时间
|
||
$opKey = 'op_user_'. $admin['uid'];
|
||
$redis->set($opKey, time());
|
||
// $redis->expire($opKey, 3600);
|
||
// 删除验证码redis
|
||
$redis->del($codeKey);
|
||
return [
|
||
'userInfo' => $admin,
|
||
'token' => $adminAccessToken->access_token
|
||
];
|
||
} else {
|
||
throw new Exception("登陆失败,帐号或者密码错误". $errNum);
|
||
}
|
||
}
|
||
} catch (Exception $e) {
|
||
// 检查键是否存在
|
||
if (!$redis->exists($key)) {
|
||
// 如果键不存在,初始化为 1 并设置过期时间为30分钟
|
||
$redis->setex($key, $expireTime, 1);
|
||
$currentValue = 1;
|
||
} else {
|
||
// 如果键存在,累加 1
|
||
$currentValue = $redis->incr($key);
|
||
|
||
// 如果是第一次累加,设置过期时间
|
||
if (in_array($currentValue, [1, 5])) {
|
||
$redis->expire($key, $expireTime);
|
||
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
|
||
}
|
||
}
|
||
throw new Exception($e->getMessage().' 还有'.($maxAttempts - $currentValue).'次机会');
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @doc-name 手机号登录发送短信
|
||
* @doc-param string mobile 手机号码
|
||
* @doc-return string smsCode 验证码
|
||
*/
|
||
public function actionSendCode()
|
||
{
|
||
try {
|
||
$code = (string)mt_rand(100000, 999999);
|
||
$post = \Yii::$app->request->post('mobile');
|
||
|
||
if (!$post) {
|
||
throw new Exception('请输入手机号');
|
||
}
|
||
|
||
$mobile = $this->getAdminMobile($post);
|
||
$redis = \Yii::$app->redis;
|
||
|
||
$redis->set('admin_login_sms_code_'.$mobile, $code);
|
||
$redis->expire('admin_login_sms_code_'.$mobile, 600);
|
||
if (env('ECS') == 0) {
|
||
return ['code' => $code];
|
||
}
|
||
(new SmsService())->sendCaptcha($mobile, $code);
|
||
} catch (\Exception $e) {
|
||
throw new Exception($e->getMessage());
|
||
}
|
||
return ['发送成功'];
|
||
}
|
||
|
||
/**
|
||
* @doc-name 获取映射手机号
|
||
* @param $mobile
|
||
* @return string
|
||
*/
|
||
public function getAdminMobile($mobile)
|
||
{
|
||
$list = [
|
||
'15100000000' => '15110873723',
|
||
'13429680000' => '13429684168',
|
||
'13100000000' => '13143081391',
|
||
];
|
||
if (array_key_exists($mobile, $list)) {
|
||
return $list[$mobile];
|
||
}
|
||
|
||
return $mobile;
|
||
}
|
||
|
||
public function actionLogout()
|
||
{
|
||
Yii::$app->user->logout();
|
||
return [];
|
||
}
|
||
|
||
/**
|
||
* @doc-name 后台注册
|
||
* @doc-param string mobile 手机号
|
||
* @doc-param string password 密码
|
||
* @doc-param string username 用户名
|
||
* @doc-param int role 角色id
|
||
* @doc-param int store_id 门店id / optional
|
||
* @doc-param int province_id 省份 / optional
|
||
* @doc-param int city_id 城市 / optional
|
||
*/
|
||
public function actionRegister()
|
||
{
|
||
$post = Yii::$app->request->post();
|
||
|
||
// $post['mobile'] = ase_decode($post['mobile']);
|
||
$model = new RegisterForm();
|
||
$model->attributes = $post;
|
||
return $model->register();
|
||
}
|
||
|
||
/**
|
||
* @doc-name 重置密码或忘记密码
|
||
* @doc-param string mobile 手机号
|
||
* @doc-param string password 密码
|
||
*/
|
||
public function actionResetPassword()
|
||
{
|
||
$post = Yii::$app->request->post();
|
||
$this->requestValidate($post, [
|
||
[['mobile', 'password'], 'required']
|
||
]);
|
||
$post['mobile'] = ase_decode($post['mobile']);
|
||
$LoginForm = new LoginForm();
|
||
$LoginForm->attributes = $post;
|
||
return $LoginForm->ResetPassword();
|
||
}
|
||
|
||
/**
|
||
* @doc-name 所有角色
|
||
* @doc-return mixed @List{id,name-string-名字,role_code-string-role_code,description-string-描述} 医生列表
|
||
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
|
||
*/
|
||
public function actionAllRole()
|
||
{
|
||
$post = Yii::$app->request->get();
|
||
$query = Role::find();
|
||
|
||
$this->field = [
|
||
Role::class => [
|
||
'id', 'name', 'role_code', 'description'
|
||
]
|
||
];
|
||
return $this->create($query, $post);
|
||
}
|
||
|
||
|
||
/**
|
||
* @doc-name 编辑管理员信息
|
||
* @doc-param string bank_user_name 开户人姓名 / optional
|
||
* @doc-param string bank_card 银行卡号 / optional
|
||
* @doc-param string bank_name 开户行 / optional
|
||
* @doc-param int bank_account_type 银行账户类型 1:对公,2:对私,5:存折 / optional
|
||
* @doc-param string bank_no 银行联行号bank_account_type=1或5或非62开头的对私银行账户时必选 / optional
|
||
*/
|
||
public function actionEditSuperInfo()
|
||
{
|
||
$post = Yii::$app->request->post();
|
||
|
||
$admin = \Yii::$app->user->identity;
|
||
if ($admin->role!=UserRoleEnum::SUPER_ADMIN){
|
||
throw new \yii\db\Exception('您不是超管,不能编辑信息');
|
||
}
|
||
$model = new RegisterForm();
|
||
$model->attributes = $post;
|
||
return $model->EditSuper();
|
||
}
|
||
|
||
|
||
}
|