Skip to content

Commit

Permalink
简化登录流程
Browse files Browse the repository at this point in the history
  • Loading branch information
mailgyc committed Mar 18, 2020
1 parent e721151 commit 197111f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 105 deletions.
49 changes: 6 additions & 43 deletions doudizhu/apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ def get(self):


class LoginHandler(RestfulHandler, JwtMixin):
required_fields = ('username', 'password')
required_fields = ('username', )

async def post(self):
email = self.json.get('username')
password = self.json.get("password")
username = self.json.get('username')

account = await self.db.fetchone('SELECT id, username, password FROM account WHERE email=%s', email)
account = await self.db.fetchone('SELECT id, username, avatar FROM account WHERE username=%s', username)
if not account:
self.send_error(400, reason='This email address does not exist.')
return

verify_pass = await self.run_in_executor(bcrypt.checkpw, password.encode('utf8'),
account.get('password').encode('utf8'))
if not verify_pass:
self.send_error(400, reason='Incorrect username or password.')
uid = await self.db.insert(
'INSERT INTO account (openid, username, sex, avatar) VALUES (%s,%s,%s,%s)', username, username, 1, '')
account = {'id': uid, 'username': username}

uid, username = account.get('id'), account.get('username')
self.set_secure_cookie('user', json_encode({'uid': uid, 'username': username}))
Expand Down Expand Up @@ -61,38 +56,6 @@ async def get(self):
self.write({'uid': uid, 'username': username, 'room': Storage.find_player_room_id(uid)})


class SignupHandler(RestfulHandler):
required_fields = ('email', 'username', 'password', 'password_repeat')

async def post(self):
email = self.json.get('email')
username = self.json.get('username')
password = self.json.get('password')
password_repeat = self.json.get('password_repeat')
if password != password_repeat:
self.send_error(400, reason='The password does not match')
return

try:
uid = await self.create_account(username, email, password)
self.set_secure_cookie('user', json_encode({'uid': uid, 'username': username}))
self.write({'uid': uid, 'username': username})
except pymysql.IntegrityError as e:
if await self.account_exists(email):
self.send_error(400, reason='An account with this email address already exists.')
else:
raise e

async def create_account(self, username, email, password) -> int:
password = await self.run_in_executor(bcrypt.hashpw, password.encode('utf8'), bcrypt.gensalt())
return await self.db.insert('INSERT INTO account (email, username, password, ip_addr) VALUES (%s,%s,%s,%s)',
email, username, password, self.client_ip)

async def account_exists(self, email) -> bool:
account = await self.db.fetchone('SELECT id FROM account WHERE email=%s', email)
return True if account else False


class LogoutHandler(RestfulHandler):

@authenticated
Expand Down
5 changes: 3 additions & 2 deletions doudizhu/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ def generate_button(params):

