from bson import ObjectId
from flask import Blueprint,request,jsonify from setting import MDB,RET import datetimedevices_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() # 获取玩具的信息print(toy_info)# {'toy_name': '小狐狸', 'baby_name': '小主人', 'remark': '爸爸', 'device_key': 'undefined', 'user_id': '5d351fe2e1361657b745d191'}# {'_id': ObjectId('5d351fe2e1361657b745d191'), 'username': '3', 'password': 'eccbc87e4b5ce2fe28308fd9f2a7baf3', 'nickname': '3', 'gender': '1', 'avatar': 'mama.jpg', 'bind_toys': [], 'friend_list': []}toy_info['avatar'] = 'toy.jpg' # 加入图片? 信息user_id = toy_info.pop("user_id") # 删除获得user_id的值toy_info['bind_user'] = user_id # 并给一个新的值 # // 玩具的绑定用户toy_info['friend_list'] = [] # // 玩具通讯录信息user_info = MDB.Users.find_one({'_id':ObjectId(user_id)}) #print(user_info,user_id)toy_add_user = { "friend_id": user_id, # app id "friend_nick": user_info.get('nickname'), # user 昵称 "friend_remark": user_info.get('remark'), # remark在哪里呢? "friend_avatar": user_info.get('avatar'), # user有头像吗? # "friend_chat": user_id, "friend_type": "app" # 好友的用户类型 app / toy }toy_info["friend_list"].append(toy_add_user)toy_id = MDB.Toys.insert_one(toy_info)print(toy_info)# print(list(toy_id)) # TypeError: 'InsertOneResult' object is not iterable# print()# '''user_add_toy = { "friend_id": toy_id.inserted_id, # toy_id str "friend_nick": toy_info.get('baby_name'), # baby_name "friend_remark": toy_info.get('toy_name'), # toy_name "friend_avatar": toy_info.get('avatar'), # 阿凡达 # "friend_chat": toy_id.inserted_id, # chat_id # // 私聊窗口ID 聊天数据表对应值 "friend_type": "toy" # 好友的类型 toy }user_info["bind_toys"].append(str(toy_id.inserted_id))user_info["friend_list"].append(user_add_toy)print(user_info,toy_info)# MDB.Users.update_one({'_id':ObjectId(toy_info["bind_user"])})# 修改 Users 的全部数据MDB.Users.update_one({'_id':ObjectId(user_id)},{"$set":user_info})# Chats 数据也会变化 user_list player_list 将 toy_id 和 user_id 加入# MDB.Chats.update_one({},{"$set":{"user_list":["toy_id","user_id"]}})u_chat_list = []t_chat_list = []u_chat_t = { "from_user": user_id, # 信息发送方ID "to_user": toy_id.inserted_id, # 信息接收方ID "chat": "回家吃饭?", # 语音消息文件名 "createTime": datetime.datetime.now() # 聊天创建时间}# MDB.Chats.insert_one(u_chat_t)# chat_id = MDB.Chats.insert_one(u_chat_t).inserted_id# print(chat_id)# toy_add_user['friend_chat'] = chat_idt_chat_u = { "from_user": toy_id.inserted_id, # 信息发送方ID "to_user": user_id, # 信息接收方ID "chat": "在网吧呢!", # 语音消息文件名 "createTime": datetime.datetime.now() # 聊天创建时间}u_chat_list.append(u_chat_t)t_chat_list.append(t_chat_u)MDB.Chats.insert_one({'chat_list':t_chat_list})MDB.Chats.insert_one({'chat_list':u_chat_list})id1 = str(list(MDB.Chats.find({'chat_list': t_chat_list}))[0].get('_id')) # u_add 的聊天的 那个idid2 = str(list(MDB.Chats.find({'chat_list': u_chat_list}))[0].get('_id'))# toy_add_user['friend_chat'] = id1# user_add_toy['friend_chat'] = id1# print(id1,id2,user_add_toy)# print(toy_add_user,user_add_toy,type(toy_add_user),type(user_add_toy)) #添加成元祖了怎么整# 把聊天id 传给friend , 然后 保存到数据库print(1)toy_add_user['friend_chat'] = id1user_add_toy['friend_chat'] = id2toy_info["friend_list"].append(toy_add_user)MDB.Toys.update({'_id': ObjectId(str(toy_id.inserted_id))}, {"$set": toy_info})print(2)user_info["bind_toys"].append(str(toy_id.inserted_id))user_info["friend_list"].append(user_add_toy)MDB.Users.update_one({'_id': ObjectId(str(user_id))}, {"$set": user_info})# MDB.Chats.insert_one({'chat_list':t_chat_list})# MDB.Chats.insert_one({'chat_list':u_chat_list})print(3)user_toy = [user_id, str(toy_id.inserted_id)]toy_user = [str(toy_id.inserted_id), user_id]MDB.Chats.update_one({'chat_list': t_chat_list}, {"$set": {'user_list': user_toy}})MDB.Chats.update_one({'chat_list': u_chat_list}, {"$set": {'user_list': toy_user}})print(4)# print(str(list(MDB.Chats.find({'chat_list':t_chat_list}))[0].get('_id')))# chat_id = MDB.Chats.insert_one(u_chat_t).inserted_id# print(chat_id)# print(list(MDB.Chats.find({})))RET["CODE"] = 0RET["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'] = 0RET['MSG'] = '获取Toy列表'RET["DATA"] = toylreturn jsonify(RET)