@ -0,0 +1,227 @@ | |||
package coupon | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"encoding/json" | |||
"errors" | |||
"log" | |||
"strconv" | |||
"strings" | |||
"time" | |||
"git.tetele.net/ttlpkg/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
type AvailableReqArg struct { | |||
Dbname string `json:"dbname"` | |||
UserCouponId string `json:"user_coupon_id"` | |||
UserId string `json:"user_id"` | |||
ProductId string `json:"product_id"` | |||
} | |||
type AvailableRes struct { | |||
Available bool `json:"available"` | |||
Money string `json:"money"` | |||
Name string `json:"name"` | |||
} | |||
type UseReqArg struct { | |||
Dbname string `json:"dbname"` | |||
UserCouponId string `json:"user_coupon_id"` | |||
OrderSn string `json:"order_sn"` | |||
} | |||
type UseRes struct { | |||
Success bool | |||
} | |||
/** | |||
* 优惠券是否可用 | |||
* @param data {"user_id":"","product_id":"","coupon_id":""} | |||
* @param return is_available,name,money,error | |||
*/ | |||
func IsAvailable(dbname, user_id, product_id, user_coupon_id string, url ...string) (*AvailableRes, error) { | |||
var coupon_rpc_url string = "127.0.0.1:7972" | |||
if len(url) > 0 && url[0] != "" { | |||
coupon_rpc_url = url[0] | |||
} | |||
conn, _, err := DialCouponService("tcp", coupon_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := AvailableReqArg{dbname, user_coupon_id, user_id, product_id} | |||
data_json, err := json.Marshal(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
encryData := crypter.DesEn(string(data_json), "conaapon") | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
req := &CouponRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil} | |||
res := &CouponResponse{} | |||
err = conn.IsAvailable(req, res) | |||
if err != nil { | |||
log.Println("coupon rpc error:", err) | |||
return nil, err | |||
} | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 = time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return nil, errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return nil, errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
res_data_de := crypter.DesDe(res_data, "conaapon") | |||
var res_arr AvailableRes | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &res_arr, nil | |||
} | |||
return nil, nil | |||
} | |||
/** | |||
* 使用优惠券 | |||
* @param data {"user_coupon_id":"","order_sn":""} | |||
* @param return is_available,name,money,error | |||
*/ | |||
func Use(dbname, user_coupon_id, order_sn string, url ...string) (*UseRes, error) { | |||
var coupon_rpc_url string = "127.0.0.1:7972" | |||
if len(url) > 0 && url[0] != "" { | |||
coupon_rpc_url = url[0] | |||
} | |||
conn, _, err := DialCouponService("tcp", coupon_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := UseReqArg{dbname, user_coupon_id, order_sn} | |||
data_json, err := json.Marshal(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
encryData := crypter.DesEn(string(data_json), "conaapon") | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
req := &CouponRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil} | |||
res := &CouponResponse{} | |||
err = conn.Use(req, res) | |||
if err != nil { | |||
log.Println("coupon rpc error:", err) | |||
return nil, err | |||
} | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 = time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return nil, errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return nil, errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
res_data_de := crypter.DesDe(res_data, "conaapon") | |||
var res_arr UseRes | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &res_arr, nil | |||
} | |||
return nil, nil | |||
} | |||
/** | |||
* 签名 | |||
*/ | |||
func Sign(data string, salt string) string { | |||
var build strings.Builder | |||
build.WriteString(data) | |||
build.WriteString(salt) | |||
build.WriteString("cou$%po87n") | |||
data_str := build.String() | |||
h := md5.New() | |||
h.Write([]byte(data_str)) // 需要加密的字符串 | |||
return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 | |||
} | |||
/** | |||
* 验证签名 | |||
*/ | |||
func CheckSign(sign_str, data, salt string) bool { | |||
sign := Sign(data, salt) | |||
if strings.Compare(sign_str, sign) > -1 { | |||
return true | |||
} | |||
return false | |||
} |
@ -0,0 +1,17 @@ | |||
package coupon | |||
import ( | |||
"testing" | |||
) | |||
func Test_IsAvailable(t *testing.T) { | |||
dbname := "test1_tetele_com" | |||
user_id := "2" | |||
product_id := "2" | |||
user_coupon_id := "3" | |||
res, err := IsAvailable(dbname, user_id, product_id, user_coupon_id) | |||
t.Log(res) | |||
t.Log(err) | |||
} |
@ -0,0 +1,198 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: coupon.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package coupon is a generated protocol buffer package. | |||
It is generated from these files: | |||
coupon.proto | |||
It has these top-level messages: | |||
CouponRequest | |||
CouponResponse | |||
*/ | |||
package coupon | |||
import proto "github.com/chai2010/protorpc/proto" | |||
import math "math" | |||
import "io" | |||
import "log" | |||
import "net" | |||
import "net/rpc" | |||
import "time" | |||
import protorpc "github.com/chai2010/protorpc" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = math.Inf | |||
// 是否可用请求结构 | |||
type CouponRequest struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *CouponRequest) Reset() { *m = CouponRequest{} } | |||
func (m *CouponRequest) String() string { return proto.CompactTextString(m) } | |||
func (*CouponRequest) ProtoMessage() {} | |||
func (m *CouponRequest) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *CouponRequest) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *CouponRequest) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
// 是否可用响应结构 | |||
type CouponResponse struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *CouponResponse) Reset() { *m = CouponResponse{} } | |||
func (m *CouponResponse) String() string { return proto.CompactTextString(m) } | |||
func (*CouponResponse) ProtoMessage() {} | |||
func (m *CouponResponse) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *CouponResponse) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *CouponResponse) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
func init() { | |||
} | |||
type CouponService interface { | |||
IsAvailable(in *CouponRequest, out *CouponResponse) error | |||
Use(in *CouponRequest, out *CouponResponse) error | |||
} | |||
// AcceptCouponServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptCouponServiceClient(lis net.Listener, x CouponService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("CouponService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
// RegisterCouponService publish the given CouponService implementation on the server. | |||
func RegisterCouponService(srv *rpc.Server, x CouponService) error { | |||
if err := srv.RegisterName("CouponService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewCouponServiceServer returns a new CouponService Server. | |||
func NewCouponServiceServer(x CouponService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("CouponService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeCouponService listen announces on the local network address laddr | |||
// and serves the given CouponService implementation. | |||
func ListenAndServeCouponService(network, addr string, x CouponService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("CouponService", x); err != nil { | |||
return err | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
type CouponServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewCouponServiceClient returns a CouponService rpc.Client and stub to handle | |||
// requests to the set of CouponService at the other end of the connection. | |||
func NewCouponServiceClient(conn io.ReadWriteCloser) (*CouponServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &CouponServiceClient{c}, c | |||
} | |||
func (c *CouponServiceClient) IsAvailable(in *CouponRequest, out *CouponResponse) error { | |||
return c.Call("CouponService.IsAvailable", in, out) | |||
} | |||
func (c *CouponServiceClient) Use(in *CouponRequest, out *CouponResponse) error { | |||
return c.Call("CouponService.Use", in, out) | |||
} | |||
// DialCouponService connects to an CouponService at the specified network address. | |||
func DialCouponService(network, addr string) (*CouponServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &CouponServiceClient{c}, c, nil | |||
} | |||
// DialCouponServiceTimeout connects to an CouponService at the specified network address. | |||
func DialCouponServiceTimeout(network, addr string, | |||
timeout time.Duration) (*CouponServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &CouponServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,23 @@ | |||
syntax = "proto3"; | |||
package coupon; | |||
// 是否可用请求结构 | |||
message CouponRequest { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// 是否可用响应结构 | |||
message CouponResponse { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// rpc方法 | |||
service CouponService { | |||
rpc isAvailable (CouponRequest) returns (CouponResponse); // 优惠券是否可用 | |||
rpc use (CouponRequest) returns (CouponResponse); // 使用优惠券 | |||
} |
@ -0,0 +1,42 @@ | |||
package mastermsg | |||
import ( | |||
"encoding/json" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
func SendEventMsg(site_id, domainname, _type, memo string, data map[string]string, url ...string) (string, error) { | |||
var msg_rpc_url string = "127.0.0.1:7974" | |||
if len(url) > 0 && url[0] != "" { | |||
msg_rpc_url = url[0] | |||
} | |||
conn, _, err := DialMsgService("tcp", msg_rpc_url) | |||
if err != nil { | |||
return "", err | |||
} | |||
defer conn.Close() | |||
_data, err := json.Marshal(data) | |||
if err != nil { | |||
return "", err | |||
} | |||
req := &EventRequest{proto.String(site_id), proto.String(domainname), proto.String(_type), proto.String(memo), proto.String(string(_data)), nil} | |||
res := &EventResponse{} | |||
err = conn.SendEvent(req, res) | |||
if err != nil { | |||
return "", err | |||
} | |||
event_id := res.GetEventId() | |||
if event_id != "" { | |||
return event_id, nil | |||
} | |||
return "", nil | |||
} |
@ -0,0 +1,18 @@ | |||
package mastermsg | |||
import ( | |||
"testing" | |||
) | |||
func Test_SendEventMsg(t *testing.T) { | |||
site_id := "1000064" | |||
domainname := "http://dev.tetele.net" | |||
_type := "warning" | |||
memo := "余额不足" | |||
data := map[string]string{"title": "sss顶替"} | |||
res, err := SendEventMsg(site_id, domainname, _type, memo, data) | |||
t.Log(res) | |||
t.Log(err) | |||
} |
@ -0,0 +1,194 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: mastermsg.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package mastermsg is a generated protocol buffer package. | |||
It is generated from these files: | |||
mastermsg.proto | |||
It has these top-level messages: | |||
EventRequest | |||
EventResponse | |||
*/ | |||
package mastermsg | |||
import proto "github.com/chai2010/protorpc/proto" | |||
import math "math" | |||
import "io" | |||
import "log" | |||
import "net" | |||
import "net/rpc" | |||
import "time" | |||
import protorpc "github.com/chai2010/protorpc" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = math.Inf | |||
// 算术运算请求结构 | |||
type EventRequest struct { | |||
SiteId *string `protobuf:"bytes,1,opt,name=site_id" json:"site_id,omitempty"` | |||
Domainname *string `protobuf:"bytes,2,opt,name=domainname" json:"domainname,omitempty"` | |||
Type *string `protobuf:"bytes,3,opt,name=type" json:"type,omitempty"` | |||
Memo *string `protobuf:"bytes,4,opt,name=memo" json:"memo,omitempty"` | |||
Data *string `protobuf:"bytes,5,opt,name=data" json:"data,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *EventRequest) Reset() { *m = EventRequest{} } | |||
func (m *EventRequest) String() string { return proto.CompactTextString(m) } | |||
func (*EventRequest) ProtoMessage() {} | |||
func (m *EventRequest) GetSiteId() string { | |||
if m != nil && m.SiteId != nil { | |||
return *m.SiteId | |||
} | |||
return "" | |||
} | |||
func (m *EventRequest) GetDomainname() string { | |||
if m != nil && m.Domainname != nil { | |||
return *m.Domainname | |||
} | |||
return "" | |||
} | |||
func (m *EventRequest) GetType() string { | |||
if m != nil && m.Type != nil { | |||
return *m.Type | |||
} | |||
return "" | |||
} | |||
func (m *EventRequest) GetMemo() string { | |||
if m != nil && m.Memo != nil { | |||
return *m.Memo | |||
} | |||
return "" | |||
} | |||
func (m *EventRequest) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
// 算术运算响应结构 | |||
type EventResponse struct { | |||
EventId *string `protobuf:"bytes,1,opt,name=event_id" json:"event_id,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *EventResponse) Reset() { *m = EventResponse{} } | |||
func (m *EventResponse) String() string { return proto.CompactTextString(m) } | |||
func (*EventResponse) ProtoMessage() {} | |||
func (m *EventResponse) GetEventId() string { | |||
if m != nil && m.EventId != nil { | |||
return *m.EventId | |||
} | |||
return "" | |||
} | |||
func init() { | |||
} | |||
type MsgService interface { | |||
SendEvent(in *EventRequest, out *EventResponse) error | |||
} | |||
// AcceptMsgServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptMsgServiceClient(lis net.Listener, x MsgService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("MsgService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
// RegisterMsgService publish the given MsgService implementation on the server. | |||
func RegisterMsgService(srv *rpc.Server, x MsgService) error { | |||
if err := srv.RegisterName("MsgService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewMsgServiceServer returns a new MsgService Server. | |||
func NewMsgServiceServer(x MsgService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("MsgService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeMsgService listen announces on the local network address laddr | |||
// and serves the given MsgService implementation. | |||
func ListenAndServeMsgService(network, addr string, x MsgService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("MsgService", x); err != nil { | |||
return err | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
type MsgServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewMsgServiceClient returns a MsgService rpc.Client and stub to handle | |||
// requests to the set of MsgService at the other end of the connection. | |||
func NewMsgServiceClient(conn io.ReadWriteCloser) (*MsgServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &MsgServiceClient{c}, c | |||
} | |||
func (c *MsgServiceClient) SendEvent(in *EventRequest, out *EventResponse) error { | |||
return c.Call("MsgService.SendEvent", in, out) | |||
} | |||
// DialMsgService connects to an MsgService at the specified network address. | |||
func DialMsgService(network, addr string) (*MsgServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &MsgServiceClient{c}, c, nil | |||
} | |||
// DialMsgServiceTimeout connects to an MsgService at the specified network address. | |||
func DialMsgServiceTimeout(network, addr string, | |||
timeout time.Duration) (*MsgServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &MsgServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,21 @@ | |||
syntax = "proto3"; | |||
package mastermsg; | |||
// 算术运算请求结构 | |||
message EventRequest { | |||
string site_id = 1; | |||
string domainname = 2; | |||
string type = 3; | |||
string memo = 4; | |||
string data = 5; | |||
} | |||
// 算术运算响应结构 | |||
message EventResponse { | |||
string event_id = 1; //ID | |||
} | |||
// rpc方法 | |||
service MsgService { | |||
rpc sendEvent (EventRequest) returns (EventResponse); // 发送事件消息 | |||
} |
@ -0,0 +1,240 @@ | |||
package order | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"encoding/json" | |||
"errors" | |||
"log" | |||
"strconv" | |||
"strings" | |||
"time" | |||
"git.tetele.net/ttlpkg/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
type OrderCreateRes struct { | |||
OrderSn string | |||
} | |||
func Create(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { | |||
var order_rpc_url string = "127.0.0.1:7973" | |||
if len(url) > 0 && url[0] != "" { | |||
order_rpc_url = url[0] | |||
} | |||
conn, _, err := DialOrderService("tcp", order_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
data["dbname"] = dbname | |||
data["site_id"] = site_id | |||
data_json, err := json.Marshal(data) | |||
if err != nil { | |||
return nil, err | |||
} | |||
encryData := crypter.DesEn(string(data_json), "ordernew") | |||
now_int64 := time.Now().Unix() | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
req := &CreateRequest{ | |||
proto.String(encryData), | |||
proto.String(now), | |||
proto.String(sign), | |||
nil} | |||
res := &CreateResponse{} | |||
err = conn.Create(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return HandleRes(res) | |||
// return map[string]interface{}{ | |||
// "code": res.GetCode(), | |||
// "msg": res.GetMsg(), | |||
// "order_sn": res.GetOrderSn()}, err | |||
} | |||
func CreateByChannel(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { | |||
var order_rpc_url string = "127.0.0.1:7973" | |||
if len(url) > 0 && url[0] != "" { | |||
order_rpc_url = url[0] | |||
} | |||
conn, _, err := DialOrderService("tcp", order_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
data["dbname"] = dbname | |||
data["site_id"] = site_id | |||
data["source"] = "channel" | |||
data_json, err := json.Marshal(data) | |||
if err != nil { | |||
return nil, err | |||
} | |||
encryData := crypter.DesEn(string(data_json), "ordernew") | |||
now := strconv.FormatInt(time.Now().Unix(), 10) | |||
sign := Sign(encryData, now) | |||
req := &CreateRequest{ | |||
proto.String(encryData), | |||
proto.String(now), | |||
proto.String(sign), | |||
nil} | |||
res := &CreateResponse{} | |||
err = conn.Create(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return HandleRes(res) | |||
// return map[string]interface{}{ | |||
// "code": res.GetCode(), | |||
// "msg": res.GetMsg(), | |||
// "order_sn": res.GetOrderSn()}, err | |||
} | |||
func CreateByCart(dbname, site_id string, data map[string]interface{}, url ...string) (*OrderCreateRes, error) { | |||
var order_rpc_url string = "127.0.0.1:7973" | |||
if len(url) > 0 && url[0] != "" { | |||
order_rpc_url = url[0] | |||
} | |||
conn, _, err := DialOrderService("tcp", order_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
data["dbname"] = dbname | |||
data["site_id"] = site_id | |||
data_json, err := json.Marshal(data) | |||
if err != nil { | |||
return nil, err | |||
} | |||
log.Println(string(data_json)) | |||
encryData := crypter.DesEn(string(data_json), "cartsnew") | |||
now := strconv.FormatInt(time.Now().Unix(), 10) | |||
sign := Sign(encryData, now) | |||
req := &CreateRequest{ | |||
proto.String(encryData), | |||
proto.String(now), | |||
proto.String(sign), | |||
nil} | |||
res := &CreateResponse{} | |||
err = conn.CreateByCart(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return HandleRes(res) | |||
// return map[string]interface{}{ | |||
// "code": res.GetCode(), | |||
// "msg": res.GetMsg(), | |||
// "order_sn": res.GetOrderSn()}, err | |||
} | |||
/** | |||
* 签名 | |||
*/ | |||
func Sign(data string, salt string) string { | |||
var build strings.Builder | |||
build.WriteString(data) | |||
build.WriteString(salt) | |||
build.WriteString("neworder") | |||
data_str := build.String() | |||
h := md5.New() | |||
h.Write([]byte(data_str)) // 需要加密的字符串 | |||
return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 | |||
} | |||
/** | |||
* 验证签名 | |||
*/ | |||
func CheckSign(sign_str, data, salt string) bool { | |||
sign := Sign(data, salt) | |||
if strings.Compare(sign_str, sign) > -1 { | |||
return true | |||
} | |||
return false | |||
} | |||
/** | |||
* 处理返回结果 | |||
*/ | |||
func HandleRes(res *CreateResponse) (*OrderCreateRes, error) { | |||
res_data := res.GetData() | |||
if res_data == "" { | |||
return nil, errors.New("未收到收据") | |||
} | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return nil, errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return nil, errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
res_data_de := crypter.DesDe(res_data, "ordernew") | |||
var res_arr OrderCreateRes | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &res_arr, nil | |||
} |
@ -0,0 +1,31 @@ | |||
package order | |||
import ( | |||
"testing" | |||
) | |||
func Test_CreateByCart(t *testing.T) { | |||
products := []map[string]string{ | |||
map[string]string{ | |||
"product_id": "18", | |||
"sku_id": "6", | |||
"quantity": "3", | |||
}, | |||
map[string]string{ | |||
"product_id": "6", | |||
"sku_id": "7", | |||
"quantity": "2", | |||
}, | |||
} | |||
data := map[string]interface{}{ | |||
"product": products, | |||
"name": "5BC02AB31C", | |||
"mobile": "326598744", | |||
} | |||
ret, err := CreateByCart("test1_tetele_com", "100064", data) | |||
t.Log(ret) | |||
t.Log(err) | |||
} |
@ -0,0 +1,198 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: order.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package order is a generated protocol buffer package. | |||
It is generated from these files: | |||
order.proto | |||
It has these top-level messages: | |||
CreateRequest | |||
CreateResponse | |||
*/ | |||
package order | |||
import proto "github.com/chai2010/protorpc/proto" | |||
import math "math" | |||
import "io" | |||
import "log" | |||
import "net" | |||
import "net/rpc" | |||
import "time" | |||
import protorpc "github.com/chai2010/protorpc" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = math.Inf | |||
// 下单请求结构 | |||
type CreateRequest struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *CreateRequest) Reset() { *m = CreateRequest{} } | |||
func (m *CreateRequest) String() string { return proto.CompactTextString(m) } | |||
func (*CreateRequest) ProtoMessage() {} | |||
func (m *CreateRequest) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *CreateRequest) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *CreateRequest) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
// 下单响应结构 | |||
type CreateResponse struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *CreateResponse) Reset() { *m = CreateResponse{} } | |||
func (m *CreateResponse) String() string { return proto.CompactTextString(m) } | |||
func (*CreateResponse) ProtoMessage() {} | |||
func (m *CreateResponse) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *CreateResponse) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *CreateResponse) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
func init() { | |||
} | |||
type OrderService interface { | |||
Create(in *CreateRequest, out *CreateResponse) error | |||
CreateByCart(in *CreateRequest, out *CreateResponse) error | |||
} | |||
// AcceptOrderServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptOrderServiceClient(lis net.Listener, x OrderService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("OrderService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
// RegisterOrderService publish the given OrderService implementation on the server. | |||
func RegisterOrderService(srv *rpc.Server, x OrderService) error { | |||
if err := srv.RegisterName("OrderService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewOrderServiceServer returns a new OrderService Server. | |||
func NewOrderServiceServer(x OrderService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("OrderService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeOrderService listen announces on the local network address laddr | |||
// and serves the given OrderService implementation. | |||
func ListenAndServeOrderService(network, addr string, x OrderService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("OrderService", x); err != nil { | |||
return err | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
type OrderServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewOrderServiceClient returns a OrderService rpc.Client and stub to handle | |||
// requests to the set of OrderService at the other end of the connection. | |||
func NewOrderServiceClient(conn io.ReadWriteCloser) (*OrderServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &OrderServiceClient{c}, c | |||
} | |||
func (c *OrderServiceClient) Create(in *CreateRequest, out *CreateResponse) error { | |||
return c.Call("OrderService.Create", in, out) | |||
} | |||
func (c *OrderServiceClient) CreateByCart(in *CreateRequest, out *CreateResponse) error { | |||
return c.Call("OrderService.CreateByCart", in, out) | |||
} | |||
// DialOrderService connects to an OrderService at the specified network address. | |||
func DialOrderService(network, addr string) (*OrderServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &OrderServiceClient{c}, c, nil | |||
} | |||
// DialOrderServiceTimeout connects to an OrderService at the specified network address. | |||
func DialOrderServiceTimeout(network, addr string, | |||
timeout time.Duration) (*OrderServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &OrderServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,22 @@ | |||
syntax = "proto3"; | |||
package order; | |||
// 下单请求结构 | |||
message CreateRequest { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// 下单响应结构 | |||
message CreateResponse { | |||
string data = 1; //订单号 | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// rpc方法 | |||
service OrderService { | |||
rpc create (CreateRequest) returns (CreateResponse); // 创建订单 | |||
rpc createByCart (CreateRequest) returns (CreateResponse); // 创建订单 | |||
} |
@ -0,0 +1,109 @@ | |||
package site | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"encoding/json" | |||
"errors" | |||
"strconv" | |||
"strings" | |||
"time" | |||
"git.tetele.net/ttlpkg/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
type SiteBalanceTypeReqArg struct { | |||
SiteId string `json:"site_id"` | |||
Dbname string `json:"dbname"` | |||
} | |||
type SiteBalanceType struct { | |||
Id string | |||
Name string | |||
UseLimit string | |||
Unit string | |||
Type string | |||
CleanRate string | |||
CleanTime string | |||
} | |||
/** | |||
* 由配置key取对应value | |||
* 请求及回均加密验签 | |||
* 2021/01/20 | |||
* GZ | |||
*/ | |||
func GetBalanceType(site_id, dbname, url ...string) (*[]SiteBalanceType, error) { | |||
var site_rpc_url string = "127.0.0.1:7971" | |||
if len(url) > 0 && url[0] != "" { | |||
site_rpc_url = url[0] | |||
} | |||
conn, _, err := DialSiteService("tcp", site_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := SiteConfigItemReqArg{site_id, dbname, key} | |||
data_json, err := json.Marshal(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
encryData := crypter.DesEn(string(data_json), "confdata") | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
req := &ConfigRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil} | |||
res := &ConfigResponse{} | |||
err = conn.GetConfig(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 = time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return nil, errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return nil, errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
res_data_de := crypter.DesDe(res_data, "confdata") | |||
var res_arr []SiteBalanceType | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &res_arr, nil | |||
} | |||
return nil, nil | |||
} |
@ -0,0 +1,135 @@ | |||
package site | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"encoding/json" | |||
"errors" | |||
"strconv" | |||
"strings" | |||
"time" | |||
"git.tetele.net/ttlpkg/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
type SiteConfigItemReqArg struct { | |||
SiteId string `json:"site_id"` | |||
Dbname string `json:"dbname"` | |||
Key string `json:"key"` | |||
} | |||
type SiteConfigItemRes struct { | |||
Type string | |||
Value string | |||
} | |||
/** | |||
* 由配置key取对应value | |||
* 请求及回均加密验签 | |||
* 2021/01/20 | |||
* GZ | |||
*/ | |||
func GetConfigItem(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) { | |||
var site_rpc_url string = "127.0.0.1:7971" | |||
if len(url) > 0 && url[0] != "" { | |||
site_rpc_url = url[0] | |||
} | |||
conn, _, err := DialSiteService("tcp", site_rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
defer conn.Close() | |||
arg := SiteConfigItemReqArg{site_id, dbname, key} | |||
data_json, err := json.Marshal(arg) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 := time.Now().Unix() | |||
encryData := crypter.DesEn(string(data_json), "confdata") | |||
now := strconv.FormatInt(now_int64, 10) | |||
sign := Sign(encryData, now) | |||
req := &ConfigRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil} | |||
res := &ConfigResponse{} | |||
err = conn.GetConfig(req, res) | |||
if err != nil { | |||
return nil, err | |||
} | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) | |||
if err != nil { | |||
return nil, err | |||
} | |||
now_int64 = time.Now().Unix() | |||
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { | |||
//时间误差前后10秒,返回 | |||
return nil, errors.New("返回时间错误") | |||
} | |||
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) | |||
if !check_sign { | |||
return nil, errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
res_data_de := crypter.DesDe(res_data, "confdata") | |||
var res_arr SiteConfigItemRes | |||
err = json.Unmarshal([]byte(res_data_de), &res_arr) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &res_arr, nil | |||
} | |||
return nil, nil | |||
} | |||
/** | |||
* 签名 | |||
*/ | |||
func Sign(data string, salt string) string { | |||
var build strings.Builder | |||
build.WriteString(data) | |||
build.WriteString(salt) | |||
build.WriteString("site55sign33") | |||
data_str := build.String() | |||
h := md5.New() | |||
h.Write([]byte(data_str)) // 需要加密的字符串 | |||
return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 | |||
} | |||
/** | |||
* 验证签名 | |||
*/ | |||
func CheckSign(sign_str, data, salt string) bool { | |||
sign := Sign(data, salt) | |||
if strings.Compare(sign_str, sign) > -1 { | |||
return true | |||
} | |||
return false | |||
} |
@ -0,0 +1,15 @@ | |||
package site | |||
import ( | |||
"testing" | |||
) | |||
func Test_GetConfigItem(t *testing.T) { | |||
siteid := "100065" | |||
dbname := "test1_tetele_com" | |||
key := "percentage" | |||
res, err := GetConfigItem(siteid, dbname, key) | |||
t.Log(res) | |||
t.Log(err) | |||
} |
@ -0,0 +1,198 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: site.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package site is a generated protocol buffer package. | |||
It is generated from these files: | |||
site.proto | |||
It has these top-level messages: | |||
ConfigRequest | |||
ConfigResponse | |||
*/ | |||
package site | |||
import proto "github.com/chai2010/protorpc/proto" | |||
import math "math" | |||
import "io" | |||
import "log" | |||
import "net" | |||
import "net/rpc" | |||
import "time" | |||
import protorpc "github.com/chai2010/protorpc" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = math.Inf | |||
// 配置信息请求结构 | |||
type ConfigRequest struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *ConfigRequest) Reset() { *m = ConfigRequest{} } | |||
func (m *ConfigRequest) String() string { return proto.CompactTextString(m) } | |||
func (*ConfigRequest) ProtoMessage() {} | |||
func (m *ConfigRequest) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *ConfigRequest) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *ConfigRequest) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
// 配置信息响应结构 | |||
type ConfigResponse struct { | |||
Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` | |||
Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` | |||
Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *ConfigResponse) Reset() { *m = ConfigResponse{} } | |||
func (m *ConfigResponse) String() string { return proto.CompactTextString(m) } | |||
func (*ConfigResponse) ProtoMessage() {} | |||
func (m *ConfigResponse) GetData() string { | |||
if m != nil && m.Data != nil { | |||
return *m.Data | |||
} | |||
return "" | |||
} | |||
func (m *ConfigResponse) GetTime() string { | |||
if m != nil && m.Time != nil { | |||
return *m.Time | |||
} | |||
return "" | |||
} | |||
func (m *ConfigResponse) GetSign() string { | |||
if m != nil && m.Sign != nil { | |||
return *m.Sign | |||
} | |||
return "" | |||
} | |||
func init() { | |||
} | |||
type SiteService interface { | |||
GetConfig(in *ConfigRequest, out *ConfigResponse) error | |||
GetBalanceType(in *ConfigRequest, out *ConfigResponse) error | |||
} | |||
// AcceptSiteServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptSiteServiceClient(lis net.Listener, x SiteService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("SiteService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
// RegisterSiteService publish the given SiteService implementation on the server. | |||
func RegisterSiteService(srv *rpc.Server, x SiteService) error { | |||
if err := srv.RegisterName("SiteService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewSiteServiceServer returns a new SiteService Server. | |||
func NewSiteServiceServer(x SiteService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("SiteService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeSiteService listen announces on the local network address laddr | |||
// and serves the given SiteService implementation. | |||
func ListenAndServeSiteService(network, addr string, x SiteService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("SiteService", x); err != nil { | |||
return err | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
type SiteServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewSiteServiceClient returns a SiteService rpc.Client and stub to handle | |||
// requests to the set of SiteService at the other end of the connection. | |||
func NewSiteServiceClient(conn io.ReadWriteCloser) (*SiteServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &SiteServiceClient{c}, c | |||
} | |||
func (c *SiteServiceClient) GetConfig(in *ConfigRequest, out *ConfigResponse) error { | |||
return c.Call("SiteService.GetConfig", in, out) | |||
} | |||
func (c *SiteServiceClient) GetBalanceType(in *ConfigRequest, out *ConfigResponse) error { | |||
return c.Call("SiteService.GetBalanceType", in, out) | |||
} | |||
// DialSiteService connects to an SiteService at the specified network address. | |||
func DialSiteService(network, addr string) (*SiteServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &SiteServiceClient{c}, c, nil | |||
} | |||
// DialSiteServiceTimeout connects to an SiteService at the specified network address. | |||
func DialSiteServiceTimeout(network, addr string, | |||
timeout time.Duration) (*SiteServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &SiteServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,23 @@ | |||
syntax = "proto3"; | |||
package site; | |||
// 配置信息请求结构 | |||
message ConfigRequest { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// 配置信息响应结构 | |||
message ConfigResponse { | |||
string data = 1; | |||
string time = 2; | |||
string sign = 3; | |||
} | |||
// rpc方法 | |||
service SiteService { | |||
rpc getConfig (ConfigRequest) returns (ConfigResponse); // 查询配置信息 | |||
rpc getBalanceType (ConfigRequest) returns (ConfigResponse); // 查询账户类型信息 | |||
} |
@ -0,0 +1,91 @@ | |||
package user | |||
import ( | |||
"github.com/golang/protobuf/proto" | |||
) | |||
func GetUserByToken(dbname, token string, url ...string) (map[string]string, error) { | |||
var user_rpc_url string = "127.0.0.1:7976" | |||
if len(url) > 0 && url[0] != "" { | |||
user_rpc_url = url[0] | |||
} | |||
conn, _, err := DialUserService("tcp", user_rpc_url) | |||
if err != nil { | |||
return map[string]string{}, err | |||
} | |||
defer conn.Close() | |||
req := &UserRequest{proto.String(dbname), proto.String(token), nil} | |||
res := &UserResponse{} | |||
err = conn.GetByToken(req, res) | |||
if err != nil { | |||
return map[string]string{}, err | |||
} | |||
if res.GetUserId() != "" { | |||
return map[string]string{ | |||
"UserId": res.GetUserId(), | |||
"Username": res.GetUsername(), | |||
"Nickname": res.GetNickname(), | |||
"Mobile": res.GetMobile(), | |||
"Email": res.GetEmail(), | |||
"Status": res.GetStatus(), | |||
"BusinessId": res.GetBusinessId(), | |||
"StoreId": res.GetStoreId(), | |||
"FansTo": res.GetFansTo(), | |||
"IsVip": res.GetIsVip(), | |||
"Usercode": res.GetUsercode(), | |||
"GroupId": res.GetGroupId(), | |||
}, nil | |||
} | |||
return map[string]string{}, nil | |||
} | |||
func Login(dbname, account, password string, url ...string) (map[string]string, error) { | |||
var user_rpc_url string = "127.0.0.1:7976" | |||
if len(url) > 0 && url[0] != "" { | |||
user_rpc_url = url[0] | |||
} | |||
conn, _, err := DialUserService("tcp", user_rpc_url) | |||
if err != nil { | |||
return map[string]string{}, err | |||
} | |||
defer conn.Close() | |||
req := &LoginRequest{proto.String(dbname), proto.String(account), proto.String(password), nil} | |||
res := &LoginResponse{} | |||
err = conn.Login(req, res) | |||
if err != nil { | |||
return map[string]string{}, err | |||
} | |||
if res.GetUserId() != "" { | |||
return map[string]string{ | |||
"UserId": res.GetUserId(), | |||
"Username": res.GetUsername(), | |||
"Nickname": res.GetNickname(), | |||
"Mobile": res.GetMobile(), | |||
"Email": res.GetEmail(), | |||
"Status": res.GetStatus(), | |||
"BusinessId": res.GetBusinessId(), | |||
"StoreId": res.GetStoreId(), | |||
"FansTo": res.GetFansTo(), | |||
"IsVip": res.GetIsVip(), | |||
"Token": res.GetToken(), | |||
"Usercode": res.GetUsercode(), | |||
"GroupId": res.GetGroupId(), | |||
}, nil | |||
} | |||
return map[string]string{}, nil | |||
} |
@ -0,0 +1,30 @@ | |||
package user | |||
import ( | |||
"testing" | |||
) | |||
func Test_GetUserByToken(t *testing.T) { | |||
dbname := "dev_tetele_net" | |||
token := "5536827c-36a5-4ec7-946d-49e4380c5103" | |||
res, err := GetUserByToken(dbname, token, "111.229.34.252:7976") | |||
t.Log(res) | |||
t.Log(res["UserId"]) | |||
t.Log(err) | |||
res, err = GetUserById(dbname, "114") | |||
t.Log(res) | |||
t.Log(res["UserId"]) | |||
t.Log(err) | |||
} | |||
func Test_Login(t *testing.T) { | |||
dbname := "dev_tetele_net" | |||
account := "adminw" | |||
password := "e10adc3949ba59abbe56e057f20f883e" | |||
res, err := Login(dbname, account, password) | |||
t.Log(res) | |||
t.Log(res["UserId"]) | |||
t.Log(err) | |||
} |
@ -0,0 +1,410 @@ | |||
// Code generated by protoc-gen-go. | |||
// source: user.proto | |||
// DO NOT EDIT! | |||
/* | |||
Package user is a generated protocol buffer package. | |||
It is generated from these files: | |||
user.proto | |||
It has these top-level messages: | |||
UserRequest | |||
UserResponse | |||
LoginRequest | |||
LoginResponse | |||
*/ | |||
package user | |||
import proto "github.com/chai2010/protorpc/proto" | |||
import math "math" | |||
import "io" | |||
import "log" | |||
import "net" | |||
import "net/rpc" | |||
import "time" | |||
import protorpc "github.com/chai2010/protorpc" | |||
// Reference imports to suppress errors if they are not otherwise used. | |||
var _ = proto.Marshal | |||
var _ = math.Inf | |||
// 使用token查询用户信息请求结构 | |||
type UserRequest struct { | |||
Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` | |||
Token *string `protobuf:"bytes,2,opt,name=token" json:"token,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *UserRequest) Reset() { *m = UserRequest{} } | |||
func (m *UserRequest) String() string { return proto.CompactTextString(m) } | |||
func (*UserRequest) ProtoMessage() {} | |||
func (m *UserRequest) GetDbname() string { | |||
if m != nil && m.Dbname != nil { | |||
return *m.Dbname | |||
} | |||
return "" | |||
} | |||
func (m *UserRequest) GetToken() string { | |||
if m != nil && m.Token != nil { | |||
return *m.Token | |||
} | |||
return "" | |||
} | |||
// 使用token查询用户信息响应结构 | |||
type UserResponse struct { | |||
UserId *string `protobuf:"bytes,1,opt,name=user_id" json:"user_id,omitempty"` | |||
Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"` | |||
Nickname *string `protobuf:"bytes,3,opt,name=nickname" json:"nickname,omitempty"` | |||
Mobile *string `protobuf:"bytes,4,opt,name=mobile" json:"mobile,omitempty"` | |||
Email *string `protobuf:"bytes,5,opt,name=email" json:"email,omitempty"` | |||
Status *string `protobuf:"bytes,6,opt,name=status" json:"status,omitempty"` | |||
BusinessId *string `protobuf:"bytes,7,opt,name=business_id" json:"business_id,omitempty"` | |||
StoreId *string `protobuf:"bytes,8,opt,name=store_id" json:"store_id,omitempty"` | |||
FansTo *string `protobuf:"bytes,9,opt,name=fans_to" json:"fans_to,omitempty"` | |||
IsVip *string `protobuf:"bytes,10,opt,name=is_vip" json:"is_vip,omitempty"` | |||
Usercode *string `protobuf:"bytes,11,opt,name=usercode" json:"usercode,omitempty"` | |||
GroupId *string `protobuf:"bytes,12,opt,name=group_id" json:"group_id,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *UserResponse) Reset() { *m = UserResponse{} } | |||
func (m *UserResponse) String() string { return proto.CompactTextString(m) } | |||
func (*UserResponse) ProtoMessage() {} | |||
func (m *UserResponse) GetUserId() string { | |||
if m != nil && m.UserId != nil { | |||
return *m.UserId | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetUsername() string { | |||
if m != nil && m.Username != nil { | |||
return *m.Username | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetNickname() string { | |||
if m != nil && m.Nickname != nil { | |||
return *m.Nickname | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetMobile() string { | |||
if m != nil && m.Mobile != nil { | |||
return *m.Mobile | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetEmail() string { | |||
if m != nil && m.Email != nil { | |||
return *m.Email | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetStatus() string { | |||
if m != nil && m.Status != nil { | |||
return *m.Status | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetBusinessId() string { | |||
if m != nil && m.BusinessId != nil { | |||
return *m.BusinessId | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetStoreId() string { | |||
if m != nil && m.StoreId != nil { | |||
return *m.StoreId | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetFansTo() string { | |||
if m != nil && m.FansTo != nil { | |||
return *m.FansTo | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetIsVip() string { | |||
if m != nil && m.IsVip != nil { | |||
return *m.IsVip | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetUsercode() string { | |||
if m != nil && m.Usercode != nil { | |||
return *m.Usercode | |||
} | |||
return "" | |||
} | |||
func (m *UserResponse) GetGroupId() string { | |||
if m != nil && m.GroupId != nil { | |||
return *m.GroupId | |||
} | |||
return "" | |||
} | |||
// 用户登录请求结构 | |||
type LoginRequest struct { | |||
Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` | |||
Account *string `protobuf:"bytes,2,opt,name=account" json:"account,omitempty"` | |||
Password *string `protobuf:"bytes,3,opt,name=password" json:"password,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *LoginRequest) Reset() { *m = LoginRequest{} } | |||
func (m *LoginRequest) String() string { return proto.CompactTextString(m) } | |||
func (*LoginRequest) ProtoMessage() {} | |||
func (m *LoginRequest) GetDbname() string { | |||
if m != nil && m.Dbname != nil { | |||
return *m.Dbname | |||
} | |||
return "" | |||
} | |||
func (m *LoginRequest) GetAccount() string { | |||
if m != nil && m.Account != nil { | |||
return *m.Account | |||
} | |||
return "" | |||
} | |||
func (m *LoginRequest) GetPassword() string { | |||
if m != nil && m.Password != nil { | |||
return *m.Password | |||
} | |||
return "" | |||
} | |||
// 使用token查询用户信息响应结构 | |||
type LoginResponse struct { | |||
UserId *string `protobuf:"bytes,1,opt,name=user_id" json:"user_id,omitempty"` | |||
Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"` | |||
Nickname *string `protobuf:"bytes,3,opt,name=nickname" json:"nickname,omitempty"` | |||
Mobile *string `protobuf:"bytes,4,opt,name=mobile" json:"mobile,omitempty"` | |||
Email *string `protobuf:"bytes,5,opt,name=email" json:"email,omitempty"` | |||
Status *string `protobuf:"bytes,6,opt,name=status" json:"status,omitempty"` | |||
BusinessId *string `protobuf:"bytes,7,opt,name=business_id" json:"business_id,omitempty"` | |||
StoreId *string `protobuf:"bytes,8,opt,name=store_id" json:"store_id,omitempty"` | |||
FansTo *string `protobuf:"bytes,9,opt,name=fans_to" json:"fans_to,omitempty"` | |||
IsVip *string `protobuf:"bytes,10,opt,name=is_vip" json:"is_vip,omitempty"` | |||
Token *string `protobuf:"bytes,11,opt,name=token" json:"token,omitempty"` | |||
Usercode *string `protobuf:"bytes,12,opt,name=usercode" json:"usercode,omitempty"` | |||
GroupId *string `protobuf:"bytes,13,opt,name=group_id" json:"group_id,omitempty"` | |||
XXX_unrecognized []byte `json:"-"` | |||
} | |||
func (m *LoginResponse) Reset() { *m = LoginResponse{} } | |||
func (m *LoginResponse) String() string { return proto.CompactTextString(m) } | |||
func (*LoginResponse) ProtoMessage() {} | |||
func (m *LoginResponse) GetUserId() string { | |||
if m != nil && m.UserId != nil { | |||
return *m.UserId | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetUsername() string { | |||
if m != nil && m.Username != nil { | |||
return *m.Username | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetNickname() string { | |||
if m != nil && m.Nickname != nil { | |||
return *m.Nickname | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetMobile() string { | |||
if m != nil && m.Mobile != nil { | |||
return *m.Mobile | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetEmail() string { | |||
if m != nil && m.Email != nil { | |||
return *m.Email | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetStatus() string { | |||
if m != nil && m.Status != nil { | |||
return *m.Status | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetBusinessId() string { | |||
if m != nil && m.BusinessId != nil { | |||
return *m.BusinessId | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetStoreId() string { | |||
if m != nil && m.StoreId != nil { | |||
return *m.StoreId | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetFansTo() string { | |||
if m != nil && m.FansTo != nil { | |||
return *m.FansTo | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetIsVip() string { | |||
if m != nil && m.IsVip != nil { | |||
return *m.IsVip | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetToken() string { | |||
if m != nil && m.Token != nil { | |||
return *m.Token | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetUsercode() string { | |||
if m != nil && m.Usercode != nil { | |||
return *m.Usercode | |||
} | |||
return "" | |||
} | |||
func (m *LoginResponse) GetGroupId() string { | |||
if m != nil && m.GroupId != nil { | |||
return *m.GroupId | |||
} | |||
return "" | |||
} | |||
func init() { | |||
} | |||
type UserService interface { | |||
GetByToken(in *UserRequest, out *UserResponse) error | |||
Login(in *LoginRequest, out *LoginResponse) error | |||
} | |||
// AcceptUserServiceClient accepts connections on the listener and serves requests | |||
// for each incoming connection. Accept blocks; the caller typically | |||
// invokes it in a go statement. | |||
func AcceptUserServiceClient(lis net.Listener, x UserService) { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("UserService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
// RegisterUserService publish the given UserService implementation on the server. | |||
func RegisterUserService(srv *rpc.Server, x UserService) error { | |||
if err := srv.RegisterName("UserService", x); err != nil { | |||
return err | |||
} | |||
return nil | |||
} | |||
// NewUserServiceServer returns a new UserService Server. | |||
func NewUserServiceServer(x UserService) *rpc.Server { | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("UserService", x); err != nil { | |||
log.Fatal(err) | |||
} | |||
return srv | |||
} | |||
// ListenAndServeUserService listen announces on the local network address laddr | |||
// and serves the given UserService implementation. | |||
func ListenAndServeUserService(network, addr string, x UserService) error { | |||
lis, err := net.Listen(network, addr) | |||
if err != nil { | |||
return err | |||
} | |||
defer lis.Close() | |||
srv := rpc.NewServer() | |||
if err := srv.RegisterName("UserService", x); err != nil { | |||
return err | |||
} | |||
for { | |||
conn, err := lis.Accept() | |||
if err != nil { | |||
log.Fatalf("lis.Accept(): %v\n", err) | |||
} | |||
go srv.ServeCodec(protorpc.NewServerCodec(conn)) | |||
} | |||
} | |||
type UserServiceClient struct { | |||
*rpc.Client | |||
} | |||
// NewUserServiceClient returns a UserService rpc.Client and stub to handle | |||
// requests to the set of UserService at the other end of the connection. | |||
func NewUserServiceClient(conn io.ReadWriteCloser) (*UserServiceClient, *rpc.Client) { | |||
c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) | |||
return &UserServiceClient{c}, c | |||
} | |||
func (c *UserServiceClient) GetByToken(in *UserRequest, out *UserResponse) error { | |||
return c.Call("UserService.GetByToken", in, out) | |||
} | |||
func (c *UserServiceClient) Login(in *LoginRequest, out *LoginResponse) error { | |||
return c.Call("UserService.Login", in, out) | |||
} | |||
// DialUserService connects to an UserService at the specified network address. | |||
func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.Dial(network, addr) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &UserServiceClient{c}, c, nil | |||
} | |||
// DialUserServiceTimeout connects to an UserService at the specified network address. | |||
func DialUserServiceTimeout(network, addr string, | |||
timeout time.Duration) (*UserServiceClient, *rpc.Client, error) { | |||
c, err := protorpc.DialTimeout(network, addr, timeout) | |||
if err != nil { | |||
return nil, nil, err | |||
} | |||
return &UserServiceClient{c}, c, nil | |||
} |
@ -0,0 +1,56 @@ | |||
syntax = "proto3"; | |||
package user; | |||
// 使用token查询用户信息请求结构 | |||
message UserRequest { | |||
string dbname = 1; | |||
string token = 2; | |||
} | |||
// 使用token查询用户信息响应结构 | |||
message UserResponse { | |||
string user_id = 1; //ID | |||
string username = 2; //用户名 | |||
string nickname = 3; //昵称 | |||
string mobile = 4; //手机 | |||
string email = 5; //邮箱 | |||
string status = 6; //状态 | |||
string business_id = 7; //所属商家 | |||
string store_id = 8; //门店 | |||
string fans_to = 9;//被谁锁粉 | |||
string is_vip = 10;//是否VIP | |||
string usercode = 11;//工号 | |||
string group_id = 12;//分组ID | |||
} | |||
// 用户登录请求结构 | |||
message LoginRequest { | |||
string dbname = 1; | |||
string account = 2; | |||
string password = 3; | |||
} | |||
// 使用token查询用户信息响应结构 | |||
message LoginResponse { | |||
string user_id = 1; //ID | |||
string username = 2; //用户名 | |||
string nickname = 3; //昵称 | |||
string mobile = 4; //手机 | |||
string email = 5; //邮箱 | |||
string status = 6; //状态 | |||
string business_id = 7; //所属商家 | |||
string store_id = 8; //门店 | |||
string fans_to = 9;//被谁锁粉 | |||
string is_vip = 10;//是否VIP | |||
string token = 11;//token | |||
string usercode = 12;//工号 | |||
string group_id = 13;//分组ID | |||
} | |||
// rpc方法 | |||
service UserService { | |||
rpc getByToken (UserRequest) returns (UserResponse); // 使用token查询用户 | |||
rpc login (LoginRequest) returns (LoginResponse); // 登录 | |||
} |