Skip to content

Commit a043d59

Browse files
committed
增加GET/POST接口
1 parent 69cad3b commit a043d59

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

api/__init__.py

Whitespace-only changes.

api/user.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from flask import Flask, jsonify, request
2+
3+
app = Flask(__name__)
4+
app.config["JSON_AS_ASCII"] = False # jsonify返回的中文正常显示
5+
6+
data = [
7+
{"id": 1, "username": "小明", "password": "123456", "role": 0, "sex": 0, "telephone": "10086", "address": "北京市海淀区"},
8+
{"id": 2, "username": "李华", "password": "abc", "role": 1, "sex": 0, "telephone": "10010", "address": "广州市天河区"},
9+
{"id": 3, "username": "大白", "password": "666666", "role": 0, "sex": 1, "telephone": "10000", "address": "深圳市南山区"}
10+
]
11+
12+
13+
@app.route('/')
14+
def hello_world():
15+
return 'Hello World!'
16+
17+
18+
@app.route("/users", methods=["GET"])
19+
def get_all_users():
20+
"""获取所有用户信息"""
21+
return jsonify({"code": "0", "data": data, "msg": "操作成功"})
22+
23+
24+
@app.route("/users/<int:user_id>", methods=["GET"])
25+
def get_user(user_id):
26+
"""获取某个用户信息"""
27+
if user_id > 0 and user_id <= len(data):
28+
return jsonify({"code": "0", "data": data[user_id - 1], "msg": "操作成功"})
29+
return jsonify({"code": "1", "msg": "用户不存在"})
30+
31+
32+
@app.route("/register", methods=['POST'])
33+
def user_register():
34+
username = request.json.get("username").strip() # 用户名
35+
password = request.json.get("password").strip() # 密码
36+
sex = request.json.get("sex", "0").strip() # 性别,默认为0(男性)
37+
telephone = request.json.get("telephone", "").strip() # 手机号,默认为空
38+
address = request.json.get("telphone", "").strip() # 地址,默认为空
39+
if username and password and telephone:
40+
import re
41+
if username == "wintest":
42+
return jsonify({"code": 2002, "msg": "用户名已存在!!!"})
43+
elif not (sex == "0" or sex == "1"):
44+
return jsonify({"code": 2003, "msg": "输入的性别只能是 0(男) 或 1(女)!!!"})
45+
elif not (len(telephone) == 11 and re.match("^1[3,5,7,8]\d{9}$", telephone)):
46+
return jsonify({"code": 2004, "msg": "手机号格式不正确!!!"})
47+
else:
48+
return jsonify({"code": 0, "msg": "恭喜,注册成功!"})
49+
else:
50+
return jsonify({"code": 2001, "msg": "用户名/密码/手机号不能为空,请检查!!!"})
51+
52+
53+
@app.route("/login", methods=['POST'])
54+
def user_login():
55+
username = request.values.get("username")
56+
password = request.values.get("password")
57+
if username and password:
58+
if username == "wintest" and password == "123456":
59+
return jsonify({"code": 0, "msg": "恭喜,登录成功!"})
60+
return jsonify({"code": 1002, "msg": "用户名或密码错误!!!"})
61+
else:
62+
return jsonify({"code": 1001, "msg": "用户名或密码不能为空!!!"})
63+
64+
65+
if __name__ == '__main__':
66+
app.run()

app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/')
6+
def hello_world():
7+
return 'Hello World!'
8+
9+
if __name__ == '__main__':
10+
app.run()

0 commit comments

Comments
 (0)