Skip to content

Commit

Permalink
Merge pull request #10 from shanghaobo/dev #9
Browse files Browse the repository at this point in the history
接口传参支持自定义图标声音和持续时间 #9
  • Loading branch information
shanghaobo authored Aug 30, 2023
2 parents 49d0c1b + 7d41b15 commit ad7856f
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 10 deletions.
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
res = requests.get(f"http://127.0.0.1:19000/api/toast?msg={msg}&title={title}")
print(res.json())
```
详情可见 [接口文档](#接口文档)
5. 可右击托盘图标,点击配置文件修改端口号
6. 可打开web界面查看历史消息记录

Expand All @@ -74,6 +75,66 @@

![](images/demo1.png)

## 接口文档

#### 发送通知

- API: `http://hostname:port/api/toast`
- 请求方式:`GET`
- 请求参数

| 参数名 | 默认值 | 解释 |
|----------|----------|--------------------------------|
| title | 消息通知 | 通知标题 |
| msg | 这是一条测试消息 | 通知内容 | |
| remark || 备注 | |
| icon | 0 | 图标索引,配合配置文件使用 |
| duration | short | 持续时长,short-短,大概6long-长,大概25|
| audio | default | 声音 |

其中`icon`需修改配置文件,参考配置如下:
```yaml
toast:
icons:
- logo.png
- logo2.png
```
此时传`icon=1`表示使用`logo2.png`图标,图标图片需放在`config.yml`同级的`images`目录内

<details>
<summary>audio的参数可选值如下:</summary>

|| 描述 |
|----------------|----------------|
| default | Default |
| im | IM |
| mail | Mail |
| reminder | Reminder |
| sms | SMS |
| loopingalarm | LoopingAlarm |
| loopingalarm2 | LoopingAlarm2 |
| loopingalarm3 | LoopingAlarm3 |
| loopingalarm4 | LoopingAlarm4 |
| loopingalarm5 | LoopingAlarm5 |
| loopingalarm6 | LoopingAlarm6 |
| loopingalarm7 | LoopingAlarm7 |
| loopingalarm8 | LoopingAlarm8 |
| loopingalarm9 | LoopingAlarm9 |
| loopingalarm10 | LoopingAlarm10 |
| loopingcall | LoopingCall |
| loopingcall2 | LoopingCall2 |
| loopingcall3 | LoopingCall3 |
| loopingcall4 | LoopingCall4 |
| loopingcall5 | LoopingCall5 |
| loopingcall6 | LoopingCall6 |
| loopingcall7 | LoopingCall7 |
| loopingcall8 | LoopingCall8 |
| loopingcall9 | LoopingCall9 |
| loopingcall10 | LoopingCall10 |

</details>



## 开启frp内网穿透

Expand Down Expand Up @@ -126,4 +187,23 @@ frp:

#### 测试

浏览器打开`http://123.123.1.2:19001/api/toast?msg=哈喽` 如果windows通知出现证明开启成功(ip和端口号替换为自己的)
浏览器打开`http://123.123.1.2:19001/api/toast?msg=哈喽` 如果windows通知出现证明开启成功(ip和端口号替换为自己的)


## 其他

#### 完整配置参考

