pointsRpc.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package rpc
  2. import (
  3. "app.yhyue.com/moapp/jyPoints/rpc/integralclient"
  4. "context"
  5. "fmt"
  6. "github.com/zeromicro/go-zero/core/discov"
  7. "github.com/zeromicro/go-zero/zrpc"
  8. "jy-docs/config"
  9. "log"
  10. )
  11. // 剑鱼积分RPC接口
  12. var jyIntegralLib integralclient.Integral
  13. func init() {
  14. jyIntegralLib = integralclient.NewIntegral(zrpc.MustNewClient(zrpc.RpcClientConf{
  15. Etcd: discov.EtcdConf{
  16. Key: config.JyDocsAppConfig.RpcServers.Points.Key,
  17. Hosts: config.JyDocsAppConfig.RpcServers.Points.Address,
  18. },
  19. }))
  20. }
  21. /*
  22. 积分查询
  23. param
  24. userId 用户id
  25. return
  26. int64 剩余积分
  27. error 异常
  28. */
  29. func GetUserPointValue(userId string) (int64, error) {
  30. resp, err := jyIntegralLib.IntegralBalanceCheck(context.Background(), &integralclient.Req{
  31. AppId: config.JyDocsAppConfig.AppId,
  32. UserId: userId,
  33. })
  34. if err != nil {
  35. log.Printf("%s getUserPointValue call error %v\n", userId, err)
  36. return -1, err
  37. }
  38. if resp.Code != 1 {
  39. log.Printf("%s getUserPointValue fail Message %v\n", userId, resp.Message)
  40. return -1, fmt.Errorf("查询积分异常")
  41. }
  42. return resp.Data, nil
  43. }
  44. /*
  45. 积分消费
  46. param
  47. userId 用户id
  48. docId 文库id
  49. abstract 文库简介(积分流水列表展示)
  50. return
  51. error 异常
  52. */
  53. func SpendUserPoint(userId, docId, abstract string, value int64) (string, error) {
  54. resp, err := jyIntegralLib.IntegralConsume(context.Background(), &integralclient.Req{
  55. AppId: config.JyDocsAppConfig.AppId,
  56. UserId: userId,
  57. PointType: 2003, //文库下载编号
  58. Point: value,
  59. SourceId: docId,
  60. SourceType: "积分消耗",
  61. Abstract: abstract,
  62. })
  63. if err != nil {
  64. log.Printf("%s SpendUserPoint call error %v\n", userId, err)
  65. return "", err
  66. }
  67. if resp.Code != 1 {
  68. log.Printf("%s SpendUserPoint fail Message %v\n", userId, resp.Message)
  69. return "", fmt.Errorf("积分扣除异常")
  70. }
  71. return resp.SerialNumber, nil
  72. }