if __name__ == '__main__':
db = (
('quick', '挑战AI', (160, 60), 36),
('quick', '开始游戏', (160, 60), 36),
('start', '真人对抗', (160, 60), 36),
('exit', '退出游戏', (160, 60), 36),
('setting', '设置', (160, 60), 36),
('register', '注册', (160, 60), 36),
('signup', '注册', (160, 60), 36),
('login', '登录', (160, 60), 36),

('score_0', '不抢', (128, 48), 28),
('score_1', '抢地主', (128, 48), 28),
Expand Down
40 changes: 28 additions & 12 deletions doudizhu/static/i/btn.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"filename": "exit.png",
"frame": {"x":0,"y":180,"w":160,"h":60},
"frame": {"x":160,"y":240,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
Expand Down Expand Up @@ -48,33 +48,41 @@
"spriteSourceSize": {"x":0,"y":0,"w":84,"h":84},
"sourceSize": {"w":84,"h":84}
},
{
"filename": "login.png",
"frame": {"x":0,"y":300,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
"sourceSize": {"w":160,"h":60}
},
{
"filename": "pass.png",
"frame": {"x":256,"y":132,"w":128,"h":48},
"frame": {"x":0,"y":180,"w":128,"h":48},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":128,"h":48},
"sourceSize": {"w":128,"h":48}
},
{
"filename": "quick.png",
"frame": {"x":0,"y":240,"w":160,"h":60},
"frame": {"x":160,"y":300,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
"sourceSize": {"w":160,"h":60}
},
{
"filename": "ready.png",
"frame": {"x":384,"y":132,"w":128,"h":48},
"frame": {"x":128,"y":180,"w":128,"h":48},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":128,"h":48},
"sourceSize": {"w":128,"h":48}
},
{
"filename": "register.png",
"frame": {"x":160,"y":240,"w":160,"h":60},
"frame": {"x":320,"y":300,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
Expand All @@ -90,7 +98,7 @@
},
{
"filename": "score_1.png",
"frame": {"x":128,"y":132,"w":128,"h":48},
"frame": {"x":256,"y":132,"w":128,"h":48},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":128,"h":48},
Expand All @@ -106,31 +114,39 @@
},
{
"filename": "score_3.png",
"frame": {"x":0,"y":132,"w":128,"h":48},
"frame": {"x":128,"y":132,"w":128,"h":48},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":128,"h":48},
"sourceSize": {"w":128,"h":48}
},
{
"filename": "setting.png",
"frame": {"x":320,"y":180,"w":160,"h":60},
"frame": {"x":320,"y":240,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
"sourceSize": {"w":160,"h":60}
},
{
"filename": "shot.png",
"frame": {"x":384,"y":84,"w":128,"h":48},
"frame": {"x":0,"y":132,"w":128,"h":48},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":128,"h":48},
"sourceSize": {"w":128,"h":48}
},
{
"filename": "signup.png",
"frame": {"x":0,"y":240,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
"sourceSize": {"w":160,"h":60}
},
{
"filename": "start.png",
"frame": {"x":160,"y":180,"w":160,"h":60},
"frame": {"x":256,"y":180,"w":160,"h":60},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":160,"h":60},
Expand All @@ -149,8 +165,8 @@
"version": "1.0",
"image": "btn.png",
"format": "RGBA8888",
"size": {"w":512,"h":300},
"size": {"w":480,"h":360},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:a1bdf11aa856b03481006e0c68ae1617:48b37945e23656f9ba9811fb5c24cb0e:382bbd42eff5a2b763aa915dc00d7597$"
"smartupdate": "$TexturePacker:SmartUpdate:1c02124bb030b0481191bbda391a07a1:d5b9818ce84bf91ded032e3295ed48b1:382bbd42eff5a2b763aa915dc00d7597$"
}
}
Binary file modified doudizhu/static/i/btn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 8 additions & 6 deletions doudizhu/static/i/btn/btn.tps
Original file line number Diff line number Diff line change
Expand Up @@ -220,29 +220,31 @@
<key>scale9FromFile</key>
<false/>
</struct>
<key type="filename">ready.png</key>
<key type="filename">login.png</key>
<key type="filename">register.png</key>
<key type="filename">signup.png</key>
<struct type="IndividualSpriteSettings">
<key>pivotPoint</key>
<point_f>0.5,0.5</point_f>
<key>scale9Enabled</key>
<false/>
<key>scale9Borders</key>
<rect>32,12,64,24</rect>
<rect>40,15,80,30</rect>
<key>scale9Paddings</key>
<rect>32,12,64,24</rect>
<rect>40,15,80,30</rect>
<key>scale9FromFile</key>
<false/>
</struct>
<key type="filename">register.png</key>
<key type="filename">ready.png</key>
<struct type="IndividualSpriteSettings">
<key>pivotPoint</key>
<point_f>0.5,0.5</point_f>
<key>scale9Enabled</key>
<false/>
<key>scale9Borders</key>
<rect>40,15,80,30</rect>
<rect>32,12,64,24</rect>
<key>scale9Paddings</key>
<rect>40,15,80,30</rect>
<rect>32,12,64,24</rect>
<key>scale9FromFile</key>
<false/>
</struct>
Expand Down
Binary file added doudizhu/static/i/btn/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doudizhu/static/i/btn/quick.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doudizhu/static/i/btn/signup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 9 additions & 34 deletions doudizhu/static/js/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,58 +162,33 @@ PG.Login = {
let bg = this.game.add.sprite(this.game.width / 2, 0, 'bg');
bg.anchor.set(0.5, 0);

let style = {
font: '24px Arial', fill: '#000', width: 300, padding: 12,
this.game.add.plugin(PhaserInput.Plugin);
const style = {
font: '32px Arial', fill: '#000', width: 300, padding: 12,
borderWidth: 1, borderColor: '#c8c8c8', borderRadius: 2,
textAlign: 'center', placeHolder: '姓名'
textAlign: 'center', placeHolder: '请输入用户名'
};
this.game.add.plugin(PhaserInput.Plugin);

this.username = this.game.add.inputField((this.game.world.width - 300) / 2, this.game.world.centerY - 160, style);
this.username = this.game.add.inputField((this.game.world.width - 300) / 2, this.game.world.centerY - 40, style);

style.placeHolder = '密码';
this.password = this.game.add.inputField((this.game.world.width - 300) / 2, this.game.world.centerY - 90, style);

style.placeHolder = '再次输入密码';
this.passwordAgain = this.game.add.inputField((this.game.world.width - 300) / 2, this.game.world.centerY - 15, style);

const errorStyle = {font: "22px Arial", fill: "#f00", align: "center"};
this.errorText = this.game.add.text(this.game.world.centerX, this.game.world.centerY + 45, '', errorStyle);
this.errorText = this.game.add.text(this.game.world.centerX, this.game.world.centerY + 24, '', {font: "24px Arial", fill: "#f00", align: "center"});
this.errorText.anchor.set(0.5, 0);

let login = this.game.add.button(this.game.world.centerX, this.game.world.centerY + 100, 'btn', this.onLogin, this, 'register.png', 'register.png', 'register.png');
let login = this.game.add.button(this.game.world.centerX, this.game.world.centerY + 100, 'btn', this.onLogin, this, 'login.png', 'login.png', 'login.png');
login.anchor.set(0.5);
},

onLogin: function () {
this.errorText.text = '';
if (!this.username.value) {
this.username.startFocus();
this.errorText.text = '请输入用户名';
return;
}
if (!this.password.value) {
this.password.startFocus();
this.errorText.text = '请输入密码';
return;
}
if (!this.passwordAgain.value) {
this.passwordAgain.startFocus();
this.errorText.text = '请再次输入密码';
return;
}
if (this.password.value !== this.passwordAgain.value) {
this.errorText.text = "两次输入的密码不一致";
return;
}

let that = this;
const payload = {
"email": this.username.value,
"username": this.username.value,
"password": this.password.value,
"password_repeat": this.password.value
};
post('/signup', payload, function(status, response) {
post('/login', payload, function(status, response) {
if (status === 200) {
PG.playerInfo = response;
that.state.start('MainMenu');
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
aiomysql~=0.0.20
bcrypt~=3.1
pyjwt~=1.7
sentry-sdk~=0.14
tornado~=6.0
Expand Down
13 changes: 6 additions & 7 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ CREATE DATABASE IF NOT EXISTS ddz DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_genera

CREATE TABLE IF NOT EXISTS ddz.account (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
unionid VARCHAR(50) NOT NULL UNIQUE,
nickname VARCHAR(50) NOT NULL,
openid VARCHAR(50) NOT NULL UNIQUE,
username VARCHAR(50) NOT NULL,
sex TINYINT(1) NOT NULL,
head_img_url VARCHAR(200),
ip_address CHAR(39) NOT NULL,
date_joined DATETIME(6) DEFAULT CURRENT_TIMESTAMP,
last_modified DATETIME(6) ON UPDATE CURRENT_TIMESTAMP
avatar VARCHAR(256),
date_joined DATETIME DEFAULT CURRENT_TIMESTAMP,
last_modified DATETIME ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS ddz.record (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
round VARCHAR(100) NOT NULL,
round TEXT NOT NULL,
last_modified DATETIME ON UPDATE CURRENT_TIMESTAMP
);

0 comments on commit 197111f

Please sign in to comment.