def groupChatCreate(self, user_info, params): """ 创建聊天群组 @Param dict userInfo @param dict params @return bool """ room_uuid = Utils.unique_id() name = '' now_item = int(time.time()) for id in params['ids']: user_data = Users().getOne({Users.id == id}) name = name + ',' + user_data['nick_name'] userRoomRelationData = { 'user_id': id, 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0, 'updated_at': now_item, 'created_at': now_item } UserRoomRelation().add(userRoomRelationData) room_data = { 'room_uuid': room_uuid, 'last_msg': '', 'type': 1, 'updated_at': now_item, 'created_at': now_item, 'name': name.strip(','), 'user_id': user_info['data']['id'] } room = Room().addByClass(room_data) return {'room_uuid': room_uuid}
def add(params,user_info): ''' 添加通讯录 ''' if params['focused_user_id'] == user_info['data']['id']: return Utils.formatError(Code.BAD_REQUEST,msg='无法添加自己为新朋友') filters = { AddressBook.focused_user_id == params['focused_user_id'], AddressBook.be_focused_user_id == user_info['data']['id'] } addressBookData = AddressBook().getOne(filters) if(addressBookData == None): # 加入房间号 room_uuid = Utils.unique_id() # 建立通讯录关系 status = AddressBook.addRoomAndAddressBook( room_uuid, params['focused_user_id'], user_info['data']['id']) if status == False: return Utils.formatError(Code.BAD_REQUEST,msg='添加失败') #回复被添加用户 socketio.emit('beg', Utils.formatBody({ 'action':'beg_add_success', 'focused_user_id' : user_info['data']['id'] }), namespace='/room', room='@broadcast.'+str(params['focused_user_id'])) #添加后同步房间 addressBookData = AddressBook.get(room_uuid) for item in addressBookData: roomList = AddressBook.getRoomList(item.be_focused_user_id)['data'] socketio.emit('room',Utils.formatBody(roomList), namespace="/room",room='@broadcast.'+str(item.be_focused_user_id)) return Utils.formatBody({},msg='添加成功') return Utils.formatError(Code.BAD_REQUEST,msg='已添加')
def beg(params, user_info): if user_info['data']['id'] == params['be_focused_user_id']: return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], '无法添加自己为新朋友') addressBookdata = AddressBook.getAdddressBookByFocusedUserId( user_info['data']['id'], params['be_focused_user_id']) if addressBookdata != None: return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], '已添加') msg_uuid = Utils.unique_id() userData = Users().getOne({Users.id == user_info['data']['id']}) data = { 'data': userData, 'action': 'beg_add', 'msg_uuid': msg_uuid, 'be_focused_user_id': params['be_focused_user_id'], 'focused_user_id': user_info['data']['id'] } socketio.emit('beg', Utils.formatBody(data), namespace='/api', room='@broadcast.' + str(params['be_focused_user_id'])) #,callback=success) #只缓存最新的一条 cache.set('@broadcast.beg' + str(params['be_focused_user_id']), data) return Utils.formatBody({}, msg='发送成功')
def json(self, body={}): if (DEBUG_LOG): debug_id = Utils.unique_id() log().debug( json.dumps({ 'LOG_ID': debug_id, 'IP_ADDRESS': request.remote_addr, 'REQUEST_URL': request.url, 'REQUEST_METHOD': request.method, 'PARAMETERS': request.args, 'RESPONSES': body })) body['debug_id'] = debug_id return jsonify(body)
def upload(): f = request.files['file'] filename = secure_filename(f.filename) if (f and Utils.allowed_file(filename)): file_suffix = f.filename.split('.')[1] path = UPLOAD_FOLDER + Utils.unique_id() + '.' + file_suffix full_path = os.getcwd() + path f.save(full_path) return BaseController().successData(data={ 'path': path, 'name': f.filename }, msg='图片提交成功') return BaseController().error('文件类型不允许')
def addressBookBeg(params): if params['focused_user_id'] == params['be_focused_user_id']: return BaseController().error(msg='无法添加自己为新朋友') addressBookdata = AddressBook.getAdddressBookByFocusedUserId(params['focused_user_id'], params['be_focused_user_id']) if addressBookdata != None: return BaseController().error(msg='已添加') msg_uuid = Utils.unique_id() userData = Users().getOne({Users.id == params['focused_user_id']}) socketio.emit('beg', Utils.formatBody({ 'data':userData, 'action':'beg_add', 'msg_uuid': msg_uuid, 'be_focused_user_id': params['be_focused_user_id'] }), namespace='/room', room='@broadcast.'+str(params['be_focused_user_id']), callback=success) return BaseController().successData(msg='发送成功')
def uploadBase64(params): userImg = params['imgDatas'].split(',')[1] typeImg = params['imgDatas'].split(',')[0] if 'png' in typeImg: typeImg = 'png' elif 'gif' in typeImg: typeImg = 'gif' elif 'jpeg' in typeImg: typeImg = 'jpeg' imgdata = base64.b64decode(userImg) path = UPLOAD_FOLDER + Utils.unique_id() + '.' + typeImg file = open(os.getcwd() + path, 'wb') file.write(imgdata) file.close() return Utils.formatBody({'path': path}, '上传成功')
def uploadBase64(params): userImg = params['imgDatas'].split(',')[1] typeImg = params['imgDatas'].split(',')[0] if 'png' in typeImg: typeImg = 'png' elif 'gif' in typeImg: typeImg = 'gif' elif 'jpeg' in typeImg: typeImg = 'jpeg' imgdata = base64.b64decode(userImg) path = UPLOAD_FOLDER + Utils.unique_id() + '.' + typeImg file = open(os.getcwd() + path, 'wb') file.write(imgdata) file.close() return BaseController().successData(data={'path': path}, msg='图片提交成功')
def ExceptionApi(code, e): """ 接口异常处理 """ exc_type, exc_value, exc_traceback = sys.exc_info() if DEBUG_LOG: if SAVE_LOG == 1: log().exception(e) elif SAVE_LOG == 2: LogService().add(e, 1, 3) #导致文件互相引用 body = {} body['error_code'] = code body['error'] = True body['show'] = False body['debug_id'] = Utils.unique_id() dBSession.close() #这里exc_type 和exc_value信息重复,所以不打印 body['traceback'] = traceback.format_exception([], exc_value, exc_traceback) return make_response(jsonify(body))
def json(self, body={}): if DEBUG_LOG: debug_id = Utils.unique_id() data = { 'LOG_ID': debug_id, 'IP_ADDRESS': request.remote_addr, 'REQUEST_URL': request.url, 'REQUEST_METHOD': request.method, 'PARAMETERS': request.args, 'RESPONSES': body } if SAVE_LOG == 1: log().debug(data) elif SAVE_LOG == 2: LogService().add(json.dumps(data), 1, 2) body['debug_id'] = debug_id return jsonify(body)
def SocketExceptionApi(code, e): """ 接口异常处理 """ exc_type, exc_value, exc_traceback = sys.exc_info() error_trace = traceback.format_exception(exc_type, exc_value, exc_traceback) if DEBUG_LOG: if SAVE_LOG == 1: log().exception(error_trace) elif SAVE_LOG == 2: LogService().add(json.dumps(error_trace), 1, 3) #导致文件互相引用 body = {} body['error_code'] = code body['error'] = True body['show'] = False body['debug_id'] = Utils.unique_id() #这里exc_type 和exc_value信息重复,所以不打印 body['traceback'] = error_trace dBSession.close() return body
def upload(params): filename = secure_filename(params['name']) base64Data = params['dataUrl'].split(',')[1] if (base64Data and Utils.allowed_file(filename)): file_suffix = params['name'].split('.')[1] path = UPLOAD_FOLDER + Utils.unique_id() + '.' + file_suffix full_path = os.getcwd() + path f = open(full_path, 'wb') data = base64.b64decode(base64Data) f.write(data) #f.write(params['arrayBuffer']) f.close return Utils.formatBody({ 'path': path, 'name': filename }, msg='上传成功') return Utils.formatError(Code.BAD_REQUEST, '文件类型不允许')
def imgSwitch(): """ 接收图片uuid并转换成图片 """ rules = {'uuid': {'required': True, 'type': 'string'}} error_msg = {'uuid': {'required': u'唯一id是必须的', 'type': u'唯一id必须是字符串'}} error = BaseController().validateInput(rules, error_msg) if (error is not True): return error uuid = request.json.get('uuid') data = ImgShard.getData(uuid) base64Data = '' for i in data: base64Data = base64Data + i['imgString'] userImg = base64Data.split(',')[1] imgdata = base64.b64decode(userImg) rela_path = "/uploads/" + Utils.unique_id() + '.jpg' path = os.getcwd() + rela_path file = open(path, 'wb') file.write(imgdata) file.close() return BaseController().successData(data={"url": rela_path}, msg='图片提交成功')
def addressBookAdd(params): if params['focused_user_id'] == params['be_focused_user_id']: return BaseController().error(msg='无法添加自己为新朋友') filters = { AddressBook.focused_user_id == params['focused_user_id'], AddressBook.be_focused_user_id == params['be_focused_user_id'] } addressBookData = AddressBook().getOne(filters) if(addressBookData == None): # 加入房间号 room_uuid = Utils.unique_id() # 建立通讯录关系 status = AddressBook.addRoomAndAddressBook( room_uuid, params['focused_user_id'], params['be_focused_user_id']) if status == False: return BaseController().error(msg='添加失败') #回复被添加用户 socketio.emit('beg', Utils.formatBody({ 'action':'beg_add_success' }), namespace='/room', room='@broadcast.'+str(params['focused_user_id'])) return BaseController().successData(msg='添加成功') return BaseController().error(msg='已添加')
def adminCreateRoom(message): admin_user_info = UsersAuthJWT().adminIdentify( message['Authorization']) if isinstance(admin_user_info, str): return Utils.formatError(CONST['CODE']['ERROR_AUTH_CHECK_TOKEN_FAIL']['value'], admin_user_info) filters = { Admin.id == admin_user_info['data']['id'], } admin_user_info = Admin().getOne(filters) filters = { AddressBook.be_focused_user_id == message['user_id'], AddressBook.focused_user_id == admin_user_info['id'], AddressBook.type == 1 } addressBookInfo = AddressBook().getOne(filters) if addressBookInfo == None: room_uuid = Utils.unique_id() # 建立通讯录关系 status = AddressBook.adminAddRoomAndAddressBook( room_uuid, admin_user_info, message['user_id']) if status == False: return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], msg='添加失败') # 添加后同步房间 addressBookData = AddressBook.get(room_uuid) for item in addressBookData: roomList = AddressBook.getRoomList( item.be_focused_user_id)['list'] if item['type'] == CONST['ADDRESSBOOK']['ADMIN']['value']: socketio.emit('room', Utils.formatBody( roomList), namespace="/api", room='@broadcast.'+str(item.be_focused_user_id)) else: socketio.emit('room', Utils.formatBody( roomList), namespace="/api", room='@broadcast.'+str(item.be_focused_user_id)) else: room_uuid = addressBookInfo['room_uuid'] return Utils.formatBody({'room_uuid': room_uuid})
def groupChatCreate(params, user_info): """ 创建聊天群组,userInfo.id是群主 @Param dict userInfo @param dict params @return bool """ room_uuid = Utils.unique_id() name = '' userRoomRelationData = { 'user_id': user_info['data']['id'], 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0, 'type':CONST['GROUP']['MASTER']['value'] } UserRoomRelation().add(userRoomRelationData) for id in params['ids']: user_data = Users().getOne({Users.id == id}) name = name + ',' + user_data['nick_name'] userRoomRelationData = { 'user_id': id, 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0 } UserRoomRelation().add(userRoomRelationData) room_data = { 'room_uuid': room_uuid, 'last_msg': '', 'type': CONST['CHAT']['TEXT']['value'], 'name': name.strip(','), 'user_id': user_info['data']['id'] } Room().addByClass(room_data) return {'room_uuid': room_uuid, 'name': name.strip(',')}
def documentUploadBase64(): """上传base64形式文件并杨峥 需要前端传入文件类型 """ # 二维数组验证 rules = { 'userImgOne': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': True, 'type': 'string', 'minlength': 2 }, 'name': { 'required': True, 'type': 'string', 'minlength': 2 }, 'size': { 'required': True, 'type': 'integer', 'minlength': 2 }, 'type': { 'required': True, 'type': 'string', 'minlength': 2 } } }, 'userImgTwo': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': True, 'type': 'string', 'minlength': 2 }, 'name': { 'required': True, 'type': 'string', 'minlength': 2 }, 'size': { 'required': True, 'type': 'integer', 'minlength': 2 }, 'type': { 'required': True, 'type': 'string', 'minlength': 2 } } }, 'userImgThree': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': True, 'type': 'string', 'minlength': 2 }, 'name': { 'required': True, 'type': 'string', 'minlength': 2 }, 'size': { 'required': True, 'type': 'integer', 'minlength': 2 }, 'type': { 'required': True, 'type': 'string', 'minlength': 2 } } } } error_msg = { 'userImgOne': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': u'图一是必须的', 'type': u'图一必须是字符串', 'minlength': u'图一字符最小是2' }, 'name': { 'required': u'图一是必须的', 'type': u'图一必须是字符串', 'minlength': u'图一字符最小是2' }, 'size': { 'required': u'图一是必须的', 'type': u'图一必须是字符串', 'minlength': u'图一字符最小是2' }, 'type': { 'required': u'图一是必须的', 'type': u'图一必须是字符串', 'minlength': u'图一字符最小是2' } } }, 'userImgTwo': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': u'图二是必须的', 'type': u'图二必须是字符串', 'minlength': u'图二字符最小是2' }, 'name': { 'required': u'图二是必须的', 'type': u'图二必须是字符串', 'minlength': u'图二字符最小是2' }, 'size': { 'required': u'图二是必须的', 'type': u'图二必须是整数', 'minlength': u'图二字符最小是2' }, 'type': { 'required': u'图二是必须的', 'type': u'图二必须是字符串', 'minlength': u'图二字符最小是2' } } }, 'userImgThree': { 'type': 'dict', 'schema': { 'imgBase64': { 'required': u'图三是必须的', 'type': u'图三必须是字符串', 'minlength': u'图三字符最小是2' }, 'name': { 'required': u'图三是必须的', 'type': u'图三必须是字符串', 'minlength': u'图三字符最小是2' }, 'size': { 'required': u'图三是必须的', 'type': u'图三必须是整数', 'minlength': u'图三字符最小是2' }, 'type': { 'required': u'图三是必须的', 'type': u'图三必须是字符串', 'minlength': u'图三字符最小是2' } } } } error = BaseController().validateInput(rules, error_msg) if (error is not True): return error # 这边图片类型,大小判断请根据需求自己判断,暂不展开 for (k, v) in request.json.items(): userImg = v['imgBase64'].split(',')[1] imgdata = base64.b64decode(userImg) path = os.getcwd() + "/uploads/" + Utils.unique_id() + '.jpg' file = open(path, 'wb') file.write(imgdata) file.close() """userImgOne = request.json.get('userImgOne')['imgBase64'].split(',')[1] userImgTwo = request.json.get('userImgTwo')['imgBase64'].split(',')[1] userImgThree = request.json.get('userImgThree')['imgBase64'].split(',')[1] imgdata = base64.b64decode(userImgOne) """ return BaseController().successData(msg='图片提交成功')