增加壁纸及壁纸地址缓存

This commit is contained in:
hoteas 2022-10-08 16:52:42 +08:00
parent c2468a7389
commit 2f3a5a0a59
2 changed files with 51 additions and 1 deletions

View File

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

48
code.go
View File

@ -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")) //返回类型0json1302跳转到图片
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)
},
},
}