diff --git a/handle/replyImage.go b/handle/replyImage.go index 09e7263..0814a2a 100644 --- a/handle/replyImage.go +++ b/handle/replyImage.go @@ -1,12 +1,12 @@ package handle import ( - "WechatGateWay/global" "WechatGateWay/third_part" "WechatGateWay/utils" "net/http" ) func replyImage(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) { - third_part.SendPicMid(msgContent.FromUsername, utils.RandMapValue(global.ImageMap)) + //third_part.SendPicMid(msgContent.FromUsername, utils.RandMapValue(global.ImageMap)) + third_part.SendPicFile(msgContent.FromUsername, "/app/data/"+utils.RandomFile("/app/data")) } diff --git a/main.go b/main.go index 3387ad1..f53d389 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,14 @@ import ( "github.com/robfig/cron" "github.com/spf13/viper" "log" + "math/rand" "net/http" "os" + "time" ) func init() { + rand.Seed(time.Now().UnixNano()) // 读取配置文件 workDir, _ := os.Getwd() viper.SetConfigName("application") diff --git a/utils/file.go b/utils/file.go new file mode 100644 index 0000000..666a329 --- /dev/null +++ b/utils/file.go @@ -0,0 +1,17 @@ +package utils + +import ( + "log" + "math/rand" + "os" +) + +func RandomFile(dirname string) string { + fileList, err := os.ReadDir(dirname) + if err != nil { + log.Println(err) + return "" + } + return fileList[rand.Intn(len(fileList))].Name() + +} diff --git a/utils/file_test.go b/utils/file_test.go new file mode 100644 index 0000000..6d334aa --- /dev/null +++ b/utils/file_test.go @@ -0,0 +1,12 @@ +package utils + +import ( + "fmt" + "testing" +) + +func TestRandomFile(tt *testing.T) { + dirname := "D:\\SynologyDrive\\SynologyDrive\\Pictures\\照片\\gg\\2022" + fmt.Println(RandomFile(dirname)) + +}