소스 검색

feat:修改

wangchuanjin 2 년 전
부모
커밋
1a6dfc5fb9
3개의 변경된 파일83개의 추가작업 그리고 5개의 파일을 삭제
  1. 7 3
      src/github.com/dchest/captcha/captcha_test.go
  2. 3 2
      src/github.com/dchest/captcha/image.go
  3. 73 0
      src/qfw/util/jylog/jylog.go

+ 7 - 3
src/github.com/dchest/captcha/captcha_test.go

@@ -6,6 +6,7 @@ package captcha
 
 import (
 	"bytes"
+	"fmt"
 	"os"
 	"testing"
 )
@@ -41,10 +42,13 @@ func TestReload(t *testing.T) {
 
 //
 func TestCap(t *testing.T) {
-	id := NewLen(4)
+	for i := 1; i <= 20; i++ {
 
-	fi, _ := os.OpenFile("C:\\Users\\admin\\Desktop\\12.png", os.O_CREATE|os.O_SYNC|os.O_RDWR, 0x666)
-	WriteImage(fi, id, 90, 30)
+		id := NewLen(4)
+
+		fi, _ := os.OpenFile(fmt.Sprintf("E:\\%d.png", i), os.O_CREATE|os.O_SYNC|os.O_RDWR|os.O_APPEND, 0x666)
+		WriteImage(fi, id, 90, 30)
+	}
 }
 
 func TestRandomDigits(t *testing.T) {

+ 3 - 2
src/github.com/dchest/captcha/image.go

@@ -50,13 +50,14 @@ func NewImage(id string, digits []byte, width, height int) *Image {
 	} else {
 		border = width / 5
 	}
-	x := m.rng.Int(border, maxx-border)
+	x := m.rng.Int(border, (maxx-border)/len(digits)+border)
 	y := m.rng.Int(border, maxy-border)
 	m.numWidth = m.numWidth + 4 //补值
+	margin := (width-x-(m.numWidth+m.dotSize)*len(digits))/len(digits) - 1
 	// Draw digits.
 	for _, n := range digits {
 		m.drawDigit(font[n], x, y)
-		x += m.numWidth + m.dotSize
+		x += m.numWidth + m.dotSize + margin
 	}
 	// Draw strike-through line.
 

+ 73 - 0
src/qfw/util/jylog/jylog.go

@@ -0,0 +1,73 @@
+package jylog
+
+/**
+日志文件自动切换,默认保留15天内日志
+**/
+
+import (
+	"log"
+	"os"
+	"path/filepath"
+	"regexp"
+	"time"
+
+	"github.com/go-xweb/xweb"
+	"github.com/robfig/cron"
+)
+
+//日志格式
+var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
+
+//当前日志文件句柄
+var LogFile *os.File
+
+//时间格式
+var FMT = "2006_01_02_15_04_05"
+
+//日志目录
+var LogPath = "./jylog"
+
+func init() {
+	os.Mkdir(LogPath, os.ModePerm)
+	//默认保留15天内的日志,-1为永久保留
+	initLog(15)
+}
+
+func initLog(saveDay int) {
+	go logfile()
+	task := cron.New()
+	task.Start()
+	task.AddFunc("0 0 0 * * ?", func() {
+		go logfile()
+		time.Sleep(50 * time.Second)
+		if saveDay > 0 {
+			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
+				str := fileReg.FindStringSubmatch(info.Name())
+				if len(str) == 2 {
+					t, er := time.ParseInLocation(FMT, str[1], time.Local)
+					if er == nil {
+						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
+							log.Println("delete log file:", path, os.Remove(path))
+						}
+					}
+				}
+				return nil
+			})
+		}
+	})
+}
+
+//创建并切换输出文件
+func logfile() {
+	now := time.Now().Format(FMT)
+	file, _ := os.Create(LogPath + "/" + now + ".log")
+	log.SetOutput(file)
+	xweb.RootApp().Logger.SetOutput(file)
+	go func(file *os.File) {
+		time.Sleep(5 * time.Second)
+		if LogFile != nil {
+			LogFile.Close()
+		}
+		LogFile = file
+	}(file)
+}