kbService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. . "biBackService/config"
  5. "biBackService/public"
  6. "bytes"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "time"
  15. )
  16. // 定义消息结构体
  17. type WechatWorkMessage struct {
  18. MsgType string `json:"msgtype"`
  19. Text TextContent `json:"text"`
  20. }
  21. type TextContent struct {
  22. Content string `json:"content"`
  23. }
  24. type WordTask struct {
  25. Title string `json:"title"`
  26. Description string `json:"description"`
  27. Color_id string `json:"color_id"`
  28. Project_id int `json:"project_id"`
  29. Column_id int `json:"column_Id"`
  30. Position int `json:"position"`
  31. Score int `json:"score"`
  32. Creator_id int `json:"creator_id"`
  33. Swimlane_id int `json:"swimlane_Id"`
  34. }
  35. func CheckCreateUserAccount(name string) (accountId int, err error) {
  36. if name == "" {
  37. return 0, errors.New("名字不能为空")
  38. }
  39. res := KbDb.FindOne("users", map[string]interface{}{"name": name}, "id", "id DESC")
  40. if res != nil && len(*res) > 0 {
  41. accountId = common.IntAll((*res)["id"])
  42. } else {
  43. fullNamePy, username := public.ConvertToPinyin(name)
  44. in := KbDb.Insert("users", map[string]interface{}{
  45. "username": username,
  46. "password": "$10$pZhH7qLlW2bh1Qk.UjOkgekVPkpCjG1KfRhv3H0FXidx51a1ZntB2",
  47. "is_ldap_user": 0,
  48. "name": name,
  49. "email": fmt.Sprintf("%s@topnet.net.cn", fullNamePy),
  50. })
  51. if in < 0 {
  52. return 0, errors.New("创建用户信息出错")
  53. }
  54. accountId = int(in)
  55. }
  56. return accountId, nil
  57. }
  58. func (wt *WordTask) WordTaskSave() (err error) {
  59. in := KbDb.Insert("tasks", map[string]interface{}{
  60. "title": wt.Title,
  61. "description": wt.Description,
  62. "color_id": wt.Color_id,
  63. "project_id": wt.Project_id,
  64. "column_Id": wt.Column_id,
  65. "position": wt.Position,
  66. "score": wt.Score,
  67. "creator_id": wt.Creator_id,
  68. "swimlane_id": wt.Swimlane_id,
  69. "date_creation": time.Now().Unix(),
  70. })
  71. if in < 0 {
  72. return errors.New("创建任务失败")
  73. }
  74. return nil
  75. }
  76. func GetFilePath(fm, types string) (s string) {
  77. var tm = time.Now().Format("20060102")
  78. var path = SysConfig.FilePath + types + "/" + tm[:4] + "/" + tm[4:6] + "/"
  79. os.MkdirAll(path, 0700)
  80. return path + fm
  81. }
  82. func SendWechatWorkMessage() error {
  83. // 构造请求URL
  84. baseURL := SysConfig.WechatWorkUrl
  85. params := url.Values{}
  86. params.Add("key", SysConfig.WechatWorkKey)
  87. fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
  88. message := map[string]interface{}{
  89. "msgtype": "text",
  90. "text": map[string]interface{}{
  91. "content": "您有一个新工单,请及时处理",
  92. "mentioned_mobile_list": SysConfig.WechatRemind,
  93. },
  94. }
  95. payload, err := json.Marshal(message)
  96. if err != nil {
  97. return fmt.Errorf("JSON序列化失败: %v", err)
  98. }
  99. // 创建HTTP请求
  100. req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(payload))
  101. if err != nil {
  102. return fmt.Errorf("发送企微消息创建请求失败: %v", err)
  103. }
  104. req.Header.Set("Content-Type", "application/json")
  105. // 发送请求
  106. client := &http.Client{}
  107. resp, err := client.Do(req)
  108. if err != nil {
  109. return fmt.Errorf("发送企微消息请求发送失败: %v", err)
  110. }
  111. defer resp.Body.Close()
  112. // 处理响应
  113. if resp.StatusCode != http.StatusOK {
  114. body, _ := ioutil.ReadAll(resp.Body)
  115. return fmt.Errorf("发送企微消息API返回错误: 状态码 %d, 响应: %s", resp.StatusCode, string(body))
  116. }
  117. // 解析响应内容(可选)
  118. var result map[string]interface{}
  119. if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
  120. return fmt.Errorf("发送企微消息响应解析失败: %v", err)
  121. }
  122. if errcode, ok := result["errcode"].(float64); ok && errcode != 0 {
  123. return fmt.Errorf("企业微信API错误: %v", result["errmsg"])
  124. }
  125. return nil
  126. }