def operation_log(request): """ 插入日志 """ if not config.OPEN_OPERATION_LOG: return operation_type = "query" name = "" for route in request.app.routes: if request.scope['path'] == route.path: operation_type = route.tags[0] if route.tags else operation_type name = route.name # 是否开启查询日志 if not config.OPEN_QUERY_OPERATION_LOG and operation_type == 'query': return token_key = get_token_key(request) if token_key: user = get_current_user(request) db: Session = request.state.db operation_log = SysOperationLog() dh_handle = DBHandleBase() browser = None operation_log.operation_url = request.scope['path'] operation_log.ip = request.scope['client'][0] operation_log.login_name = user['login_name'] operation_log.name = name for raw in request.headers.raw: if bytes.decode(raw[0]) == 'user-agent': browser = bytes.decode(raw[1]) operation_log.browser = browser dh_handle.create(db, token_key, operation_log)
async def get_user_menu(request: Request): """ 获取用户菜单权限 """ token_key = get_token_key(request) if not cache.exists(token_key): return JSONResponse({'code': config.HTTP_403, 'message': 'token已失效,请重新登录!'}) menu_key = 'menu' + token_key[5:] if cache.exists(menu_key): tree_menu_list = eval(cache.get(menu_key)) else: db: Session = request.state.db user = get_current_user(request) role_id = user['role_id'] all_menu_list = db.query(Menu.id, Menu.parent_id, Menu.menu_name, Menu.menu_code, Menu.menu_url, Menu.menu_icon, Menu.menu_type).join(roleMenu, Menu.id == roleMenu.menu_id).filter( roleMenu.state == 1, Menu.state == 1, roleMenu.role_id == role_id).order_by('idx').all() menu_list = orm_all_to_dict(all_menu_list) # 使用程序处理树形结构, 使用程序会比访问数据快 tree_menu_list = [] paren_menu_list = [] child_menu_list = [] if menu_list: for menu in menu_list: #第一层 if not menu['parent_id']: paren_menu_list.append(menu) else: child_menu_list.append(menu) # 递归获取子的层 tree_menu_list = get_tree_data(paren_menu_list, child_menu_list) cache.set(menu_key, str(tree_menu_list), config.REDIS_TIME_OUT) return JSONResponse({'code': config.HTTP_200, 'tree_menu_list': tree_menu_list})
async def get_user_info(request: Request): token_key = get_token_key(request) if not cache.exists(token_key): return JSONResponse({'code': config.HTTP_403, 'message': 'token已失效,请重新登录!'}) user = get_current_user(request) return JSONResponse({'code': config.HTTP_200, 'user': user})
async def login(request: Request, form: LoginForm): ''' 登录 ''' db: Session = request.state.db session_user = db.query(SysUser.id, SysUser.login_name, SysUser.user_name, SysUser.role_id).filter( SysUser.login_name == form.login_name, SysUser.password == encrypt_password(form.password)).first() user = orm_one_to_dict(session_user) print(form) # 写入登录日志 new_log = SysLoginLog() new_log.login_name = form.login_name new_log.login_ip = request.scope['client'][0] if request.scope and request.scope['client'] else '' # 获取浏览器信息 db_handle = DBHandleBase() for raw in request.headers.raw: if bytes.decode(raw[0]) == 'user-agent': new_log.browser = bytes.decode(raw[1]) if not user: new_log.is_success = False db_handle.create(db, get_token_key(request), new_log) return JSONResponse({'code': config.HTTP_400, 'message': '账号或密码错误'}) token_key = 'token' + encrypt_password(str(user['id'])) if not cache.exists(token_key): token = 'token' + encrypt_password(str(user['id'])) cache.set(token, str(user), config.REDIS_TIME_OUT) new_log.user_name = user['user_name'] new_log.is_success = True db_handle.create(db, token_key, new_log) return JSONResponse({'code': config.HTTP_200, 'token': token_key, 'user': user})
def logout(request: Request): """ 登出 """ token_key = get_token_key(request) if cache.exists(token_key): cache.delete(token_key) menu_key = 'menu' + token_key[5:] cache.delete(menu_key) if cache.exists(menu_key) else None return JSONResponse({'code': config.HTTP_200})
async def save_user(request: Request, form: UserForm): token_key = get_token_key(request) db: Session = request.state.db handle_db = DBHandleBase() if form.id: if not db.query(User.id).filter(User.id == form.id, User.state == 1).scalar(): return JSONResponse({ 'code': config.HTTP_404, 'message': '找不到该部门信息' }) update_dic = { 'user_name': form.user_name, 'gender': form.gender, 'wechat_no': form.wechat_no, 'phone': form.phone, 'email': form.email, 'birthday': form.birthday, 'department_id': form.department_id, 'role_id': form.role_id } db_user = db.query(User).filter(User.id == form.id).update(update_dic) db.commit() if not db_user: return JSONResponse({'code': config.HTTP_404, 'message': '修改用户失败'}) return JSONResponse({'code': config.HTTP_200, 'message': '修改用户成功'}) new_user = User() new_user.login_name = form.login_name new_user.password = encrypt_password(form.password) new_user.gender = form.gender new_user.user_name = form.user_name new_user.phone = form.phone new_user.wechat_no = form.wechat_no new_user.email = form.email new_user.birthday = form.birthday new_user.department_id = form.department_id new_user.role_id = form.role_id status_code, user_obj = handle_db.create(db, token_key, new_user) if status_code != 200: return JSONResponse({'code': config.HTTP_404, 'message': '创建用户失败'}) return JSONResponse({'code': config.HTTP_200, 'message': '创建用户成功'})