Browse Source

增加getBusinessInfo方法

master v0.5.0
guzeng 2 years ago
parent
commit
44d8eeb79f
3 changed files with 111 additions and 0 deletions
  1. +106
    -0
      data.go
  2. +4
    -0
      user.pb.go
  3. +1
    -0
      user.proto

+ 106
- 0
data.go View File

@ -0,0 +1,106 @@
package userrpc
import (
"encoding/json"
"errors"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
func SetResData(data interface{}, res *Response) {
res_data_json, err := json.Marshal(data)
if err == nil {
encryData := crypter.DesEn(string(res_data_json), DES_KEY)
now_str := strconv.FormatInt(time.Now().Unix(), 10)
res_sign := Sign(encryData, now_str)
res.Data = proto.String(encryData)
res.Time = proto.String(now_str)
res.Sign = proto.String(res_sign)
}
}
func SetReqData(arg interface{}) (*Request, error) {
data_json, err := json.Marshal(arg)
if err != nil {
return nil, err
}
now_int64 := time.Now().Unix()
encryData := crypter.DesEn(string(data_json), DES_KEY)
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil
}
func GetReqData(req *Request) (string, error) {
res_data := req.GetData()
if res_data != "" {
time_int64, err := strconv.ParseInt(req.GetTime(), 10, 64)
if err != nil {
return "", err
}
now_int64 := time.Now().Unix()
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
//时间误差前后10秒,返回
return "", errors.New("返回时间错误")
}
check_sign := CheckSign(req.GetSign(), res_data, req.GetTime())
if !check_sign {
return "", errors.New("返回数据签名错误")
}
//解密
return crypter.DesDe(res_data, DES_KEY), nil
}
return "", nil
}
func GetResData(res *Response) (string, error) {
res_data := res.GetData()
if res_data != "" {
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
if err != nil {
return "", err
}
now_int64 := time.Now().Unix()
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
//时间误差前后10秒,返回
return "", errors.New("返回时间错误")
}
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
if !check_sign {
return "", errors.New("返回数据签名错误")
}
//解密
return crypter.DesDe(res_data, DES_KEY), nil
}
return "", nil
}

+ 4
- 0
user.pb.go View File

@ -480,6 +480,7 @@ type UserService interface {
GetThird(in *Request, out *Response) error
GetBusiness(in *Request, out *Response) error
GetById(in *Request, out *Response) error
GetBusinessInfo(in *Request, out *Response) error
}
// AcceptUserServiceClient accepts connections on the listener and serves requests
@ -575,6 +576,9 @@ func (c *UserServiceClient) GetBusiness(in *Request, out *Response) error {
func (c *UserServiceClient) GetById(in *Request, out *Response) error {
return c.Call("UserService.GetById", in, out)
}
func (c *UserServiceClient) GetBusinessInfo(in *Request, out *Response) error {
return c.Call("UserService.GetBusinessInfo", in, out)
}
// DialUserService connects to an UserService at the specified network address.
func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) {


+ 1
- 0
user.proto View File

@ -90,4 +90,5 @@ service UserService {
rpc getThird(Request) returns (Response); //
rpc getBusiness(Request) returns (Response); //
rpc getById(Request) returns (Response); //
rpc getBusinessInfo(Request) returns (Response); //
}

Loading…
Cancel
Save