egg(101)–egg之注册 输入密码完成注册 完成注册后实现登录

38次阅读

共计 3278 个字符,预计需要花费 9 分钟才能阅读完成。

router.js
router.get(‘/register/registerStep3’, initMiddleware, controller.default.pass.registerStep3);
router.post(‘/pass/doRegister’, initMiddleware, controller.default.pass.doRegister);
model
app/model/user.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;

var d = new Date();
const User = new Schema({
password: {type: String},
phone: {type: Number},
last_ip: {type: String},
add_time: {
type: Number,
default: d.getTime()
},
email: {type: String},
status: {
type: Number,
default: d.getTime()
}
});

return mongoose.model(‘User’, User, ‘user’);
}
注册页面
controller
app/controller/default/pass.js
async registerStep3() {
var sign = this.ctx.request.query.sign;
var phone_code = this.ctx.request.query.phone_code;
var msg = this.ctx.request.query.msg || ”;
var add_day = await this.service.tools.getDay(); // 年月日
var userTempResult = await this.ctx.model.UserTemp.find({“sign”: sign, add_day: add_day});

if (userTempResult.length == 0) {

this.ctx.redirect(‘/register/registerStep1’);
} else {

await this.ctx.render(‘default/pass/register_step3.html’, {
sign: sign,
phone_code: phone_code,
msg: msg
});
}
}
view
appviewdefaultpassregister_step3.html
<form action=”/pass/doRegister?_csrf=<%=csrf%>” method=”post” id=”form”>

<input type=”hidden” name=”sign” value=”<%=sign%>” />
<input type=”hidden” name=”phone_code” value=”<%=phone_code%>” />
<div>
<input class=”form_input” type=”password” id=”password” name=”password” placeholder=” 请输入密码 ” />

</div>

<div>
<input class=”form_input” type=”password” id=”rpassword” name=”rpassword” placeholder=” 请输入确认密码 ” />

</div>

<%if(msg){%>
<p style=”color:red”>
<%=msg%>
</p>

<%}%>

<div class=”regist_submit”>
<input class=”submit” id=”nextStep” type=”submit” name=”submit” value=” 下一步 ”>

</div>
</form>
$(function() {
$(“#form”).submit(function() {

var password = $(‘#password’).val();

var rpassword = $(‘#rpassword’).val();

if (password.length < 6) {

alert(‘ 用户名的长度不能小于 6 位 ’);

return false;
}

if (password != rpassword) {

alert(‘ 密码和确认密码不一致 ’);

return false;
}

return true;

})
})
效果

提交注册, 跳转到首页
controller
app/controller/default/pass.js
async doRegister() {

var sign = this.ctx.request.body.sign;
var phone_code = this.ctx.request.body.phone_code;
var add_day = await this.service.tools.getDay(); // 年月日
var password = this.ctx.request.body.password;

var rpassword = this.ctx.request.body.rpassword;

var ip = this.ctx.request.ip.replace(/::ffff:/, ”);

if (this.ctx.session.phone_code != phone_code) {
// 非法操作
this.ctx.redirect(‘/pass/registerStep1’);
}

var userTempResult = await this.ctx.model.UserTemp.find({“sign”: sign, add_day: add_day});

if (userTempResult.length == 0) {
// 非法操作
this.ctx.redirect(‘/pass/registerStep1’);
} else {

// 传入参数正确 执行增加操作

if (password.length < 6 || password != rpassword) {
var msg = ‘ 密码不能小于 6 位并且密码和确认密码必须一致 ’;
this.ctx.redirect(‘/register/registerStep3?sign=’ + sign + ‘&phone_code=’ + phone_code + ‘&msg=’ + msg);
} else {
var userModel = new this.ctx.model.User({
phone: userTempResult[0].phone,
password: await this.service.tools.md5(password),
last_ip: ip
})
// 保存用户
var userReuslt = await userModel.save();

if (userReuslt) {

// 获取用户信息

var userinfo = await this.ctx.model.User.find({“phone”: userTempResult[0].phone }, ‘_id phone last_ip add_time email status’)

// 用户注册成功以后默认登录

//cookies 安全 加密
this.service.cookies.set(‘userinfo’, userinfo[0]);

this.ctx.redirect(‘/’);
}

}

}

}
中间件
app/middleware/init.js
ctx.state.csrf = ctx.csrf; // 全局变量
// 获取用户信息
ctx.state.userinfo = ctx.service.cookies.get(‘userinfo’);
view
app/view/default/public/header.html
<%if(userinfo && userinfo.phone){%>
<li>
<a href=”#”>
<%=userinfo.phone%>
</a>
</li>
<%}%>
效果

正文完
 0