Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
project-api
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
project-api
Commits
24ca7c6c
Commit
24ca7c6c
authored
Mar 30, 2018
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加登录系统和发布记录功能
parent
a1f65f08
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
12 deletions
+53
-12
common/decorator.py
common/decorator.py
+17
-5
common/error_type.py
common/error_type.py
+7
-1
daylife/record_service.py
daylife/record_service.py
+4
-4
daylife/user_service.py
daylife/user_service.py
+25
-2
No files found.
common/decorator.py
View file @
24ca7c6c
# coding=utf-8
from
functools
import
wraps
import
flask
from
flask
import
request
from
common
import
error_type
from
common.error
import
BussinessException
from
config
import
config
from
daylife.dao
import
user_dao
def
login_require
(
func
):
@
wraps
(
func
)
def
decorator
(
*
args
,
**
kwargs
):
user_id
=
request
.
args
.
get
(
'user_id'
)
token
=
request
.
args
.
get
(
'token'
)
if
not
user_id
or
not
token
:
raise
BussinessException
(
error_type
.
NEED_LOGIN
)
user_id
=
request
.
headers
.
get
(
'user_id'
)
#测试环境不需要token,但是必须要user_id,要不有些用到user_id的地方会出问题
if
not
user_id
:
raise
BussinessException
(
error_type
.
NEED_LOGIN
)
#正式环境对token进行校验
if
not
config
.
isDebug
:
token
=
request
.
headers
.
get
(
'token'
)
if
not
user_id
or
not
token
:
raise
BussinessException
(
error_type
.
NEED_LOGIN
)
user_token
=
user_dao
.
select_user_token_info
(
token
)
#token信息不存在或与存在信息不符合需要进行登录
if
not
user_token
or
user_token
.
user_id
!=
user_id
:
raise
BussinessException
(
error_type
.
NEED_LOGIN
)
return
func
(
*
args
,
**
kwargs
)
return
decorator
\ No newline at end of file
common/error_type.py
View file @
24ca7c6c
...
...
@@ -14,6 +14,12 @@ DEFAUL_ERROR = ErrorType(500, '服务器开小差了,请稍后再试')
VERIFY_CODE_EXPIRE
=
ErrorType
(
10000001
,
'验证码过期'
)
VERIFY_CODE_ERROR
=
ErrorType
(
1000000
0
2
,
'验证码错误'
)
VERIFY_CODE_ERROR
=
ErrorType
(
10000002
,
'验证码错误'
)
PHONE_EXIST
=
ErrorType
(
10000003
,
'号码已经注册,请直接登录'
)
NICKNAME_EXIST
=
ErrorType
(
10000004
,
'用户名已经存在'
)
USER_NOT_EXIST
=
ErrorType
(
10000005
,
'用户不存在,请先注册'
)
daylife/record_service.py
View file @
24ca7c6c
...
...
@@ -23,22 +23,22 @@ def get_my_record():
@
record
.
route
(
'/my_follow'
)
@
login_require
def
get_my_
attention
():
def
get_my_
follow
():
"""
我的关注人发布的记录
:return:
"""
pass
@
record
.
route
(
'/publish'
)
@
record
.
route
(
'/publish'
,
methods
=
[
'post'
,
'get'
]
)
@
login_require
def
publish_record
():
"""
发布关注信息
:return:
"""
user_id
=
request
.
value
s
.
get
(
'user_id'
)
content
=
request
.
arg
s
.
get
(
'content'
)
user_id
=
request
.
header
s
.
get
(
'user_id'
)
content
=
request
.
value
s
.
get
(
'content'
)
user_record
=
UserRecord
(
user_id
=
user_id
,
content
=
content
)
record_dao
.
add_record
(
user_record
)
return
http
.
BaseRes
(
message
=
'发布成功!'
)
.
to_json
()
...
...
daylife/user_service.py
View file @
24ca7c6c
# coding=utf-8
import
uuid
from
flask
import
Blueprint
,
request
from
sqlalchemy.exc
import
IntegrityError
from
common
import
error_type
from
common.decorator
import
login_require
...
...
@@ -31,12 +34,13 @@ def login():
else
:
check_verify_code
(
phone
,
verify_code
)
if
not
user_dao
.
select_by_phone
(
phone
):
user_dao
.
insert_user
(
UserInfo
(
phone
=
phone
,
nickname
=
'黑米包子'
)
)
raise
BussinessException
(
error_type
.
USER_NOT_EXIST
)
user_info
=
user_dao
.
select_by_phone
(
phone
)
token
=
user_dao
.
create_token
(
user_info
)
return
http
.
BaseRes
(
data
=
UserInfoVo
(
user_info
.
phone
,
user_info
.
nickname
,
user_info
.
id
,
token
))
.
to_json
()
def
check_verify_code
(
phone
,
verify_code
):
"""
校验用户发来的验证码
...
...
@@ -53,6 +57,25 @@ def check_verify_code(phone, verify_code):
redis_service
.
delete
(
verify_code_key
)
@
user
.
route
(
'/register'
)
def
register
():
"""
用户注册接口
:return:
"""
phone
=
request
.
args
.
get
(
'phone'
)
verify_code
=
request
.
args
.
get
(
'verify_code'
)
nickname
=
request
.
args
.
get
(
'nickname'
)
check_verify_code
(
phone
,
verify_code
)
try
:
user_dao
.
insert_user
(
UserInfo
(
phone
=
phone
,
nickname
=
nickname
))
except
IntegrityError
as
e
:
message
=
e
.
orig
.
args
[
1
]
if
'phone'
in
message
:
raise
BussinessException
(
error_type
.
PHONE_EXIST
)
if
'nickname'
in
message
:
raise
BussinessException
(
error_type
.
NICKNAME_EXIST
)
return
http
.
BaseRes
()
.
to_json
()
@
user
.
route
(
'/logout'
)
def
logout
():
...
...
@@ -71,7 +94,7 @@ def send_verify_code():
:return:
"""
phone
=
request
.
args
.
get
(
'phone'
)
verify_code
=
request
.
args
.
get
(
'verify_code'
)
verify_code
=
str
(
uuid
.
uuid1
())[
0
:
4
]
res
=
sms_service
.
send_verify_code
(
phone
,
verify_code
)
if
res
==
'ok'
:
redis_service
.
put_with_expire
(
USER_VERIFY_CODE_PREFIX
+
phone
,
verify_code
,
5
*
60
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment