1. 程式人生 > >接口-用戶登錄,返回session

接口-用戶登錄,返回session

log TP 當前 values dex server () gin 存在

   # 2、參照接口文檔上
# http://doc.nnzhp.cn/index.php?s=/6&page_id=12 登錄接口
# 登錄成功之後,返回seesionid,用戶登錄時間
# sessionid
# 用戶名+當前的時間戳 md5,seesion失效時間是60分鐘
# sessionid:niuhanyang
# {"seessionid":a5dfcb047721e02a6f8bff779c815165,"login_time":201805051820}
# 如果這個用戶已經登錄過了,那麽就返回他的seesionid,登錄時間
import flask,time,json
from z_lib.tools import m_redis,md5
server=flask.Flask(__name__)
@server.route(‘/index‘,methods=[‘POST‘])
def index():
username=flask.request.values.get(‘username‘)
passwd=flask.request.values.get(‘passwd‘)
if username and passwd:
key_start="my_userz:%s"%username
if m_redis(key_start):
res={‘msg‘:‘用戶以存在 ‘,‘msg_code‘:3001}
else:
m_redis(key_start,md5(passwd))
res={‘msg‘:‘註冊成功 ‘,‘msg_code‘:0}
else:
res={‘msg‘:‘必填字段未填,請查看接口文檔!‘,‘msg_code‘:1001}
return json.dumps(res,ensure_ascii=False)

@server.route(‘/login‘,methods=[‘POST‘])
def login():
username=flask.request.values.get(‘username‘)
pwd=flask.request.values.get(‘passwd‘)
if username and pwd:
key_start="my_userz:%s"%username
passwd=m_redis(key_start)
if passwd:
if md5(pwd)==passwd.decode():
login_key="my_session:%s"%username
session=m_redis(login_key)
if session:
res=json.loads(session)
print(‘第二次‘)
else:
print(‘第一次‘)
session_id=md5(username+str(int(time.time())))
now=time.strftime(‘%Y%m%d%H%M%S‘)
res={‘session‘:session_id,‘time‘:now}
m_redis(login_key,json.dumps(res,ensure_ascii=False),600)
else:
res={‘msg‘:‘密碼錯誤 ‘,‘msg_code‘:3001}
else:
res={‘msg‘:‘用戶不存在 ‘,‘msg_code‘:0}
else:
res={‘msg‘:‘必填字段未填,請查看接口文檔!‘,‘msg_code‘:1001}
return json.dumps(res,ensure_ascii=False)
server.run(port=7878,debug=True)

接口-用戶登錄,返回session