user.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package service
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/errors/gerror"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "gof_ppw_api/internal/dao"
  7. "gof_ppw_api/internal/model/do"
  8. "gof_ppw_api/internal/model/entity"
  9. )
  10. var User = &UserService{}
  11. type UserService struct {
  12. Service
  13. }
  14. // Create 创建用户
  15. func (p *UserService) Create(ctx context.Context, data map[string]interface{}) (err error) {
  16. //事务
  17. //return dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  18. // _, err = dao.User.Ctx(ctx).Data(data).Insert()
  19. // return err
  20. //})
  21. _, err = dao.User.Ctx(ctx).Data(data).Insert()
  22. return err
  23. }
  24. // Update 修改用户
  25. func (p *UserService) Update(ctx context.Context, data map[string]interface{}) (err error) {
  26. _, err = dao.User.Ctx(ctx).Where("id", data["user_id"]).Data(data).Update()
  27. return err
  28. }
  29. // Delete 删除用户
  30. func (p *UserService) Delete(ctx context.Context, data map[string]interface{}) (err error) {
  31. _, err = dao.User.Ctx(ctx).Delete("id", data["user_id"])
  32. return err
  33. }
  34. // Login 用户登录
  35. func (p *UserService) Login(ctx context.Context, data map[string]interface{}) (err error) {
  36. var user *entity.User
  37. err = dao.User.Ctx(ctx).Where(do.User{
  38. UserName: data["user_name"],
  39. Password: data["password"],
  40. }).Scan(&user)
  41. if err != nil {
  42. return err
  43. }
  44. if user == nil {
  45. return gerror.New("用户名或密码错误")
  46. }
  47. _ = g.RequestFromCtx(ctx).Session.Set("user_id", user.Id)
  48. return nil
  49. }
  50. // Logout 用户登出
  51. func (p *UserService) Logout(ctx context.Context) (err error) {
  52. _ = g.RequestFromCtx(ctx).Session.Remove("user_id")
  53. return nil
  54. }