def add(self, data=[]):
     #添加
     if 'update_time' in data.keys():
         data['update_time'] = int(time.time())
     #判断用户名
     if 'name' in data.keys():
         filters = {Admin.delete_time == 0, Admin.name == data['name']}
         adminData = Admin().getOne(filters, 'add_time desc')
         if adminData != None:
             return Utils.formatError(CONST['CODE']['ERROR']['value'],
                                      '用户名已经存在')
     #判断手机号码
     if 'mobile' in data.keys():
         filters = {Admin.delete_time == 0, Admin.mobile == data['mobile']}
         admin = Admin().getOne(filters, 'add_time desc')
         if admin:
             return Utils.formatError(CONST['CODE']['ERROR']['value'],
                                      '手机号码已经存在')
     #判断邮箱
     if 'email' in data.keys():
         filters = {Admin.delete_time == 0, Admin.email == data['email']}
         admin = Admin().getOne(filters, Admin.add_time.desc())
         if admin:
             return Utils.formatError(CONST['CODE']['ERROR']['value'],
                                      '邮箱已经存在')
     #生成一个md5对象
     m1 = hashlib.md5()
     #使用md5对象里的update方法md5转换
     m1.update(data['pwd'].encode("utf-8"))
     data['pwd'] = m1.hexdigest()
     res = Admin().add(data)
     if res:
         return Utils.formatBody(msg='添加成功')
     return Utils.formatError(CONST['CODE']['ERROR']['value'], '添加失败')
 def login(self, data):
     name = data['name']
     #生成一个md5对象
     m1 = hashlib.md5()
     #使用md5对象里的update方法md5转换
     m1.update(data['pwd'].encode("utf-8"))
     pwd = m1.hexdigest()
     #用户名/邮箱/手机登录
     filters = {
         Admin.name == name, Admin.pwd == pwd, Admin.delete_time == 0
     }
     admin = Admin().getOne(filters, 'add_time desc')
     if admin == None:
         filters = {
             Admin.email == name, Admin.pwd == pwd, Admin.delete_time == 0
         }
         admin = Admin().getOne(filters, 'add_time desc')
         if admin == None:
             filters = {
                 Admin.mobile == name, Admin.pwd == pwd,
                 Admin.delete_time == 0
             }
             admin = Admin().getOne(filters, 'add_time desc')
     if admin != None:
         #更新登录时间
         filters = {Admin.id == admin['id']}
         update_time = int(time.time())
         Admin().edit({Admin.update_time: update_time}, filters)
         token = UsersAuthJWT.encode_auth_token(admin['id'], update_time)
         return Utils.formatBody({'token': token, 'user': admin})
     return Utils.formatError(CONST['CODE']['ERROR']['value'])
Beispiel #3
0
    def edit(self, data, filters = {}):
    
        if 'update_time' in data.keys():
            data['update_time'] = time.time()
        adminData = Admin().getOne(filters, 'add_time desc')
        if adminData == None:
            return Utils.formatError(Code.ERROR, '用户不存在')
        #判断用户名
        if 'name' in data.keys():
            filters = {
                Admin.delete_time == 0,
                Admin.name        == data['name'],
                Admin.id          != adminData['id']
            }
            admin = Admin().getOne(filters, 'add_time desc')
            if admin != None:
                return Utils.formatError(Code.ERROR, '用户名已经存在')


        
        #判断手机号码
        if 'mobile' in data.keys():
            filters = {
                Admin.delete_time == 0,
                Admin.mobile      == data['mobile'],
                Admin.id          != adminData['id']
            }
            admin = Admin().getOne(filters, 'add_time desc')
            if admin != None:
                return Utils.formatError(Code.ERROR, '手机号码已经存在')
        
        #判断邮箱
        if 'email' in data.keys():
            filters = {
                Admin.delete_time == 0,
                Admin.email       == data['email'],
                Admin.id          != adminData['id']
            }
            admin = Admin().getOne(filters, Admin.add_time.desc())
            if admin != None:
                return Utils.formatError(Code.ERROR, '邮箱已经存在')
        
        if 'pwd' in data.keys():
            #生成一个md5对象
            m1 = hashlib.md5()
            #使用md5对象里的update方法md5转换
            m1.update(data['pwd'].encode("utf-8"))
            data['pwd'] = m1.hexdigest()      
        res = Admin().edit(data, filters)
        if res==True:
            return Utils.formatBody(msg='编辑成功')
        return Utils.formatError(Code.ERROR, '编辑失败', 0)
