Beego 自动切换Linux壁纸
2019-05-24 本文已影响0人
霡霂976447044
controllers/wallpaper.go
package controllers
import (
"github.com/astaxie/beego"
"fmt"
"strings"
"io/ioutil"
"os/exec"
"os"
)
type MsgBean struct {
Status string
Message string
}
type WallPaperController struct {
beego.Controller
}
func exec_shell(cmd string) MsgBean(output []byte, err error) {
arr := strings.Split(cmd, " ")
command := exec.Command(arr[0], arr[1:len(arr)]...)
output, err = command.Output()
return
}
func (c *WallPaperController) Get() {
// read config file 从配置文件读取照片文件夹
var imgPath string = "/home/alonebo/图片/background/"
HOME := os.Getenv("HOME")
content, err := ioutil.ReadFile(HOME + "/.gowalpaper.conf")
parmBg := c.GetString("bg")
monitorNum := c.GetString("monnum")
if monitorNum == "" {
monitorNum = "0" // 如果没传递了显示器参数 那么默认设置为0
}
if err != nil {
errs := fmt.Sprintf("%s", err)
c.Data["json"] = MsgBean{"error", string(errs)}
c.ServeJSON()
return
}
contents := strings.Replace(string(content), "\n", "", -1)
if contents != "" {
fmt.Println("content = " + contents)
imgPath = contents
}
// xfce desktop's change walpaper command
cmd := "/usr/bin/xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor"+monitorNum+"/workspace0/last-image -s " + imgPath + "/"
bgName := "pubg2.jpg"
if parmBg != "" {
bgName = parmBg
}
cmd += bgName
fmt.Println("cmd = " + cmd)
// exec switch walpaper
output, err := exec_shell(cmd)
if err != nil {
fmt.Println("Execute Command failed:" + err.Error())
c.Data["json"] = MsgBean{"error", string(output)}
return
}
fmt.Println("Execute Command finished. result->", string(output))
{
// test code
var msg MsgBean
msg.Status = "ok"
msg.Message = "ok"
var msg2 *MsgBean = new(MsgBean)
msg2.Status = "ok"
msg2.Message = "ok"
// msg3 := &MsgBean{"ok", "ok"}
// c.Data["json"] = msg
}
// if no err omit, return success json message
c.Data["json"] = MsgBean{"ok", "success"}
c.ServeJSON()
}
主要逻辑是从home目录读取配置文件,得到切换需要的壁纸的目录。调用xfce命令进行切换壁纸,这里默认是单屏幕。
controllers/task.go
package controllers
import (
"fmt"
"github.com/astaxie/beego"
// "strings"
// "bytes"
"github.com/astaxie/beego/toolbox"
// "time"
"io/ioutil"
"math/rand"
"net/http"
)
type TaskController struct {
beego.Controller
}
func switchTask() error {
fmt.Println("switch tasks")
// 读取照片文件夹的照片名 切换
files, _ := ioutil.ReadDir("/home/alonebo/图片/background")
var fileList []string
for _, file := range files {
if file.IsDir() {
continue
} else {
fileList = append(fileList, file.Name())
}
}
fmt.Println(fileList)
randBgName := fileList[rand.Intn(len(fileList))]
randBgName2 := fileList[rand.Intn(len(fileList))]
resp, err := http.Get("http://localhost:8080/hello?bg=" + randBgName)
resp2, err2 := http.Get("http://localhost:8080/hello?monnum=1&bg=" + randBgName2)
if err != nil {
fmt.Println(err)
return err
}
if err2 != nil {
fmt.Println(err2)
return err2
}
defer resp.Body.Close()
defer resp2.Body.Close()
return nil
}
// 停止任务
func (c *TaskController) Get() {
stop := c.GetString("stop")
fmt.Println("stop =" + stop)
defer c.ServeJSON()
if stop == "true" {
fmt.Println("stop task")
toolbox.StopTask()
c.Data["json"] = MsgBean{"ok", "stop task success"}
return
}
tk := toolbox.NewTask("myTask", "0/5 * * * * *", switchTask) // 设置5s触发一次任务
toolbox.AddTask("myTask", tk)
toolbox.StartTask()
c.Data["json"] = MsgBean{"ok", "start task success"}
}
开启一个切换壁纸的任务,使用beego的自带的任务模块。不断的调用自身的切换壁纸的http请求。
需要注意的是,要关闭文件描述符。
前端编写
<template>
<div>
<img v-for="(item, index) in fileList" v-bind:src="'//localhost:8081/static/'+item"
v-bind:key="index" @click="choice($event, item, index)" :class="{select: cur==index}"
>
<md-button class="md-fab md-fixed md-fab-bottom-right" @click="change()">
<md-icon>done</md-icon>
</md-button>
</div>
</template>
<script>
/* eslint-disable */
import axios from "axios";
export default {
name: "WallPaper",
data: ()=>{
return {
fileList: ["pubg3.jpg", "pubg2.jpg"],
cur: -1,
curImg: ''
}
},
created() {
axios.get("/wallpaper/").then(resp => {
console.log(resp.data);
this.fileList = resp.data
});
},
methods: {
choice(e, curImg, index) {
this.cur = index
this.curImg = curImg
},
change() {
if (this.cur == -1) {
alert("你没有选择壁纸")
}else {
axios.get("wallpaper/wallpaper?bg="+this.curImg).then(resp=>{
console.log(resp)
})
}
}
}
};
</script>
<style scoped>
div {
padding: 0;
margin-left: 10%;
}
img {
width: 30%;
height: 300px;
}
.select {
border: 5px solid red
}
</style>
效果
