Commit 763e05fc by 朱继来

添加邮件发送

parent 1129ba91
...@@ -2,9 +2,10 @@ package OrderActionLog ...@@ -2,9 +2,10 @@ package OrderActionLog
import ( import (
"database/sql" "database/sql"
"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db" "go-queue-server/dal/db"
"go-queue-server/util" "go-queue-server/util"
"fmt"
"log" "log"
"time" "time"
) )
...@@ -76,23 +77,24 @@ func Query() { ...@@ -76,23 +77,24 @@ func Query() {
func Insert(order_id int, operator_id int, operator_type int, event string) (err error) { func Insert(order_id int, operator_id int, operator_type int, event string) (err error) {
create_time := time.Now().Unix() create_time := time.Now().Unix()
result, err := db.DB.Exec("insert into lie_order_action_log (order_id, operator_id, operator_type, event, create_time) values (?, ?, ?, ?, ?)", order_id, operator_id, operator_type, event, create_time) _, err1 := db.DB.Exec("insert into lie_order_action_log (order_id, operator_id, operator_type, event, create_time) values (?, ?, ?, ?, ?)", order_id, operator_id, operator_type, event, create_time)
if err != nil {
panic(err)
}
id, err := result.LastInsertId() if err1 != nil {
if err != nil { logger.Fatal("数据库操作失败")
panic(err) return err1
}
affected, err := result.RowsAffected()
if err != nil {
panic(err)
} }
fmt.Printf("last insert id:%d affect rows:%d\n", id, affected) //id, err := result.LastInsertId()
//if err != nil {
// panic(err)
//}
//
//affected, err := result.RowsAffected()
//if err != nil {
// panic(err)
//}
//
//fmt.Printf("last insert id:%d affect rows:%d\n", id, affected)
return err return err
} }
\ No newline at end of file
...@@ -7,7 +7,7 @@ require ( ...@@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.5.0 github.com/gin-gonic/gin v1.5.0
github.com/go-sql-driver/mysql v1.4.1 github.com/go-sql-driver/mysql v1.4.1
github.com/ichunt2019/go-msgserver v1.0.4 github.com/ichunt2019/go-msgserver v1.0.4
github.com/ichunt2019/logger v1.0.5 // indirect github.com/ichunt2019/logger v1.0.5
github.com/jmoiron/sqlx v1.2.0 github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/common v0.7.0 // indirect github.com/prometheus/common v0.7.0 // indirect
github.com/tealeg/xlsx v1.0.5 github.com/tealeg/xlsx v1.0.5
......
...@@ -3,10 +3,10 @@ package main ...@@ -3,10 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"github.com/ichunt2019/go-msgserver/utils/rabbitmq" "github.com/ichunt2019/go-msgserver/utils/rabbitmq"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db" "go-queue-server/dal/db"
"go-queue-server/order/sendPurchaseEmail/sendEmail" "go-queue-server/order/sendPurchaseEmail/sendEmail"
"go-queue-server/util" "go-queue-server/util"
"github.com/ichunt2019/logger"
"sync" "sync"
"time" "time"
) )
......
package sendEmail package sendEmail
import ( import (
"fmt" "encoding/json"
_ "fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/order/OrderActionLog"
"go-queue-server/util" "go-queue-server/util"
"go-queue-server/util/ding"
"io/ioutil"
"net/http" "net/http"
"encoding/json" "strconv"
"strings" "strings"
"io/ioutil"
"go-queue-server/dal/order/OrderActionLog"
) )
// json 返回值
type JosnResp struct { type JosnResp struct {
Errcode int `json:"err_code"` Errcode int `json:"err_code"`
Errmsg string `json:"msg"` Errmsg string `json:"err_msg"`
}
// 队列参数
type DataParams struct {
OrderId int `json:"order_id"`
OperatorId int `json:"operator_id"`
OperatorEvent string `json:"operator_event"`
} }
func SendPurchaseEmail(data string) (err error) { // 发送采购邮件
func SendPurchaseEmail(data string) (err error) {
crm_url := util.Configs.Crm_domain.SendMailUrl // CRM发送邮件接口 crm_url := util.Configs.Crm_domain.SendMailUrl // CRM发送邮件接口
header_map := map[string]string{
"api-key": "crm a1b2c3d4e5f6g7h8i9jk",
"Content-Type": "application/json",
}
client := &http.Client{} err = http_post(crm_url, data, header_map)
// 钉钉通知 默认参数
var mobile []string = make([]string, 0)
var isAtAll bool = false
if err != nil {
ding.Send("邮件接口请求失败,原因:" + err.Error(), mobile, isAtAll)
logger.Info(err.Error())
return err
}
req, _ := http.NewRequest("POST", crm_url, strings.NewReader(string(data))) // 添加操作日志
var data_params *DataParams
json.Unmarshal([]byte(data), &data_params)
req.Header.Set("api-key", "crm a1b2c3d4e5f6g7h8i9jk") order_id := data_params.OrderId
req.Header.Set("Content-Type", "application/json") operator_id := data_params.OperatorId
operator_event := data_params.OperatorEvent
resp, err := client.Do(req)
defer resp.Body.Close() err = OrderActionLog.Insert(order_id, operator_id, 2, operator_event)
if err != nil { if err != nil {
fmt.Println(err.Error()) ding.Send("添加操作日志失败", mobile, isAtAll)
} return err
}
body, _ := ioutil.ReadAll(resp.Body)
res := JosnResp{} ding.Send("订单ID:" + strconv.Itoa(order_id) + "," + operator_event, mobile, isAtAll)
err1 := json.Unmarshal([]byte(body), &res) // 推送ERP
api_url := util.Configs.Api_domain.ApiUrl + "order/sysiteminfo"
params := "order_id="+strconv.Itoa(order_id)
api_header_map := make(map[string]string, 0)
if err1 !=nil{ err = http_post(api_url, params, api_header_map)
fmt.Println(err1)
}
var data_map map[string]interface{} if err != nil {
json.Unmarshal([]byte(data), &data_map) ding.Send("ERP同步接口请求失败,原因:" + err.Error(), mobile, isAtAll)
logger.Info(err.Error())
return err
}
order_id := data_map["order_id"]; return
operator_id := data_map["operator_id"]; }
operator_event := data_map["operator_event"];
// 添加操作日志 func http_post(url string, data string, header map[string]string) (err error) {
OrderActionLog.Insert(order_id, operator_id, 2, operator_event) client := &http.Client{}
// 推送ERP req, _ := http.NewRequest("POST", url, strings.NewReader(string(data)))
if len(header) > 0 {
for k, v := range header {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
logger.Info(err.Error())
}
body, _ := ioutil.ReadAll(resp.Body)
res := JosnResp{}
if err := json.Unmarshal([]byte(body), &res); err != nil {
json_res, _ := json.Marshal(res)
logger.Info(string(json_res)) // 记录body错误信息
return err
}
return return
} }
......
...@@ -11,6 +11,7 @@ type Config struct{ ...@@ -11,6 +11,7 @@ type Config struct{
Rabbitmq_ichunt *RabbitmqIchunt Rabbitmq_ichunt *RabbitmqIchunt
Crm_domain *SendMail Crm_domain *SendMail
Ding_msg *Ding Ding_msg *Ding
Api_domain *ApiDomain
} }
...@@ -35,6 +36,10 @@ type Ding struct { ...@@ -35,6 +36,10 @@ type Ding struct {
JingDiao string `toml:"jingDiao"` JingDiao string `toml:"jingDiao"`
} }
type ApiDomain struct {
ApiUrl string `toml:"api_url"`
}
var Configs *Config =new (Config) var Configs *Config =new (Config)
func Init(){ func Init(){
......
package ding package ding
import ( import (
_"fmt"
"go-queue-server/util"
"net/http" "net/http"
"strings"
"encoding/json" "encoding/json"
"io/ioutil"
"github.com/ichunt2019/logger"
) )
func send() { type Msg struct {
Msgtype string `json:"msgtype"`
Text Contents `json:"text"`
At Ats `json:"at"`
}
type Contents struct {
Content string `json:"content"`
}
type Ats struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
// json 返回值
type JosnResp struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func Send(textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
var msg Msg
msg = Msg{Msgtype:"text"}
msg.Text.Content = "监控告警: " + textMsg // 固定标签 + 文本
msg.At.AtMobiles = mobiles
msg.At.IsAtAll = isAtAll;
content, _ := json.Marshal(msg)
// content := `{
// "msgtype": "text",
// "text": {
// "content": "`+ msg + `"
// }
// }`
ding_url := util.Configs.Ding_msg.Webhook // 钉钉请求接口
client := &http.Client{}
req, _ := http.NewRequest("POST", ding_url, strings.NewReader(string(content)))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req);
defer resp.Body.Close()
if err != nil {
logger.Info(err.Error())
}
body, _ := ioutil.ReadAll(resp.Body) // 获取接口返回数据
res := JosnResp{}
if err1 := json.Unmarshal([]byte(body), &res); err1 != nil { // 将接口数据写入res
logger.Info(err1.Error())
}
result, _ := json.Marshal(res)
return string(result)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment