50], [['password'], 'string', 'max' => 255], [['auth_key', 'access_token'], 'string', 'max' => 128], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'username' => '登录账号', 'account' => '账号名称', 'password' => '密码', 'mall_id' => '商城id', 'mch_id' => '多商户id', 'auth_key' => 'Auth Key', 'access_token' => 'Access Token', 'admin_type' => '管理员类型1超级管理员2管理员3操作员', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'status' => '状态[-1:删除;0:禁用;1启用]', ]; } /**YII登录机制实现接口 */ public static function findIdentity($id) { return static::findOne($id); } public static function findIdentityByAccessToken($token, $type = null) { return static::findOne(['access_token' => $token]); } public function getId() { return $this->id; } public function getAuthKey() { return $this->auth_key; } public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password); } /**YII登录机制实现接口 */ /** * 关联授权角色 * * @return \yii\db\ActiveQuery */ public function getAssignment() { return $this->hasOne(AuthAssignment::class, ['user_id' => 'id']) ->where(['app_id' => Yii::$app->id]); } public function getMall() { return $this->hasMany(Mall::class, ['admin_id' => 'id']); } /** * 管理员登录 * @Author bing * @DateTime 2021-08-12 19:12:55 * @return void * @copyright: Copyright (c) 2020 广东七件事集团 */ public static function login($params) { try { $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; $where = ['status' => StatusEnum::ENABLED, 'username' => $username,'mall_id'=>$params['mall_id']]; $admin = self::findOne($where); if(empty($admin)) throw new Exception('登录账号不存在'); $mch = Mch::findOne(['shop_mobile'=>$username,'mall_id'=>$params['mall_id'],'status'=>StatusEnum::ENABLED]); if(empty($mch)||$mch->status==StatusEnum::DELETE) throw new Exception('商户不存在'); if($mch->status==StatusEnum::DISABLED) throw new Exception('该商户已被禁用'); if(!empty($mch['expire_time'])&&time()>$mch['expire_time']){ throw new Exception('您的账号已过期'); } // 密码校验 if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误'); // 管理员类型处理 $redirect_url = 'mch/client/index/index'; Yii::$app->session->set('mch', $mch); $mall = Mall::findOne($params['mall_id']); Yii::$app->session->set('mall',$mall); // 执行登录 $mch_login_remember_time = $config_params['mch_login_remember_time'] ?? 86400 * 30; $mch_login_no_remember_time = $config_params['mch_login_no_remember_time'] ?? 3600 * 8; if (Yii::$app->mch_user->login($admin, $params['is_remember'] ? $mch_login_remember_time : $mch_login_no_remember_time)) { return ['redirect_url'=>$redirect_url,'sign'=>$mall['mall_sign']]; } else { throw new \Exception('登录失败'); } } catch (Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 添加管理员 * @param array $params * @return bool|Admin */ public static function setData(array $params) { $tr = \Yii::$app->db->beginTransaction(); try { if (!empty($params['id'])) { $where = ['id' => $params['id'], ['status' => StatusEnum::ENABLED]]; if (isset($params['mall_id'])) $where[] = ['mall_id' => $params['mall_id']]; $model = self::findOne($where); if (empty($model)) throw new Exception('服务不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); } if(!empty($params['password'])){ $params['password'] = Yii::$app->security->generatePasswordHash($params['password']); }else{ unset($params['password']); } if(!empty($params['expired_at'])) $params['expired_at'] = strtotime($params['expired_at']); if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage()); if(!empty($params['role_id'])){ $res = AdminRole::setData(['id' => $params['admin_role_id'], 'role_id' => $params['role_id'], 'admin_id' => $model->id]); if ($res === false) throw new Exception(AdminRole::getStaticError()); } $tr->commit(); return $model; } catch (Exception $e) { $tr->rollBack(); self::$static_error = $e->getMessage(); return false; } } /** * 检测参数数据 * @Author: 广东七件事 zal * @Date: 2020-04-08 * @Time: 19:49 * @param $arr * @param $data * @throws \Exception */ private static function checkData($arr, $data) { foreach ($arr as $key => $item) { if (!isset($data[$key])) { throw new \Exception('请传参数' . $key . '->' . $item); } } } public static function isSuperAdmin($id) { $res = self::findOne($id); if (empty($res)) return false; if ($res->admin_type == self::ADMIN_TYPE_SUPER) return true; return false; } public static function del($params){ $tr = \Yii::$app->db->beginTransaction(); try { $model = self::findOne($params); $model->status = StatusEnum::DELETE; if($model->admin_type=='1'&& $model->username=='admin') throw new Exception('超级管理员不允许删除'); $res = $model->save(); if ($res === false) throw new Exception(self::getStaticError()); $res = AdminRole::deleteAll(['admin_id'=>$params['id']]); if ($res === false) throw new Exception(AdminRole::getStaticError()); $tr->commit(); return $model; }catch (Exception $e){ $tr->rollBack(); self::$static_error = $e->getMessage(); return false; } } }