resourceCenter.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package rpc
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/gtrace"
  6. "strings"
  7. . "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
  8. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  9. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/resource"
  10. "github.com/gogf/gf/v2/os/gcfg"
  11. "github.com/gogf/gf/v2/os/gctx"
  12. "github.com/zeromicro/go-zero/core/discov"
  13. "github.com/zeromicro/go-zero/zrpc"
  14. )
  15. var ResourceCenterRpc resource.Resource
  16. func initResourceCenterRpc() {
  17. ResourceCenterRpc = resource.NewResource(zrpc.MustNewClient(zrpc.RpcClientConf{
  18. Etcd: discov.EtcdConf{
  19. Key: gcfg.Instance().MustGet(gctx.New(), "etcd.baseserver.resourceCenter.key", nil).String(),
  20. Hosts: gcfg.Instance().MustGet(gctx.New(), "etcd.baseserver.resourceCenter.address", nil).Strings(),
  21. },
  22. }))
  23. }
  24. // CheckResourcePower 校验账户是否有对应权益
  25. // accountId 账户id
  26. // entAccountId 企业账户id
  27. // entId 企业id
  28. // entUserId 企业员工id
  29. // funcCode 业务代码
  30. // reqFuncCode 通用结构&&需要校验权限则先判断此接口是否有此functionCode的方法
  31. // appid 平台标识
  32. func CheckResourcePower(ctx gctx.Ctx, accountId, entAccountId, entId, entUserId int64, funcCodeRule, reqFuncCode, Appid string) (UseCount, SurplusCount int64, err error) {
  33. ctx, span := gtrace.NewSpan(ctx, "CheckResourcePower")
  34. defer span.End()
  35. funcCode := funcCodeRule
  36. funcCodeArr := strings.Split(funcCodeRule, ",")
  37. if reqFuncCode != "" || len(funcCodeArr) > 1 {
  38. var checkPass bool
  39. for _, tFunc := range funcCodeArr {
  40. if reqFuncCode == tFunc {
  41. checkPass = true
  42. break
  43. }
  44. }
  45. if !checkPass {
  46. err = NewErrorWithCode(GLOBAL_ERR_RESOURCE_POWERCODE_ERR, fmt.Sprintf("账户 accountId:%d entAccountId:%d entId:%d entUserId:%d 无效funccode:%s req:%s权限", accountId, entAccountId, entId, entUserId, funcCodeRule, reqFuncCode))
  47. return
  48. }
  49. funcCode = reqFuncCode
  50. }
  51. res, err := ResourceCenterRpc.CheckPower(ctx, &pb.CheckPowerReq{
  52. Appid: Appid,
  53. FunctionCode: funcCode,
  54. AccountId: accountId,
  55. EntAccountId: entAccountId,
  56. EntId: entId,
  57. EntUserId: entUserId,
  58. })
  59. if err != nil {
  60. err = NewErrorWithCode(GATEWAY_RPC_RESOURCECENTER_ERR, err.Error())
  61. return
  62. }
  63. //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
  64. switch res.Status {
  65. case 0:
  66. err = NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_FAIL, fmt.Sprintf("账户 accountId:%d entAccountId:%d entId:%d entUserId:%d 无业务%s权限", accountId, entAccountId, entId, entUserId, funcCode))
  67. return
  68. case -1:
  69. err = NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_EXPIRED, fmt.Sprintf("账户 accountId:%d entAccountId:%d entId:%d entUserId:%d 业务%s权限已过期", accountId, entAccountId, entId, entUserId, funcCode))
  70. return
  71. case -2:
  72. err = NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH, fmt.Sprintf("账户 accountId:%d entAccountId:%d entId:%d entUserId:%d 业务%s权限余额不足", accountId, entAccountId, entId, entUserId, funcCode))
  73. return
  74. case -3:
  75. err = NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTHAS, fmt.Sprintf("账户 accountId:%d entAccountId:%d entId:%d entUserId:%d 业务%s权限无授权", accountId, entAccountId, entId, entUserId, funcCode))
  76. return
  77. }
  78. return res.UseCount, res.SurplusCount, nil
  79. }
  80. // ResourcePowerDeduct 资源中心消费
  81. // accountId 账户id
  82. // entAccountId 企业账户id
  83. // funcCode 业务代码
  84. // deductNum 扣除数量
  85. // ids 扣除详情id;例如数据导出信息id等数据
  86. func ResourcePowerDeduct(ctx g.Ctx, accountId, entAccountId int64, funcCode, appId string, deductNum int64, ids []string) error {
  87. ctx, span := gtrace.NewSpan(ctx, "ResourcePowerDeduct")
  88. defer span.End()
  89. res, err := ResourceCenterRpc.Deduction(ctx, &pb.DeductionReq{
  90. Appid: appId,
  91. AccountId: accountId,
  92. EntAccountId: entAccountId,
  93. FunctionCode: funcCode,
  94. Count: deductNum,
  95. Ids: ids,
  96. })
  97. if err != nil {
  98. return NewErrorWithCode(GATEWAY_RPC_RESOURCECENTER_ERR, err.Error())
  99. }
  100. //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
  101. switch res.Status {
  102. case 0:
  103. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_FAIL, fmt.Sprintf("账户 accountId:%d entAccountId:%d 扣除业务%s权限失败", accountId, entAccountId, funcCode))
  104. case -1:
  105. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_EXPIRED, fmt.Sprintf("账户 accountId:%d entAccountId:%d 扣除业务%s权限失败,权限已过期", accountId, entAccountId, funcCode))
  106. case -2:
  107. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH, fmt.Sprintf("账户 accountId:%d entAccountId:%d 扣除业务%s权限失败,余额不足", accountId, entAccountId, funcCode))
  108. case -3:
  109. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTHAS, fmt.Sprintf("账户 accountId:%d entAccountId:%d 扣除业务%s权限失败", accountId, entAccountId, funcCode))
  110. }
  111. return nil
  112. }
  113. // ResourcePowerRecharge 资源中心充值
  114. // accountId 账户id
  115. // entAccountId 企业账户id
  116. // funcCode 业务代码
  117. // deductNum 扣除数量
  118. // ids 扣除详情id;例如数据导出信息id等数据
  119. func ResourcePowerRecharge(ctx g.Ctx, accountId, entAccountId int64, funcCode, appId string, deductNum int64, ids []string) error {
  120. ctx, span := gtrace.NewSpan(ctx, "ResourcePowerRecharge")
  121. defer span.End()
  122. res, err := ResourceCenterRpc.Recharge(ctx, &pb.RechargeReq{
  123. Appid: appId,
  124. AccountId: accountId,
  125. EntAccountId: entAccountId,
  126. FunctionCode: funcCode,
  127. Count: deductNum,
  128. Ids: ids,
  129. })
  130. if err != nil {
  131. return NewErrorWithCode(GATEWAY_RPC_RESOURCECENTER_ERR, err.Error())
  132. }
  133. switch res.Status {
  134. case 0:
  135. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_FAIL, fmt.Sprintf("账户 accountId:%d entAccountId:%d 充值业务%s权限失败", accountId, entAccountId, funcCode))
  136. case -1:
  137. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_EXPIRED, fmt.Sprintf("账户 accountId:%d entAccountId:%d 充值业务%s权限失败,权限已过期", accountId, entAccountId, funcCode))
  138. case -2:
  139. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH, fmt.Sprintf("账户 accountId:%d entAccountId:%d 充值业务%s权限失败,余额不足", accountId, entAccountId, funcCode))
  140. case -3:
  141. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_NOTHAS, fmt.Sprintf("账户 accountId:%d entAccountId:%d 充值业务%s权限失败", accountId, entAccountId, funcCode))
  142. case -4: //超出可拥有最大限额
  143. return NewErrorWithCode(GLOBAL_ERR_RESOURCE_PORWE_RECHARGE_FULL, fmt.Sprintf("账户 accountId:%d entAccountId:%d 充值业务%s权限失败", accountId, entAccountId, funcCode))
  144. }
  145. return nil
  146. }