4 Commits

Author SHA1 Message Date
  listen be8480c3dc 发布新版 2 years ago
  listen e1fd8ec9ff 添加获取优惠券信息方法 2 years ago
  guzeng 8c110e8f6b 增加GetCouponInfo方法 2 years ago
  listen 43d9126f7c 增加方法 2 years ago
5 changed files with 180 additions and 1 deletions
Split View
  1. +4
    -0
      coupon.pb.go
  2. +2
    -1
      coupon.proto
  3. +106
    -0
      data.go
  4. +55
    -0
      get_coupon.go
  5. +13
    -0
      get_coupon_test.go

+ 4
- 0
coupon.pb.go View File

@ -100,6 +100,7 @@ func init() {
type CouponService interface {
IsAvailable(in *CouponRequest, out *CouponResponse) error
Use(in *CouponRequest, out *CouponResponse) error
GetCouponInfo(in *CouponRequest, out *CouponResponse) error
}
// AcceptCouponServiceClient accepts connections on the listener and serves requests
@ -177,6 +178,9 @@ func (c *CouponServiceClient) IsAvailable(in *CouponRequest, out *CouponResponse
func (c *CouponServiceClient) Use(in *CouponRequest, out *CouponResponse) error {
return c.Call("CouponService.Use", in, out)
}
func (c *CouponServiceClient) GetCouponInfo(in *CouponRequest, out *CouponResponse) error {
return c.Call("CouponService.GetCouponInfo", in, out)
}
// DialCouponService connects to an CouponService at the specified network address.
func DialCouponService(network, addr string) (*CouponServiceClient, *rpc.Client, error) {


+ 2
- 1
coupon.proto View File

@ -20,4 +20,5 @@ message CouponResponse {
service CouponService {
rpc isAvailable (CouponRequest) returns (CouponResponse); //
rpc use (CouponRequest) returns (CouponResponse); // 使
}
rpc GetCouponInfo (CouponRequest) returns (CouponResponse); //
}

+ 106
- 0
data.go View File

@ -0,0 +1,106 @@
package couponrpc
import (
"encoding/json"
"errors"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
func SetResData(data interface{}, res *CouponResponse) {
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{}) (*CouponRequest, 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 &CouponRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil
}
func GetReqData(req *CouponRequest) (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 *CouponResponse) (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
}

+ 55
- 0
get_coupon.go View File

@ -0,0 +1,55 @@
package couponrpc
import (
"encoding/json"
"errors"
)
func GetCouponInfo(dbname,couponId string, url ...string) (map[string]string, error) {
if dbname == "" {
return nil, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
arg := map[string]string{
"dbname": dbname,
"coupon_id": couponId,
}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &CouponResponse{}
err = conn.GetCouponInfo(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(res)
if err != nil {
return nil, err
}
var couponInfo map[string]string
err = json.Unmarshal([]byte(res_data_de),&couponInfo)
if err != nil {
return nil, err
}
if res_data_de == "" {
return nil, nil
}
return couponInfo, nil
}

+ 13
- 0
get_coupon_test.go View File

@ -0,0 +1,13 @@
package couponrpc
import "testing"
func Test_getCoupon(t *testing.T) {
dbname := "shop_v2"
couponId := "4"
res, err := GetCouponInfo(dbname, couponId)
t.Log(res)
t.Log(err)
}

Loading…
Cancel
Save