flask简单接口实现

安木 0

# -*- coding: utf-8 -*-
“””
@Author:5PING
@IDE:PyCharm
@Datetime:2022/7/2 22:35
“””
# coding:utf-8
import datetime
import json
import flask

from flask import request

server = flask.Flask(__name__)

users = [{‘mobile’: ‘13311111111’, ‘password’: ‘123456’}]

@server.route(‘/index’, methods=[‘get’])
def index():
res = {‘date’: str(datetime.datetime.now()), ‘code’: 200}
return json.dumps(res, ensure_ascii=False)

# 菜谱搜索
@server.route(‘/api/searchMenu’, methods=[‘get’])
def searchMenu():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: {
“total”: 103,
“num”: 20,
“list”: [
{
“id”: 3,
“classid”: 2,
“name”: “虾仁娃娃菜”,
“peoplenum”: “1-2人”,
“preparetime”: “10-20分钟”,
“cookingtime”: “10分钟内”,
“content”: “娃娃菜鲜中带甜,虾仁鲜香可口,一道营养丰富,味道清新的虾仁娃娃菜,可是老少皆宜的哦!”,
“pic”: “http://api.jisuapi.com/recipe/upload/20160719/115137_43195.jpg”,
“tag”: “减肥,宴请,家常菜,排毒,春季,滋阴润燥,热菜,白领,私房菜,美白”,
“material”: [
{
“mname”: “蒜子”,
“type”: 0,
“amount”: “适量”
},
]
}
]
}
}
return json.dumps(res, ensure_ascii=False)

# 菜谱分类
@server.route(‘/api/menuClass’, methods=[‘get’])
def menuClass():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: [
{
“classid”: 1,
“name”: “功效”,
“parentid”: 0,
“list”: [
{
“classid”: 2,
“name”: “减肥”,
“parentid”: 1
}
]
}
]
}
return json.dumps(res, ensure_ascii=False)

# 按分类检索
@server.route(‘/api/searchByClass’, methods=[‘get’])
def searchByClass():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: [
{
“classid”: 1,
“name”: “功效”,
“parentid”: 0,
“list”: [
{
“classid”: 2,
“name”: “减肥”,
“parentid”: 1
}
]
}
]
}
return json.dumps(res, ensure_ascii=False)

# 根据ID查询
@server.route(‘/api/menuDetail’, methods=[‘get’])
def menuDetail():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: [
{
“classid”: 1,
“name”: “功效”,
“parentid”: 0,
“list”: [
{
“classid”: 2,
“name”: “减肥”,
“parentid”: 1
}
]
}
]
}
return json.dumps(res, ensure_ascii=False)

# 获取新闻频道
@server.route(‘/api/getChannel’, methods=[‘get’])
def getChannel():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: [
{
“classid”: 1,
“name”: “功效”,
“parentid”: 0,
“list”: [
{
“classid”: 2,
“name”: “减肥”,
“parentid”: 1
}
]
}
]
}
return json.dumps(res, ensure_ascii=False)

# IP查询
@server.route(‘/api/searchIP’, methods=[‘get’])
def searchIP():
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: [
{
“classid”: 1,
“name”: “功效”,
“parentid”: 0,
“list”: [
{
“classid”: 2,
“name”: “减肥”,
“parentid”: 1
}
]
}
]
}
return json.dumps(res, ensure_ascii=False)

# 注册用户
@server.route(‘/api/register’, methods=[‘post’])
def register():
user = {}
user[‘mobile’] = request.args.get(“mobile”).strip()
user[‘password’] = request.args.get(“password”).strip()
user[‘name’] = request.args.get(“name”).strip()
user[‘industry’] = request.args.get(“industry”).strip()
user[‘birthday’] = request.args.get(“name”).strip()
user[‘birthplace’] = request.args.get(“name”).strip()
user[‘company’] = request.args.get(“name”).strip()
user[‘gender’] = request.args.get(“name”).strip()
user[‘job’] = request.args.get(“name”).strip()
user[‘signature’] = request.args.get(“name”).strip()
users.append(user)
res = dict(status=’true’, msg=”ok”, result=”注册成功”)
return json.dumps(res, ensure_ascii=False)

# 注册用户登录
@server.route(‘/api/registerLogin’, methods=[‘post’])
def registerLogin():
mobile = request.args.get(“mobile”).strip()
password = request.args.get(“password”).strip()
for i in users:
if i[‘mobile’] == mobile and i[‘password’] == password:
res = {“status”: ‘true’, “msg”: “ok”, “result”: “登录成功”}
else:
res = {“status”: ‘false’, “msg”: “ok”, “result”: “登录失败”}
res = json.dumps(res, ensure_ascii=False)
return res

# 修改密码
@server.route(‘/api/updatePW’, methods=[‘post’])
def updatePW():
mobile = request.args.get(“mobile”).strip()
password = request.args.get(“password”).strip()
for i in users:
if i[‘mobile’] == mobile:
i[‘password’] = password
res = {
“status”: ‘true’,
“msg”: “ok”,
“result”: “Password is updated.”
}
else:
res = {
“status”: ‘false’,
“msg”: “ok”,
“result”: “没有这样的用户.”
}
return json.dumps(res, ensure_ascii=False)

server.run(host=’127.0.0.1′, port=8099, debug=True)


发表回复