perf: remove extra code

main
zggsong 2 years ago
parent 50956515dd
commit f527f9f62b

@ -15,7 +15,7 @@ import (
"io/ioutil"
"luxshare-daily-report/global"
"luxshare-daily-report/serve/core"
"luxshare-daily-report/support"
"luxshare-daily-report/util"
"net/http"
"os"
"strconv"
@ -97,7 +97,7 @@ func DeclarationService(files map[string]string) {
ticket, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord)
if ticket == "" || err != nil {
log.Printf("ticket: %v, err:%v", ticket, err.Error())
support.SendMessageError(err)
util.SendMessageError(err)
return
}
@ -114,13 +114,13 @@ func DeclarationService(files map[string]string) {
//log.Printf("[DEBUG] get images links: %s", imagesLinks)
if err != nil {
log.Printf(err.Error())
support.SendMessageError(err)
util.SendMessageError(err)
return
}
if imagesLinks == nil {
err = errors.New("[ERROR] Get no images links")
log.Printf(err.Error())
support.SendMessageError(err)
util.SendMessageError(err)
return
}
@ -129,7 +129,7 @@ func DeclarationService(files map[string]string) {
err = core.EpidemicRegistration(ticket, imagesLinks)
if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error())
support.SendMessageError(err)
util.SendMessageError(err)
return
} else {
break
@ -145,7 +145,7 @@ func DeclarationService(files map[string]string) {
err = core.RefreshDoor(ticket)
if err != nil && i > 1 {
log.Printf("重试3次失败%v", err.Error())
support.SendMessageError(err)
util.SendMessageError(err)
return
} else {
break
@ -153,7 +153,7 @@ func DeclarationService(files map[string]string) {
}
log.Printf("[INFO] 刷新门禁成功")
support.SendSuccess("【成功】每日申报、刷新门禁")
util.SendSuccess("【成功】每日申报、刷新门禁")
}
//

@ -1,35 +0,0 @@
package support
import (
"fmt"
"luxshare-daily-report/message"
)
// SendMessageError 发送错误信息
func SendMessageError(err error) {
var body = message.Body{}
body.Title = "LuxShareReportServer"
body.Content = fmt.Sprintf("Error occurred: %s", err.Error())
send(body)
}
// SendSuccess 发送成功信息
func SendSuccess(content string) {
body := message.Body{
Title: "LuxShareReportServer",
Content: content,
}
send(body)
}
// send 发送
func send(body message.Body) {
m := message.GetSupport()
if m == nil {
return
}
enabled := message.Enabled()
if enabled {
m.Send(body)
}
}

@ -1,100 +1,35 @@
package util
import (
"encoding/json"
"fmt"
"io/ioutil"
"luxshare-daily-report/global"
"luxshare-daily-report/model"
"net/http"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"luxshare-daily-report/message"
)
//
// ListeningDirectory
// @Description: 监听目录
// @param path 目录路径
//
func ListeningDirectory(path string) {
//创建一个监控对象
watch, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watch.Close()
//添加要监控的对象,文件或文件夹
err = watch.Add(path)
if err != nil {
log.Fatal(err)
}
//我们另启一个goroutine来处理监控对象的事件
go func() {
for {
select {
case ev := <-watch.Events:
{
//判断事件发生的类型如下5种
// Create 创建
// Write 写入
// Remove 删除
// Rename 重命名
// Chmod 修改权限
if ev.Op&fsnotify.Create == fsnotify.Create {
log.Println("创建文件 : ", ev.Name)
}
if ev.Op&fsnotify.Write == fsnotify.Write {
log.Println("写入文件 : ", ev.Name)
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("删除文件 : ", ev.Name)
}
if ev.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("重命名文件 : ", ev.Name)
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
log.Println("修改权限 : ", ev.Name)
}
}
case err := <-watch.Errors:
{
log.Println("error : ", err)
return
}
}
}
}()
//循环
select {}
// SendMessageError 发送错误信息
func SendMessageError(err error) {
var body = message.Body{}
body.Title = "LuxShareReportServer"
body.Content = fmt.Sprintf("Error occurred: %s", err.Error())
send(body)
}
//
// Send
// @Description: bark通知
// @param msg
//
func Send(msg string) {
favicon := "https://m.luxshare-ict.com/favicon.ico"
if global.GLO_CONFIG.BarkUrl == "" {
log.Printf("[ERROR] bark url is empty!!!")
return
}
getUrl := fmt.Sprintf("%s/KSAT MRSB Serve/%s?icon=%s", global.GLO_CONFIG.BarkUrl, msg, favicon)
resp, err := http.Get(getUrl)
defer resp.Body.Close()
if err != nil {
log.Printf("[ERROR] Send to bark err: %v", err)
return
// SendSuccess 发送成功信息
func SendSuccess(content string) {
body := message.Body{
Title: "LuxShareReportServer",
Content: content,
}
send(body)
}
body, _ := ioutil.ReadAll(resp.Body)
var barkResp model.BarkResp
if err = json.Unmarshal(body, &barkResp); err != nil {
log.Printf("[ERROR] bark unmarshal err: %v", err)
// send 发送
func send(body message.Body) {
m := message.GetSupport()
if m == nil {
return
}
log.Printf("[INFO] Send to bark success")
enabled := message.Enabled()
if enabled {
m.Send(body)
}
}

Loading…
Cancel
Save