|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +const validator = require('validator'); |
3 | 4 | const Controller = require('egg').Controller; |
4 | 5 |
|
| 6 | +const tools = require('../common/tools'); |
| 7 | + |
5 | 8 | class SignController extends Controller { |
6 | 9 | async showLogin() { |
7 | 10 | const { ctx } = this; |
8 | | - const locals = {}; |
9 | | - await ctx.render('/sign/signin', locals); |
| 11 | + await ctx.render('/sign/signin', {}); |
| 12 | + } |
| 13 | + |
| 14 | + // sign up |
| 15 | + async showSignup() { |
| 16 | + const { ctx } = this; |
| 17 | + await ctx.render('/sign/signup', {}); |
10 | 18 | } |
11 | 19 |
|
12 | 20 | async signup() { |
13 | | - this.ctx.render('sign/signup'); |
| 21 | + const { ctx, service, config } = this; |
| 22 | + const loginname = validator.trim(ctx.request.body.loginname).toLowerCase(); |
| 23 | + const email = validator.trim(ctx.request.body.email).toLowerCase(); |
| 24 | + const pass = validator.trim(ctx.request.body.pass); |
| 25 | + const rePass = validator.trim(ctx.request.body.re_pass); |
| 26 | + |
| 27 | + let msg; |
| 28 | + // 验证信息的正确性 |
| 29 | + if ([ loginname, pass, rePass, email ].some(item => { |
| 30 | + return item === ''; |
| 31 | + })) { |
| 32 | + msg = '信息不完整。'; |
| 33 | + } else if (loginname.length < 5) { |
| 34 | + msg = '用户名至少需要5个字符。'; |
| 35 | + } else if (!tools.validateId(loginname)) { |
| 36 | + msg = '用户名不合法。'; |
| 37 | + } else if (!validator.isEmail(email)) { |
| 38 | + msg = '邮箱不合法。'; |
| 39 | + } else if (pass !== rePass) { |
| 40 | + msg = '两次密码输入不一致。'; |
| 41 | + } |
| 42 | + // END 验证信息的正确性 |
| 43 | + |
| 44 | + if (msg) { |
| 45 | + ctx.status = 422; |
| 46 | + await ctx.render('sign/signup', { |
| 47 | + error: msg, |
| 48 | + loginname, |
| 49 | + email, |
| 50 | + }); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const users = await service.user.getUsersByQuery({ $or: [ |
| 55 | + { loginname }, |
| 56 | + { email }, |
| 57 | + ] }, {}); |
| 58 | + |
| 59 | + if (users.length > 0) { |
| 60 | + ctx.status = 422; |
| 61 | + await ctx.render('sign/signup', { |
| 62 | + error: '用户名或邮箱已被使用。', |
| 63 | + loginname, |
| 64 | + email, |
| 65 | + }); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const passhash = tools.bhash(pass); |
| 70 | + |
| 71 | + // create gravatar |
| 72 | + const avatarUrl = service.user.makeGravatar(email); |
| 73 | + |
| 74 | + await service.user.newAndSave(loginname, loginname, passhash, email, avatarUrl, false); |
| 75 | + // 发送激活邮件 |
| 76 | + // await mail.sendActiveMail(email, utility.md5(email + passhash + config.session_secret), loginname); |
| 77 | + await ctx.render('sign/signup', { |
| 78 | + success: '欢迎加入 ' + config.name + '!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号。', |
| 79 | + }); |
14 | 80 | } |
15 | 81 |
|
16 | 82 | async signout() { |
17 | 83 | const { ctx } = this; |
18 | 84 | ctx.session = null; |
19 | 85 | ctx.logout(); |
20 | | - ctx.body = 'signout'; |
| 86 | + ctx.redirect('/'); |
21 | 87 | } |
22 | 88 | } |
23 | 89 |
|
|
0 commit comments