kbService.go 6.0 KB

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