123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package service
- import (
- "app.yhyue.com/moapp/jybase/common"
- . "biBackService/config"
- "biBackService/public"
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "net/http"
- "net/url"
- "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"`
- Swimlane_id int `json:"swimlane_Id"`
- }
- func CheckCreateUserAccount(name string) (accountId int, err error) {
- if name == "" {
- return 0, errors.New("名字不能为空")
- }
- 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": "$10$pZhH7qLlW2bh1Qk.UjOkgekVPkpCjG1KfRhv3H0FXidx51a1ZntB2",
- "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 (wt *WordTask) WordTaskSave() (err error) {
- in := KbDb.Insert("tasks", map[string]interface{}{
- "title": wt.Title,
- "description": wt.Description,
- "color_id": wt.Color_id,
- "project_id": wt.Project_id,
- "column_Id": wt.Column_id,
- "position": wt.Position,
- "score": wt.Score,
- "creator_id": wt.Creator_id,
- "swimlane_id": wt.Swimlane_id,
- "date_creation": time.Now().Unix(),
- })
- if in < 0 {
- return errors.New("创建任务失败")
- }
- return nil
- }
- 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
- }
- func SendWechatWorkMessage() error {
- // 构造请求URL
- baseURL := SysConfig.WechatWorkUrl
- params := url.Values{}
- params.Add("key", SysConfig.WechatWorkKey)
- fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
- message := map[string]interface{}{
- "msgtype": "text",
- "text": map[string]interface{}{
- "content": "您有一个新工单,请及时处理",
- "mentioned_mobile_list": SysConfig.WechatRemind,
- },
- }
- 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
- }
|