api.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package im
  2. import (
  3. "context"
  4. "sportfitness/base/global"
  5. im "sportfitness/base/repository/grpc/bsw/im/im"
  6. "google.golang.org/grpc/status"
  7. "git.beswell.com/gframe/application"
  8. "google.golang.org/grpc/metadata"
  9. )
  10. func handleErr(err error) {
  11. s, _ := status.FromError(err)
  12. panic(application.ErrorBusinessF(application.ErrorCode(s.Code()), s.Message()))
  13. }
  14. func getClient() im.ApiClient {
  15. conn, err := application.GetServiceGrpcConn("bsw/im")
  16. if err != nil {
  17. panic(err)
  18. }
  19. client := im.NewApiClient(conn)
  20. return client
  21. }
  22. func ctx() context.Context {
  23. md := metadata.Pairs("sys_token", global.SysToken)
  24. return metadata.NewOutgoingContext(context.Background(), md)
  25. }
  26. func SignUpUserCode(params *im.SignUpRequest) (userId int64) {
  27. r, err := getClient().SignUpUserCode(ctx(), params)
  28. handleErr(err)
  29. userId = r.GetId()
  30. return
  31. }
  32. func GenVerifyImage(height, width int32) (id, imageBase64 string) {
  33. r, err := getClient().GenVerifyImage(ctx(), &im.GenVerifyImageRequest{
  34. Height: int32(height),
  35. Width: int32(width),
  36. })
  37. handleErr(err)
  38. id = r.CodeId
  39. imageBase64 = r.ImageBase64
  40. return
  41. }
  42. func SignInUserCodePassword(userCode, password, ip, ClientInfo, codeId, verifyCode string) (token string, userId int) {
  43. r, err := getClient().SignInUserCode(ctx(), &im.SignInPasswordRequest{
  44. Credential: userCode,
  45. Password: password,
  46. ExpirationSec: global.TokenExpireSec,
  47. Ip: ip,
  48. ClientInfo: ClientInfo,
  49. CodeId: codeId,
  50. VerifyCode: verifyCode,
  51. })
  52. handleErr(err)
  53. token = r.Token
  54. userId = int(r.UserId)
  55. return
  56. }
  57. func RegisterServiceList(servicePathList []string) {
  58. request := &im.SaveServiceListRequest{}
  59. for _, p := range servicePathList {
  60. request.List = append(request.List, &im.Service{
  61. Path: p,
  62. Memo: "自动添加",
  63. })
  64. }
  65. _, err := getClient().SubServiceSaveList(ctx(), request)
  66. if err != nil {
  67. panic(err)
  68. }
  69. }
  70. func SignOut(token string) {
  71. request := &im.TokenParam{Token: token}
  72. _, err := getClient().SubSignOut(ctx(), request)
  73. handleErr(err)
  74. }
  75. func SubSessionCheck(token string) int64 {
  76. request := &im.TokenParam{Token: token}
  77. rs, err := getClient().SubSessionCheck(ctx(), request)
  78. handleErr(err)
  79. return rs.UserId
  80. }
  81. func SubPermissionListCheck(token string, shopId int64, servicePathList []string) *im.PermissionCheckReply {
  82. rs, err := getClient().SubPermissionListCheck(ctx(), &im.PermissionCheck{
  83. Token: token,
  84. ShopId: shopId,
  85. ServicePathList: servicePathList,
  86. })
  87. handleErr(err)
  88. return rs
  89. }
  90. func ShopListRequest(name string, status im.Status, sId int64) *im.ShopListReply {
  91. request := &im.ShopListRequest{SysId: 1, Name: name, Status: status, SId: sId}
  92. rs, err := getClient().ShopList(ctx(), request)
  93. handleErr(err)
  94. return rs
  95. }
  96. func GetShopNavi(userId, shopId int64, parentId int32) *im.UserGetColumnListReply {
  97. request := &im.UserGetColumnListRequest{UserId: userId, ShopId: shopId, ParentId: parentId}
  98. rs, err := getClient().UserGetColumnList(ctx(), request)
  99. handleErr(err)
  100. return rs
  101. }