Commit 699d2a1c by chenxianqi

推荐项目

parent b6e64b63
......@@ -5,8 +5,8 @@ httpport = 8080
copyrequestbody = true
viewspath = "public"
# 用于本地服务今天地址
static_host = "http://localhost:8080/static/uploads/images"
# 用于本地服务静态资源地址
static_host = "http://localhost:8080"
# 进程监控
EnableAdmin = false
......
......@@ -64,7 +64,7 @@ func (c *MessageController) List() {
}
// push notify update current service contacts list
// 待处理888 推送给客的的聊天列表
// 待处理888 推送给客的的聊天列表
// if len(robotlbrary.Robots) > 0 {
// robotlbrary.PushNewContacts(auth.UID, robotlbrary.Robots[0])
// }
......@@ -166,7 +166,7 @@ func (c *MessageController) Transfer() {
// 待处理888 发送消息
// robot.SendMessage(strconv.FormatInt(admin.ID, 10), []byte(messageStringOne))
utils.MessageInto(message, true)
utils.MessageInto(message)
// Send to forwarded customer service
......@@ -178,7 +178,7 @@ func (c *MessageController) Transfer() {
// 待处理888 发送消息
// robot.SendMessage(strconv.FormatInt(transferDto.ToAccount, 10), []byte(messageStringTwo))
utils.MessageInto(message, true)
utils.MessageInto(message)
// send to user
// message.FromAccount = robot.ID
......@@ -191,7 +191,7 @@ func (c *MessageController) Transfer() {
// 待处理888 发送消息
// robot.SendMessage(strconv.FormatInt(transferDto.UserAccount, 10), []byte(messageString3))
utils.MessageInto(message, false)
utils.MessageInto(message)
// Transfer to the library for counting service times
servicesStatistical := models.ServicesStatistical{UserAccount: transferDto.UserAccount, ServiceAccount: transferDto.ToAccount, TransferAccount: admin.ID, Platform: user.Platform, CreateAt: time.Now().Unix()}
......
......@@ -381,7 +381,7 @@ func (c *PublicController) PushMessage() {
var msgContent []byte
msgContent, _ = base64.StdEncoding.DecodeString(pushMessage.Payload)
utils.StringToInterface(string(msgContent), &getMessage)
utils.MessageInto(getMessage, false)
utils.MessageInto(getMessage)
c.JSON(configs.ResponseSucess, "push success", nil)
......@@ -426,7 +426,7 @@ func (c *PublicController) Upload() {
c.JSON(configs.ResponseFail, "上传失败!", &err)
}
c.JSON(configs.ResponseSucess, "上传成功!", &fileName)
c.JSON(configs.ResponseSucess, "上传成功!", "/"+fpath)
}
// CancelMessage cancel a message
......
......@@ -5,6 +5,11 @@ service Kefu {
rpc GetOnlineAllRobots (Request) returns (Respones) {}
rpc PushMessage (Request) returns (Respones) {}
rpc CancelMessage (Request) returns (Respones) {}
rpc SearchKnowledgeTitles (Request) returns (Respones) {}
rpc GetOnlineAdmins (Request) returns (Respones) {}
rpc InsertStatistical (Request) returns (Respones) {}
rpc PushNewContacts (Request) returns (Respones) {}
rpc GetKnowledgeBaseWithTitleAndPlatform (Request) returns (Respones) {}
}
message Respones {
string data = 1;
......
......@@ -3,11 +3,12 @@ package grpcs
import (
context "context"
"encoding/base64"
"encoding/json"
"kefu_server/models"
"kefu_server/services"
"kefu_server/utils"
"log"
"net"
"strconv"
"github.com/astaxie/beego/logs"
grpc "google.golang.org/grpc"
......@@ -29,7 +30,7 @@ func (s *kefuServer) PushMessage(ctx context.Context, in *Request) (*Respones, e
var message models.Message
msgContent, _ := base64.StdEncoding.DecodeString(in.Data)
utils.StringToInterface(string(msgContent), &message)
utils.MessageInto(message, false)
utils.MessageInto(message)
return &Respones{Data: "push success"}, nil
}
......@@ -40,18 +41,63 @@ func (s *kefuServer) CancelMessage(ctx context.Context, in *Request) (*Respones,
// 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
}
// SearchKnowledgeTitles
func (s *kefuServer) SearchKnowledgeTitles(ctx context.Context, in *Request) (*Respones, error) {
var request models.KnowledgeBaseTitleRequestDto
utils.StringToInterface(in.Data, &request)
knowledgeBaseRepository := services.GetKnowledgeBaseRepositoryInstance()
titles := knowledgeBaseRepository.SearchKnowledgeTitles(request)
return &Respones{Data: utils.InterfaceToString(titles)}, nil
}
// GetOnlineAdmins
func (s *kefuServer) GetOnlineAdmins(ctx context.Context, in *Request) (*Respones, error) {
adminRepository := services.GetAdminRepositoryInstance()
admins := adminRepository.GetOnlineAdmins()
return &Respones{Data: utils.InterfaceToString(admins)}, nil
}
// PushNewContacts
func (s *kefuServer) PushNewContacts(ctx context.Context, in *Request) (*Respones, error) {
uid, _ := strconv.ParseInt(in.Data, 10, 64)
utils.PushNewContacts(uid)
return &Respones{Data: "PushNewContacts success"}, nil
}
// InsertStatistical
func (s *kefuServer) InsertStatistical(ctx context.Context, in *Request) (*Respones, error) {
var servicesStatistical models.ServicesStatistical
utils.StringToInterface(in.Data, &servicesStatistical)
statisticalRepository := services.GetStatisticalRepositoryInstance()
_, err := statisticalRepository.Add(&servicesStatistical)
if err != nil {
logs.Info("InsertStatistical err == ", err)
}
return &Respones{Data: "insert success"}, nil
}
// GetKnowledgeBaseWithTitle
func (s *kefuServer) GetKnowledgeBaseWithTitleAndPlatform(ctx context.Context, in *Request) (*Respones, error) {
request := make(map[string]string)
json.Unmarshal([]byte(in.Data), &request)
knowledgeBaseRepository := services.GetKnowledgeBaseRepositoryInstance()
platform, _ := strconv.ParseInt(request["platform"], 10, 64)
knowledgeBase := knowledgeBaseRepository.GetKnowledgeBaseWithTitleAndPlatform(request["title"], platform)
return &Respones{Data: utils.InterfaceToString(knowledgeBase)}, nil
}
// Run run grpc server
func Run() {
lis, err := net.Listen("tcp", ":8028")
if err != nil {
log.Fatalf("grpc server failed: %v", err)
logs.Info("grpc server failed: %v", err)
}
s := grpc.NewServer()
RegisterKefuServer(s, &kefuServer{})
......
......@@ -34,7 +34,7 @@ func initLog() {
}
func main() {
end := make(chan bool, 1)
// init db
db.Run()
......@@ -60,4 +60,5 @@ func main() {
// run application
beego.Run()
<-end
}
package models
// KnowledgeBaseTitleDto struct
type KnowledgeBaseTitleDto struct {
Title string `json:"title"`
}
// KnowledgeBaseTitleRequestDto struct
type KnowledgeBaseTitleRequestDto struct {
Payload string `json:"payload"`
KeyWords string `json:"keyWords"`
IsSerachSub bool `json:"isSerachSub"`
Platform int64 `json:"platform"`
Limit int64 `json:"limit"`
}
......@@ -15,6 +15,7 @@ type AdminRepositoryInterface interface {
Add(admin *models.Admin, col1 string) (bool, int64, error)
Delete(id int64) (int64, error)
GetAdmins(request *AdminPaginationDto) (*AdminPaginationDto, error)
GetOnlineAdmins() []models.Admin
CheckAdminsLoginTimeOutAndSetOffline(lastMessageUnixTimer int64) int64
}
......@@ -85,6 +86,17 @@ func (r *AdminRepository) Update(id int64, params orm.Params) (int64, error) {
return index, err
}
// GetOnlineAdmins online all admin
func (r *AdminRepository) GetOnlineAdmins() []models.Admin {
var admins []models.Admin
_, err := r.o.Raw("SELECT a.*, IFNULL(c.count,0) AS `count` FROM admin as a LEFT JOIN (SELECT to_account,COUNT(*) AS count FROM `contact` WHERE is_session_end = 0 GROUP BY to_account) c ON a.id = c.to_account WHERE a.`online` = 1 ORDER BY c.count").QueryRows(&admins)
if err != nil {
logs.Warn("GetOnlineAdmins online all admin------------", err)
return []models.Admin{}
}
return admins
}
// Add create a admin
func (r *AdminRepository) Add(admin *models.Admin, col1 string) (bool, int64, error) {
_bool, index, err := r.o.ReadOrCreate(admin, col1)
......
......@@ -101,7 +101,7 @@ func (r *ContactRepository) GetContacts(uid int64) ([]models.ContactDto, error)
_, err := r.o.Raw("SELECT c.id AS cid,c.to_account,c.is_session_end, c.last_message,c.last_message_type,c.from_account, c.create_at AS contact_create_at,u.*, IFNULL(m.`count`,0) AS `read` FROM `contact` c LEFT JOIN `user` u ON c.from_account = u.id LEFT JOIN (SELECT to_account,from_account, COUNT(*) as `count` FROM message WHERE `read` = 1 GROUP BY to_account,from_account) m ON m.to_account = c.to_account AND m.from_account = c.from_account WHERE c.to_account = ? AND c.delete = 0 ORDER BY c.create_at DESC", uid).QueryRows(&contactDto)
if err != nil {
logs.Warn("GetContacts get Contacts------------", err)
return nil, err
return []models.ContactDto{}, err
}
// content base 64 decode
for index, contact := range contactDto {
......
......@@ -16,6 +16,8 @@ type KnowledgeBaseRepositoryInterface interface {
Add(knowledgeBase *models.KnowledgeBase, col1 string) (bool, int64, error)
Update(id int64, params *orm.Params) (int64, error)
Delete(id int64) (int64, error)
SearchKnowledgeTitles(request models.KnowledgeBaseTitleRequestDto) []models.KnowledgeBaseTitleDto
GetKnowledgeBaseWithTitleAndPlatform(title string, platform int64) *models.KnowledgeBase
}
// KnowledgeBaseRepository struct
......@@ -43,6 +45,43 @@ func (r *KnowledgeBaseRepository) Add(knowledgeBase *models.KnowledgeBase, col1
return _bool, index, err
}
// SearchKnowledgeTitles Search Knowledge Titles
// payload = "key1"
// keyWords = "key1|key2|key3"
func (r *KnowledgeBaseRepository) SearchKnowledgeTitles(request models.KnowledgeBaseTitleRequestDto) []models.KnowledgeBaseTitleDto {
if request.Payload == "" {
return []models.KnowledgeBaseTitleDto{}
}
var knowledgeBaseTitleDto []models.KnowledgeBaseTitleDto
subTitle := ""
fields := "title"
if request.IsSerachSub {
fields = "title,sub_title"
}
roobtKeyWords := strings.Split(strings.Trim(request.KeyWords, "|"), "|")
if request.KeyWords == "" {
subTitle := ""
for _, value := range roobtKeyWords {
if strings.Contains(value, request.Payload) {
subTitle += subTitle + " sub_title LIKE \"%" + value + "%\" OR "
}
}
}
if request.IsSerachSub && request.KeyWords != "" {
for _, value := range roobtKeyWords {
subTitle += " sub_title LIKE \"%" + value + "%\" OR "
}
}
subTitle += " sub_title LIKE \"%" + request.Payload + "%\" "
_, err := r.o.Raw("SELECT "+fields+" FROM knowledge_base WHERE ( "+subTitle+" ) AND platform IN (?,?) ORDER by rand() limit ?", 1, request.Platform, request.Limit).QueryRows(&knowledgeBaseTitleDto)
if err != nil {
logs.Warn("Search Knowledge Titles------------", err)
return []models.KnowledgeBaseTitleDto{}
}
return knowledgeBaseTitleDto
}
// Update knowledgeBase
func (r *KnowledgeBaseRepository) Update(id int64, params orm.Params) (int64, error) {
index, err := r.q.Filter("id", id).Update(params)
......@@ -108,6 +147,17 @@ func (r *KnowledgeBaseRepository) GetKnowledgeBaseWithTitle(title string) *model
return &base
}
// GetKnowledgeBaseWithTitleAndPlatform get one KnowledgeBase with title and platform
func (r *KnowledgeBaseRepository) GetKnowledgeBaseWithTitleAndPlatform(title string, platform int64) *models.KnowledgeBase {
var knowledge models.KnowledgeBase
if err := r.q.Filter("title", title).Filter("platform__in", 1, platform).One(&knowledge); err != nil {
logs.Warn("GetKnowledgeBaseWithTitle get one KnowledgeBase with title------------", err)
return nil
}
knowledge.SubTitle = strings.Trim(knowledge.SubTitle, "|")
return &knowledge
}
// Delete delete a KnowledgeBase
func (r *KnowledgeBaseRepository) Delete(id int64) (int64, error) {
index, err := r.q.Filter("id", id).Delete()
......
......@@ -63,7 +63,7 @@ func appTask() {
message.ToAccount = contact.ToAccount
messageString = utils.InterfaceToString(message)
utils.PushMessage(contact.ToAccount, messageString)
utils.MessageInto(message, true)
utils.MessageInto(message)
// Message after timeout
if robot.TimeoutText != "" {
......
......@@ -59,7 +59,8 @@ func HTTPRequest(path string, method string, data interface{}, token string) *HT
if err != nil {
response.Code = 500
response.Message = "链接错误"
logs.Error(err)
logs.Error("链接错误====err", err)
return nil
}
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
......@@ -70,12 +71,17 @@ func HTTPRequest(path string, method string, data interface{}, token string) *HT
req.Header.Set("Authorization", token)
req.Header.Set("usertype", "cmp_app")
resp, err := client.Do(req)
if err != nil {
response.Code = 502
response.Message = "链接错误:" + err.Error()
return response
}
response.Code = resp.StatusCode
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
response.Code = 400
response.Message = "请求错误"
response.Code = 502
response.Message = "读取数据错处:" + err.Error()
return response
}
if resp.StatusCode != 200 {
......@@ -84,6 +90,5 @@ func HTTPRequest(path string, method string, data interface{}, token string) *HT
return response
}
json.Unmarshal(body, response)
// response.Data = string(body)
return response
}
......@@ -10,10 +10,10 @@ import (
)
// MessageInto push message
func MessageInto(message models.Message, isKF bool) {
func MessageInto(message models.Message) {
// 不处理的类型
if message.BizType == "contacts" || message.BizType == "pong" || message.BizType == "welcome" || message.BizType == "into" || message.BizType == "search_knowledge" {
if message.BizType == "contacts" || message.BizType == "handshake" || message.BizType == "pong" || message.BizType == "welcome" || message.BizType == "into" || message.BizType == "search_knowledge" {
return
}
......@@ -26,13 +26,12 @@ func MessageInto(message models.Message, isKF bool) {
// content内容转base64
message.Payload = base64.StdEncoding.EncodeToString([]byte(message.Payload))
// 过滤掉下面类型的消息不入库
if !(message.BizType == "handshake") {
if !isKF {
// UserRepository instance
userRepository := services.GetUserRepositoryInstance()
user := userRepository.GetUser(message.ToAccount)
// 接收者是用户
if user != nil {
// 默认已读消息
message.Read = 0
......@@ -43,6 +42,7 @@ func MessageInto(message models.Message, isKF bool) {
if user != nil && user.IsWindow == 0 {
message.Read = 1
}
}
// message.BizType == "end" is not read
......@@ -53,8 +53,6 @@ func MessageInto(message models.Message, isKF bool) {
// 消息入库
_, _ = messageRepository.Add(&message)
}
// RobotRepository instance
robotRepository := services.GetRobotRepositoryInstance()
......
package utils
import (
"encoding/base64"
"encoding/json"
"kefu_server/models"
"kefu_server/services"
"time"
)
// PushNewContacts 推送最新聊天列表给客服
func PushNewContacts(uid int64) {
contactData, _ := services.GetContactRepositoryInstance().GetContacts(uid)
// 消息体
message := models.Message{}
message.BizType = "contacts"
message.FromAccount = 1
message.Timestamp = time.Now().Unix()
for index, contact := range contactData {
payload, _ := base64.StdEncoding.DecodeString(contact.LastMessage)
contactData[index].LastMessage = string(payload)
}
message.ToAccount = uid
messageContentByte, _ := json.Marshal(contactData)
message.Payload = string(messageContentByte)
PushMessage(uid, InterfaceToString(message))
}
......@@ -30,7 +30,6 @@ func PushMessage(toAccount int64, msg string) bool {
fmt.Println(response)
return false
}
return true
}
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