Commit 763e05fc by 朱继来

添加邮件发送

parent 1129ba91
......@@ -2,9 +2,10 @@ package OrderActionLog
import (
"database/sql"
"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/util"
"fmt"
"log"
"time"
)
......@@ -76,23 +77,24 @@ func Query() {
func Insert(order_id int, operator_id int, operator_type int, event string) (err error) {
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)
if err != nil {
panic(err)
}
_, 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)
id, err := result.LastInsertId()
if err != nil {
panic(err)
}
affected, err := result.RowsAffected()
if err != nil {
panic(err)
if err1 != nil {
logger.Fatal("数据库操作失败")
return err1
}
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
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.5.0
github.com/go-sql-driver/mysql v1.4.1
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/prometheus/common v0.7.0 // indirect
github.com/tealeg/xlsx v1.0.5
......
......@@ -3,10 +3,10 @@ package main
import (
"fmt"
"github.com/ichunt2019/go-msgserver/utils/rabbitmq"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/order/sendPurchaseEmail/sendEmail"
"go-queue-server/util"
"github.com/ichunt2019/logger"
"sync"
"time"
)
......
package sendEmail
import (
"fmt"
"encoding/json"
_ "fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/order/OrderActionLog"
"go-queue-server/util"
"go-queue-server/util/ding"
"io/ioutil"
"net/http"
"encoding/json"
"strconv"
"strings"
"io/ioutil"
"go-queue-server/dal/order/OrderActionLog"
)
// json 返回值
type JosnResp struct {
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发送邮件接口
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")
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
order_id := data_params.OrderId
operator_id := data_params.OperatorId
operator_event := data_params.OperatorEvent
defer resp.Body.Close()
err = OrderActionLog.Insert(order_id, operator_id, 2, operator_event)
if err != nil {
fmt.Println(err.Error())
}
body, _ := ioutil.ReadAll(resp.Body)
ding.Send("添加操作日志失败", mobile, isAtAll)
return err
}
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{
fmt.Println(err1)
}
err = http_post(api_url, params, api_header_map)
var data_map map[string]interface{}
json.Unmarshal([]byte(data), &data_map)
if err != nil {
ding.Send("ERP同步接口请求失败,原因:" + err.Error(), mobile, isAtAll)
logger.Info(err.Error())
return err
}
order_id := data_map["order_id"];
operator_id := data_map["operator_id"];
operator_event := data_map["operator_event"];
return
}
// 添加操作日志
OrderActionLog.Insert(order_id, operator_id, 2, operator_event)
func http_post(url string, data string, header map[string]string) (err error) {
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
}
......
......@@ -11,6 +11,7 @@ type Config struct{
Rabbitmq_ichunt *RabbitmqIchunt
Crm_domain *SendMail
Ding_msg *Ding
Api_domain *ApiDomain
}
......@@ -35,6 +36,10 @@ type Ding struct {
JingDiao string `toml:"jingDiao"`
}
type ApiDomain struct {
ApiUrl string `toml:"api_url"`
}
var Configs *Config =new (Config)
func Init(){
......
package ding
import (
_"fmt"
"go-queue-server/util"
"net/http"
"strings"
"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