1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package service
- import (
- "context"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "gof_ppw_api/internal/dao"
- "gof_ppw_api/internal/model/do"
- "gof_ppw_api/internal/model/entity"
- )
- var User = &UserService{}
- type UserService struct {
- Service
- }
- // Create 创建用户
- func (p *UserService) Create(ctx context.Context, data map[string]interface{}) (err error) {
- //事务
- //return dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- // _, err = dao.User.Ctx(ctx).Data(data).Insert()
- // return err
- //})
- _, err = dao.User.Ctx(ctx).Data(data).Insert()
- return err
- }
- // Update 修改用户
- func (p *UserService) Update(ctx context.Context, data map[string]interface{}) (err error) {
- _, err = dao.User.Ctx(ctx).Where("id", data["user_id"]).Data(data).Update()
- return err
- }
- // Delete 删除用户
- func (p *UserService) Delete(ctx context.Context, data map[string]interface{}) (err error) {
- _, err = dao.User.Ctx(ctx).Delete("id", data["user_id"])
- return err
- }
- // Login 用户登录
- func (p *UserService) Login(ctx context.Context, data map[string]interface{}) (err error) {
- var user *entity.User
- err = dao.User.Ctx(ctx).Where(do.User{
- UserName: data["user_name"],
- Password: data["password"],
- }).Scan(&user)
- if err != nil {
- return err
- }
- if user == nil {
- return gerror.New("用户名或密码错误")
- }
- _ = g.RequestFromCtx(ctx).Session.Set("user_id", user.Id)
- return nil
- }
- // Logout 用户登出
- func (p *UserService) Logout(ctx context.Context) (err error) {
- _ = g.RequestFromCtx(ctx).Session.Remove("user_id")
- return nil
- }
|