package storeAdmin import ( "context" "server/internal/consts" "server/internal/dao" "server/internal/model" "server/internal/model/do" "server/internal/model/entity" "server/internal/service" "server/utility/encrypt" "server/utility/ecode" "server/utility/jwt" ) type sStoreAdmin struct { } func New() service.IStoreAdmin { return &sStoreAdmin{} } func init() { service.RegisterStoreAdmin(New()) go checkStoreAdminRole() } func checkStoreAdminRole() { ctx := context.Background() exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.StoreRoleCode}).Exist() if err != nil { return } if !exist { _, err = dao.Roles.Ctx(ctx).Insert(do.Roles{ Name: "门店管理员", Code: consts.StoreRoleCode, Description: "门店管理员角色", Status: consts.StoreAdminEnable, IsDeletable: false, }) if err != nil { return } } } func (s *sStoreAdmin) Login(ctx context.Context, in *model.StoreAdminLoginIn) (out *model.StoreAdminLoginOut, err error) { one, err := dao.StoreAdmins.Ctx(ctx).Where(do.StoreAdmins{Username: in.Username}).One() if err != nil { return nil, ecode.Fail.Sub("查询商户管理员失败") } if one.IsEmpty() { return nil, ecode.Params.Sub("该用户不存在") } if one[dao.StoreAdmins.Columns().Status].Int() == consts.StoreAdminDisable { return nil, ecode.Params.Sub("该用户已被禁用") } if !encrypt.ComparePassword(one[dao.StoreAdmins.Columns().PasswordHash].String(), in.Password) { return nil, ecode.Params.Sub("密码错误") } value, err := dao.Roles.Ctx(ctx).WherePri(one[dao.StoreAdmins.Columns().RoleId].Int()).Fields(dao.Roles.Columns().Code).Value() if err != nil { return nil, ecode.Fail.Sub("查询角色失败") } token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: one[dao.StoreAdmins.Columns().Id].Int64(), Role: value.String()}) if err != nil { return nil, ecode.Fail.Sub("生成token失败") } out = &model.StoreAdminLoginOut{ Token: token, } return } func (s *sStoreAdmin) Info(ctx context.Context, in *model.StoreAdminInfoIn) (out *model.StoreAdminInfoOut, err error) { exist, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Exist() if err != nil { return nil, ecode.Fail.Sub("查询门店管理员失败") } if !exist { return nil, ecode.Params.Sub("该用户不存在") } var storeAdmin entity.StoreAdmins err = dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Scan(&storeAdmin) if err != nil { return nil, ecode.Fail.Sub("查询门店管理员失败") } out = &model.StoreAdminInfoOut{ Username: storeAdmin.Username, } return }