```yaml
port: 19000
toast:
icons:
- logo.png
- logo2.png
frp:
enable: 0
server_addr: 127.0.0.1
server_port: 7000
token: httpwinnotice123456
remote_port: 19001
```
6 changes: 5 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
func ToastApi(c *gin.Context) {
msg := c.DefaultQuery("msg", constant.DefaultMsg)
title := c.DefaultQuery("title", constant.DefaultTitle)
icon := c.DefaultQuery("icon", constant.DefaultIcon)
duration := c.DefaultQuery("duration", string(constant.DefaultDuration))
audio := c.DefaultQuery("audio", string(constant.DefaultAudio))

remark := c.Query("remark")

data := model.Msg{
Expand All @@ -27,7 +31,7 @@ func ToastApi(c *gin.Context) {
return
}
go func() {
err := notice.Notice(msg, title)
err := notice.Notice(msg, title, icon, duration, audio)
if err != nil {
model.UpdateMsgStatus(data.ID, 9)
} else {
Expand Down
7 changes: 5 additions & 2 deletions utils/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ const (
const AppID = "HttpWinNotice"

const (
DefaultTitle = "消息通知"
DefaultMsg = "这是一条测试消息"
DefaultTitle = "消息通知"
DefaultMsg = "这是一条测试消息"
DefaultIcon = "0"
DefaultDuration = "short"
DefaultAudio = "default"
)

const DefaultWebPageSize = 10
Expand Down
90 changes: 87 additions & 3 deletions utils/notice/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,103 @@ import (
"github.com/go-toast/toast"
"http-win-notice/utils/constant"
"http-win-notice/utils/setting"
"path"
"strconv"
)

func Notice(msg, title string) error {
func notificationSetAttr(n *toast.Notification, duration string, audio string) {
switch duration {
case "long":
n.Duration = toast.Long
case "short":
n.Duration = toast.Short
default:
n.Duration = toast.Short
}

switch audio {
case "default":
n.Audio = toast.Default
case "im":
n.Audio = toast.IM
case "mail":
n.Audio = toast.Mail
case "reminder":
n.Audio = toast.Reminder
case "sms":
n.Audio = toast.SMS
case "loopingalarm":
n.Audio = toast.LoopingAlarm
case "loopingalarm2":
n.Audio = toast.LoopingAlarm2
case "loopingalarm3":
n.Audio = toast.LoopingAlarm3
case "loopingalarm4":
n.Audio = toast.LoopingAlarm4
case "loopingalarm5":
n.Audio = toast.LoopingAlarm5
case "loopingalarm6":
n.Audio = toast.LoopingAlarm6
case "loopingalarm7":
n.Audio = toast.LoopingAlarm7
case "loopingalarm8":
n.Audio = toast.LoopingAlarm8
case "loopingalarm9":
n.Audio = toast.LoopingAlarm9
case "loopingalarm10":
n.Audio = toast.LoopingAlarm10
case "loopingcall":
n.Audio = toast.LoopingCall
case "loopingcall2":
n.Audio = toast.LoopingCall2
case "loopingcall3":
n.Audio = toast.LoopingCall3
case "loopingcall4":
n.Audio = toast.LoopingCall4
case "loopingcall5":
n.Audio = toast.LoopingCall5
case "loopingcall6":
n.Audio = toast.LoopingCall6
case "loopingcall7":
n.Audio = toast.LoopingCall7
case "loopingcall8":
n.Audio = toast.LoopingCall8
case "loopingcall9":
n.Audio = toast.LoopingCall9
case "loopingcall10":
n.Audio = toast.LoopingCall10
case "silent":
n.Audio = toast.Silent
default:
n.Audio = toast.Default
}

}

func Notice(msg, title, icon, duration, audio string) error {
iconIndex, err := strconv.Atoi(icon)
if err != nil {
iconIndex = 0
}

var iconPath string
if len(setting.Config.Toast.Icons) <= iconIndex {
iconPath = path.Join(setting.ImagesDir, "logo.png")
} else {
iconPath = path.Join(setting.ImagesDir, setting.Config.Toast.Icons[iconIndex])
}
notification := toast.Notification{
AppID: constant.AppID,
Title: title,
Message: msg,
Icon: setting.LogoPath,
Icon: iconPath,
Actions: []toast.Action{
{"protocol", "查看", setting.HomeUrl()},
},
}
err := notification.Push()
notificationSetAttr(&notification, duration, audio)
//fmt.Println("n=", notification)
err = notification.Push()
return err
//if err != nil {
// panic(err)
Expand Down
28 changes: 25 additions & 3 deletions utils/setting/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ var Config ConfigType
var LogPath string
var DbPath string
var LogoPath string
var ImagesDir string

type ConfigType struct {
Port int `yaml:"port"`
Frp FrpType `yaml:"frp"`
Port int `yaml:"port"`
Toast ToastType `yaml:"toast"`
Frp FrpType `yaml:"frp"`
}

type ToastType struct {
Icons []string `yaml:"icons"`
}

type FrpType struct {
Expand All @@ -39,10 +45,20 @@ func init() {
InitConfig()
LogPath = path.Join(utils.RootDir, "log.log")
DbPath = path.Join(utils.RootDir, constant.DbFile)
LogoPath = path.Join(utils.RootDir, "logo.png")

ImagesDir = path.Join(utils.RootDir, "images")
if _, err := os.Stat(ImagesDir); os.IsNotExist(err) {
err = os.Mkdir(ImagesDir, os.ModeDir)
if err != nil {
log.Fatalln(err)
}
}

LogoPath = path.Join(ImagesDir, "logo.png")
if _, err := os.Stat(LogoPath); os.IsNotExist(err) {
b64.WriteImgFileByBase64(constant.LogoImgBase64, LogoPath)
}

}

func initConfigFile(ConfigPath string) {
Expand All @@ -57,6 +73,12 @@ func initConfigFile(ConfigPath string) {
}
Config.Frp = Frp

icons := []string{"logo.png"}
Toast := ToastType{
Icons: icons,
}
Config.Toast = Toast

updatedData, err := yaml.Marshal(Config)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit ad7856f

Please sign in to comment.