package controller import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "project_management/app/e" "project_management/app/model" "project_management/app/services" "project_management/unity" ) var Apply services.Apply = &model.Apply{} // GetApplyList 用户获取自己的应用列表 func GetApplyList(c *gin.Context) { var params unity.QueryPageParams var apply model.Apply if err := c.ShouldBindJSON(¶ms); err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } queryCond := "app_name like ?" params.Query = "%" + params.Query + "%" uId, _, _ := unity.GetUId(c) apply.UserId = uId result, total, err := Apply.GetApplyList(params, apply, queryCond) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } else { e.ResPonsePage(c, result, total, params) return } } // GetApplyState 根据状态筛选 func GetApplyState(c *gin.Context) { var params unity.QueryPageParams var apply model.Apply uId, _, role := unity.GetUId(c) if err := c.ShouldBindJSON(¶ms); err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } queryCond := "app_name like ?" params.Query = "%" + params.Query + "%" apply.UserId = uId if role == "admin" { result, total, err := Apply.GetApplyAminList(params, apply, queryCond) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } e.ResPonsePage(c, result, total, params) return } result, total, err := Apply.GetApplyList(params, apply, queryCond) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } else { e.ResPonsePage(c, result, total, params) return } } // AddApply 用户添加应用 func AddApply(c *gin.Context) { var apply model.Apply if err := c.ShouldBindJSON(&apply); err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } validate := validator.New() if err := validate.Struct(apply); err != nil { e.ResponseWithMsg(c, e.ERROR, "请检查必填项") return } // 生成应用ID id := unity.RandomAppID() for model.AppIdISRepeat(id) { id = unity.RandomAppID() } //获取当前用户ID uId, username, _ := unity.GetUId(c) apply.UserId = uId apply.AppID = id apply.UserName = username addApply := Apply.AddApply(apply) if addApply != e.SUCCESS { e.ResponseWithMsg(c, addApply, addApply.GetMsg()) return } else { e.ResponseSuccess(c, nil) } } // UserUpdateApply 用户更新应用 func UserUpdateApply(c *gin.Context) { var apply model.Apply if err := c.ShouldBindJSON(&apply); err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } id, _, role := unity.GetUId(c) if role == "admin" { byId, err := unity.UpdateById(apply.ID, apply) if err != nil { e.ResponseWithMsg(c, e.UPDATEFAIL, e.UPDATEFAIL.GetMsg()) return } e.ResponseSuccess(c, byId) return } apply.UserId = id updateApply := Apply.UserUpdateApply(apply) if updateApply != e.SUCCESS { e.ResponseWithMsg(c, updateApply, "更新失败") return } e.ResponseSuccess(c, nil) } // GetApplyByAPPID 用户获取应用详情 func GetApplyByAPPID(c *gin.Context) { appid := c.Query("appid") validate := validator.New() if err := validate.Var(appid, "required"); err != nil { e.ResponseWithMsg(c, e.ERROR, err.Error()) return } apply, err := Apply.GetApplyById(appid) if err != nil { e.ResponseWithMsg(c, e.ERROR, "当前应用不可用") return } e.ResponseSuccess(c, apply) } // GetApplyByName 未登录状态下的模糊查询获取应用 func GetApplyByName(c *gin.Context) { appname := c.Query("appname") value, _ := c.Get("phone") validate := validator.New() if err := validate.Var(appname, "required"); err != nil { e.ResponseWithMsg(c, e.ERROR, err.Error()) return } apply, err := Apply.QueryApplyByAppName(appname, value.(string)) if err != nil { e.ResponseWithMsg(c, e.ERROR, "未查找到相关应用") return } e.ResponseSuccess(c, apply) } // GetApplyByNameAll 未登录状态下获取所有应用列表 func GetApplyByNameAll(c *gin.Context) { var params unity.QueryPageParams var apply model.Apply if err := c.ShouldBindJSON(¶ms); err != nil { e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg()) return } phone, _ := c.Get("phone") result, total, err := Apply.CollectionList(params, apply, phone.(string)) if err != nil { e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg()) return } e.ResPonsePage(c, result, total, params) }