登录页咨询弹窗和定时后台删权限文件

pull/30/head
taoshihan1991 4 years ago
parent 4395defbaa
commit 67edf948bb

@ -15,7 +15,11 @@ func ActionChatMain(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w)
render.Display("chat_main", nil)
}
//聊天客户端界面
func ActionChatPage(w http.ResponseWriter, r *http.Request){
render := tmpl.NewRender(w)
render.Display("chat_page", nil)
}
//获取在线用户
func ChatUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;")

@ -0,0 +1,37 @@
//+build !linux
package controller
import (
"github.com/taoshihan1991/imaptool/config"
"os"
"path/filepath"
"runtime"
"syscall"
"time"
)
var osType = runtime.GOOS
const expireTime=30*60
//检测权限文件是否过期,超过30分钟删除掉
func TimerSessFile(){
go func() {
for {
time.Sleep(time.Second*10)
files,_:=filepath.Glob(config.Dir+"sess_*")
for _,file:=range files{
fileInfo, _ := os.Stat(file)
var createTime int64
now:=time.Now().Unix()
if osType == "windows" {
wFileSys := fileInfo.Sys().(*syscall.Win32FileAttributeData)
tNanSeconds := wFileSys.CreationTime.Nanoseconds() /// 返回的是纳秒
createTime = tNanSeconds/1e9 ///秒
}
diffTime:=now-createTime
if diffTime>expireTime{
os.Remove(file)
}
}
}
}()
}

@ -0,0 +1,35 @@
package controller
import (
"github.com/taoshihan1991/imaptool/config"
"log"
"os"
"path/filepath"
"runtime"
"time"
)
var osType = runtime.GOOS
const expireTime=30*60
//检测权限文件是否过期,超过30分钟删除掉
func TimerSessFile(){
go func() {
for {
time.Sleep(time.Second*10)
files,_:=filepath.Glob(config.Dir+"sess_*")
for _,file:=range files{
fileInfo, _ := os.Stat(file)
var createTime int64
now:=time.Now().Unix()
if osType == "linux" {
stat_t := fileInfo.Sys().(*syscall.Stat_t)
createTime = int64(stat_t.Ctim.Sec)
}
diffTime:=now-createTime
if diffTime>expireTime{
os.Remove(file)
}
}
log.Println(files)
}
}()
}

