from bson import ObjectId
from flask import Blueprint , request, jsonify
from setting import RET,MDB
devices_bp
devices_bp = Blueprint('devices_bp',name)
@devices_bp.route('/scan_qr',methods=['post'])
def scan_qr():
print(request.form) # ImmutableMultiDict([('device_key', '745d644b7f8c1e7f339864f33f3c78f2')])
device = request.form.to_dict()
device_info = MDB.Devices.find_one(device) # 忘了
if device_info:
RET['CODE'] = 0
RET['MSG'] = '二维码扫描成功'
RET['DATA'] = device
else :
RET['CODE'] = 1
RET['MSG'] = '请扫描玩具二维码'
RET['DATA'] = {}
# // 3.
# 二维码扫描成功, 但设备已经进行绑定
# {
# "code": 2,
# "msg": "设备已经进行绑定",
# "data":
# {
# "toy_id": toy_id
# }
# }
return jsonify(RET) # 返回前端数据,告诉前端的扫描情况
@devices_bp.route('/bind_toy',methods=['post'])
def bind_toy():
toy_info = request.form.to_dict() # {'toy_name': '小狐狸', 'baby_name': '小王子', 'remark': '爸爸', 'device_key': 'undefined', 'user_id': '5d35c755798c4257b6c65dfb'}
# 增加一些数据结构要求的信息,提交没有的
toy_info['avatar'] = 'toy.jpg'
user_id = toy_info.pop('user_id') # APPID
toy_info['bind_user'] = user_id
# toy_info['device_key'] = device_info.get('')
toy_info['friend_list'] = [] # 先给一个空
user_info = MDB.Users.find_one({'_id':ObjectId(user_id)}) # 这一步什么意思啊 和上一不toy_info 似的
print(user_info) #{'_id': ObjectId('5d35c755798c4257b6c65dfb'), 'username': '22', 'password': 'b6d767d2f8ed5d21a44b0e5886680cb9',
# 'nickname': '22', 'gender': '1', 'avatar': 'mama.jpg', 'bind_toys': ['5d35cd4dda21259fc796acc3', '5d35cd4dda21259fc796acc3'], 'friend_list': [{'friend_id': ObjectId('5d35cd4dda21259fc796acc3'), 'friend_nick': '小王子', 'friend_remark': '小狐狸', 'friend_avatar': 'toy.jpg', 'friend_type': 'toy', 'friend_chat': '5d35cd4dda21259fc796acc5'}, {'friend_id': ObjectId('5d35cd4dda21259fc796acc3'), 'friend_nick': '小王子', 'friend_remark': '小狐狸', 'friend_avatar': 'toy.jpg', 'friend_type': 'toy', 'friend_chat': '5d35cd4dda21259fc796acc5'}]}
chat_id = MDB.Chats.insert_one({'user_list':[],'chat_list':[]}) # 先新建了 Chat表之后,就自动生成了 我是先没有设置 然后设置了之后又回来添加的id
toy_add_user = {
"friend_id": user_id,
"friend_nick": user_info.get('nickname'),
"friend_remark": toy_info.get('remark'), # 我写错了 , 写成user_info 了,其实也可以用 user.avatar的值 截取出mama或者baba
"friend_avatar": user_info.get('avatar'),
"friend_chat": str(chat_id.inserted_id), # inserted时期的id 很方便
"friend_type": "app"
}
toy_info['friend_list'].append(toy_add_user)
toy_id = MDB.Toys.insert_one(toy_info) # 又可以取 他的id了
user_add_toy = {
"friend_id": str(toy_id.inserted_id), # 好友id 这个id怎么加啊 哦 是 toy的id 先要有了 数据库里的值才行
"friend_nick": toy_info.get('baby_name'), # 好友的昵称
"friend_remark": toy_info.get('toy_name'), # 好友备注
"friend_avatar": toy_info.get('avatar'), # 好友头像
"friend_chat": str(chat_id.inserted_id), # 私聊窗口ID 不是重了吗 ? 先重着把
# 聊天数据表对应值
"friend_type": "toy" # 好友类型
}
user_info['bind_toys'].append(str(toy_id.inserted_id)) # 互相加 toy 加的user_id 绑定toys 绑定用户
user_info['friend_list'].append(user_add_toy)
MDB.Users.update_one({'_id':ObjectId(user_id)},{'$set':user_info})
MDB.Chats.update_one({'_id':ObjectId(str(chat_id.inserted_id))},{"$set":{'user_list':[user_id,str(toy_id.inserted_id)]}})
RET['CODE'] = 0
RET['MSG'] = '绑定完成'
RET['DATA'] = {}
return jsonify(RET)
@devices_bp.route('/toy_list',methods=['post'])
def toy_list():
bind_user = request.form.get('_id')
toyl = list(MDB.Toys.find({'bind_user':bind_user}))
for toy in toyl:
toy['_id'] = str(toy.get('_id'))
RET['CODE'] = 0
RET['MSG'] ='获取Toy列表'
RET['DATA'] = toyl
return jsonify(RET)