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', 'user_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); } public static function login($params) { try { $username = $params['username'] ?? ''; $password = $params['password'] ?? ''; //获取会员手机号 $user=User::find()->where(['mobile' => $username,'status'=>StatusEnum::ENABLED,'mall_id'=>$params['mall_id']])->asArray()->one(); if (empty($user)){ new Exception('会员不存在'); } $where = ['status' => StatusEnum::ENABLED, 'user_id' => $user['id'], 'mall_id' => $params['mall_id']]; $agent_list = HappinessAgent::findOne(['user_id' => $user['id'], 'mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED]); if (empty($agent_list)) throw new Exception('您不是代理'); $admin = HappinessAgentAdmin::findOne($where); if (empty($admin)) { //若不存在登录账号则生成一个 $pwd_str = substr($user['mobile'], -6); $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str); $admin_info = [ 'username' => $user['mobile'], 'account' => $user['nickname'], 'password' => $pwd_hash_str, 'mall_id' => $params['mall_id'], 'admin_type' => 2, 'status' => StatusEnum::ENABLED, 'user_id' => $user['id'], ]; $admin_model = new HappinessAgentAdmin(); $admin_model->loadDefaultValues(); $admin_model->attributes = $admin_info; $admin_model->updated_at = time(); $admin_model->created_at = time(); $res_ad = $admin_model->save(); if (!$res_ad) throw new Exception('系统错误'); $admin=$admin_model; } // 密码校验 if (!$admin || !$admin->validatePassword($password)) throw new Exception('登录账号或密码错误'); // 管理员类型处理 $redirect_url = 'happiness/agent/store/index'; $mall = Mall::findOne($params['mall_id']); Yii::$app->session->set('mall', $mall); // 执行登录 $config_params = \Yii::$app->params; $happiness_login_remember_time = $config_params['happiness_login_remember_time'] ?? 86400 * 30; $happiness_login_no_remember_time = $config_params['happiness_login_no_remember_time'] ?? 3600 * 8; if (Yii::$app->happiness_agent_user->login($admin, $params['is_remember'] ? $happiness_login_remember_time : $happiness_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; } } /** * 检测参数数据 * @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 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()); $tr->commit(); return $model; } catch (Exception $e) { $tr->rollBack(); self::$static_error = $e->getMessage(); return false; } } /** * 修改密码 * * @param string $pwd * @return boolean */ public function changePwd(string $pwd) { $this->password = Yii::$app->security->generatePasswordHash($pwd); return $this->save(); } }