diff --git a/app/api/admin/api/tenant.go b/app/api/admin/api/tenant.go index 00cd635..42af858 100644 --- a/app/api/admin/api/tenant.go +++ b/app/api/admin/api/tenant.go @@ -98,25 +98,25 @@ func (a *Tenant) MemberBind(c *gin.Context) { Mobile string `json:"mobile" form:"mobile" binding:"required"` Password string `json:"password" form:"password" binding:"required"` }{} - if err := bind(form)(c); err != nil { - APIFailure(err.(error))(c) + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) return } err := tenant.NewMember()(api.GetSession()(c).(*session.Admin)).Form((&api.IDStringForm{ID: form.TenantID}).Convert(), &tenant.MemberParams{Mobile: form.Mobile, Password: form.Password}) - APIResponse(err)(c) + api.APIResponse(err)(c) } func (a *Tenant) Menu(c *gin.Context) { form := &struct { TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"` }{} - if err := bind(form)(c); err != nil { - APIFailure(err.(error))(c) + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) return } data, err := tenant.NewMenu()(getSession()(c).(*service.Session)).List(form.TenantID) - APIResponse(err, data)(c) + api.APIResponse(err, data)(c) } func (a *Tenant) MenuBind(c *gin.Context) { @@ -124,12 +124,12 @@ func (a *Tenant) MenuBind(c *gin.Context) { TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"` MenuIDs []uint64 `json:"menu_ids" form:"menu_ids" binding:"required"` }{} - if err := bind(form)(c); err != nil { - APIFailure(err.(error))(c) + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) return } err := tenant.NewMenu()(getSession()(c).(*service.Session)).Bind(form.TenantID, form.MenuIDs) - APIResponse(err)(c) + api.APIResponse(err)(c) } func (a *Tenant) AuthBind(c *gin.Context) { @@ -137,10 +137,10 @@ func (a *Tenant) AuthBind(c *gin.Context) { TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"` AuthIDs []uint64 `json:"auth_ids" form:"auth_ids" binding:"required"` }{} - if err := bind(form)(c); err != nil { - APIFailure(err.(error))(c) + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) return } err := tenant.NewAuth()(getSession()(c).(*service.Session)).Bind(form.TenantID, form.AuthIDs) - APIResponse(err)(c) + api.APIResponse(err)(c) } diff --git a/app/api/admin/api/user.go b/app/api/admin/api/user.go index 5c08779..5ddfabd 100644 --- a/app/api/admin/api/user.go +++ b/app/api/admin/api/user.go @@ -2,6 +2,7 @@ package api import ( user2 "SciencesServer/app/api/admin/controller/user" + "SciencesServer/app/basic/api" "SciencesServer/app/service" "github.com/gin-gonic/gin" @@ -39,7 +40,7 @@ func (a *User) List(c *gin.Context) { APIFailure(err.(error))(c) return } - data, err := user2.NewInstance()(getSession()(c).(*service.Session)).List(form.Name, form.Mobile, form.Status, form.Page, form.PageSize) + data, err := user2.NewInstance()(getSession()(c).(*service.Session)).Index(form.Name, form.Mobile, form.Status, form.Page, form.PageSize) APIResponse(err, data)(c) } @@ -148,18 +149,18 @@ func (a *User) Edit(c *gin.Context) { * "data": null * } */ -func (a *User) QuickPassword(c *gin.Context) { +func (a *User) Password(c *gin.Context) { form := &struct { - idForm + api.IDStringForm Password string `json:"password" form:"password" binding:"required"` RepeatPwd string `json:"repeat_pwd" form:"repeat_pwd" binding:"required"` }{} - if err := bind(form)(c); err != nil { - APIFailure(err.(error))(c) + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) return } - err := user2.NewInstance()(getSession()(c).(*service.Session)).Password(form.ID, form.Password, form.RepeatPwd) - APIResponse(err)(c) + err := user2.NewInstance()(getSession()(c).(*service.Session)).Password(form.Convert(), form.Password, form.RepeatPwd) + api.APIResponse(err)(c) } /** @@ -186,7 +187,7 @@ func (a *User) QuickPassword(c *gin.Context) { * "data": null * } */ -func (a *User) EditPassword(c *gin.Context) { +func (a *User) PasswordEdit(c *gin.Context) { form := &struct { OldPwd string `json:"original_pwd" form:"original_pwd" binding:"required"` Password string `json:"password" form:"password" binding:"required"` diff --git a/app/api/admin/controller/account.go b/app/api/admin/controller/account.go index 25b34c1..7dda5d3 100644 --- a/app/api/admin/controller/account.go +++ b/app/api/admin/controller/account.go @@ -68,7 +68,7 @@ func loginForSmsCaptcha(params *AccountLoginParams, tenantID uint64) (*model.Sys mSysUser := model.NewSysUser() isExist, err := model2.FirstField(mSysUser.SysUser, []string{ - "id", "tenant_id", "name", "mobile", "is_admin", "status", + "id", "uuid", "tenant_id", "name", "mobile", "is_admin", "status", }, model2.NewWhere("tenant_id", tenantID), model2.NewWhere("mobile", params.Account)) if err != nil { @@ -105,7 +105,7 @@ func (c *Account) Login(mode int, params *AccountLoginParams, equipment, ip stri _session.Token = utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{config.TokenForUID: _uid}) service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccountAdmin, _uid, _session) - service.Publish(config.EventForAccountLoginProduce, data.TenantID, _session.UID, equipment, ip) + service.Publish(config.EventForAccountLoginProduce, data.TenantID, data.ID, equipment, ip) return &AccountLoginResponse{Token: _session.Token, EffectTime: config.SettingInfo.TokenEffectTime}, nil } diff --git a/app/api/admin/controller/user/instance.go b/app/api/admin/controller/user/instance.go index 7fc1a97..c389b81 100644 --- a/app/api/admin/controller/user/instance.go +++ b/app/api/admin/controller/user/instance.go @@ -2,16 +2,13 @@ package user import ( "SciencesServer/app/api/admin/controller" - model3 "SciencesServer/app/api/admin/model" + "SciencesServer/app/api/admin/model" model2 "SciencesServer/app/common/model" "SciencesServer/app/service" - "SciencesServer/serve/orm" + "SciencesServer/config" "SciencesServer/utils" "errors" - "strings" "time" - - "gorm.io/gorm" ) type Instance struct{ *controller.Platform } @@ -21,16 +18,18 @@ type InstanceHandle func(session *service.Session) *Instance type ( // InstanceInfo 基本信息 InstanceInfo struct { + ID string `json:"id"` + InstanceUserInfo + Gender model2.GenderKind `json:"gender"` + IsAdmin model2.SysUserAdministrator `json:"is_admin"` + CreatedAt time.Time `json:"created_at"` + } + // InstanceUserInfo 用户信息 + InstanceUserInfo struct { UID string `json:"uid"` Name string `json:"name"` Email string `json:"email"` Mobile string `json:"mobile"` - Remark string `json:"remark"` - } - // InstanceUserInfo 用户信息 - InstanceUserInfo struct { - *model3.SysUserTenantUser - UID string `json:"uid"` } ) @@ -43,45 +42,64 @@ type InstanceForm struct { } // Info 用户信息 -func (c *Instance) Info() (*InstanceInfo, error) { - mSysUser := model3.NewSysUser() +func (c *Instance) Info() (*InstanceUserInfo, error) { + mSysUser := model.NewSysUser() _, err := model2.FirstWhere(mSysUser.SysUser, model2.NewWhere("uuid", c.UID)) if err != nil { return nil, err } - return &InstanceInfo{ - UID: mSysUser.UUIDString(), Name: mSysUser.Name, Email: mSysUser.Email, Mobile: mSysUser.Mobile, Remark: mSysUser.Remark, + return &InstanceUserInfo{ + UID: mSysUser.UUIDString(), Name: mSysUser.Name, Email: mSysUser.Email, Mobile: mSysUser.Mobile, }, nil } -// List 列表信息 -func (c *Instance) List(name, mobile string, status, page, pageSize int) (*controller.ReturnPages, error) { - mSysUserTenant := model3.NewSysUserTenant() - - where := []*model2.ModelWhere{model2.NewWhere("u_t.tenant_id", c.TenantID)} - +// Index 列表信息 +func (c *Instance) Index(name, mobile string, status, page, pageSize int) (*controller.ReturnPages, error) { + where := []*model2.ModelWhereOrder{&model2.ModelWhereOrder{ + Where: model2.NewWhere("tenant_id", c.TenantID), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }} if name != "" { - where = append(where, model2.NewWhereLike("u.name", name)) + where = append(where, &model2.ModelWhereOrder{ + Where: model2.NewWhereLike("name", name), + }) } if mobile != "" { - where = append(where, model2.NewWhereLike("u.mobile", mobile)) + where = append(where, &model2.ModelWhereOrder{ + Where: model2.NewWhereLike("mobile", mobile), + }) } if status > 0 { - where = append(where, model2.NewWhere("u_t.status", status)) + where = append(where, &model2.ModelWhereOrder{ + Where: model2.NewWhere("status", status), + }) } + mSysUser := model.NewSysUser() + + out := make([]*model2.SysUser, 0) + var count int64 - out, err := mSysUserTenant.User(page, pageSize, &count, where...) - - if err != nil { + if err := model2.PagesFields(mSysUser.SysUser, out, []string{"id", "uid", "account", "name", "mobile", "gender", + "is_admin", "created_at"}, page, pageSize, &count, where...); err != nil { return nil, err } - list := make([]*InstanceUserInfo, 0) + list := make([]*InstanceInfo, 0) for _, v := range out { - list = append(list, &InstanceUserInfo{SysUserTenantUser: v, UID: utils.UintToString(v.UID)}) + list = append(list, &InstanceInfo{ID: v.GetEncodeID(), + InstanceUserInfo: InstanceUserInfo{ + UID: v.UUIDString(), + Name: v.Name, + Email: v.Email, + Mobile: v.Mobile, + }, + Gender: v.Gender.Gender, + IsAdmin: v.IsAdmin, + CreatedAt: v.CreatedAt, + }) } return &controller.ReturnPages{Data: list, Count: count}, nil } @@ -89,20 +107,18 @@ func (c *Instance) List(name, mobile string, status, page, pageSize int) (*contr // Add 添加用户 func (c *Instance) Add(params *InstanceForm) error { if utils.ValidateMobile(params.Mobile) { - return errors.New("手机号码格式错误") + return errors.New("操作错误,手机号码格式错误") } - mSysUser := model3.NewSysUser() + mSysUser := model.NewSysUser() // 查询登录账户或手机号码是否注册 var count int64 - err := model2.Count(mSysUser.SysUser, &count, model2.NewWhere("account", params.Account)) + err := model2.Count(mSysUser.SysUser, &count, model2.NewWhere("mobile", params.Mobile), + model2.NewWhere("tenant_id", c.TenantID)) if err != nil { return err } - if err = model2.Count(mSysUser.SysUser, &count, model2.NewWhere("mobile", params.Mobile)); err != nil { - return err - } mSysUser.Account = params.Account mSysUser.Name = params.Name mSysUser.Mobile = params.Mobile @@ -110,136 +126,111 @@ func (c *Instance) Add(params *InstanceForm) error { mSysUser.Password = params.Password mSysUser.Remark = params.Remark - return orm.GetDB().Transaction(func(tx *gorm.DB) error { - if err = model2.Create(mSysUser.SysUser, tx); err != nil { - return err - } - mSysUserTenant := model3.NewSysUserTenant() - mSysUserTenant.TenantID = c.TenantID - mSysUserTenant.UID = mSysUser.UUID - if len(params.Departments) > 0 { - mSysUserTenant.Department = strings.Join(utils.ArrayStrings(params.Departments), ",") - } - if len(params.Roles) > 0 { - mSysUserTenant.Role = strings.Join(utils.ArrayStrings(params.Roles), ",") - } - mSysUserTenant.Identity = model2.SysUserTenantIdentityForSystemUser - - if err = model2.Create(mSysUserTenant.SysUserTenant, tx); err != nil { - return err - } - return nil - }) + return model2.Create(mSysUser.SysUser) } // Edit 修改用户信息 func (c *Instance) Edit(params *InstanceForm) error { - if utils.ValidateMobile(params.Mobile) { - return errors.New("手机号码格式错误") - } - mSysUserTenant := model3.NewSysUserTenant() - mSysUserTenant.ID = params.ID + mSysUser := model.NewSysUser() + mSysUser.ID = params.ID - isExist, err := model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "tenant_id", "identity"}) + isExist, err := model2.First(mSysUser.SysUser) if err != nil { return nil } else if !isExist { - return errors.New("用户信息不存在") - } else if mSysUserTenant.TenantID != c.TenantID { - return errors.New("不可修改他人用户信息") + return errors.New("操作错误,用户信息不存在或已被删除") + } else if c.TenantID > 0 && mSysUser.TenantID != c.TenantID { + return errors.New("操作错误,无权限操作") } - // 查询用户信息 - mSysUser := model3.NewSysUser() - - if isExist, err = model2.FirstWhere(mSysUser.SysUser, model2.NewWhere("uuid", mSysUserTenant.UID)); err != nil { - return err - } else if !isExist { - return errors.New("用户信息不存在") - } - if params.Mobile != mSysUser.Mobile { + if mSysUser.Mobile != params.Mobile { + if utils.ValidateMobile(params.Mobile) { + return errors.New("操作错误,手机号码格式错误") + } var count int64 - if err = model2.Count(mSysUser.SysUser, &count, model2.NewWhere("mobile", params.Mobile)); err != nil { - return err + if err = model2.Count(mSysUser.SysUser, &count, model2.NewWhere("mobile", params.Mobile), + model2.NewWhere("tenant_id", c.TenantID)); err != nil { + return nil } else if count > 0 { - return errors.New("该手机号码已注册") + return errors.New("操作错误,当前手机号码已注册") } + mSysUser.Mobile = params.Mobile } - now := time.Now() - return orm.GetDB().Transaction(func(tx *gorm.DB) error { - if err = model2.Updates(mSysUserTenant.SysUserTenant, map[string]interface{}{ - "department": strings.Join(utils.ArrayStrings(params.Departments), ","), - "role": strings.Join(utils.ArrayStrings(params.Roles), ","), - "updated_at": now, - }, tx); err != nil { - return err - } - if err = model2.Updates(mSysUser.SysUser, map[string]interface{}{ - "name": params.Name, "mobile": params.Mobile, "gender": params.Gender, "remark": params.Remark, "updated_at": now, - }, tx); err != nil { - return err - } - return nil - }) + mSysUser.Account = params.Account + mSysUser.Name = params.Name + mSysUser.Gender.Gender = model2.GenderKind(params.Gender) + mSysUser.Remark = params.Remark + + return model2.Updates(mSysUser.SysUser, mSysUser.SysUser) } func (c *Instance) Password(id uint64, password, repeatPwd string) error { if password != repeatPwd { - return errors.New("两次密码不一致") + return errors.New("操作错误,两次密码输入不一致") } - mSysUserTenant := model3.NewSysUserTenant() - mSysUserTenant.ID = id + mSysUser := model.NewSysUser() - isExist, err := model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "tenant_id", "identity"}) + isExist, err := model2.FirstField(mSysUser.SysUser, []string{"id", "uuid", "is_admin"}) if err != nil { return nil } else if !isExist { - return errors.New("用户信息不存在") - } else if mSysUserTenant.TenantID != c.TenantID { - return errors.New("不可修改他人用户密码") + return errors.New("操作错误,用户信息不存在或已被删除") + } else if c.TenantID > 0 && mSysUser.TenantID != c.TenantID { + return errors.New("操作错误,无权限操作") } - mSysUser := model3.NewSysUser() mSysUser.Password = password mSysUser.Pass() - if err = model2.UpdatesWhere(mSysUser.SysUser, map[string]interface{}{ + return model2.Updates(mSysUser.SysUser, map[string]interface{}{ "password": mSysUser.Password, "salt": mSysUser.Salt, "updated_at": time.Now(), - }, []*model2.ModelWhere{ - model2.NewWhere("uuid", mSysUserTenant.UID), - }); err != nil { + }) +} + +// PasswordEdit 主动修改密码 +func (c *Person) PasswordEdit(oldPassword, password, repeatPwd string) error { + if password != repeatPwd { + return errors.New("操作错误,两次密码输入不一致") + } + mSysUser := model.NewSysUser() + + _, err := model2.FirstField(mSysUser.SysUser, []string{"id", "uuid", "is_admin"}, model2.NewWhere("uuid", c.UID)) + + if err != nil { return err } - return nil + if !mSysUser.ValidatePassword(oldPassword) { + return errors.New("操作错误,旧密码输入错误") + } + mSysUser.Password = password + mSysUser.Pass() + + return model2.Updates(mSysUser.SysUser, map[string]interface{}{ + "password": mSysUser.Password, "salt": mSysUser.Salt, "updated_at": time.Now(), + }) } func (c *Instance) Delete(id uint64) error { - mSysUserTenant := model3.NewSysUserTenant() - mSysUserTenant.ID = id + mSysUser := model.NewSysUser() + mSysUser.ID = id - isExist, err := model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "tenant_id", "identity"}) + isExist, err := model2.FirstField(mSysUser.SysUser, []string{"id", "uuid", "is_admin"}) if err != nil { return nil } else if !isExist { - return errors.New("用户信息不存在") - } else if mSysUserTenant.TenantID != c.TenantID { - return errors.New("不可删除他人用户信息") + return errors.New("操作错误,用户信息不存在或已被删除") + } else if c.TenantID > 0 && mSysUser.TenantID != c.TenantID { + return errors.New("操作错误,无权限操作") + } else if mSysUser.IsAdminUser() { + return errors.New("操作错误,超管用户不允许删除") } - return orm.GetDB().Transaction(func(tx *gorm.DB) error { - if err = model2.Delete(mSysUserTenant.SysUserTenant, tx); err != nil { - return err - } - mSysUser := model3.NewSysUser() - - if err = model2.DeleteWhere(mSysUser.SysUser, []*model2.ModelWhere{ - model2.NewWhere("uuid", mSysUserTenant.UID), - }, tx); err != nil { - return err - } - return nil - }) + if err = model2.Delete(mSysUser.SysUser); err != nil { + return err + } + service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccountAdmin, utils.UintToString(mSysUser.UUID)) + return nil } func NewInstance() InstanceHandle { diff --git a/app/api/admin/controller/user/menu.go b/app/api/admin/controller/user/menu.go index 625a3a7..38430a4 100644 --- a/app/api/admin/controller/user/menu.go +++ b/app/api/admin/controller/user/menu.go @@ -3,8 +3,7 @@ package user import ( "SciencesServer/app/api/admin/controller" menu2 "SciencesServer/app/api/admin/controller/menu" - model3 "SciencesServer/app/api/admin/model" - model2 "SciencesServer/app/common/model" + "SciencesServer/app/api/admin/model" "SciencesServer/app/service" ) @@ -14,23 +13,24 @@ type MenuHandle func(session *service.Session) *Menu // List 菜单列表 func (c *Menu) List() (interface{}, error) { - mSysMenu := model3.NewSysMenu() + mSysMenu := model.NewSysMenu() if c.IsAdmin { return menu2.MenuForSystem(mSysMenu) } - mSysUserTenant := model3.NewSysUserTenant() - - if isExist, err := model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "identity"}, - model2.NewWhere("tenant_id", c.TenantID), model2.NewWhere("uid", c.UID)); err != nil { - return nil, err - } else if !isExist { - return nil, nil - } - if mSysUserTenant.Identity == model2.SysUserTenantIdentityForSystemAdmin { - return menu2.MenuForTenant(mSysMenu, c.TenantID) - } - return menu2.MenuForUser(mSysMenu, c.TenantID, c.UID) + //mSysUserTenant := model.NewSysUserTenant() + // + //if isExist, err := model2.FirstField(mSysUserTenant.SysUserTenant, []string{"id", "identity"}, + // model2.NewWhere("tenant_id", c.TenantID), model2.NewWhere("uid", c.UID)); err != nil { + // return nil, err + //} else if !isExist { + // return nil, nil + //} + //if mSysUserTenant.Identity == model2.SysUserTenantIdentityForSystemAdmin { + // return menu2.MenuForTenant(mSysMenu, c.TenantID) + //} + //return menu2.MenuForUser(mSysMenu, c.TenantID, c.UID) + return nil, nil } func NewMenu() MenuHandle { diff --git a/app/api/admin/model/sys_user.go b/app/api/admin/model/sys_user.go index b80da7b..69a307f 100644 --- a/app/api/admin/model/sys_user.go +++ b/app/api/admin/model/sys_user.go @@ -24,7 +24,7 @@ func (m *SysUser) IsAdminUser() bool { func (m *SysUser) GetByAccountOrMobile(account string, tenantID uint64) (bool, error) { db := orm.GetDB().Table(m.TableName()). - Select("id", "tenant_id", "name", "mobile", "password", "salt", "is_admin", "status"). + Select("id", "uuid", "tenant_id", "name", "mobile", "password", "salt", "is_admin", "status"). Where("tenant_id = ?", tenantID). Where("(account = ? OR mobile = ?)", account, account). Where("is_deleted = ?", model.DeleteStatusForNot) diff --git a/app/api/admin/model/sys_user_tenant.go b/app/api/admin/model/sys_user_tenant.go deleted file mode 100644 index 0b3cc14..0000000 --- a/app/api/admin/model/sys_user_tenant.go +++ /dev/null @@ -1,83 +0,0 @@ -package model - -import ( - "SciencesServer/app/common/model" - "SciencesServer/serve/orm" - "fmt" -) - -type SysUserTenant struct { - *model.SysUserTenant -} - -type ( - // SysUserTenantBasic 基本信息 - SysUserTenantBasic struct { - ID uint64 `json:"id"` - Name string `json:"name"` - Mobile string `json:"mobile"` - Status model.AccountStatus `json:"status"` - } - // SysUserTenantUser 用户信息 - SysUserTenantUser struct { - *SysUserTenantBasic - UID uint64 `json:"uid"` - Email string `json:"email"` - Identity model.SysUserTenantIdentity `json:"identity"` - Remark string `json:"remark"` - } -) - -// Member 成员信息 -func (m *SysUserTenant) Member(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysUserTenantBasic, error) { - mSysUser := NewSysUser() - - db := orm.GetDB().Table(m.TableName()+" AS u_t"). - Select("u_t.id, u.name, u.mobile, u_t.status"). - Joins(fmt.Sprintf("LEFT JOIN %s AS u ON u_t.uid = u.uuid", mSysUser.TableName())). - Where("u_t.is_deleted = ? AND u.is_deleted = ?", model.DeleteStatusForNot, model.DeleteStatusForNot) - - if len(where) > 0 { - for _, wo := range where { - db = db.Where(wo.Condition, wo.Value) - } - } - out := make([]*SysUserTenantBasic, 0) - - if err := db.Count(count).Error; err != nil { - return nil, err - } - if err := db.Order("u_t.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { - return nil, err - } - return out, nil -} - -// User 用户信息 -func (m *SysUserTenant) User(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysUserTenantUser, error) { - mSysUser := NewSysUser() - - db := orm.GetDB().Table(m.TableName()+" AS u_t"). - Select("u_t.id, u_t.uid, u.name, u.mobile, u.email, u_t.identity, u_t.status, u.remark, u_t.created_at"). - Joins(fmt.Sprintf("LEFT JOIN %s AS u ON u_t.uid = u.uuid", mSysUser.TableName())). - Where("u_t.is_deleted = ? AND u.is_deleted = ?", model.DeleteStatusForNot, model.DeleteStatusForNot) - - if len(where) > 0 { - for _, wo := range where { - db = db.Where(wo.Condition, wo.Value) - } - } - out := make([]*SysUserTenantUser, 0) - - if err := db.Count(count).Error; err != nil { - return nil, err - } - if err := db.Order("u_t.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { - return nil, err - } - return out, nil -} - -func NewSysUserTenant() *SysUserTenant { - return &SysUserTenant{SysUserTenant: model.NewSysUserTenant()} -} diff --git a/app/api/website/api/config.go b/app/api/website/api/config.go index 778f64e..1eb8a7e 100644 --- a/app/api/website/api/config.go +++ b/app/api/website/api/config.go @@ -1 +1,21 @@ package api + +import ( + "SciencesServer/app/api/website/controller" + "SciencesServer/app/basic/api" + "github.com/gin-gonic/gin" +) + +type Config struct{} + +func (*Config) Area(c *gin.Context) { + form := &struct { + Code string `json:"code" form:"code"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data := controller.NewConfig()(nil).Area(form.Code) + api.APIResponse(nil, data)(c) +} diff --git a/app/api/website/api/index.go b/app/api/website/api/index.go index 73435be..ee8a1c2 100644 --- a/app/api/website/api/index.go +++ b/app/api/website/api/index.go @@ -9,6 +9,71 @@ import ( type Index struct{} func (*Index) Instance(c *gin.Context) { - data, err := controller.NewIndex()(nil, "").Instance() + data, err := controller.NewIndex()(nil).Instance() + api.APIResponse(err, data)(c) +} + +func (*Index) DistributionExpert(c *gin.Context) { + form := &struct { + Province string `json:"province" form:"province"` + City string `json:"city" form:"city"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewIndex()(nil).DistributionExpert(form.Province, form.City) + api.APIResponse(err, data)(c) +} + +func (*Index) DistributionLaboratory(c *gin.Context) { + form := &struct { + Province string `json:"province" form:"province"` + City string `json:"city" form:"city"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewIndex()(nil).DistributionLaboratory(form.Province, form.City) + api.APIResponse(err, data)(c) +} + +func (*Index) DistributionDemand(c *gin.Context) { + form := &struct { + Province string `json:"province" form:"province"` + City string `json:"city" form:"city"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewIndex()(nil).DistributionDemand(form.Province, form.City) + api.APIResponse(err, data)(c) +} + +func (*Index) DistributionPatent(c *gin.Context) { + form := &struct { + Province string `json:"province" form:"province"` + City string `json:"city" form:"city"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewIndex()(nil).DistributionPatent(form.Province, form.City) + api.APIResponse(err, data)(c) +} + +func (*Index) DistributionAchievement(c *gin.Context) { + form := &struct { + Province string `json:"province" form:"province"` + City string `json:"city" form:"city"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewIndex()(nil).DistributionAchievement(form.Province, form.City) api.APIResponse(err, data)(c) } diff --git a/app/api/website/controller/config.go b/app/api/website/controller/config.go index 3fe9fb6..428af62 100644 --- a/app/api/website/controller/config.go +++ b/app/api/website/controller/config.go @@ -1,19 +1,29 @@ package controller import ( - "SciencesServer/app/service" + "SciencesServer/app/basic/config" + "SciencesServer/app/session" + config2 "SciencesServer/config" ) -type Config struct{ *service.Session } +type Config struct{ *session.Admin } -type ConfigHandle func(session *service.Session) *Config +type ConfigHandle func(session *session.Admin) *Config func (c *Config) Basic() { } +// Area 区域信息 +func (c *Config) Area(key string) map[string]string { + if key == "" { + key = config2.DefaultChinaAreaCode + } + return config.MemoryForAreaInfo[key] +} + func NewConfig() ConfigHandle { - return func(session *service.Session) *Config { - return &Config{Session: session} + return func(session *session.Admin) *Config { + return &Config{Admin: session} } } diff --git a/app/api/website/controller/index.go b/app/api/website/controller/index.go index 4809a4c..38c2fa7 100644 --- a/app/api/website/controller/index.go +++ b/app/api/website/controller/index.go @@ -12,10 +12,9 @@ import ( type Index struct { *service.Session - local string } -type IndexHandle func(session *service.Session, local string) *Index +type IndexHandle func(session *service.Session) *Index type ( // InstanceInfo 首页信息 @@ -152,10 +151,10 @@ func (c *Index) static() (*InstanceStaticInfo, error) { return out, nil } -// distributionExpert 专家分布 -func (c *Index) distributionExpert() (map[string]*InstanceDistributionDetailInfo, error) { +// DistributionExpert 专家分布 +func (c *Index) DistributionExpert(province, city string) (map[string]*InstanceDistributionDetailInfo, error) { mManageExpert := model.NewManageExpert() - out, err := mManageExpert.Distribution() + out, err := mManageExpert.Distribution(province, city) if err != nil { return nil, err @@ -163,8 +162,8 @@ func (c *Index) distributionExpert() (map[string]*InstanceDistributionDetailInfo return c.distribution(out), nil } -// distributionLaboratory 实验室分布 -func (c *Index) distributionLaboratory() (map[string]*InstanceDistributionDetailInfo, error) { +// DistributionLaboratory 实验室分布 +func (c *Index) DistributionLaboratory(province, city string) (map[string]*InstanceDistributionDetailInfo, error) { mManageLaboratory := model.NewManageLaboratory() out, err := mManageLaboratory.Distribution() @@ -174,8 +173,8 @@ func (c *Index) distributionLaboratory() (map[string]*InstanceDistributionDetail return c.distribution(out), nil } -// distributionDemand 需求信息 -func (c *Index) distributionDemand() (map[string]*InstanceDistributionDetailInfo, error) { +// DistributionDemand 需求信息 +func (c *Index) DistributionDemand(province, city string) (map[string]*InstanceDistributionDetailInfo, error) { mTechnologyDemand := model.NewTechnologyDemand() out, err := mTechnologyDemand.Distribution() @@ -185,8 +184,8 @@ func (c *Index) distributionDemand() (map[string]*InstanceDistributionDetailInfo return c.distribution(out), nil } -// distributionPatent 专利信息 -func (c *Index) distributionPatent() (map[string]*InstanceDistributionDetailInfo, error) { +// DistributionPatent 专利信息 +func (c *Index) DistributionPatent(province, city string) (map[string]*InstanceDistributionDetailInfo, error) { mSysPatent := model.NewSysPatent() out, err := mSysPatent.Distribution() @@ -196,8 +195,8 @@ func (c *Index) distributionPatent() (map[string]*InstanceDistributionDetailInfo return c.distribution(out), nil } -// distributionAchievement 技术成果信息 -func (c *Index) distributionAchievement() (map[string]*InstanceDistributionDetailInfo, error) { +// DistributionAchievement 技术成果信息 +func (c *Index) DistributionAchievement(province, city string) (map[string]*InstanceDistributionDetailInfo, error) { mSysPatent := model.NewSysPatent() out, err := mSysPatent.Distribution() @@ -218,34 +217,13 @@ func (c *Index) Instance() (*InstanceInfo, error) { if out.InstanceStaticInfo, err = c.static(); err != nil { return nil, err } - // 专家区域分布信息 - if out.Distribution.Expert, err = c.distributionExpert(); err != nil { - return nil, err - } - // 实验室区域分布信息 - if out.Distribution.Laboratory, err = c.distributionLaboratory(); err != nil { - return nil, err - } - // 需求信息区域分布信息 - if out.Distribution.Demand, err = c.distributionDemand(); err != nil { - return nil, err - } - // 专利信息区域分布信息 - if out.Distribution.Patent, err = c.distributionPatent(); err != nil { - return nil, err - } - // 成果信息区域分布信息 - if out.Distribution.Achievement, err = c.distributionAchievement(); err != nil { - return nil, err - } return out, nil } func NewIndex() IndexHandle { - return func(session *service.Session, local string) *Index { + return func(session *service.Session) *Index { return &Index{ Session: session, - local: local, } } } diff --git a/app/api/website/model/manage_expert.go b/app/api/website/model/manage_expert.go index 19bfeed..28549e9 100644 --- a/app/api/website/model/manage_expert.go +++ b/app/api/website/model/manage_expert.go @@ -74,13 +74,14 @@ FROM %s AS u LEFT JOIN %s AS p ON u.parent_id = p.id WHERE u.is_deleted = %d AND } // Distribution 分布信息 -func (m *ManageExpert) Distribution() ([]*DataAreaDistributionInfo, error) { +func (m *ManageExpert) Distribution(province, city string) ([]*DataAreaDistributionInfo, error) { out := make([]*DataAreaDistributionInfo, 0) - err := orm.GetDB().Table(m.TableName()). - Select("province", "city", "GROUP_CONCAT(industry SEPARATOR '&') AS industry"). - Group("province").Group("city"). - Order("province "+model.OrderModeToAsc).Order("city "+model.OrderModeToAsc). + db := orm.GetDB().Table(m.TableName()). + Select("province", "city", "district", "GROUP_CONCAT(industry SEPARATOR '&') AS industry"). + Group("province").Group("city").Group("district") + + err := db.Order("province "+model.OrderModeToAsc).Order("city "+model.OrderModeToAsc).Order("district "+model.OrderModeToAsc). Where("examine_status = ?", model.ExamineStatusForAgree). Scan(&out).Error diff --git a/app/basic/api/base.go b/app/basic/api/base.go index eb59ea5..deeff07 100644 --- a/app/basic/api/base.go +++ b/app/basic/api/base.go @@ -32,8 +32,12 @@ func GetSession() ApiHandle { func GetTenantID() ApiHandle { return func(c *gin.Context) interface{} { - value, _ := c.Get(config.ContentForTenantID) - return value + _, isExist := c.Get(config.ContentForTenantID) + //value + if !isExist { + return 0 + } + return uint64(0) } } diff --git a/app/common/migrate/instance.go b/app/common/migrate/instance.go index fe0d4b1..e9b315b 100644 --- a/app/common/migrate/instance.go +++ b/app/common/migrate/instance.go @@ -30,9 +30,9 @@ func (this *Instance) Handle() { if !db.Migrator().HasTable(v.iModel) { err := db.Migrator().CreateTable(v.iModel) if err != nil { - successCount++ - } else { failureCount++ + } else { + successCount++ } if v.iValues != nil && v.iValues() != nil { db.Table(v.iModel.TableName()).Create(v.iValues()) @@ -47,8 +47,9 @@ func (this *Instance) Handle() { &synchronized{iModel: model.NewSysConfig()}, &synchronized{iModel: model.NewSysMenu()}, &synchronized{iModel: model.NewSysAuth()}, &synchronized{iModel: model.NewSysUser(), iValues: func() interface{} { - return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888", Password: utils.Md5String("123456"), - IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"} + return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888", + Password: utils.Md5String("123456"), + IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"} }}, &synchronized{iModel: model.NewSysUserRole()}, &synchronized{iModel: model.NewSysUserTenant()}, &synchronized{iModel: model.NewSysDepartment()}, @@ -141,7 +142,10 @@ func (this *Instance) Handle() { &synchronized{iModel: model.NewActivityInstance()}, &synchronized{iModel: model.NewActivityApply()}, &synchronized{iModel: model.NewActivityExamine()}, &synchronized{iModel: model.NewActivityJoin()}, ) - fmt.Printf("========================\n=== 数据迁移,成功【%d】,失败【%d】 ===\n========================\n", successCount, failureCount) + fmt.Println("=== 数据迁移 ===") + fmt.Printf("=== 成功【%d】 ===\n", successCount) + fmt.Printf("=== 失败【%d】 ===\n", failureCount) + fmt.Println("========================") } func WithGormDBOption(db *gorm.DB) Option { diff --git a/router/address.go b/router/address.go index d9546b8..a2dd72f 100644 --- a/router/address.go +++ b/router/address.go @@ -28,6 +28,17 @@ func registerAPI(app *gin.Engine) { { _api := new(api2.Index) indexV1.GET("", _api.Instance) + indexV1.GET("/distribution/expert", _api.DistributionExpert) + indexV1.GET("/distribution/laboratory", _api.DistributionLaboratory) + indexV1.GET("/distribution/demand", _api.DistributionDemand) + indexV1.GET("/distribution/patent", _api.DistributionPatent) + indexV1.GET("/distribution/achievement", _api.DistributionAchievement) + } + // Config 首页信息管理 + configV1 := v1.Group("/config") + { + _api := new(api2.Config) + configV1.POST("area", _api.Area) } // User 用户信息管理 userV1 := v1.Group("/user") @@ -121,11 +132,13 @@ func registerAdminAPI(app *gin.Engine) { apiPrefix := "/admin" g := app.Group(apiPrefix) + v1 := g.Group("/v1") + // 登录验证 g.Use(NeedLogin(config.RedisKeyForAccountAdmin, session.NewAdmin(), AddSkipperURL([]string{ - apiPrefix + "/captcha", - apiPrefix + "/account/login", - apiPrefix + "/account/logout", + apiPrefix + "/v1/captcha", + apiPrefix + "/v1/account/login", + apiPrefix + "/v1/account/logout", }...))) // 权限验证 //g.Use(NeedPermission(AddSkipperURL([]string{ @@ -134,18 +147,18 @@ func registerAdminAPI(app *gin.Engine) { // apiPrefix + "/account/logout", //}...))) // Captcha 验证码 - g.GET("/captcha", new(api1.Captcha).Captcha) + v1.GET("/captcha", new(api1.Captcha).Captcha) // Upload 上传管理 - g.POST("/upload", new(api.Upload).Upload) + v1.POST("/upload", new(api.Upload).Upload) // Account 账户管理 - account := g.Group("/account") + account := v1.Group("/account") { _api := new(api1.Account) account.POST("/login", _api.Login) account.POST("/logout", _api.Logout) } // User 用户管理 - user := g.Group("/user") + user := v1.Group("/user") { _api := new(api1.User) user.GET("/info", _api.Info) @@ -154,13 +167,13 @@ func registerAdminAPI(app *gin.Engine) { user.POST("/add", _api.Add) user.POST("/edit", _api.Edit) user.POST("/delete", _api.Delete) - user.POST("/edit/password", _api.EditPassword) - user.POST("/password/quick", _api.QuickPassword) + user.POST("/password", _api.Password) + user.POST("/password/edit", _api.PasswordEdit) user.POST("/role", _api.Role) user.POST("/role/bind", _api.RoleBind) } // Tenant 租户管理 - tenant := g.Group("/tenant") + tenant := v1.Group("/tenant") { _api := new(api1.Tenant) tenant.POST("/", _api.Instance) @@ -174,7 +187,7 @@ func registerAdminAPI(app *gin.Engine) { tenant.POST("/auth/bind", _api.AuthBind) } // Menu 菜单管理 - menu := g.Group("/menu") + menu := v1.Group("/menu") { _api := new(api1.Menu) menu.GET("/list", _api.List) @@ -184,13 +197,13 @@ func registerAdminAPI(app *gin.Engine) { menu.POST("/delete", _api.Delete) } // Auth 权限管理 - auth := g.Group("/auth") + auth := v1.Group("/auth") { _api := new(api1.Auth) auth.POST("/list", _api.List) } // Department 部门管理 - department := g.Group("/department") + department := v1.Group("/department") { _api := new(api1.Department) department.GET("/list", _api.List) @@ -200,7 +213,7 @@ func registerAdminAPI(app *gin.Engine) { department.POST("/delete", _api.Delete) } // Role 角色管理 - role := g.Group("/role") + role := v1.Group("/role") { _api := new(api1.Role) role.POST("/list", _api.List) @@ -215,7 +228,7 @@ func registerAdminAPI(app *gin.Engine) { role.POST("/auth/bind", _api.AuthBind) } // Logs 日志管理 - log := g.Group("/log") + log := v1.Group("/log") { _api := new(api1.Log) log.POST("/login", _api.Login)