1

import os

from flask import Blueprint, jsonify, send_file
from setting import MDB, COVER_PATH, MUSIC_PATH, QRCODE_PATH

content_bp = Blueprint("content_bp", name)

@content_bp.route("/content_list", methods=["POST"])
def content_list():
content = list(MDB.Content.find({}))

for index, item in enumerate(content):
    content[index]["_id"] = str(item.get("_id"))

return jsonify(content)

@content_bp.route("/get_cover/", methods=["GET"])
def get_cover(filename):

cover_path = os.path.join(COVER_PATH, filename) # COVER_PATH = 'Cover' filename = 'xx.jpg'

return send_file(cover_path)

@content_bp.route("/get_music/", methods=["GET"])
def get_music(filename):

music_path = os.path.join(MUSIC_PATH, filename)

return send_file(music_path)

@content_bp.route("/toy_list", methods=["GET", 'POST'])
def toy_list():
toy = list(MDB.Devices.find({}))
for index, item in enumerate(toy):
toy[index]['_id'] = str(item.get('_id'))

return jsonify(toy)

from flask import Blueprint,request,jsonify
from setting import MDB,RET

devices_bp = Blueprint('devices_bp', name)

@devices_bp.route('/scan_qr',methods=['post'])
def scan_qr():
device = request.form.to_dict() # device key
print(device)
device_info = MDB.Devices.find_one(device)
if device_info:
RET['CODE'] = 0
RET['MSG'] = "扫描成功"
RET['DATE'] = device
# 描二维码成功
else:
# 扫描二维码失败 可能不是玩具二维码 数据库中未存在deviceKey
RET['CODE'] = 1
RET['MSG'] = "请扫描玩具二维码"
RET['DATE'] = {}

return jsonify(RET)

@devices_bp.route('/bind_toy',methods=['post'])
def bind_toy():
'''

:return:
'''

toy_info = request.form.to_dict()

toy_info['avatar'] = 'Toy.jpg'
toy_info['bind_user'] = toy_info.pop('user_id') # 删除并给一个新的值
toy_info['friend_list'] = []

MDB.Toys.insert_one(toy_info)

toy_id = MDB.Toys.insert_one(toy_info)
# MDB.Users.update_one({'_id':ObjectId(toy_info["bind_user"])})

RET['CODE'] = 0
RET['MSG'] = "绑定成功"
RET['DATE'] = {}

return jsonify(RET)
posted @ 2019-07-22 13:00  learnacode  阅读(86)  评论(0编辑  收藏  举报