diff --git a/config/go-fly.sql b/config/go-fly.sql index b6e77d1..306234e 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -115,7 +115,11 @@ INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(SMTP地址)', 'NoticeEmailSmtp', '')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(邮箱)', 'NoticeEmailAddress', '')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(密码)', 'NoticeEmailPassword', '')| - +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, 'App个推(Token)', 'GetuiToken', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, 'App个推(AppID)', 'GetuiAppID', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, 'App个推(AppKey)', 'GetuiAppKey', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, 'App个推(AppSecret)', 'GetuiAppSecret', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, 'App个推(AppMasterSecret)', 'GetuiMasterSecret', '')| DROP TABLE IF EXISTS `about`| CREATE TABLE `about` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, diff --git a/controller/response.go b/controller/response.go index 56f390f..ebe4b10 100644 --- a/controller/response.go +++ b/controller/response.go @@ -22,3 +22,8 @@ type VisitorOnline struct { Avator string `json:"avator"` LastMessage string `json:"last_message"` } +type GetuiResponse struct { + Code float64 `json:"code"` + Msg string `json:"msg"` + Data map[string]interface{} `json:"data"` +} diff --git a/controller/shout.go b/controller/shout.go index 16a00b1..3c8d9b1 100644 --- a/controller/shout.go +++ b/controller/shout.go @@ -1,11 +1,13 @@ package controller import ( + "encoding/json" "fmt" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "log" "strconv" + "time" ) func SendServerJiang(content string) string { @@ -29,8 +31,77 @@ func SendNoticeEmail(username, msg string) { if smtp == "" || email == "" || password == "" { return } - err:=tools.SendSmtp(smtp, email, password, []string{email}, "[通知]"+username, msg) - if err!=nil{ + err := tools.SendSmtp(smtp, email, password, []string{email}, "[通知]"+username, msg) + if err != nil { log.Println(err) } } +func SendAppGetuiPush(msg string) { + token := models.FindConfig("GetuiToken") + if token == "" { + token = getGetuiToken() + if token == "" { + return + } + } + appid := models.FindConfig("GetuiAppID") + format := ` +{ + "request_id":"%s", + "settings":{ + "ttl":3600000 + }, + "audience":{ + "cid":[ + "%s" + ] + }, + "push_message":{ + "notification":{ + "title":"请填写通知标题", + "body":"请填写通知内容", + "click_type":"url", + "url":"https//:xxx" + } + } +} +` + req := fmt.Sprintf(format, tools.Uuid(), "0507db8a6769af494f22e17a6177c29a") + url := "https://restapi.getui.com/v2/" + appid + "/push/single/cid" + headers := make(map[string]string) + headers["Content-Type"] = "application/json;charset=utf-8" + headers["token"] = token + res, err := tools.PostHeader(url, []byte(req), headers) + log.Println(url, req, err, res) +} +func getGetuiToken() string { + appid := models.FindConfig("GetuiAppID") + appkey := models.FindConfig("GetuiAppKey") + //appsecret := models.FindConfig("GetuiAppSecret") + appmastersecret := models.FindConfig("GetuiMasterSecret") + type req struct { + Sign string `json:"sign"` + Timestamp string `json:"timestamp"` + Appkey string `json:"appkey"` + } + timestamp := strconv.FormatInt(time.Now().UnixNano()/1e6, 10) + reqJson := req{ + Sign: tools.Sha256(appkey + timestamp + appmastersecret), + Timestamp: timestamp, + Appkey: appkey, + } + reqStr, _ := json.Marshal(reqJson) + url := "https://restapi.getui.com/v2/" + appid + "/auth" + res, err := tools.Post(url, "application/json;charset=utf-8", reqStr) + log.Println(url, string(reqStr), err, res) + if err == nil && res != "" { + var pushRes GetuiResponse + json.Unmarshal([]byte(res), &pushRes) + if pushRes.Code == 0 { + token := pushRes.Data["token"].(string) + models.UpdateConfig("GetuiToken", token) + return token + } + } + return "" +} diff --git a/controller/visitor.go b/controller/visitor.go index 8bb4654..d70a940 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -99,7 +99,11 @@ func PostVisitorLogin(c *gin.Context) { } models.CreateVisitor(name, avator, c.ClientIP(), toId, id, refer, city, client_ip) visitor := models.FindVisitorByVistorId(id) + + //各种通知 go SendNoticeEmail(visitor.Name, "来了") + go SendAppGetuiPush(visitor.Name + "来了") + c.JSON(200, gin.H{ "code": 200, "msg": "ok", diff --git a/tools/hash.go b/tools/hash.go new file mode 100644 index 0000000..5eb9dff --- /dev/null +++ b/tools/hash.go @@ -0,0 +1,23 @@ +package tools + +import ( + "crypto/md5" + "crypto/sha256" + "encoding/hex" +) + +//md5加密 +func Md5(src string) string { + m := md5.New() + m.Write([]byte(src)) + res := hex.EncodeToString(m.Sum(nil)) + return res +} + +//Sha256加密 +func Sha256(src string) string { + m := sha256.New() + m.Write([]byte(src)) + res := hex.EncodeToString(m.Sum(nil)) + return res +} diff --git a/tools/http.go b/tools/http.go index 1e073d9..e99101f 100644 --- a/tools/http.go +++ b/tools/http.go @@ -3,6 +3,7 @@ package tools import ( "io/ioutil" "net/http" + "strings" ) func Get(url string) string { @@ -17,3 +18,35 @@ func Get(url string) string { } return string(robots) } + +//Post("http://xxxx","application/json;charset=utf-8",[]byte("{'aaa':'bbb'}")) +func Post(url string, contentType string, body []byte) (string, error) { + res, err := http.Post(url, contentType, strings.NewReader(string(body))) + if err != nil { + return "", err + } + defer res.Body.Close() + content, err := ioutil.ReadAll(res.Body) + if err != nil { + return "", err + } + return string(content), nil +} +func PostHeader(url string, msg []byte, headers map[string]string) (string, error) { + client := &http.Client{} + + req, err := http.NewRequest("POST", url, strings.NewReader(string(msg))) + if err != nil { + return "", err + } + for key, header := range headers { + req.Header.Set(key, header) + } + resp, err := client.Do(req) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + return string(body), nil +} diff --git a/tools/stringutil.go b/tools/stringutil.go index ae2e21f..ba440c1 100644 --- a/tools/stringutil.go +++ b/tools/stringutil.go @@ -2,8 +2,6 @@ package tools import ( - "crypto/md5" - "encoding/hex" "fmt" "github.com/gobuffalo/packr/v2" "golang.org/x/net/html/charset" @@ -75,13 +73,6 @@ func FileGetContent(file string) string { return content } -//md5加密 -func Md5(src string) string { - m := md5.New() - m.Write([]byte(src)) - res := hex.EncodeToString(m.Sum(nil)) - return res -} func ShowStringByte(str string) { s := []byte(str) for i, c := range s {