@ -41,10 +41,14 @@ func main() {
mux.HandleFunc("/chat_main", controller.ActionChatMain)
//新邮件提醒服务
mux.HandleFunc("/push_mail", controller.PushMailServer)
//聊天界面
mux.HandleFunc("/chat_page",controller.ActionChatPage)
//聊天服务
mux.Handle("/chat_server",websocket.Handler(controller.ChatServer))
//获取在线用户
mux.HandleFunc("/chat_users", controller.ChatUsers)
//后台任务
controller.TimerSessFile()
//监听端口
//http.ListenAndServe(":8080", nil)
//var myHandler http.Handler

@ -0,0 +1,203 @@
<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>GO-FLY咨询页</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>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
html,
body {
height: 100%;
}
body {
background-color: #f5f5f5;
}
.chatContext{
background: #fff;border: solid 1px #e6e6e6;boder-top:none;padding:5px 10px;
width: 100%;
text-align: left;
}
.chatBox{
max-height: 400px;
overflow-y: auto;
overflow-x: hidden;
}
.chatBox .el-col{margin:10px 0;}
.chatUser{
line-height: 24px;
font-size: 12px;
white-space: nowrap;
color: #999;
text-align: left;
}
.chatContent{
background-color: rgb(166,212,242);
color: #000;
border: 1px solid rgb(152, 199, 230);
padding: 8px 15px;
min-height: 35px;
word-break: break-all;
position: relative;
border-radius: 5px;
}
.chatContent:after {
content: '';
position: absolute;
left: -10px;
top: 13px;
width: 0;
height: 0;
border-style: dashed;
border-color: transparent;
overflow: hidden;
border-width: 10px;
border-top-style: solid;
border-top-color: rgb(166,212,242);
}
.chatBoxMe .el-col-3{float: right;text-align: right;}
.chatBoxMe .chatUser{text-align: right}
.chatBoxMe .chatContent:after{left:auto;right: -10px;}
.chatArea{margin: 10px 0;}
.chatTitle{height: 30px;line-height: 30px;color: #1989fa}
</style>
</head>
<body class="text-center">
<div id="app" style="width:100%">
<template>
<!--客服代码-->
<div class="chatContext">
<div class="chatTitle">在线咨询</div>
<div class="chatBox">
<el-row :gutter="2" v-for="v in msgList" v-bind:class="{'chatBoxMe': v.is_kefu==true}">
<el-col :span="3"><el-avatar :size="60" :src="v.avatar"></el-avatar></el-col>
<el-col :span="21">
<div class="chatUser"><{v.name}></div>
<div class="chatContent"><{v.content}></div>
</el-col>
</el-row>
</div>
<el-input type="textarea" class="chatArea" v-model="messageContent"></el-input>
<el-button type="primary" v-on:click="chatToUser">发送</el-button>
</div>
<!--//客服代码-->
</template>
</div>
</body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script src="https://cdn.bootcss.com/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js"></script>
<script>
var guest={};
guest.from_id = "";
guest.from_name = typeof(returnCitySN)!="undefined" ?returnCitySN["cip"]+returnCitySN["cname"]:"小米";
guest.from_avatar = "https://dss2.bdstatic.com/6Ot1bjeh1BF3odCf/it/u=1539628792,3031847515&fm=85&app=92&f=JPEG?w=121&h=75&s=65F03DC608BA199ED70C5D3703009040";
guest.from_group = "1";
guest.to_id="";
new Vue({
el: '#app',
delimiters:["<{","}>"],
data: {
window:window,
server:"ws://127.0.0.1:8080/chat_server",
socket:null,
msgList:[],
messageContent:"",
},
methods: {
//初始化websocket
initConn() {
let socket = new ReconnectingWebSocket(this.server);//创建Socket实例
this.socket = socket
this.socket.onmessage = this.OnMessage;
this.socket.onopen = this.OnOpen;
},
OnOpen() {
let mes = {}
mes.type = "userInit";
let obj=this.getCache("guest");
if(!obj){
guest.from_id=this.generateUUID();
this.setCache("guest",guest)
obj=guest;
}
guest.from_id=obj.from_id;
mes.data = obj;
this.socket.send(JSON.stringify(mes));
},
OnMessage(e) {
const redata = JSON.parse(e.data);
if (redata.type == "kfOnline") {
let msg = redata.data
guest.to_id=msg.kf_id;
}
if (redata.type == "kfChatMessage") {
let msg = redata.data
guest.to_id=msg.kf_id;
let content = {}
content.avatar = msg.avatar;
content.name = msg.kf_name;
content.content = msg.content;
content.is_kefu = false;
content.time = msg.time;
this.msgList.push(content);
this.$nextTick(() => {
$('.chatBox').scrollTop($(".chatBox")[0].scrollHeight);
});
}
},
//发送给客户
chatToUser() {
let mes = {};
mes.type = "chatMessage";
guest.content = this.messageContent;
mes.data = guest;
this.socket.send(JSON.stringify(mes));
this.messageContent = "";
let content = {}
content.avatar=guest.from_avatar;
content.content = guest.content;
content.name = guest.from_name;
content.is_kefu = true;
content.time = '';
this.msgList.push(content);
this.$nextTick(() => {
$('.chatBox').scrollTop($(".chatBox")[0].scrollHeight);
});
},
setCache : function (key,obj){
if(typeof(Storage) !== "undefined"){
localStorage.setItem(key, JSON.stringify(obj));
}
},getCache : function (key){
return JSON.parse(localStorage.getItem(key));
},
generateUUID:function () {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
},
},
created: function () {
this.initConn();
}
})
</script>
</html>

@ -9,6 +9,7 @@
<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>
<script src="https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js"></script>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
@ -39,57 +40,11 @@
-webkit-box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
box-shadow: 0 1px 2px 0 rgba(101,129,156,.08);
}
.chatContext{
background: #fff;border: solid 1px #e6e6e6;boder-top:none;padding:5px 10px;
.chatBtn{
position: fixed;
right: 2px;
right: 10px;
bottom: 10px;
width: 325px;
text-align: left;
}
.chatBox{
max-height: 400px;
overflow-y: auto;
overflow-x: hidden;
}
.chatBox .el-col{margin:10px 0;}
.chatUser{
line-height: 24px;
font-size: 12px;
white-space: nowrap;
color: #999;
text-align: left;
}
.chatContent{
background-color: rgb(166,212,242);
color: #000;
border: 1px solid rgb(152, 199, 230);
padding: 8px 15px;
min-height: 35px;
word-break: break-all;
position: relative;
}
.chatContent:after {
content: '';
position: absolute;
left: -10px;
top: 13px;
width: 0;
height: 0;
border-style: dashed;
border-color: transparent;
overflow: hidden;
border-width: 10px;
border-top-style: solid;
border-top-color: rgb(166,212,242);
}
.chatBoxMe .el-col-3{float: right;text-align: right;}
.chatBoxMe .chatUser{text-align: right}
.chatBoxMe .chatContent:after{left:auto;right: -10px;}
.chatArea{margin: 10px 0;}
.chatTitle{height: 30px;line-height: 30px;color: #1989fa}
</style>
</head>
@ -148,35 +103,15 @@
<p class="mt-5 mb-3 text-muted">&copy; 2020</p>
<!--客服代码-->
<div class="chatContext">
<div class="chatTitle">在线咨询</div>
<div class="chatBox">
<el-row :gutter="2" v-for="v in msgList" v-bind:class="{'chatBoxMe': v.is_kefu==true}">
<el-col :span="3"><el-avatar :size="30" :src="v.avatar"></el-avatar></el-col>
<el-col :span="21">
<div class="chatUser"><{v.name}></div>
<div class="chatContent"><{v.content}></div>
</el-col>
</el-row>
</div>
<el-input type="textarea" class="chatArea" v-model="messageContent"></el-input>
<el-button type="primary" v-on:click="chatToUser">发送</el-button>
<div class="chatBtn">
<el-button type="primary" @click="showKefu">在线咨询</el-button>
</div>
<!--//客服代码-->
</template>
</div>
</body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script src="https://cdn.bootcss.com/reconnecting-websocket/1.0.0/reconnecting-websocket.min.js"></script>
<script>
var guest={};
guest.from_id = "";
guest.from_name = typeof(returnCitySN)!="undefined" ?returnCitySN["cip"]+returnCitySN["cname"]:"小米";
guest.from_avatar = "https://dss2.bdstatic.com/6Ot1bjeh1BF3odCf/it/u=1539628792,3031847515&fm=85&app=92&f=JPEG?w=121&h=75&s=65F03DC608BA199ED70C5D3703009040";
guest.from_group = "1";
guest.to_id="";
new Vue({
el: '#app',
delimiters:["<{","}>"],
@ -207,10 +142,6 @@
{ required: true, message: '密码不能为空', trigger: 'blur' },
],
},
server:"ws://127.0.0.1:8080/chat_server",
socket:null,
msgList:[],
messageContent:"",
},
methods: {
//提交表单
@ -278,88 +209,24 @@
}
});
},
//初始化websocket
initConn() {
let socket = new ReconnectingWebSocket(this.server);//创建Socket实例
this.socket = socket
this.socket.onmessage = this.OnMessage;
this.socket.onopen = this.OnOpen;
},
OnOpen() {
let mes = {}
mes.type = "userInit";
let obj=this.getCache("guest");
if(!obj){
guest.from_id=this.generateUUID();
this.setCache("guest",guest)
obj=guest;
}
guest.from_id=obj.from_id;
mes.data = obj;
this.socket.send(JSON.stringify(mes));
},
OnMessage(e) {
const redata = JSON.parse(e.data);
if (redata.type == "kfOnline") {
let msg = redata.data
guest.to_id=msg.kf_id;
}
if (redata.type == "kfChatMessage") {
let msg = redata.data
guest.to_id=msg.kf_id;
let content = {}
content.avatar = msg.avatar;
content.name = msg.kf_name;
content.content = msg.content;
content.is_kefu = false;
content.time = msg.time;
this.msgList.push(content);
this.$nextTick(() => {
$('.chatBox').scrollTop($(".chatBox")[0].scrollHeight);
});
}
},
//发送给客户
chatToUser() {
let mes = {};
mes.type = "chatMessage";
guest.content = this.messageContent;
mes.data = guest;
this.socket.send(JSON.stringify(mes));
this.messageContent = "";
let content = {}
content.avatar=guest.from_avatar;
content.content = guest.content;
content.name = guest.from_name;
content.is_kefu = true;
content.time = '';
this.msgList.push(content);
this.$nextTick(() => {
$('.chatBox').scrollTop($(".chatBox")[0].scrollHeight);
});
},
setCache : function (key,obj){
if(typeof(Storage) !== "undefined"){
localStorage.setItem(key, JSON.stringify(obj));
}
},getCache : function (key){
return JSON.parse(localStorage.getItem(key));
},
generateUUID:function () {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
//展示客服弹窗
showKefu:function () {
$(".chatBtn").hide();
layer.open({
type: 2,
title: '在线咨询',
shadeClose: true,
shade: false,
maxmin: true, //开启最大化最小化按钮
area: ['860px', '600px'],
content: ['/chat_page'],
end: function(){
$(".chatBtn").show();
}
});
return uuid;
},
},
created: function () {
this.initConn();
}
})

