From 2f3a5a0a5956d382574a91c96a7a257b00911e15 Mon Sep 17 00:00:00 2001 From: hoteas Date: Sat, 8 Oct 2022 16:52:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A3=81=E7=BA=B8=E5=8F=8A?= =?UTF-8?q?=E5=A3=81=E7=BA=B8=E5=9C=B0=E5=9D=80=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application.go | 4 +++- code.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/application.go b/application.go index 4628731..9657ddf 100644 --- a/application.go +++ b/application.go @@ -683,7 +683,9 @@ func setMakeCodeListener(name string, appIns *Application) { if context.RouterString[1] == "hotime" && context.RouterString[2] == "config" { return isFinished } - + if context.RouterString[1] == "hotime" && context.RouterString[2] == "wallpaper" { + return isFinished + } if context.Session(codeIns.FileConfig.GetString("table")+"_id").Data == nil { context.Display(2, "你还没有登录") return true diff --git a/code.go b/code.go index 197764b..9cf8154 100644 --- a/code.go +++ b/code.go @@ -6,6 +6,7 @@ import ( "github.com/360EntSecGroup-Skylar/excelize" "io" "io/ioutil" + "net/http" "os" "strings" "time" @@ -907,6 +908,53 @@ var TptProject = Proj{ that.Display(0, conf) }, + //壁纸 + "wallpaper": func(that *Context) { + random := ObjToCeilInt(that.Req.FormValue("random")) //壁纸随机,0为默认,1,为随机 + + tp := ObjToCeilInt(that.Req.FormValue("type")) //返回类型,0,json,1,302跳转到图片 + + images := that.Application.Cache("wallpaper").ToSlice() + if images == nil || len(images) == 0 { + + url := "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=7&mkt=zh-CN" + res, e := http.Get(url) + if e != nil { + that.Display(4, "无法取得数据0") + return + } + defer res.Body.Close() + b, err := ioutil.ReadAll(res.Body) + if err != nil { + that.Display(4, "无法取得数据1") + return + } + w := ObjToMap(string(b)) + if len(w) == 0 { + that.Display(4, "无法取得数据2") + return + } + + images = w.GetSlice("images") + that.Application.Cache("wallpaper", images) + } + if random == 1 { + random = RandX(0, len(images)-1) + } + + img := images.GetMap(random) + if !strings.Contains(img.GetString("url"), "http") { + img["url"] = "https://cn.bing.com" + img.GetString("url") + } + if tp == 0 { + that.Display(0, img) + return + } + + that.Resp.Header().Set("Location", img.GetString("url")) + that.Resp.WriteHeader(302) + + }, }, }