Rust-角色模块

举报
林太白 发表于 2025/08/08 15:01:22 2025/08/08
【摘要】 Rust-角色模块

Rust-角色模块

接下来我们完成角色模块

👉引入申明

🍎src\main.rs

先在入口引入我们的模块

  .service(
      web::scope("/api") // 这里加上 /api 前缀
        .configure(modules::role::routes::config)
  )

🍎模块申明

src\modules\mod.rs

pub mod role;

🍎搭建基础模块

src\modules\role下面搭建模块

mod.rs模块

// mod.rs
pub mod handlers;
pub mod routes;

routes.rs模块

//routes.rs
use actix_web::web;
pub fn config(cfg: &mut web::ServiceConfig) {
  cfg.route("/system/roles", web::get().to(crate::modules::role::handlers::get_list));
  cfg.route("/system/roles", web::post().to(crate::modules::role::handlers::post_add));
  cfg.route("/system/roles/{id}", web::get().to(crate::modules::role::handlers::get_detail));
  cfg.route("/system/roles", web::put().to(crate::modules::role::handlers::put_update));
  cfg.route("/system/roles/{id}", web::delete().to(crate::modules::role::handlers::del_delete));
}

handlers.rs方法逻辑

// handlers.rs方法逻辑

use actix_web::{HttpResponse};
use crate::common::response::ApiResponse; // 导入 ApiResponse 模型

// 通用查询
pub async fn get_list() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}

// 通用新增
pub async fn post_add() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用详情
pub async fn get_detail() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用更新
pub async fn put_update() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用更新
pub async fn del_delete() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}

👉功能实现

🍎查询功能

这里查询我们直接引我们之前的查询部分的参数和方法

// 通过方法
#[allow(unused_imports)]
use crate::common::apimethods::list_api_page; // 引入公共分页查询方法


// 通用查询
pub async fn get_list(
    pool: web::Data<MySqlPool>,
    query: web::Query<QueryParams>,
    filter: Option<web::Query<HashMap<String, String>>>
) -> impl Responder {
    // 调试:打印查询参数
    if let Some(ref filter_params) = filter {
        println!("查询参数: {:?}", filter_params);
    }
    // 精确查询字段(exact query)
    let exactquery = vec![
        "status".to_string(),
    ];
    // 模糊查询字段(like query) "role_name".to_string()
    let likequery = vec![
        "role_name".to_string(),
    ];

   // 调用 list_api_page 传入查询条件
   list_api_page(pool, query, filter, "sys_role", exactquery, likequery).await
}

测试功能ok

🍎新增功能

// 新增
pub async fn post_add(
    pool: web::Data<MySqlPool>,
    form: web::Json<AddRoleRequest>,
) -> HttpResponse {
    let mut data = std::collections::HashMap::new();
    data.insert("role_name".to_string(), form.roleName.clone());
    data.insert("role_key".to_string(), form.roleKey.clone());
    data.insert("role_sort".to_string(), form.roleSort.to_string());
    data.insert("status".to_string(), form.status.clone());
    data.insert("remark".to_string(), form.remark.clone());

     // 处理布尔值字段
     data.insert("menu_check_strictly".to_string(), if form.menuCheckStrictly { "1" } else { "0" }.to_string());
     data.insert("dept_check_strictly".to_string(), if form.deptCheckStrictly { "1" } else { "0" }.to_string());

    crate::common::apimethods::create_api(
        pool.get_ref(),
        "sys_role",
        &data,
        &[ 
            "role_name".to_string(),
            "role_key".to_string()
        ],
    ).await
}

🍎详情功能

// 通用详情
pub async fn get_detail(
    pool: web::Data<MySqlPool>,
    path: web::Path<i32>,
) -> HttpResponse {
    crate::common::apimethods::detail_api::<Role>(pool, "sys_role", "role_id", path.into_inner()).await
}

🍎删除功能

// 通用真删除
pub async fn del_delete(
    pool: web::Data<MySqlPool>,
    id: web::Path<i32>
) -> HttpResponse {
    crate::common::apimethods::delete_api(
        pool.get_ref(),
        "sys_role",
        "role_id",
        *id,
        false, // 软删除,isDeleted=1
    ).await
}

测试功能ok

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。