api.go 3.0 KB

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