@ -0,0 +1,81 @@
package tools
import (
"errors"
"sync"
"time"
)
/*
* Snowflake
*
* 1 42 52 64
* +-----------------------------------------------+------------+---------------+
* | timestamp(ms) | workerid | sequence |
* +-----------------------------------------------+------------+---------------+
* | 0000000000 0000000000 0000000000 0000000000 0 | 0000000000 | 0000000000 00 |
* +-----------------------------------------------+------------+---------------+
*
* 1. 41() - )使70: (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69
* 2. 101024
* 3. 124096
*/
const (
twepoch = int64(1483228800000) //开始时间截 (2017-01-01)
workeridBits = uint(10) //机器id所占的位数
sequenceBits = uint(12) //序列所占的位数
workeridMax = int64(-1 ^ (-1 << workeridBits)) //支持的最大机器id数量
sequenceMask = int64(-1 ^ (-1 << sequenceBits)) //
workeridShift = sequenceBits //机器id左移位数
timestampShift = sequenceBits + workeridBits //时间戳左移位数
)
// A Snowflake struct holds the basic information needed for a snowflake generator worker
type Snowflake struct {
sync.Mutex
timestamp int64
workerid int64
sequence int64
}
// NewNode returns a new snowflake worker that can be used to generate snowflake IDs
func NewSnowflake(workerid int64) (*Snowflake, error) {
if workerid < 0 || workerid > workeridMax {
return nil, errors.New("workerid must be between 0 and 1023")
}
return &Snowflake{
timestamp: 0,
workerid: workerid,
sequence: 0,
}, nil
}
// Generate creates and returns a unique snowflake ID
func (s *Snowflake) Generate() int64 {
s.Lock()
now := time.Now().UnixNano() / 1000000
if s.timestamp == now {
s.sequence = (s.sequence + 1) & sequenceMask
if s.sequence == 0 {
for now <= s.timestamp {
now = time.Now().UnixNano() / 1000000
}
}
} else {
s.sequence = 0
}
s.timestamp = now
r := int64((now-twepoch)<<timestampShift | (s.workerid << workeridShift) | (s.sequence))
s.Unlock()
return r
}
Loading…
Cancel
Save