Sfoglia il codice sorgente

new:1.z增加gfile模块

QiuSW 10 mesi fa
parent
commit
f66a4c5819
9 ha cambiato i file con 156 aggiunte e 321 eliminazioni
  1. 0 135
      autoit/mouse.go
  2. 0 175
      autoit/process.go
  3. 29 0
      chis/record.go
  4. 1 0
      chis/util.go
  5. 31 0
      format/struct.go
  6. 36 0
      gfile/gfile.go
  7. 11 10
      gfyne/date_picker.go
  8. 1 1
      grpa/autoit.go
  9. 47 0
      office/wps.go

+ 0 - 135
autoit/mouse.go

@@ -1,135 +0,0 @@
-//go:build windows && amd64
-// +build windows,amd64
-
-package goautoit
-
-import (
-	"log"
-	"unsafe"
-)
-
-// MouseClickDrag -- Perform a mouse click and drag operation.
-func MouseClickDrag(button string, x1, y1, x2, y2 int, args ...interface{}) int {
-	var nSpeed int
-	var ok bool
-
-	if len(args) == 0 {
-		nSpeed = 10
-	} else if len(args) == 1 {
-		if nSpeed, ok = args[0].(int); !ok {
-			panic("nSpeed must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := mouseClickDrag.Call(strPtr(button), intPtr(x1), intPtr(x2), intPtr(y2), intPtr(nSpeed))
-	if int(ret) != 1 {
-		log.Print("failure!!!")
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// MouseDown -- Perform a mouse down event at the current mouse position.
-func MouseDown(args ...interface{}) int {
-	var button string
-	var ok bool
-
-	if len(args) == 0 {
-		button = DefaultMouseButton
-	} else if len(args) == 1 {
-		if button, ok = args[0].(string); !ok {
-			panic("nSpeed must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := mouseDown.Call(strPtr(button))
-	if int(ret) != 1 {
-		log.Print("failure!!!")
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// MouseUp -- Perform a mouse up event at the current mouse position.
-func MouseUp(args ...interface{}) int {
-	var button string
-	var ok bool
-
-	if len(args) == 0 {
-		button = DefaultMouseButton
-	} else if len(args) == 1 {
-		if button, ok = args[0].(string); !ok {
-			panic("nSpeed must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := mouseUp.Call(strPtr(button))
-	if int(ret) != 1 {
-		log.Print("failure!!!")
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// MouseGetCursor -- Returns the cursor ID Number for the current Mouse Cursor.
-func MouseGetCursor() int {
-	ret, _, lastErr := mouseGetCursor.Call()
-	if int(ret) == -1 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// MouseGetPos -- Retrieves the current position of the mouse cursor.
-func MouseGetPos() (int32, int32) {
-	var point = POINT{}
-	ret, _, lastErr := mouseGetPos.Call(uintptr(unsafe.Pointer(&point)))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return point.X, point.Y
-}
-
-// MouseMove -- Moves the mouse pointer.
-func MouseMove(x, y int, args ...interface{}) {
-	var nSpeed int
-	var ok bool
-
-	if len(args) == 0 {
-		nSpeed = -1
-	} else if len(args) == 1 {
-		if nSpeed, ok = args[0].(int); !ok {
-			panic("nSpeed must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := mouseMove.Call(intPtr(x), intPtr(y), intPtr(nSpeed))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-}
-
-// MouseWheel -- Moves the mouse wheel up or down.
-func MouseWheel(szDirection string, args ...interface{}) int {
-	var nClicks int
-	var ok bool
-
-	if len(args) == 0 {
-		nClicks = 1
-	} else if len(args) == 1 {
-		if nClicks, ok = args[0].(int); !ok {
-			panic("nClicks must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := mouseWheel.Call(strPtr(szDirection), intPtr(nClicks))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}

+ 0 - 175
autoit/process.go

@@ -1,175 +0,0 @@
-package goautoit
-
-import "log"
-
-// IsAdmin -- Checks if the current user has full administrator privileges.
-func IsAdmin() int {
-	ret, _, lastErr := isAdmin.Call()
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// ProcessClose -- Terminates a named process.
-func ProcessClose(process string) int {
-	ret, _, lastErr := processClose.Call(strPtr(process))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// ProcessExists -- Checks to see if a specified process exists.
-func ProcessExists(process string) int {
-	ret, _, lastErr := processExists.Call(strPtr(process))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// ProcessSetPriority -- Changes the priority of a process.
-func ProcessSetPriority(process string, priority int) int {
-	ret, _, lastErr := processSetPriority.Call(strPtr(process), intPtr(priority))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// ProcessWait -- Pauses script execution until a given process exists.
-func ProcessWait(process string, args ...interface{}) int {
-	var timeout int
-	var ok bool
-
-	if len(args) == 0 {
-		timeout = 0
-	} else if len(args) == 1 {
-		if timeout, ok = args[0].(int); !ok {
-			panic("timeout must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := processWait.Call(strPtr(process), intPtr(timeout))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// ProcessWaitClose -- Pauses script execution until a given process does not exist.
-func ProcessWaitClose(process string, args ...interface{}) int {
-	var timeout int
-	var ok bool
-
-	if len(args) == 0 {
-		timeout = 0
-	} else if len(args) == 1 {
-		if timeout, ok = args[0].(int); !ok {
-			panic("timeout must be a int")
-		}
-	} else {
-		panic("Error parameters")
-	}
-	ret, _, lastErr := processWaitClose.Call(strPtr(process), intPtr(timeout))
-	if ret == 0 {
-		log.Println(lastErr)
-	}
-	return int(ret)
-}
-
-// RunWait -- Runs an external program and pauses script execution until the program finishes.
-// flag 3(max) 6(min) 9(normal) 0(hide)
-func RunWait(szProgram string, args ...interface{}) int {
-	var szDir string
-	var flag int
-	var ok bool
-	if len(args) == 0 {
-		szDir = ""
-		flag = SWShowNormal
-	} else if len(args) == 1 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		flag = SWShowNormal
-	} else if len(args) == 2 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		if flag, ok = args[1].(int); !ok {
-			panic("flag must be a int")
-		}
-	} else {
-		panic("Too more parameter")
-	}
-	pid, _, lastErr := runWait.Call(strPtr(szProgram), strPtr(szDir), intPtr(flag))
-	// log.Println(pid)
-	if int(pid) == 0 {
-		log.Println(lastErr)
-	}
-	return int(pid)
-}
-
-// RunAs -- Runs an external program under the context of a different user.
-func RunAs(user, domain, password string, loginFlag int, szProgram string, args ...interface{}) int {
-	var szDir string
-	var flag int
-	var ok bool
-	if len(args) == 0 {
-		szDir = ""
-		flag = SWShowNormal
-	} else if len(args) == 1 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		flag = SWShowNormal
-	} else if len(args) == 2 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		if flag, ok = args[1].(int); !ok {
-			panic("flag must be a int")
-		}
-	} else {
-		panic("Too more parameter")
-	}
-	pid, _, lastErr := runAs.Call(strPtr(user), strPtr(domain), strPtr(password), intPtr(loginFlag), strPtr(szProgram), strPtr(szDir), intPtr(flag))
-	// log.Println(pid)
-	if int(pid) == 0 {
-		log.Println(lastErr)
-	}
-	return int(pid)
-}
-
-// RunAsWait -- Runs an external program under the context of a different user and pauses script execution until the program finishes.
-func RunAsWait(user, domain, password string, loginFlag int, szProgram string, args ...interface{}) int {
-	var szDir string
-	var flag int
-	var ok bool
-	if len(args) == 0 {
-		szDir = ""
-		flag = SWShowNormal
-	} else if len(args) == 1 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		flag = SWShowNormal
-	} else if len(args) == 2 {
-		if szDir, ok = args[0].(string); !ok {
-			panic("szDir must be a string")
-		}
-		if flag, ok = args[1].(int); !ok {
-			panic("flag must be a int")
-		}
-	} else {
-		panic("Too more parameter")
-	}
-	pid, _, lastErr := runAsWait.Call(strPtr(user), strPtr(domain), strPtr(password), intPtr(loginFlag), strPtr(szProgram), strPtr(szDir), intPtr(flag))
-	// log.Println(pid)
-	if int(pid) == 0 {
-		log.Println(lastErr)
-	}
-	return int(pid)
-}

+ 29 - 0
chis/record.go

@@ -37,3 +37,32 @@ func GetAgeFromIdCard(idCard string, increment int) (age int) {
 
 	return age
 }
+
+func CalcAgeFromIdCard(idCard string) (age int) {
+	age = 0
+	if len(idCard) != 18 {
+		return age
+	}
+	birthdayStr := idCard[6:14]
+
+	// 解析出生日期
+	birthday, err := time.Parse("20060102", birthdayStr)
+	if err != nil {
+		glog.XWarning(fmt.Sprint("time.Parse error : %v\n", err))
+		return
+	}
+	// 计算年龄,几年
+
+	today := time.Now()
+	age = today.Year() - birthday.Year() - 1
+
+	if today.Month() > birthday.Month() {
+		age += 1
+	} else if today.Month() == birthday.Month() && today.Day() > birthday.Day() {
+		age += 1
+	} else {
+		age += 0
+	}
+
+	return age
+}

+ 1 - 0
chis/util.go

@@ -0,0 +1 @@
+package chis

+ 31 - 0
format/struct.go

@@ -45,6 +45,7 @@ func MapToStruct(input map[string]interface{}, output interface{}) error {
 		columnName := tags["column"]
 
 		if value, ok := input[columnName]; ok {
+			//println(columnName)
 			fieldValue := outputValue.FieldByName(field.Name)
 			if fieldValue.CanSet() {
 				val := reflect.ValueOf(value)
@@ -63,6 +64,8 @@ func MapToStruct(input map[string]interface{}, output interface{}) error {
 						if floatValue, err := strconv.ParseFloat(fmt.Sprintf("%v", value), 64); err == nil {
 							fieldValue.SetFloat(floatValue)
 						}
+					default:
+						fieldValue.SetString(fmt.Sprintf("%v", value))
 					}
 				}
 			}
@@ -110,3 +113,31 @@ func ConvertNilToEmptyString(data interface{}) interface{} {
 
 	return data
 }
+
+func StructToMapReflect(data interface{}) map[string]interface{} {
+	val := reflect.ValueOf(data)
+	typ := reflect.TypeOf(data)
+
+	result := make(map[string]interface{})
+
+	// 迭代结构体的字段
+	for i := 0; i < val.NumField(); i++ {
+		field := typ.Field(i)
+
+		// 获取 JSON 标签
+		jsonTag := field.Tag.Get("json")
+		if jsonTag == "" {
+			jsonTag = field.Name // 如果没有 JSON 标签,使用字段名
+		} else {
+			// 如果标签中有逗号,取第一个部分
+			if commaIndex := strings.Index(jsonTag, ","); commaIndex != -1 {
+				jsonTag = jsonTag[:commaIndex]
+			}
+		}
+
+		fieldValue := val.Field(i).Interface()
+		result[jsonTag] = fieldValue
+	}
+
+	return result
+}

+ 36 - 0
gfile/gfile.go

@@ -0,0 +1,36 @@
+package gfile
+
+import (
+	"fmt"
+	"gbase/glog"
+	"io"
+	"os"
+)
+
+func CopyFile(src, dst string) (err error) {
+	f1, err := os.Open(src)
+	if err != nil {
+		glog.XWarning(fmt.Sprintf("Open %v error : %v\n", src, err))
+		return
+	}
+	defer f1.Close()
+
+	f2, err := os.Create(dst)
+	if err != nil {
+		glog.XWarning(fmt.Sprintf("Create %v error : %v\n", dst, err))
+		return
+	}
+	defer f2.Close()
+	_, err = io.Copy(f2, f1)
+	if err != nil {
+		glog.XWarning(fmt.Sprintf("Copy %v error : %v\n", dst, err))
+		return
+	}
+	srcInfo, err := f1.Stat()
+	if err != nil {
+		glog.XWarning(fmt.Sprintf("Stat %v error : %v\n", src, err))
+		return
+	}
+	os.Chmod(dst, srcInfo.Mode())
+	return nil
+}

+ 11 - 10
gfyne/date_picker.go

@@ -105,13 +105,13 @@ func adjustWeekday(d time.Weekday, weekStart time.Weekday) int {
 
 // offsets correspond to time.Weekday, hence starting on Monday
 var weekdayLabels = []*widget.Label{
-	widget.NewLabelWithStyle("Sun", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Mon", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Tue", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Wed", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Thu", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Fri", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
-	widget.NewLabelWithStyle("Sat", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周日", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周一", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周二", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周三", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周四", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周五", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
+	widget.NewLabelWithStyle("周六", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
 }
 
 func updateGrid(grid *fyne.Container, when time.Time, weekStart time.Weekday, updateWhen func(time.Time), updateSelects func(time.Time)) {
@@ -322,18 +322,19 @@ func NewDatePicker(when time.Time, weekStart time.Weekday, fn func(time.Time, bo
 
 	controlButtons := container.New(
 		layout.NewHBoxLayout(),
-		widget.NewButton("Now", func() {
+		widget.NewButton("现在", func() {
 			when = time.Now()
 
 			hourInput.SetText(when.Format("15"))
 			minuteInput.SetText(when.Format("04"))
 
 			updateSelects(when)
+			fn(when, true)
 		}),
-		widget.NewButton("Cancel", func() {
+		widget.NewButton("取消", func() {
 			fn(when, false)
 		}),
-		widget.NewButton("Ok", func() {
+		widget.NewButton("确认", func() {
 			fn(when, true)
 		}),
 	)

+ 1 - 1
autoit/goautoit.go → grpa/autoit.go

@@ -1,7 +1,7 @@
 //go:build windows && amd64
 // +build windows,amd64
 
-package goautoit
+package grpa
 
 import (
 	"log"

+ 47 - 0
office/wps.go

@@ -0,0 +1,47 @@
+package office
+
+import (
+	"github.com/go-ole/go-ole"
+	"github.com/go-ole/go-ole/oleutil"
+	"log"
+)
+
+func WPSWordSaveTo(wordFilePath, targetFilePath string, WdSaveAsFormat int) error {
+	defer func() {
+		if err := recover(); err != nil {
+			log.Printf("WPSWordSaveTo error: %v\n", err)
+		}
+	}()
+
+	ole.CoInitialize(0)
+	defer ole.CoUninitialize()
+
+	iunk, err := oleutil.CreateObject("kwps.Application")
+	if err != nil {
+		log.Printf("creating Word object error: %s", err)
+	}
+	defer iunk.Release()
+
+	word := iunk.MustQueryInterface(ole.IID_IDispatch)
+	defer word.Release()
+
+	oleutil.PutProperty(word, "DisplayAlerts", false)
+	oleutil.PutProperty(word, "Visible", false)
+	// opening then saving works due to the call to doc.Settings.SetUpdateFieldsOnOpen(true) above
+	docs := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
+	defer docs.Release()
+
+	doc := oleutil.MustCallMethod(docs, "Open", wordFilePath).ToIDispatch()
+	defer doc.Release()
+
+	/*
+		WdExportFormat
+		wdExportFormatPDF	17	将文档导出为 PDF 格式。
+		wdExportFormatXPS	18	将文档导出为 XML Paper Specification (XPS) 格式。
+	*/
+	oleutil.MustCallMethod(doc, "SaveAs", targetFilePath, WdSaveAsFormat)
+	oleutil.PutProperty(doc, "Saved", true)
+	oleutil.MustCallMethod(doc, "Close")
+	oleutil.MustCallMethod(word, "Quit")
+	return nil
+}