Browse Source

添加Server酱推送支持

main
ztino 4 years ago
parent
commit
c4f2db7638
  1. 6
      conf.ini
  2. 22
      jd_seckill/seckill.go
  3. 10
      service/email.go
  4. 21
      service/service.go
  5. 37
      service/wechat.go

6
conf.ini

@ -27,10 +27,12 @@ payment_pwd =
[messenger]
# 开启推送服务
enable = false
# 目前支持smtp邮箱推送
# 目前支持smtp邮箱推送和Server酱推送服务,选值smtp,wechat
type = none
# 消息接收人
# 邮箱推送消息接收人
email =
#Server酱推送key,当type为wechat有效
server_chan_sckey =
#smtp配置
# 开启smtp消息推送必须填入 email_user、email_pwd,email_host,port 如何使用请自行百度。

22
jd_seckill/seckill.go

@ -148,7 +148,7 @@ func (this *Seckill) SubmitSeckillOrder() bool {
paymentPwd:=this.conf.MustValue("account","payment_pwd","")
initInfo,err:=this.SeckillInitInfo()
if err!=nil {
log.Println(fmt.Sprintf("抢购失败,无法获取生成订单的基本信息,接口返回:【%s】",err))
log.Println(fmt.Sprintf("抢购失败,无法获取生成订单的基本信息,接口返回:【%s】",err.Error()))
return false
}
address:=gjson.Get(initInfo,"addressList").Array()
@ -210,18 +210,12 @@ func (this *Seckill) SubmitSeckillOrder() bool {
resp,body,err:=req.SetUrl("https://marathon.jd.com/seckillnew/orderService/pc/submitOrder.action?skuId="+skuId).SetMethod("post").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("抢购失败,网络错误")
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,网络错误")
}
_=service.SendMessage(this.conf,"茅台抢购通知","抢购失败,网络错误")
return false
}
if !gjson.Valid(body) {
log.Println("抢购失败,返回信息:"+body)
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,返回信息:"+body)
}
_=service.SendMessage(this.conf,"茅台抢购通知","抢购失败,返回信息:"+body)
return false
}
if gjson.Get(body,"success").Bool() {
@ -229,17 +223,11 @@ func (this *Seckill) SubmitSeckillOrder() bool {
totalMoney:=gjson.Get(body,"totalMoney").String()
payUrl:="https:"+gjson.Get(body,"pcUrl").String()
log.Println(fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知",fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
}
_=service.SendMessage(this.conf,"茅台抢购通知",fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
return true
}else{
log.Println("抢购失败,返回信息:"+body)
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,返回信息:"+body)
}
_=service.SendMessage(this.conf,"茅台抢购通知","抢购失败,返回信息:"+body)
return false
}
}

10
service/email.go

@ -3,6 +3,7 @@ package service
import (
"github.com/unknwon/goconfig"
"gopkg.in/gomail.v2"
"log"
"strconv"
)
@ -13,7 +14,7 @@ type Email struct {
pass string
}
func NerEmail(conf *goconfig.ConfigFile) *Email {
func NewEmail(conf *goconfig.ConfigFile) *Email {
host:=conf.MustValue("smtp","email_host","")
port:=conf.MustValue("smtp","port","")
user:=conf.MustValue("smtp","email_user","")
@ -21,7 +22,7 @@ func NerEmail(conf *goconfig.ConfigFile) *Email {
return &Email{host: host,port: port,user: user,pass: pass}
}
func (this *Email) SendMail(mailTo []string,subject,body string) error {
func (this *Email) Send(mailTo []string,subject,body string) error {
port, _ := strconv.Atoi(this.port)
m:=gomail.NewMessage()
m.SetHeader("From", "<" + this.user + ">")
@ -30,5 +31,10 @@ func (this *Email) SendMail(mailTo []string,subject,body string) error {
m.SetBody("text/html",body)
d := gomail.NewDialer(this.host,port,this.user,this.pass)
err:=d.DialAndSend(m)
if err!=nil {
log.Println("邮件发送失败,返回错误:"+err.Error())
}else{
log.Println("邮件发送成功")
}
return err
}

21
service/service.go

@ -0,0 +1,21 @@
package service
import "github.com/unknwon/goconfig"
func SendMessage(conf *goconfig.ConfigFile,title,msg string) error {
if conf.MustValue("messenger","enable","false")=="true" {
//邮件发送
if conf.MustValue("messenger","type","none")=="smtp" {
email:=NewEmail(conf)
err:=email.Send([]string{conf.MustValue("messenger","email","")},title,msg)
return err
}
//Server酱推送
if conf.MustValue("messenger","type","none")=="wechat" {
wechat:=NewWechat(conf)
err:=wechat.Send(title,msg)
return err
}
}
return nil
}

37
service/wechat.go

@ -0,0 +1,37 @@
package service
import (
"errors"
"fmt"
"github.com/Albert-Zhan/httpc"
"github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"log"
"net/http"
)
type Wechat struct {
conf *goconfig.ConfigFile
}
func NewWechat(conf *goconfig.ConfigFile) *Wechat {
return &Wechat{conf: conf}
}
func (this *Wechat) Send(title,msg string) error {
client:=httpc.NewHttpClient()
req:=httpc.NewRequest(client)
url:=fmt.Sprintf("http://sc.ftqq.com/%s.send",this.conf.MustValue("messenger","server_chan_sckey",""))
req.SetHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36")
resp,body,err:=req.SetUrl(url+"?text="+title+"&desp="+msg).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("微信推送失败,网络错误")
return errors.New("微信推送失败,网络错误")
}
if gjson.Get(body,"errno").Int()!=0 {
log.Println("微信推送失败,返回错误:"+gjson.Get(body,"errmsg").String())
return errors.New("微信推送失败,返回错误:"+gjson.Get(body,"errmsg").String())
}
log.Println("微信推送成功")
return nil
}
Loading…
Cancel
Save