推送服务端测试

pull/23/head
taoshihan1991 3 years ago
parent 3554e1b7ae
commit ac60d6b38c

@ -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,

@ -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"`
}

@ -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 ""
}

@ -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",

@ -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
}

@ -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
}

@ -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 {

Loading…
Cancel
Save