兼容旧聊天

pull/30/head
taoshihan1991 5 years ago
parent e543974e8c
commit a4a451a8b3

@ -115,6 +115,45 @@ func FoldersList(w http.ResponseWriter, r *http.Request) {
})
w.Write(msg)
}
//邮件接口
func FolderMail(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid")
id, _ :=strconv.Atoi(tools.GetUrlArg(r,"id"))
mailServer := tools.GetMailServerFromCookie(r)
w.Header().Set("content-type", "text/json;charset=utf-8;")
if mailServer == nil {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
w.Write(msg)
return
}
var wg sync.WaitGroup
result := make(map[string]interface{})
wg.Add(2)
go func() {
defer wg.Done()
folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid)
result["folders"] = folders
result["total"] = folders[fid]
}()
go func() {
defer wg.Done()
mail := tools.GetMessage(mailServer.Server, mailServer.Email, mailServer.Password, fid, uint32(id))
result["from"] = mail.From
result["to"] = mail.To
result["subject"] = mail.Subject
result["date"] = mail.Date
result["html"] = mail.Body
}()
wg.Wait()
result["fid"] = fid
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
}
//发送邮件接口
func FolderSend(w http.ResponseWriter, r *http.Request){
w.Header().Set("content-type", "text/json;charset=utf-8;")

@ -3,15 +3,14 @@ package main
import (
"encoding/json"
"github.com/taoshihan1991/imaptool/controller"
"github.com/taoshihan1991/imaptool/tools"
"golang.org/x/net/websocket"
"log"
"net/http"
"strconv"
"sync"
"time"
)
func main() {
log.Println("listen on 8080...\r\ngohttp://127.0.0.1:8080")
baseServer:="127.0.0.1:8080"
log.Println("start server...\r\ngohttp://"+baseServer)
mux:=&http.ServeMux{}
//根路径
mux.HandleFunc("/", controller.ActionIndex)
@ -26,7 +25,7 @@ func main() {
//新邮件夹接口
mux.HandleFunc("/folder_dirs", controller.FolderDir)
//邮件接口
mux.HandleFunc("/mail", mail)
mux.HandleFunc("/mail", controller.FolderMail)
//详情界面
mux.HandleFunc("/view", controller.ActionDetail)
//写信界面
@ -40,9 +39,11 @@ func main() {
//发送邮件接口
mux.HandleFunc("/send", controller.FolderSend)
//聊天界面
mux.HandleFunc("/chat_main", controller.ActionMain)
//推送新邮件
mux.HandleFunc("/chat_main", controller.ActionChatMain)
//新邮件提醒服务
mux.HandleFunc("/push_mail", controller.PushMailServer)
//聊天服务
mux.Handle("/chat_server",websocket.Handler(ChatServer))
//监听端口
//http.ListenAndServe(":8080", nil)
//var myHandler http.Handler
@ -55,43 +56,97 @@ func main() {
}
s.ListenAndServe()
}
//兼容之前的聊天服务
func ChatServer(w *websocket.Conn){
var error error
for {
//接受消息
var receive string
if error = websocket.Message.Receive(w, &receive); error != nil {
log.Println("接受消息失败", error)
break
}
message := Message{}
err := json.Unmarshal([]byte(receive), &message)
if err != nil {
log.Println(err)
}
chat := ChatUserMessage{}
json.Unmarshal([]byte(receive), &chat)
log.Println("客户端:", message)
kfMessageData := KfMessageData{
Kf_name: "客服小美",
Avatar: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4217138672,2588039002&fm=26&gp=0.jpg",
Kf_id: "KF2",
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: chat.Data.Content,
}
switch message.Type.(string) {
//用户初始化
case "userInit":
clientList[message.Uid.(string)] = w
sendMsg := ChatKfMessage{
Message_type: "kf_online",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//正常发送消息
case "chatMessage":
//邮件接口
func mail(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid")
id, _ :=strconv.Atoi(tools.GetUrlArg(r,"id"))
mailServer := tools.GetMailServerFromCookie(r)
w.Header().Set("content-type", "text/json;charset=utf-8;")
sendMsg := ChatKfMessage{
Message_type: "chatMessage",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//回应ping
case "ping":
if mailServer == nil {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
w.Write(msg)
return
sendMsg := PingMessage{
Type: "pong",
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
}
}
var wg sync.WaitGroup
result := make(map[string]interface{})
wg.Add(2)
go func() {
defer wg.Done()
folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid)
result["folders"] = folders
result["total"] = folders[fid]
}()
go func() {
defer wg.Done()
mail := tools.GetMessage(mailServer.Server, mailServer.Email, mailServer.Password, fid, uint32(id))
result["from"] = mail.From
result["to"] = mail.To
result["subject"] = mail.Subject
result["date"] = mail.Date
result["html"] = mail.Body
}()
wg.Wait()
result["fid"] = fid
}
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
type Message struct {
Type interface{}
Uid interface{}
Name interface{}
Avatar interface{}
Group interface{}
}
type KfMessageData struct {
Kf_name interface{} `json:"kf_name"`
Avatar interface{} `json:"avatar"`
Kf_id interface{} `json:"kf_id"`
Time interface{} `json:"time"`
Content interface{} `json:"content"`
}
type UserMessageData struct {
From_avatar interface{} `json:"from_avatar"`
From_id interface{} `json:"from_id"`
From_name interface{} `json:"from_name"`
To_id interface{} `json:"to_id"`
To_name interface{} `json:"to_name"`
Content interface{} `json:"content"`
}
type ChatKfMessage struct {
Message_type interface{} `json:"message_type"`
Data KfMessageData `json:"data"`
}
type ChatUserMessage struct {
Message_type interface{} `json:"message_type"`
Data UserMessageData `json:"data"`
}
type PingMessage struct {
Type interface{} `json:"type"`
}
var clientList =make(map[string]*websocket.Conn)

@ -1,21 +0,0 @@
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="陶士涵">
<title>聊天界面</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<style>
html, body {height: 100%;padding: 0;margin: 0;background-color: #f5f5f5;}
</style>
</head>
<body>
<div id="app">
</div>
</body>
</html>

@ -0,0 +1,56 @@
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="陶士涵">
<title>聊天界面</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<style>
html, body {height: 100%;padding: 0;margin: 0;background-color: #f5f5f5;}
.el-row{width:100%}
</style>
</head>
<body>
<div id="app">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">
<el-row :gutter="5">
<el-col :span="6">
<el-menu>
<el-menu-item>
<i class="el-icon-edit"></i>
<span slot="title">访客1112</span>
</el-menu-item>
</el-menu>
</el-col>
<el-col :span="12">聊天主界面</el-col>
<el-col :span="6">常用功能</el-col>
</el-row>
</el-container>
</template>
</div>
</body>
<script>
var app=new Vue({
el: '#app',
delimiters:["<{","}>"],
data: {
fullscreenLoading:true,
},
methods: {
//跳转
openUrl(url){
window.location.href=url;
},
},
created: function () {
this.fullscreenLoading=false
}
})
</script>
</html>

File diff suppressed because it is too large Load Diff

@ -143,14 +143,18 @@ func GetFolderMail(server string, email string, password string, folder string,
messages := make(chan *imap.Message, pagesize)
done := make(chan error, 1)
fetchItem:=imap.FetchItem(imap.FetchEnvelope)
items := make([]imap.FetchItem,0)
items=append(items,fetchItem)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
done <- c.Fetch(seqset, items, messages)
}()
var mailPagelist = new(MailPageList)
dec := GetDecoder()
for msg := range messages {
log.Println(msg.Envelope.Date)
ret, err := dec.Decode(msg.Envelope.Subject)
if err != nil {

Loading…
Cancel
Save