package service import ( "app.yhyue.com/moapp/jybase/common" . "bindresume/config" "bindresume/public" "bytes" "encoding/json" "errors" "fmt" "io/ioutil" "net/http" "os" "time" ) // 定义消息结构体 type WechatWorkMessage struct { MsgType string `json:"msgtype"` Text TextContent `json:"text"` } type TextContent struct { Content string `json:"content"` } type WordTask struct { Title string `json:"title"` Description string `json:"description"` Color_id string `json:"color_id"` Project_id int `json:"project_id"` Column_id int `json:"column_Id"` Position int `json:"position"` Score int `json:"score"` Creator_id int `json:"creator_id"` } // GetFilePath 获取文件路径、名称 func GetFilePath(fm, types string) (s string) { var tm = time.Now().Format("20060102") var path = SysConfig.FilePath + types + "/" + tm[:4] + "/" + tm[4:6] + "/" os.MkdirAll(path, 0700) return path + fm } // SendWechatWorkMessage 发送企业微信消息 func SendWechatWorkMessage() error { // 构造请求URL fullURL := SysConfig.WechatWorkUrl //params := url.Values{} //params.Add("key", webhookKey) //fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode()) message := &WechatWorkMessage{ MsgType: "text", Text: TextContent{ Content: "今日天气:31度,大部分晴,降雨概率:0%", }, } /*messages := map[string]interface{}{ "msgtype": "text", "text": map[string]interface{}{ "content": "今日天气:31度,大部分晴,降雨概率:0%", }, }*/ payload, err := json.Marshal(message) if err != nil { return fmt.Errorf("JSON序列化失败: %v", err) } // 创建HTTP请求 req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(payload)) if err != nil { return fmt.Errorf("发送企微消息创建请求失败: %v", err) } req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { return fmt.Errorf("发送企微消息请求发送失败: %v", err) } defer resp.Body.Close() // 处理响应 if resp.StatusCode != http.StatusOK { body, _ := ioutil.ReadAll(resp.Body) return fmt.Errorf("发送企微消息API返回错误: 状态码 %d, 响应: %s", resp.StatusCode, string(body)) } // 解析响应内容(可选) var result map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { return fmt.Errorf("发送企微消息响应解析失败: %v", err) } if errcode, ok := result["errcode"].(float64); ok && errcode != 0 { return fmt.Errorf("企业微信API错误: %v", result["errmsg"]) } return nil } func CheckCreateUserAccount(name string) (accountId int, err error) { res := KbDb.FindOne("users", map[string]interface{}{"name": name}, `"id"`, "id DESC") if res != nil && len(*res) > 0 { accountId = common.IntAll((*res)["id"]) } else { fullNamePy, username := public.ConvertToPinyin(name) in := KbDb.Insert("users", map[string]interface{}{ "username": username, "password": "$2y$10$HiZaBYTxhppdxmm259JcoO1NW4dWDkEZnPNC74ExRtj0sPFE7FB7m", "is_ldap_user": 0, "name": name, "email": fmt.Sprintf("%s@topnet.net.cn", fullNamePy), }) if in < 0 { return 0, errors.New("创建用户信息出错") } accountId = int(in) } return accountId, nil } func (t *WordTask) WordTaskSave() error { in := KbDb.Insert("tasks", map[string]interface{}{ "title": t.Title, "description": t.Description, "color_id": t.Color_id, "project_id": t.Project_id, "column_Id": t.Column_id, "position": t.Position, "score": t.Score, "creator_id": t.Creator_id, }) if in < 0 { return errors.New("创建任务失败") } return nil }