# mock接口开发:
# 1、查询产品、开户接口 xxx
# 直接查看接口文档:http://127.0.0.1:8888/docs
import fastapi, uvicorn, tools
from fastapiimport Form
from starlette.requestsimport Request
server = fastapi.FastAPI()
@server.get('/login') # 装饰器
def login(username: str, password: str):
return {'username': username, 'password': password}
@server.get('/product')
def product():
return {
'code': 0,
'data': [
{'product_name': '朝朝盈1号', 'status': 0},
{'product_name': '朝朝盈2号', 'status': 1},
{'product_name': '朝朝盈3号', 'status': 2}
]
}
@server.get('/pay') # 装饰器
def pay(money: float, status='1'):
if status == '0':
return {'code': 1, 'status': 'fail'}
elif status == '1':
return {'code': 1, 'status': 'success', 'balance': money}
@server.post('/reg') # post请求注册接口
def pay(username: str, password: str, cpassword: str):
if username.strip() and password.strip() and cpassword.strip():
if password.strip() != cpassword.strip():
return {"两次输入的密码不一致,请重新输入"}
else:
select_sql = "select * from app_myuser where username='%s';" % username
if tools.execute_sql(select_sql):
return {'code': -1, 'msg': '用户名已存在'}
else:
passd = tools.my_md5(password)
insert_sql = "insert into app_myuser(username,passwd,) value('%s','%s');" % (username, passd)
tools.execute_sql(insert_sql)
return {'code': 1, 'msg': '账户注册成功'}
@server.post('/login') # post请求注册接口
def pay(request: Request, username: str = Form(...),
password: str = Form(...)): # 直接去请求体里面获取username和password键对应的值并自动转化成字符串类型
if username.strip() and password.strip():
pasd = tools.my_md5(password) # 先将密码进行加密
select_sql = "select * from app_myuser where username='%s' and passwd='%s';" % (username, pasd)
if tools.execute_sql(select_sql):
if username.strip() == username and password.strip() == pasd:
return {'code': 1, 'msg': '账户登录成功'}
elif username.strip() != username:
return {'code': -1, 'msg': '用户名不存在'}
else:
return {'code': -1, 'msg': '密码错误'}
uvicorn.run(server, port=8888, debug=True, host='0.0.0.0') # 外部调用host='0.0.0.0'
# 内测地址:http://127.0.0.1:8888/product
# 外部调用:http://192.168.1.1:8888/product
#Flask框架,mock接口
import flask, tools
server = flask.Flask(__name__)
@server.route('/login', methods=['post', 'get'])
def login():
username = flask.request.values.get('username')
password = flask.request.values.get('password')
if username.strip() and password.strip():
pasd = tools.my_md5(password) # 先将密码进行加密
select_sql = "select * from app_myuser where username='%s' and passwd='%s';" % (username, pasd)
if tools.execute_sql(select_sql):
if username.strip() == username and password.strip() == pasd:
return {'code': 1, 'msg': '账户登录成功'}
elif username.strip() != username:
return {'code': -1, 'msg': '用户名不存在'}
else:
return {'code': -1, 'msg': '密码错误'}
else:
return {'code': -1, 'msg': '不能为空'}
网友评论