random file

master
dustoair 3 years ago
parent 331b7aa0ef
commit 095c36fbac

@ -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"))
}

@ -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")

@ -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()
}

@ -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))
}
Loading…
Cancel
Save