resourceCenter.go 6.3 KB

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