123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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)
- }
|