Beispiel #4
0
 def adminIdentify(self, auth_header):
     """
     用户鉴权
     :return: list
     """
     if (auth_header):
         auth_tokenArr = auth_header.split(" ")
         if (not auth_tokenArr or auth_tokenArr[0] != 'JWT'
                 or len(auth_tokenArr) != 2):
             return '请传递正确的验证头信息'
         else:
             auth_token = auth_tokenArr[1]
             payload = self.decode_auth_token(auth_token)
             if not isinstance(payload, str):
                 user = Admin().getOne({Admin.id == payload['data']['id']})
                 if (user is None):
                     return '找不到该用户信息'
                 else:
                     if (user['update_time'] == payload['data']['updated_at']):
                         result = payload
                     else:
                         return '令牌失效'
             else:
                 result = payload
     else:
         return '没有提供认证令牌'
     return result
Beispiel #5
0
    def adminChat(message: dict) -> dict:
        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)
        default_img_data = Config().getOne(
            {Config.type == 'img', Config.code == 'default.img', Config.status == 1})
        if default_img_data == None:
            default_img = 'static/img/about/python.jpg'
        else:
            default_img = default_img_data['config']

        admin_user_info['nick_name'] = '系统管理-'+admin_user_info['nick_name']
        admin_user_info['head_img'] = default_img  # 这里后期改成配置的
        # 使用0作为系统id
        msg = message['data']['msg']
        room_uuid = message['data']['room_uuid']
        Type = message['data']['type']
        room_data = Room.get(room_uuid)
        if room_data == None:
            return Utils.formatError(CONST['CODE']['ROOM_NO_EXIST']['value'], "房间不存在")
        room_type = room_data.type
        created_at = int(time.time())
        save_action = message['data']['save_action']
        return ChatService.sendChatMessage(msg, room_uuid, Type, room_data, room_type, created_at, save_action, admin_user_info, 1)
Beispiel #6
0
def adminList(*args, **kwargs):
    """ 获取管理员列表 """
    params = kwargs['params']
    filters = {
        Admin.name.like('%' + params['keyword'] + '%'),
        or_(Admin.mobile.like('%' + params['keyword'] + '%')),
        or_(Admin.email.like('%' + params['keyword'] + '%'))
    }
    data = Admin().getList(filters, Admin.update_time.desc(), (),
                           params['page_no'], params['per_page'])
    return BaseController().successData(data)
Beispiel #7
0
def adminIndex(user_info):
    #获取管理用户总数
    admins = Admin().getCount({})
    #获取注册用户总数
    users = Users().getCount({})
    #获取房间总数
    rooms = Room().getCount({})
    #一周管理用户注册数量
    weekAdminData = Admin().getWeekData()
    #一周注册用户注册数量
    weekUsersData = Users().getWeekData()
    #一周房间总数
    weekRoomData = Room().getWeekData()
    data = {
        'panelGroupData': {
            'adminCount': admins,
            'usersCount': users,
            'roomCount': rooms
        },
        'weekAdminData': weekAdminData,
        'weekUsersData': weekUsersData,
        'weekRoomData': weekRoomData
    }
    return BaseController().successData(data)
Beispiel #8
0
def join(message, user_info):
    if message['type'] == CONST['ROOM']['ALONE']['value']:
        room_uuid = message['room_uuid']
        join_room(room_uuid)
    elif message['type'] == CONST['ROOM']['GROUP']['value']:
        room_uuid = message['room_uuid']
        join_room(room_uuid)
    elif message['type'] == CONST['ROOM']['ADMIN']['value']:
        room_uuid = message['room_uuid']
        join_room(room_uuid)
    elif message['type'] == CONST['ROOM']['NOTIFY']['value']:
        filters = {
            Admin.id == user_info['data']['id'],
        }
        user_info = Admin().getOne(filters)
        join_room('@broadcast.'+str(user_info['uuid']))
    return Utils.formatBody({'action': "join"})
Beispiel #9
0
 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})
Beispiel #10
0
def adminLogout(user_info):
    data = {'update_time': int(time.time())}
    Admin().edit(data, {Admin.id == user_info['data']['id']})
    return BaseController().successData()
Beispiel #11
0
def adminDelete(*args, **kwargs):
    """ 删除管理员 """
    params = kwargs['params']
    filters = {Admin.id == params['id']}
    Admin().delete(filters)
    return BaseController().successData()