diff --git a/cmd/js/functions.js b/cmd/js/functions.js
deleted file mode 100644
index 1ac71f9..0000000
--- a/cmd/js/functions.js
+++ /dev/null
@@ -1,157 +0,0 @@
-function getBaseUrl() {
- var ishttps = 'https:' == document.location.protocol ? true : false;
- var url = window.location.host;
- if (ishttps) {
- url = 'https://' + url;
- } else {
- url = 'http://' + url;
- }
- return url;
-}
-function getWsBaseUrl() {
- var ishttps = 'https:' == document.location.protocol ? true : false;
- var url = window.location.host;
- if (ishttps) {
- url = 'wss://' + url;
- } else {
- url = 'ws://' + url;
- }
- return url;
-}
-function notify(title, options, callback) {
- // 先检查浏览器是否支持
- if (!window.Notification) {
- return;
- }
- var notification;
- // 检查用户曾经是否同意接受通知
- if (Notification.permission === 'granted') {
- notification = new Notification(title, options); // 显示通知
-
- } else {
- var promise = Notification.requestPermission();
- }
-
- if (notification && callback) {
- notification.onclick = function(event) {
- callback(notification, event);
- }
- setTimeout(function () {
- notification.close();
- },5000);
- }
-}
-var titleTimer=0;
-var titleNum=0;
-var originTitle = document.title;
-function flashTitle() {
- if(titleTimer!=0){
- return;
- }
- titleTimer = setInterval(function(){
- titleNum++;
- if (titleNum == 3) {
- titleNum = 1;
- }
- if (titleNum == 1) {
- document.title = '【】' + originTitle;
- }
- if (titleNum == 2) {
- document.title = '【new message】' + originTitle;
- }
- }, 500);
-
-}
-function clearFlashTitle() {
- clearInterval(titleTimer);
- document.title = originTitle;
-}
-var faceTitles = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]", "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]", "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]", "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]", "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]", "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]", "[话筒]", "[蜡烛]", "[蛋糕]"];
-function placeFace() {
- var faces=[];
- for(var i=0;i/g, '>').replace(/'/g, ''').replace(/"/g, '"') // XSS
- .replace(/face\[([^\s\[\]]+?)\]/g, function (face) { // 转义表情
- var alt = face.replace(/^face/g, '');
- return '';
- })
- .replace(/img\[([^\s\[\]]+?)\]/g, function (face) { // 转义图片
- var src = face.replace(/^img\[/g, '').replace(/\]/g, '');;
- return '';
- })
- .replace(/file\[([^\s\[\]]+?)\]/g, function (face) { // 转义图片
- var src = face.replace(/^file\[/g, '').replace(/\]/g, '');;
- return '
';
- })
- .replace(/\[([^\s\[\]]+?)\]+link\[([^\s\[\]]+?)\]/g, function (face) { // 转义超链接
- var text = face.replace(/link\[.*?\]/g, '').replace(/\[|\]/g, '');
- var src = face.replace(/^\[([^\s\[\]]+?)\]+link\[/g, '').replace(/\]/g, '');
- return '【'+text+'】';
- })
- .replace(html(), '\<$1 $2\>').replace(html('/'), '\$1\>') // 转移HTML代码
- .replace(/\n/g, '
') // 转义换行
-
- return content;
-}
-function bigPic(src,isVisitor){
- if (isVisitor) {
- window.open(src);
- return;
- }
-}
-function filter (obj){
- var imgType = ["image/jpeg","image/png","image/jpg","image/gif"];
- var filetypes = imgType;
- var isnext = false;
- for (var i = 0; i < filetypes.length; i++) {
- if (filetypes[i] == obj.type) {
- return true;
- }
- }
- return false;
-}
-function sleep(time) {
- var startTime = new Date().getTime() + parseInt(time, 10);
- while(new Date().getTime() < startTime) {}
-}
-function checkLang(){
- var langs=["cn","en"];
- var lang=getQuery("lang");
- if(lang!=""&&langs.indexOf(lang) > 0 ){
- return lang;
- }
- return "cn";
-}
-function getQuery(key) {
- var query = window.location.search.substring(1);
- var key_values = query.split("&");
- var params = {};
- key_values.map(function (key_val){
- var key_val_arr = key_val.split("=");
- params[key_val_arr[0]] = key_val_arr[1];
- });
- if(typeof params[key]!="undefined"){
- return params[key];
- }
- return "";
-}
-function utf8ToB64(str) {
- return window.btoa(unescape(encodeURIComponent(str)));
-}
-function b64ToUtf8(str) {
- return decodeURIComponent(escape(window.atob(str)));
-}
-;
\ No newline at end of file
diff --git a/cmd/js/gofly-front.js b/cmd/js/gofly-front.js
deleted file mode 100644
index 50c851d..0000000
--- a/cmd/js/gofly-front.js
+++ /dev/null
@@ -1,333 +0,0 @@
-var GOFLY={
- GOFLY_URL:"https://gofly.sopans.com",
- GOFLY_KEFU_ID:"",
- GOFLY_BTN_TEXT:"Chat with me",
- GOFLY_LANG:"en",
- GOFLY_EXTRA: {},
- GOFLY_AUTO_OPEN:true,
- GOFLY_AUTO_SHOW:false,
- GOFLY_WITHOUT_BTN:false,
-};
-GOFLY.launchButtonFlag=false;
-GOFLY.titleTimer=0;
-GOFLY.titleNum=0;
-GOFLY.noticeTimer=null;
-GOFLY.originTitle=document.title;
-GOFLY.chatPageTitle="GOFLY";
-GOFLY.kefuName="";
-GOFLY.kefuAvator="";
-GOFLY.init=function(config){
- var _this=this;
- if(typeof config=="undefined"){
- return;
- }
-
- if (typeof config.GOFLY_URL!="undefined"){
- this.GOFLY_URL=config.GOFLY_URL.replace(/([\w\W]+)\/$/,"$1");
- }
- this.dynamicLoadCss(this.GOFLY_URL+"/static/css/gofly-front.css?v=1");
-
- if (typeof config.GOFLY_KEFU_ID!="undefined"){
- this.GOFLY_KEFU_ID=config.GOFLY_KEFU_ID;
- }
- if (typeof config.GOFLY_BTN_TEXT!="undefined"){
- this.GOFLY_BTN_TEXT=config.GOFLY_BTN_TEXT;
- }
- if (typeof config.GOFLY_EXTRA!="undefined"){
- this.GOFLY_EXTRA=config.GOFLY_EXTRA;
- }
- if (typeof config.GOFLY_AUTO_OPEN!="undefined"){
- this.GOFLY_AUTO_OPEN=config.GOFLY_AUTO_OPEN;
- }
- if (typeof config.GOFLY_AUTO_SHOW!="undefined"){
- this.GOFLY_AUTO_SHOW=config.GOFLY_AUTO_SHOW;
- }
- if (typeof config.GOFLY_WITHOUT_BTN!="undefined"){
- this.GOFLY_WITHOUT_BTN=config.GOFLY_WITHOUT_BTN;
- }
- var refer=document.referrer?document.referrer:"无";
- this.GOFLY_EXTRA.refer=refer;
- this.GOFLY_EXTRA.host=document.location.href;
- this.GOFLY_EXTRA=JSON.stringify(_this.GOFLY_EXTRA);
-
- this.dynamicLoadJs(this.GOFLY_URL+"/assets/js/functions.js?v=1",function(){
- if (typeof config.GOFLY_LANG!="undefined"){
- _this.GOFLY_LANG=config.GOFLY_LANG;
- }else{
- _this.GOFLY_LANG=checkLang();
- }
- _this.GOFLY_EXTRA=utf8ToB64(_this.GOFLY_EXTRA);
- });
- if (typeof $!="function"){
- this.dynamicLoadJs("https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js",function () {
- _this.dynamicLoadJs("https://cdn.staticfile.org/layer/3.4.0/layer.min.js",function () {
- _this.jsCallBack();
- });
- });
- }else{
- this.dynamicLoadJs("https://cdn.staticfile.org/layer/3.4.0/layer.min.js",function () {
- _this.jsCallBack();
- });
- }
- _this.addEventlisten();
-}
-GOFLY.jsCallBack=function(){
- this.showKefuBtn();
- this.addClickEvent();
- this.getNotice();
-}
-GOFLY.showKefuBtn=function(){
- var _this=this;
- if(_this.GOFLY_WITHOUT_BTN){
- return;
- }
- var html="';
- $('body').append(html);
-}
-GOFLY.addClickEvent=function(){
- var _this=this;
- $(".launchButton").on("click",function() {
- _this.GOFLY_AUTO_SHOW=true;
- _this.showKefu();
- $("#launchIcon").text(0).hide();
- });
-
- $("body").on("click","#launchNoticeClose",function() {
- $("#launchButtonNotice").hide();
- });
-
- $("body").click(function () {
- clearTimeout(_this.titleTimer);
- document.title = _this.originTitle;
- });
-}
-GOFLY.addEventlisten=function(){
- var _this=this;
- window.addEventListener('message',function(e){
- var msg=e.data;
- if(msg.type=="message"){
- clearInterval(_this.noticeTimer);
- var width=$(window).width();
- if(width>768){
- _this.flashTitle();//标题闪烁
- }
- if (this.launchButtonFlag) return;
- var welcomeHtml=" "+_this.kefuName+"" +
- "
×" +
- "
";
- welcomeHtml+=""+replaceContent(msg.data.content,_this.GOFLY_URL)+"
";
- $("#launchButtonNotice").html(welcomeHtml).show();
- var news=$("#launchIcon").text();
- $("#launchIcon").text(++news).show();
- }
- if(msg.type=="focus"){
- clearTimeout(_this.titleTimer);
- _this.titleTimer=0;
- document.title = _this.originTitle;
- }
- });
- window.onfocus = function () {
- clearTimeout(_this.titleTimer);
- _this.titleTimer=0;
- document.title = _this.originTitle;
- };
-}
-GOFLY.dynamicLoadCss=function(url){
- var head = document.getElementsByTagName('head')[0];
- var link = document.createElement('link');
- link.type='text/css';
- link.rel = 'stylesheet';
- link.href = url;
- head.appendChild(link);
-}
-GOFLY.dynamicLoadJs=function(url, callback){
- var head = document.getElementsByTagName('head')[0];
- var script = document.createElement('script');
- script.type = 'text/javascript';
- script.src = url;
- if(typeof(callback)=='function'){
- script.onload = script.onreadystatechange = function () {
- if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete"){
- callback();
- script.onload = script.onreadystatechange = null;
- }
- };
- }
- head.appendChild(script);
-}
-
-GOFLY.getNotice=function(){
- var _this=this;
- $.get(this.GOFLY_URL+"/notice?kefu_id="+this.GOFLY_KEFU_ID,function(res) {
- if(res.result.status=='offline'){
- _this.chatPageTitle="";
- }else{
- _this.chatPageTitle="";
- setTimeout(function(){
- var userInfo=" "+res.result.username+""
- $('.launchButtonText').html(userInfo);
- },3000);
- }
- _this.kefuAvator=res.result.avatar;
- _this.kefuName=res.result.username;
- _this.chatPageTitle+=""+res.result.username;
- if(_this.GOFLY_AUTO_OPEN&&_this.isIE()<=0){
- _this.showKefu();
- $(".launchButtonBox").show();
- _this.launchButtonFlag=false;
- }
- if (res.result.welcome != null) {
- var msg = res.result.welcome;
- var len=msg.length;
- var i=0;
- if(len>0){
-
- _this.noticeTimer=setInterval(function(){
- if(i>=len||typeof msg[i]=="undefined"||msg[i]==null){
- clearInterval(_this.noticeTimer);
- return;
- }
- var content = msg[i];
- if(typeof content.content =="undefined"){
- return;
- }
- var welcomeHtml=" "+res.result.username+"" +
- "
×" +
- "
";
- welcomeHtml+=""+replaceContent(content.content,_this.GOFLY_URL)+"
";
-
- var obj=$("#launchButtonNotice");
- if(obj[0]){
- obj[0].innerHTML=welcomeHtml;
- obj.show();
- }
- i++;
- $("#launchIcon").text(i).show();
- },4000);
- }
-
- }
- });
-}
-GOFLY.isIE=function(){
- var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
- var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
- var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
- var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
- if(isIE) {
- var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
- reIE.test(userAgent);
- var fIEVersion = parseFloat(RegExp["$1"]);
- if(fIEVersion == 7) {
- return 7;
- } else if(fIEVersion == 8) {
- return 8;
- } else if(fIEVersion == 9) {
- return 9;
- } else if(fIEVersion == 10) {
- return 10;
- } else {
- return 6;//IE版本<=7
- }
- } else if(isEdge) {
- return 'edge';//edge
- } else if(isIE11) {
- return 11; //IE11
- }else{
- return -1;//不是ie浏览器
- }
-}
-GOFLY.showPanel=function (){
- var width=$(window).width();
- this.GOFLY_AUTO_SHOW=true;
- if(this.isIE()>0){
- this.windowOpen();
- return;
- }
- if(width<768){
- this.layerOpen("100%","100%");
- return;
- }
- this.layerOpen("400px","530px");
- this.launchButtonFlag=true;
-}
-GOFLY.showKefu=function (){
- if (this.launchButtonFlag) return;
- var width=$(window).width();
- if(this.isIE()>0){
- this.windowOpen();
- return;
- }
- if(width<768){
- this.layerOpen("100%","100%");
- return;
- }
- this.layerOpen("400px","530px");
- this.launchButtonFlag=true;
- $(".launchButtonBox").hide();
-}
-GOFLY.layerOpen=function (width,height){
- if (this.launchButtonFlag) return;
- var layBox=$("#layui-layer19911116");
- if(layBox.css("display")=="none"){
- layBox.show();
- return;
- }
- var _this=this;
- layer.index="19911115";
- layer.open({
- type: 2,
- title: this.chatPageTitle,
- closeBtn: 1, //不显示关闭按钮
- shade: 0,
- area: [width, height],
- offset: 'rb', //右下角弹出
- anim: 2,
- content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA , 'yes'], //iframe的url,no代表不显示滚动条
- success:function(){
- var layBox=$("#layui-layer19911116");
- if(_this.GOFLY_AUTO_SHOW&&layBox.css("display")=="none"){
- _this.launchButtonFlag=true;
- layBox.show();
- }
- },
- end: function(){
- _this.launchButtonFlag=false;
- $(".launchButtonBox").show();
- },
- cancel: function(index, layero){
- $("#layui-layer19911116").hide();
- _this.launchButtonFlag=false;
- $(".launchButtonBox").show();
- return false;
- }
- });
-}
-GOFLY.windowOpen=function (){
- window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA);
-}
-GOFLY.flashTitle=function () {
- if(this.titleTimer!=0){
- return;
- }
- this.titleTimer = setInterval("GOFLY.flashTitleFunc()", 500);
-}
-GOFLY.flashTitleFunc=function(){
- this.titleNum++;
- if (this.titleNum >=3) {
- this.titleNum = 1;
- }
- if (this.titleNum == 1) {
- document.title = '【】' + this.originTitle;
- }
- if (this.titleNum == 2) {
- document.title = '【new message】' + this.originTitle;
- }
-}
-
-
diff --git a/cmd/server.go b/cmd/server.go
index ffade4f..8fd86fd 100644
--- a/cmd/server.go
+++ b/cmd/server.go
@@ -1,13 +1,14 @@
package cmd
import (
- "embed"
"fmt"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
+ "github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/middleware"
"github.com/taoshihan1991/imaptool/router"
+ "github.com/taoshihan1991/imaptool/static"
"github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
"github.com/zh-five/xdaemon"
@@ -31,12 +32,6 @@ var serverCmd = &cobra.Command{
},
}
-//go:embed templates/*
-var templatesEmbed embed.FS
-
-//go:embed js/*
-var jsEmbed embed.FS
-
func init() {
serverCmd.PersistentFlags().StringVarP(&port, "port", "p", "8081", "监听端口号")
serverCmd.PersistentFlags().BoolVarP(&daemon, "daemon", "d", false, "是否为守护进程模式")
@@ -63,10 +58,15 @@ func run() {
tools.Logger().Println("start server...\r\ngo:http://" + baseServer)
engine := gin.Default()
- //engine.LoadHTMLGlob("static/html/*")
- templ := template.Must(template.New("").ParseFS(templatesEmbed, "templates/*.html"))
- engine.SetHTMLTemplate(templ)
- engine.StaticFS("/assets", http.FS(jsEmbed))
+ if common.IsCompireTemplate {
+ templ := template.Must(template.New("").ParseFS(static.TemplatesEmbed, "templates/*.html"))
+ engine.SetHTMLTemplate(templ)
+ engine.StaticFS("/assets", http.FS(static.JsEmbed))
+ } else {
+ engine.LoadHTMLGlob("static/templates/*")
+ engine.Static("/assets", "./static")
+ }
+
engine.Static("/static", "./static")
engine.Use(tools.Session("gofly"))
engine.Use(middleware.CrossSite)
diff --git a/common/common.go b/common/common.go
index 1b220a6..c40c558 100644
--- a/common/common.go
+++ b/common/common.go
@@ -1,11 +1,12 @@
package common
var (
- PageSize uint = 10
- VisitorPageSize uint = 8
- Version string = "0.3.9"
- VisitorExpire float64 = 600
- Upload string = "static/upload/"
- Dir string = "config/"
- MysqlConf string = Dir + "mysql.json"
+ PageSize uint = 10
+ VisitorPageSize uint = 8
+ Version string = "0.3.9"
+ VisitorExpire float64 = 600
+ Upload string = "static/upload/"
+ Dir string = "config/"
+ MysqlConf string = Dir + "mysql.json"
+ IsCompireTemplate bool = true //是否编译静态模板到二进制
)
diff --git a/cmd/js/chat-lang.js b/static/js/chat-lang.js
similarity index 100%
rename from cmd/js/chat-lang.js
rename to static/js/chat-lang.js
diff --git a/cmd/js/chat-main.js b/static/js/chat-main.js
similarity index 100%
rename from cmd/js/chat-main.js
rename to static/js/chat-main.js
diff --git a/cmd/js/chat-page.js b/static/js/chat-page.js
similarity index 100%
rename from cmd/js/chat-page.js
rename to static/js/chat-page.js
diff --git a/cmd/js/index.js b/static/js/index.js
similarity index 100%
rename from cmd/js/index.js
rename to static/js/index.js
diff --git a/cmd/js/reconnecting-websocket.min.js b/static/js/reconnecting-websocket.min.js
similarity index 100%
rename from cmd/js/reconnecting-websocket.min.js
rename to static/js/reconnecting-websocket.min.js
diff --git a/static/static.go b/static/static.go
new file mode 100644
index 0000000..5b6cc41
--- /dev/null
+++ b/static/static.go
@@ -0,0 +1,9 @@
+package static
+
+import "embed"
+
+//go:embed templates/*
+var TemplatesEmbed embed.FS
+
+//go:embed js/*
+var JsEmbed embed.FS
diff --git a/cmd/templates/chat_main.html b/static/templates/chat_main.html
similarity index 97%
rename from cmd/templates/chat_main.html
rename to static/templates/chat_main.html
index 1da14c8..174a321 100644
--- a/cmd/templates/chat_main.html
+++ b/static/templates/chat_main.html
@@ -4,13 +4,13 @@
GO语言开源客服系统-GOFLY
+
+
+
+
-
-
-
-
-
+