Initial commit,修正
This commit is contained in:
Executable
+64
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* ContactForm is the model behind the contact form.
|
||||
*/
|
||||
class ContactForm extends Model
|
||||
{
|
||||
public $name;
|
||||
public $email;
|
||||
public $subject;
|
||||
public $body;
|
||||
public $verifyCode;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// name, email, subject and body are required
|
||||
[['name', 'email', 'subject', 'body'], 'required'],
|
||||
// email has to be a valid email address
|
||||
['email', 'email'],
|
||||
// verifyCode needs to be entered correctly
|
||||
['verifyCode', 'captcha'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'verifyCode' => Yii::t('frontend','Verify Code'),
|
||||
'name' => Yii::t('frontend','Name'),
|
||||
'email' => Yii::t('frontend','Email'),
|
||||
'subject' => Yii::t('frontend','Subject'),
|
||||
'body' => Yii::t('frontend','Body'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email to the specified email address using the information collected by this model.
|
||||
*
|
||||
* @param string $email the target email address
|
||||
* @return bool whether the email was sent
|
||||
*/
|
||||
public function sendEmail($email)
|
||||
{
|
||||
return Yii::$app->mailer->compose()
|
||||
->setTo($email)
|
||||
->setFrom(Yii::$app->params['noticeEmail'])
|
||||
->setSubject($this->subject)
|
||||
->setTextBody($this->body)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
Executable
+137
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Author: fm453
|
||||
* @Date: 2021-09-09 22:18:46
|
||||
* @Last Modified by: fm453
|
||||
* @Last Modified time: 2021-09-10 19:43:29
|
||||
* @Email: fm453@lukegzs.com
|
||||
*/
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use common\models\Member;
|
||||
use common\models\CLog;
|
||||
|
||||
/**
|
||||
* Login form
|
||||
*/
|
||||
class LoginForm extends Model
|
||||
{
|
||||
public $username;
|
||||
public $password;
|
||||
public $rememberMe = true;
|
||||
|
||||
private $_user;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// username and password are both required
|
||||
[['username', 'password'], 'required'],
|
||||
// rememberMe must be a boolean value
|
||||
['rememberMe', 'boolean'],
|
||||
// password is validated by validatePassword()
|
||||
['password', 'validatePassword'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'username' => Yii::t('common','Username'),
|
||||
'password' => Yii::t('common','Password'),
|
||||
'rememberMe' => Yii::t('common','Remember Me'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the password.
|
||||
* This method serves as the inline validation for password.
|
||||
*
|
||||
* @param string $attribute the attribute currently being validated
|
||||
* @param array $params the additional name-value pairs given in the rule
|
||||
*/
|
||||
public function validatePassword($attribute, $params)
|
||||
{
|
||||
if (!$this->hasErrors()) {
|
||||
$user = $this->getUser();
|
||||
if (!$user || !$user->validatePassword($this->password)) {
|
||||
$this->addError($attribute, Yii::t('common','Incorrect username or password.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in a user using the provided username and password.
|
||||
*
|
||||
* @return bool whether the user is logged in successfully
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if ($this->validate()) {
|
||||
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
|
||||
}
|
||||
|
||||
return false; //如果为true 将允许任意密码登陆
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds user by [[username]]
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
//千万注意前端的登陆使用的是Member模型,不是User
|
||||
if ($this->_user === null) {
|
||||
$this->_user = Member::findByUsername($this->username); //通过用户名查找
|
||||
}
|
||||
if ($this->_user === null) {
|
||||
$this->_user = Member::findByMobile($this->username); //通过手机号查找
|
||||
}
|
||||
|
||||
return $this->_user;
|
||||
}
|
||||
|
||||
//登录记录
|
||||
public function loginLog(){
|
||||
$request = Yii::$app->getRequest();
|
||||
$nowFile = $request -> getScriptFile();
|
||||
$data = "登陆系统|途径:".$nowFile;
|
||||
|
||||
$model = new CLog();
|
||||
$model->username = $this->username;
|
||||
$model->ip = Yii::$app->request->userIP;
|
||||
$model->data = $data;
|
||||
$model->create_time = time();
|
||||
$model->save();
|
||||
|
||||
//编制邮件发送通知
|
||||
$message = Yii::$app->mailer->compose();
|
||||
$message->setFrom(Yii::$app->params['noticeEmail']);
|
||||
//给登陆者发送
|
||||
$subject = "账号登陆提醒";
|
||||
$body = "您的账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl;
|
||||
$message->setTo(Yii::$app->user->identity->email)
|
||||
->setSubject($subject)
|
||||
->setTextBody($body)
|
||||
->send();
|
||||
//给管理人员发送
|
||||
$subject = "账号登陆提醒";
|
||||
$body = "API站账号【".$this->username."】刚刚登陆了网站:".Yii::$app->getRequest()->absoluteUrl;
|
||||
$message->setTo('1280880631@qq.com')
|
||||
->setSubject($subject)
|
||||
->setTextBody($body)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use common\models\Member;
|
||||
|
||||
/**
|
||||
* Password reset request form
|
||||
*/
|
||||
class PasswordResetRequestForm extends Model
|
||||
{
|
||||
public $email;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['email', 'trim'],
|
||||
['email', 'required'],
|
||||
['email', 'email'],
|
||||
['email', 'exist',
|
||||
'targetClass' => '\common\models\Member',
|
||||
'filter' => ['status' => Member::STATUS_ACTIVE],
|
||||
'message' => 'There is no user with this email address.'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email with a link, for resetting the password.
|
||||
*
|
||||
* @return bool whether the email was send
|
||||
*/
|
||||
public function sendEmail()
|
||||
{
|
||||
/* @var $user User */
|
||||
$user = Member::findOne([
|
||||
'status' => Member::STATUS_ACTIVE,
|
||||
'email' => $this->email,
|
||||
]);
|
||||
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Member::isPasswordResetTokenValid($user->password_reset_token)) {
|
||||
$user->generatePasswordResetToken();
|
||||
if (!$user->save()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return Yii::$app
|
||||
->mailer
|
||||
->compose(
|
||||
['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
|
||||
['user' => $user]
|
||||
)
|
||||
->setFrom([Yii::$app->params['noticeEmail'] => Yii::$app->name . ' robot'])
|
||||
->setTo($this->email)
|
||||
->setSubject('Password reset for ' . Yii::$app->name)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
# @Author: fm453
|
||||
# @Date: 1970-01-01T08:00:00+08:00
|
||||
# @Email: fm43@hiluker.com
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2021-09-14T19:25:39+08:00
|
||||
|
||||
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use yii\base\Model;
|
||||
use yii\base\InvalidParamException;
|
||||
use common\models\Member;
|
||||
|
||||
/**
|
||||
* Password reset form
|
||||
*/
|
||||
class ResetPasswordForm extends Model
|
||||
{
|
||||
public $password;
|
||||
|
||||
/**
|
||||
* @var \common\models\Member
|
||||
*/
|
||||
private $_user;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a form model given a token.
|
||||
*
|
||||
* @param string $token
|
||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
||||
* @throws \yii\base\InvalidParamException if token is empty or not valid
|
||||
*/
|
||||
public function __construct($token, $config = [])
|
||||
{
|
||||
if (empty($token) || !is_string($token)) {
|
||||
throw new InvalidParamException('密码不可设置为空白.');
|
||||
}
|
||||
$this->_user = User::findByPasswordResetToken($token);
|
||||
if (!$this->_user) {
|
||||
throw new InvalidParamException('密码重置凭证不对.');
|
||||
}
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['password', 'required'],
|
||||
['password', 'string', 'min' => 6],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets password.
|
||||
*
|
||||
* @return bool if password was reset.
|
||||
*/
|
||||
public function resetPassword()
|
||||
{
|
||||
$user = $this->_user;
|
||||
$user->setPassword($this->password);
|
||||
$user->removePasswordResetToken();
|
||||
|
||||
return $user->save(false);
|
||||
}
|
||||
}
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
# @Author: fm453
|
||||
# @Date: 1970-01-01T08:00:00+08:00
|
||||
# @Email: fm43@hiluker.com
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2021-09-14T19:52:26+08:00
|
||||
|
||||
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use common\models\Member;
|
||||
|
||||
/**
|
||||
* Signup form
|
||||
*/
|
||||
class SignupForm extends Model
|
||||
{
|
||||
public $username;
|
||||
public $mobile;
|
||||
public $email;
|
||||
public $password;
|
||||
public $verifyCode;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['username', 'trim'],
|
||||
['username', 'required'],
|
||||
['username', 'unique', 'targetClass' => '\common\models\Member', 'message' => '该用户名已经有人注册了.'],
|
||||
['username', 'string', 'min' => 2, 'max' => 255],
|
||||
|
||||
['mobile', 'trim'],
|
||||
['mobile', 'required'],
|
||||
['mobile', 'string', 'length' => 11],
|
||||
['mobile', 'unique', 'targetClass' => '\common\models\Member', 'message' => '该手机号已经注册使用了.'],
|
||||
|
||||
['email', 'trim'],
|
||||
['email', 'required'],
|
||||
['email', 'email'],
|
||||
['email', 'string', 'max' => 255],
|
||||
['email', 'unique', 'targetClass' => '\common\models\Member', 'message' => '该邮箱地址已经有人使用了.'],
|
||||
|
||||
['verifyCode', 'captcha'],
|
||||
|
||||
['password', 'required'],
|
||||
['password', 'string', 'min' => 6],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'username' => Yii::t('frontend','Username'),
|
||||
'mobile' => Yii::t('frontend','Mobile'),
|
||||
'password' => Yii::t('frontend','Password'),
|
||||
'email' => Yii::t('frontend','Email'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs user up.
|
||||
*
|
||||
* @return User|null the saved model or null if saving fails
|
||||
*/
|
||||
public function signup()
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$user = new Member();
|
||||
$user->username = $this->username;
|
||||
$user->mobile = $this->mobile;
|
||||
$user->email = $this->email;
|
||||
$user->setPassword($this->password);
|
||||
$user->generateAuthKey();
|
||||
|
||||
return $user->save() ? $user : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user