Commit b6e64b63 by chenxianqi

upate

parent 735ba425
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"os" "os"
"path" "path"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/astaxie/beego" "github.com/astaxie/beego"
...@@ -381,8 +380,7 @@ func (c *PublicController) PushMessage() { ...@@ -381,8 +380,7 @@ func (c *PublicController) PushMessage() {
var getMessage models.Message var getMessage models.Message
var msgContent []byte var msgContent []byte
msgContent, _ = base64.StdEncoding.DecodeString(pushMessage.Payload) msgContent, _ = base64.StdEncoding.DecodeString(pushMessage.Payload)
msgContent, _ = base64.StdEncoding.DecodeString(strings.Replace(string(msgContent), "\"", "", -1)) utils.StringToInterface(string(msgContent), &getMessage)
json.Unmarshal(msgContent, &getMessage)
utils.MessageInto(getMessage, false) utils.MessageInto(getMessage, false)
c.JSON(configs.ResponseSucess, "push success", nil) c.JSON(configs.ResponseSucess, "push success", nil)
......
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: client.proto
package grpcs
import (
context "context"
fmt "fmt"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type Respones struct {
Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Respones) Reset() { *m = Respones{} }
func (m *Respones) String() string { return proto.CompactTextString(m) }
func (*Respones) ProtoMessage() {}
func (*Respones) Descriptor() ([]byte, []int) {
return fileDescriptor_014de31d7ac8c57c, []int{0}
}
func (m *Respones) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Respones.Unmarshal(m, b)
}
func (m *Respones) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Respones.Marshal(b, m, deterministic)
}
func (m *Respones) XXX_Merge(src proto.Message) {
xxx_messageInfo_Respones.Merge(m, src)
}
func (m *Respones) XXX_Size() int {
return xxx_messageInfo_Respones.Size(m)
}
func (m *Respones) XXX_DiscardUnknown() {
xxx_messageInfo_Respones.DiscardUnknown(m)
}
var xxx_messageInfo_Respones proto.InternalMessageInfo
func (m *Respones) GetData() string {
if m != nil {
return m.Data
}
return ""
}
type Request struct {
Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Request) Reset() { *m = Request{} }
func (m *Request) String() string { return proto.CompactTextString(m) }
func (*Request) ProtoMessage() {}
func (*Request) Descriptor() ([]byte, []int) {
return fileDescriptor_014de31d7ac8c57c, []int{1}
}
func (m *Request) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Request.Unmarshal(m, b)
}
func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Request.Marshal(b, m, deterministic)
}
func (m *Request) XXX_Merge(src proto.Message) {
xxx_messageInfo_Request.Merge(m, src)
}
func (m *Request) XXX_Size() int {
return xxx_messageInfo_Request.Size(m)
}
func (m *Request) XXX_DiscardUnknown() {
xxx_messageInfo_Request.DiscardUnknown(m)
}
var xxx_messageInfo_Request proto.InternalMessageInfo
func (m *Request) GetData() string {
if m != nil {
return m.Data
}
return ""
}
func init() {
proto.RegisterType((*Respones)(nil), "grpcs.Respones")
proto.RegisterType((*Request)(nil), "grpcs.Request")
}
func init() {
proto.RegisterFile("client.proto", fileDescriptor_014de31d7ac8c57c)
}
var fileDescriptor_014de31d7ac8c57c = []byte{
// 168 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xce, 0xc9, 0x4c,
0xcd, 0x2b, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x2f, 0x2a, 0x48, 0x2e, 0x56,
0x92, 0xe3, 0xe2, 0x08, 0x4a, 0x2d, 0x2e, 0xc8, 0xcf, 0x4b, 0x2d, 0x16, 0x12, 0xe2, 0x62, 0x49,
0x49, 0x2c, 0x49, 0x94, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x95, 0x64, 0xb9, 0xd8,
0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0xb0, 0x49, 0x1b, 0x2d, 0x65, 0xe4, 0x62, 0xf1, 0x4e,
0x4d, 0x2b, 0x15, 0x32, 0xe7, 0x12, 0x72, 0x4f, 0x2d, 0xf1, 0xcf, 0xcb, 0xc9, 0xcc, 0x4b, 0x75,
0xcc, 0xc9, 0x09, 0xca, 0x4f, 0xca, 0x2f, 0x29, 0x16, 0xe2, 0xd3, 0x03, 0xdb, 0xa2, 0x07, 0x35,
0x42, 0x8a, 0x1f, 0xce, 0x87, 0x58, 0xa9, 0xc4, 0x20, 0x64, 0xc0, 0xc5, 0x1d, 0x50, 0x5a, 0x9c,
0xe1, 0x9b, 0x5a, 0x5c, 0x9c, 0x98, 0x9e, 0x4a, 0x8c, 0x0e, 0x23, 0x2e, 0x5e, 0xe7, 0xc4, 0xbc,
0xe4, 0xd4, 0x1c, 0xe2, 0xf5, 0x24, 0xb1, 0x81, 0x3d, 0x6d, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff,
0x0b, 0x6e, 0x06, 0xcc, 0x04, 0x01, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6
// KefuClient is the client API for Kefu service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type KefuClient interface {
GetOnlineAllRobots(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error)
PushMessage(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error)
CancelMessage(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error)
}
type kefuClient struct {
cc grpc.ClientConnInterface
}
func NewKefuClient(cc grpc.ClientConnInterface) KefuClient {
return &kefuClient{cc}
}
func (c *kefuClient) GetOnlineAllRobots(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error) {
out := new(Respones)
err := c.cc.Invoke(ctx, "/grpcs.Kefu/GetOnlineAllRobots", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *kefuClient) PushMessage(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error) {
out := new(Respones)
err := c.cc.Invoke(ctx, "/grpcs.Kefu/PushMessage", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *kefuClient) CancelMessage(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Respones, error) {
out := new(Respones)
err := c.cc.Invoke(ctx, "/grpcs.Kefu/CancelMessage", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// KefuServer is the server API for Kefu service.
type KefuServer interface {
GetOnlineAllRobots(context.Context, *Request) (*Respones, error)
PushMessage(context.Context, *Request) (*Respones, error)
CancelMessage(context.Context, *Request) (*Respones, error)
}
// UnimplementedKefuServer can be embedded to have forward compatible implementations.
type UnimplementedKefuServer struct {
}
func (*UnimplementedKefuServer) GetOnlineAllRobots(ctx context.Context, req *Request) (*Respones, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetOnlineAllRobots not implemented")
}
func (*UnimplementedKefuServer) PushMessage(ctx context.Context, req *Request) (*Respones, error) {
return nil, status.Errorf(codes.Unimplemented, "method PushMessage not implemented")
}
func (*UnimplementedKefuServer) CancelMessage(ctx context.Context, req *Request) (*Respones, error) {
return nil, status.Errorf(codes.Unimplemented, "method CancelMessage not implemented")
}
func RegisterKefuServer(s *grpc.Server, srv KefuServer) {
s.RegisterService(&_Kefu_serviceDesc, srv)
}
func _Kefu_GetOnlineAllRobots_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Request)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(KefuServer).GetOnlineAllRobots(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/grpcs.Kefu/GetOnlineAllRobots",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(KefuServer).GetOnlineAllRobots(ctx, req.(*Request))
}
return interceptor(ctx, in, info, handler)
}
func _Kefu_PushMessage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Request)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(KefuServer).PushMessage(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/grpcs.Kefu/PushMessage",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(KefuServer).PushMessage(ctx, req.(*Request))
}
return interceptor(ctx, in, info, handler)
}
func _Kefu_CancelMessage_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Request)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(KefuServer).CancelMessage(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/grpcs.Kefu/CancelMessage",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(KefuServer).CancelMessage(ctx, req.(*Request))
}
return interceptor(ctx, in, info, handler)
}
var _Kefu_serviceDesc = grpc.ServiceDesc{
ServiceName: "grpcs.Kefu",
HandlerType: (*KefuServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetOnlineAllRobots",
Handler: _Kefu_GetOnlineAllRobots_Handler,
},
{
MethodName: "PushMessage",
Handler: _Kefu_PushMessage_Handler,
},
{
MethodName: "CancelMessage",
Handler: _Kefu_CancelMessage_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "client.proto",
}
syntax = "proto3";
package grpcs;
service Kefu {
rpc GetOnlineAllRobots (Request) returns (Respones) {}
rpc PushMessage (Request) returns (Respones) {}
rpc CancelMessage (Request) returns (Respones) {}
}
message Respones {
string data = 1;
}
message Request {
string data = 1;
}
\ No newline at end of file
No preview for this file type
...@@ -2,49 +2,62 @@ package grpcs ...@@ -2,49 +2,62 @@ package grpcs
import ( import (
context "context" context "context"
"crypto/md5" "encoding/base64"
fmt "fmt" "kefu_server/models"
"kefu_server/services"
"kefu_server/utils"
"log" "log"
"net" "net"
"github.com/astaxie/beego/logs"
grpc "google.golang.org/grpc" grpc "google.golang.org/grpc"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
) )
// 业务实现方法的容器 // kefuServer
type server struct{} type kefuServer struct{}
// 为server定义 DoMD5 方法 内部处理请求并返回结果 // GetOnlineAllRobots
// 参数 (context.Context[固定], *test.Req[相应接口定义的请求参数]) func (s *kefuServer) GetOnlineAllRobots(ctx context.Context, in *Request) (*Respones, error) {
// 返回 (*test.Res[相应接口定义的返回参数,必须用指针], error) // query
func (s *server) DoMD5(ctx context.Context, in *Req) (*Res, error) { robots, _ := services.GetRobotRepositoryInstance().GetRobotOnlineAll()
fmt.Println("MD5方法请求JSON:" + in.JsonStr) return &Respones{Data: utils.InterfaceToString(robots)}, nil
return &Res{BackJson: "MD5 :" + fmt.Sprintf("%x", md5.Sum([]byte(in.JsonStr)))}, nil }
// PushMessage
func (s *kefuServer) PushMessage(ctx context.Context, in *Request) (*Respones, error) {
var message models.Message
msgContent, _ := base64.StdEncoding.DecodeString(in.Data)
utils.StringToInterface(string(msgContent), &message)
utils.MessageInto(message, false)
return &Respones{Data: "push success"}, nil
}
// CancelMessage
func (s *kefuServer) CancelMessage(ctx context.Context, in *Request) (*Respones, error) {
var request models.RemoveMessageRequestDto
utils.StringToInterface(in.Data, &request)
// cancel
messageRepository := services.GetMessageRepositoryInstance()
_, err := messageRepository.Delete(request)
logs.Info("messageRepository== ", request)
if err != nil {
logs.Info("grpc CancelMessage err == ", err)
}
return &Respones{Data: "cancel message success"}, nil
} }
// Run run grpc server // Run run grpc server
func Run() { func Run() {
lis, err := net.Listen("tcp", ":8028") //监听所有网卡8028端口的TCP连接 lis, err := net.Listen("tcp", ":8028")
if err != nil { if err != nil {
log.Fatalf("监听失败: %v", err) log.Fatalf("grpc server failed: %v", err)
} }
s := grpc.NewServer() //创建gRPC服务 s := grpc.NewServer()
RegisterKefuServer(s, &kefuServer{})
/**注册接口服务
* 以定义proto时的service为单位注册,服务中可以有多个方法
* (proto编译时会为每个service生成Register***Server方法)
* 包.注册服务方法(gRpc服务实例,包含接口方法的结构体[指针])
*/
RegisterWaiterServer(s, &server{})
/**如果有可以注册多个接口服务,结构体要实现对应的接口方法
* user.RegisterLoginServer(s, &server{})
* minMovie.RegisterFbiServer(s, &server{})
*/
// 在gRPC服务器上注册反射服务
reflection.Register(s) reflection.Register(s)
// 将监听交给gRPC服务处理
err = s.Serve(lis) err = s.Serve(lis)
if err != nil { if err != nil {
log.Fatalf("failed to serve: %v", err) logs.Info("failed to serve: ", err)
} }
} }
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: test.proto
// 定义包名
package grpcs
import (
context "context"
fmt "fmt"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// 定义 Req 消息结构
type Req struct {
// 类型 字段 = 标识号
JsonStr string `protobuf:"bytes,1,opt,name=jsonStr,proto3" json:"jsonStr,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Req) Reset() { *m = Req{} }
func (m *Req) String() string { return proto.CompactTextString(m) }
func (*Req) ProtoMessage() {}
func (*Req) Descriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{0}
}
func (m *Req) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Req.Unmarshal(m, b)
}
func (m *Req) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Req.Marshal(b, m, deterministic)
}
func (m *Req) XXX_Merge(src proto.Message) {
xxx_messageInfo_Req.Merge(m, src)
}
func (m *Req) XXX_Size() int {
return xxx_messageInfo_Req.Size(m)
}
func (m *Req) XXX_DiscardUnknown() {
xxx_messageInfo_Req.DiscardUnknown(m)
}
var xxx_messageInfo_Req proto.InternalMessageInfo
func (m *Req) GetJsonStr() string {
if m != nil {
return m.JsonStr
}
return ""
}
// 定义 Res 消息结构
type Res struct {
BackJson string `protobuf:"bytes,1,opt,name=backJson,proto3" json:"backJson,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Res) Reset() { *m = Res{} }
func (m *Res) String() string { return proto.CompactTextString(m) }
func (*Res) ProtoMessage() {}
func (*Res) Descriptor() ([]byte, []int) {
return fileDescriptor_c161fcfdc0c3ff1e, []int{1}
}
func (m *Res) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Res.Unmarshal(m, b)
}
func (m *Res) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Res.Marshal(b, m, deterministic)
}
func (m *Res) XXX_Merge(src proto.Message) {
xxx_messageInfo_Res.Merge(m, src)
}
func (m *Res) XXX_Size() int {
return xxx_messageInfo_Res.Size(m)
}
func (m *Res) XXX_DiscardUnknown() {
xxx_messageInfo_Res.DiscardUnknown(m)
}
var xxx_messageInfo_Res proto.InternalMessageInfo
func (m *Res) GetBackJson() string {
if m != nil {
return m.BackJson
}
return ""
}
func init() {
proto.RegisterType((*Req)(nil), "grpcs.Req")
proto.RegisterType((*Res)(nil), "grpcs.Res")
}
func init() {
proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e)
}
var fileDescriptor_c161fcfdc0c3ff1e = []byte{
// 130 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0x49, 0x2d, 0x2e,
0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x2f, 0x2a, 0x48, 0x2e, 0x56, 0x92, 0xe7,
0x62, 0x0e, 0x4a, 0x2d, 0x14, 0x92, 0xe0, 0x62, 0xcf, 0x2a, 0xce, 0xcf, 0x0b, 0x2e, 0x29, 0x92,
0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0x71, 0x95, 0x14, 0x41, 0x0a, 0x8a, 0x85, 0xa4, 0xb8,
0x38, 0x92, 0x12, 0x93, 0xb3, 0xbd, 0x8a, 0xf3, 0xf3, 0xa0, 0x2a, 0xe0, 0x7c, 0x23, 0x6d, 0x2e,
0xb6, 0xf0, 0xc4, 0xcc, 0x92, 0xd4, 0x22, 0x21, 0x45, 0x2e, 0x56, 0x97, 0x7c, 0x5f, 0x17, 0x53,
0x21, 0x2e, 0x3d, 0xb0, 0xf1, 0x7a, 0x41, 0xa9, 0x85, 0x52, 0x08, 0x76, 0xb1, 0x12, 0x43, 0x12,
0x1b, 0xd8, 0x7a, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc3, 0x16, 0xa5, 0x68, 0x8c, 0x00,
0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6
// WaiterClient is the client API for Waiter service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type WaiterClient interface {
// 定义接口 (结构体可以复用)
// 方法 (请求消息结构体) returns (返回消息结构体) {}
DoMD5(ctx context.Context, in *Req, opts ...grpc.CallOption) (*Res, error)
}
type waiterClient struct {
cc grpc.ClientConnInterface
}
func NewWaiterClient(cc grpc.ClientConnInterface) WaiterClient {
return &waiterClient{cc}
}
func (c *waiterClient) DoMD5(ctx context.Context, in *Req, opts ...grpc.CallOption) (*Res, error) {
out := new(Res)
err := c.cc.Invoke(ctx, "/grpcs.Waiter/DoMD5", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// WaiterServer is the server API for Waiter service.
type WaiterServer interface {
// 定义接口 (结构体可以复用)
// 方法 (请求消息结构体) returns (返回消息结构体) {}
DoMD5(context.Context, *Req) (*Res, error)
}
// UnimplementedWaiterServer can be embedded to have forward compatible implementations.
type UnimplementedWaiterServer struct {
}
func (*UnimplementedWaiterServer) DoMD5(ctx context.Context, req *Req) (*Res, error) {
return nil, status.Errorf(codes.Unimplemented, "method DoMD5 not implemented")
}
func RegisterWaiterServer(s *grpc.Server, srv WaiterServer) {
s.RegisterService(&_Waiter_serviceDesc, srv)
}
func _Waiter_DoMD5_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(Req)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WaiterServer).DoMD5(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/grpcs.Waiter/DoMD5",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WaiterServer).DoMD5(ctx, req.(*Req))
}
return interceptor(ctx, in, info, handler)
}
var _Waiter_serviceDesc = grpc.ServiceDesc{
ServiceName: "grpcs.Waiter",
HandlerType: (*WaiterServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "DoMD5",
Handler: _Waiter_DoMD5_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "test.proto",
}
syntax = "proto3";
// 定义包名
package grpcs;
// 可以定义多个服务,每个服务内可以定义多个接口
service Waiter {
// 定义接口 (结构体可以复用)
// 方法 (请求消息结构体) returns (返回消息结构体) {}
rpc DoMD5 (Req) returns (Res) {}
}
// 定义 Req 消息结构
message Req {
// 类型 字段 = 标识号
string jsonStr = 1;
}
// 定义 Res 消息结构
message Res {
string backJson = 1;
}
// PSjsonStrbackJson只是随手写的名字,并没有用json
\ No newline at end of file
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"kefu_server/controllers" "kefu_server/controllers"
"kefu_server/db" "kefu_server/db"
"kefu_server/grpcs"
_ "kefu_server/routers" _ "kefu_server/routers"
"kefu_server/task" "kefu_server/task"
) )
...@@ -54,7 +55,7 @@ func main() { ...@@ -54,7 +55,7 @@ func main() {
beego.ErrorController(&controllers.ErrorController{}) beego.ErrorController(&controllers.ErrorController{})
// grpcserver init // grpcserver init
// go grpcs.Run() go grpcs.Run()
// run application // run application
beego.Run() beego.Run()
......
package task package task
import ( import (
"encoding/base64"
"encoding/json"
"kefu_server/models" "kefu_server/models"
"kefu_server/services" "kefu_server/services"
"kefu_server/utils" "kefu_server/utils"
...@@ -56,17 +54,14 @@ func appTask() { ...@@ -56,17 +54,14 @@ func appTask() {
message.Timestamp = time.Now().Unix() message.Timestamp = time.Now().Unix()
message.Payload = "由于您长时间未回复,本次会话超时了" message.Payload = "由于您长时间未回复,本次会话超时了"
message.ToAccount = contact.FromAccount message.ToAccount = contact.FromAccount
var messageJSON []byte
var messageString string var messageString string
messageJSON, _ = json.Marshal(message) messageString = utils.InterfaceToString(message)
messageString = base64.StdEncoding.EncodeToString([]byte(messageJSON))
utils.PushMessage(contact.FromAccount, messageString) utils.PushMessage(contact.FromAccount, messageString)
// Send a reminder message to customer service // Send a reminder message to customer service
message.FromAccount = contact.FromAccount message.FromAccount = contact.FromAccount
message.ToAccount = contact.ToAccount message.ToAccount = contact.ToAccount
messageJSON, _ = json.Marshal(message) messageString = utils.InterfaceToString(message)
messageString = base64.StdEncoding.EncodeToString([]byte(messageJSON))
utils.PushMessage(contact.ToAccount, messageString) utils.PushMessage(contact.ToAccount, messageString)
utils.MessageInto(message, true) utils.MessageInto(message, true)
...@@ -77,8 +72,7 @@ func appTask() { ...@@ -77,8 +72,7 @@ func appTask() {
message.BizType = "text" message.BizType = "text"
message.Key = time.Now().Unix() message.Key = time.Now().Unix()
message.Payload = robot.TimeoutText message.Payload = robot.TimeoutText
messageJSON, _ = json.Marshal(message) messageString = utils.InterfaceToString(message)
messageString = base64.StdEncoding.EncodeToString([]byte(messageJSON))
utils.PushMessage(contact.FromAccount, messageString) utils.PushMessage(contact.FromAccount, messageString)
} }
......
package utils
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/astaxie/beego/logs"
)
// InterfaceToString to string
func InterfaceToString(data interface{}) string {
byteData, err := json.Marshal(data)
if err != nil {
logs.Info("InterfaceToString to string error == ", err)
return ""
}
return base64.StdEncoding.EncodeToString(byteData)
}
// StringToInterface to Interface
func StringToInterface(s string, v interface{}) error {
byteData, err := base64.StdEncoding.DecodeString(strings.Replace(s, "\"", "", -1))
if err != nil {
logs.Info("StringToInterface to Interface error == ", err)
return err
}
json.Unmarshal(byteData, &v)
return nil
}
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"encoding/base64" "encoding/base64"
"kefu_server/models" "kefu_server/models"
"kefu_server/services" "kefu_server/services"
"strconv"
"time" "time"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
...@@ -21,12 +20,6 @@ func MessageInto(message models.Message, isKF bool) { ...@@ -21,12 +20,6 @@ func MessageInto(message models.Message, isKF bool) {
// MessageRepository instance // MessageRepository instance
messageRepository := services.GetMessageRepositoryInstance() messageRepository := services.GetMessageRepositoryInstance()
// 判断是否是撤回消息(软删除)
if message.BizType == "cancel" {
key, _ := strconv.ParseInt(message.Payload, 10, 64)
_, _ = messageRepository.Delete(models.RemoveMessageRequestDto{FromAccount: message.FromAccount, ToAccount: message.ToAccount, Key: key})
}
// message create time // message create time
message.Timestamp = time.Now().Unix() message.Timestamp = time.Now().Unix()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment