kbService.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "net/http"
  11. "os"
  12. "strings"
  13. "time"
  14. )
  15. // 定义消息结构体
  16. type WechatWorkMessage struct {
  17. MsgType string `json:"msgtype"`
  18. Text TextContent `json:"text"`
  19. }
  20. type TextContent struct {
  21. Content string `json:"content"`
  22. }
  23. type WordTask struct {
  24. Title string `json:"title"`
  25. Description string `json:"description"`
  26. Color_id string `json:"color_id"`
  27. Project_id int `json:"project_id"`
  28. Column_id int `json:"column_Id"`
  29. Position int `json:"position"`
  30. Score int `json:"score"`
  31. Creator_id int `json:"creator_id"`
  32. Swimlane_id int `json:"swimlane_Id"`
  33. }
  34. func CheckCreateUserAccount(name string) (accountId int, err error) {
  35. if name == "" {
  36. return 0, errors.New("名字不能为空")
  37. }
  38. if len([]rune(name)) == 11 {
  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": "$2y$10$9ZAo6H5.wwH5oEdnRWAKEORy8z9KkVE0Fw6BAv.0W2EtZ2Lr6T0oG",
  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. //查询最新泳道
  62. swimlane_id := 0
  63. swimlane := KbDb.FindOne("swimlanes", map[string]interface{}{"project_id": wt.Project_id}, "id", "id DESC")
  64. if swimlane != nil && len(*swimlane) > 0 {
  65. swimlane_id = common.IntAll((*swimlane)["id"])
  66. }
  67. in := KbDb.Insert("tasks", map[string]interface{}{
  68. "title": wt.Title,
  69. "description": wt.Description,
  70. "color_id": wt.Color_id,
  71. "project_id": wt.Project_id,
  72. "column_Id": wt.Column_id,
  73. "position": wt.Position,
  74. "score": wt.Score,
  75. "creator_id": wt.Creator_id,
  76. "swimlane_id": common.If(swimlane_id != 0, swimlane_id, wt.Swimlane_id),
  77. "date_creation": time.Now().Unix(),
  78. "date_modification": time.Now().Unix(),
  79. "date_moved": time.Now().Unix(),
  80. "priority": 2,
  81. })
  82. if in < 0 {
  83. return errors.New("创建任务失败")
  84. }
  85. return nil
  86. }
  87. func GetFilePath(fm, types string) (s string) {
  88. var tm = time.Now().Format("20060102")
  89. var path = SysConfig.FilePath + types + "/" + tm[:4] + "/" + tm[4:6] + "/"
  90. os.MkdirAll(path, 0700)
  91. return path + fm
  92. }
  93. func SendWechatWorkMessage(creator string) error {
  94. /*// 构造请求URL
  95. baseURL := SysConfig.WechatWorkUrl
  96. params := url.Values{}
  97. params.Add("key", SysConfig.WechatWorkKey)
  98. fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
  99. message := map[string]interface{}{
  100. "msgtype": "text",
  101. "text": map[string]interface{}{
  102. "content": "您有一个新工单,请及时处理",
  103. "mentioned_mobile_list": SysConfig.WechatRemind,
  104. },
  105. }
  106. payload, err := json.Marshal(message)
  107. if err != nil {
  108. return fmt.Errorf("JSON序列化失败: %v", err)
  109. }
  110. // 创建HTTP请求
  111. req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(payload))
  112. if err != nil {
  113. return fmt.Errorf("发送企微消息创建请求失败: %v", err)
  114. }
  115. req.Header.Set("Content-Type", "application/json")
  116. // 发送请求
  117. client := &http.Client{}
  118. resp, err := client.Do(req)
  119. if err != nil {
  120. return fmt.Errorf("发送企微消息请求发送失败: %v", err)
  121. }
  122. defer resp.Body.Close()
  123. // 处理响应
  124. if resp.StatusCode != http.StatusOK {
  125. body, _ := ioutil.ReadAll(resp.Body)
  126. return fmt.Errorf("发送企微消息API返回错误: 状态码 %d, 响应: %s", resp.StatusCode, string(body))
  127. }
  128. // 解析响应内容(可选)
  129. var result map[string]interface{}
  130. if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
  131. return fmt.Errorf("发送企微消息响应解析失败: %v", err)
  132. }
  133. if errcode, ok := result["errcode"].(float64); ok && errcode != 0 {
  134. return fmt.Errorf("企业微信API错误: %v", result["errmsg"])
  135. }
  136. return nil*/
  137. msg := fmt.Sprintf("%s在kb“剑鱼工单管理”中创建一个新工单,请及时处理", creator)
  138. ok, err := public.SendMsg(SysConfig.WorkPrivateMsg.WxAdmin, msg)
  139. if !ok && err != nil {
  140. log.Printf("创建工单,发送企业微信消息出错:%s", err)
  141. }
  142. return err
  143. }
  144. func GetTitleByContent(content string) (toTitle string, err error) {
  145. reqUrl := SysConfig.WorkflowsUrl
  146. method := "POST"
  147. param := map[string]interface{}{
  148. "inputs": map[string]interface{}{
  149. "content": content,
  150. },
  151. "response_mode": "blocking",
  152. "user": SysConfig.WorkflowsUser,
  153. }
  154. jsonData, err := json.Marshal(param)
  155. if err != nil {
  156. return
  157. }
  158. payload := strings.NewReader(string(jsonData))
  159. client := &http.Client{}
  160. req, err := http.NewRequest(method, reqUrl, payload)
  161. if err != nil {
  162. return
  163. }
  164. req.Header.Add("Content-Type", "application/json")
  165. req.Header.Add("Authorization", "Bearer app-w7OhSixqrVvZLa3wIEXNeoOh")
  166. res, err := client.Do(req)
  167. if err != nil {
  168. return
  169. }
  170. defer res.Body.Close()
  171. var result map[string]interface{}
  172. if err = json.NewDecoder(res.Body).Decode(&result); err != nil {
  173. return
  174. }
  175. log.Println("result", result)
  176. if data, ok := result["data"].(map[string]interface{}); ok {
  177. if outputs, oks := data["outputs"].(map[string]interface{}); oks {
  178. toTitle = strings.Replace(common.ObjToString(outputs["title"]), "标题:", "", -1)
  179. }
  180. }
  181. //toTitle = string(body)
  182. return
  183. }
  184. func GetQywxUserId() {
  185. userIdMap := map[string]string{}
  186. data := GfastDb.SelectBySql("select userid,email from jy_qywx_user where status = 1")
  187. if data != nil && len(*data) > 0 {
  188. for _, v := range *data {
  189. userIdMap[common.InterfaceToStr(v["email"])] = common.InterfaceToStr(v["userid"])
  190. }
  191. }
  192. QywxUserIdMap = userIdMap
  193. }