diff --git a/conf.ini b/conf.ini index 1c62f6b..f7b5006 100644 --- a/conf.ini +++ b/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 如何使用请自行百度。 diff --git a/jd_seckill/seckill.go b/jd_seckill/seckill.go index 443650b..d804327 100644 --- a/jd_seckill/seckill.go +++ b/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 } } \ No newline at end of file diff --git a/service/email.go b/service/email.go index 8bff65b..2a64907 100644 --- a/service/email.go +++ b/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 } \ No newline at end of file diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..83b17af --- /dev/null +++ b/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 +} \ No newline at end of file diff --git a/service/wechat.go b/service/wechat.go new file mode 100644 index 0000000..a60e630 --- /dev/null +++ b/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 +} \ No newline at end of file