kbService.go 4.9 KB

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