-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
59 lines (50 loc) · 1.58 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package service
import (
"context"
"github.com/gin-gonic/gin"
"github.com/hopeio/cherry/_example/user/dao"
gormi "github.com/hopeio/cherry/utils/dao/db/gorm"
"google.golang.org/protobuf/types/known/wrapperspb"
"strconv"
"github.com/hopeio/cherry/_example/protobuf/user"
"github.com/hopeio/cherry/_example/user/confdao"
"github.com/hopeio/cherry/context/httpctx"
"github.com/hopeio/cherry/protobuf/errorcode"
)
type UserService struct {
user.UnimplementedUserServiceServer
}
func (u *UserService) Signup(ctx context.Context, req *user.SignupReq) (*wrapperspb.StringValue, error) {
ctxi, span := httpctx.ContextFromContext(ctx).StartSpan("")
defer span.End()
ctx = ctxi.Context()
if req.Mail == "" && req.Phone == "" {
return nil, errorcode.DBError.Message("请填写邮箱或手机号")
}
formatNow := ctxi.TimeString
var user = &user.User{
Name: req.Name,
Mail: req.Mail,
Phone: req.Phone,
Gender: req.Gender,
Role: user.RoleNormal,
CreatedAt: formatNow,
Status: user.UserStatusInActive,
}
db := gormi.NewTraceDB(confdao.Dao.GORMDB.DB, ctx, ctxi.TraceID)
err := db.Create(&user).Error
if err != nil {
return nil, ctxi.ErrorLog(errorcode.DBError.Message("新建出错"), err, "UserService.Creat")
}
return &wrapperspb.StringValue{Value: "注册成功"}, nil
}
func Test(ctx *gin.Context) {
idStr := ctx.Param("id")
id, _ := strconv.Atoi(idStr)
ctxi := httpctx.ContextFromContext(ctx.Request.Context())
t, err := dao.GetDao(ctxi, confdao.Dao.GORMDB.DB).GetJsonArrayT(id)
if err != nil {
ctx.Writer.WriteString(err.Error())
}
ctx.JSON(200, t)
}