kbService.go 3.8 KB

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