kbService.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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$pZhH7qLlW2bh1Qk.UjOkgekVPkpCjG1KfRhv3H0FXidx51a1ZntB2",
  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. in := KbDb.Insert("tasks", map[string]interface{}{
  65. "title": wt.Title,
  66. "description": wt.Description,
  67. "color_id": wt.Color_id,
  68. "project_id": wt.Project_id,
  69. "column_Id": wt.Column_id,
  70. "position": wt.Position,
  71. "score": wt.Score,
  72. "creator_id": wt.Creator_id,
  73. "swimlane_id": wt.Swimlane_id,
  74. "date_creation": time.Now().Unix(),
  75. "date_modification": time.Now().Unix(),
  76. "date_moved": time.Now().Unix(),
  77. "priority": 2,
  78. })
  79. if in < 0 {
  80. return errors.New("创建任务失败")
  81. }
  82. return nil
  83. }
  84. func GetFilePath(fm, types string) (s string) {
  85. var tm = time.Now().Format("20060102")
  86. var path = SysConfig.FilePath + types + "/" + tm[:4] + "/" + tm[4:6] + "/"
  87. os.MkdirAll(path, 0700)
  88. return path + fm
  89. }
  90. func SendWechatWorkMessage() error {
  91. // 构造请求URL
  92. baseURL := SysConfig.WechatWorkUrl
  93. params := url.Values{}
  94. params.Add("key", SysConfig.WechatWorkKey)
  95. fullURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
  96. message := map[string]interface{}{
  97. "msgtype": "text",
  98. "text": map[string]interface{}{
  99. "content": "您有一个新工单,请及时处理",
  100. "mentioned_mobile_list": SysConfig.WechatRemind,
  101. },
  102. }
  103. payload, err := json.Marshal(message)
  104. if err != nil {
  105. return fmt.Errorf("JSON序列化失败: %v", err)
  106. }
  107. // 创建HTTP请求
  108. req, err := http.NewRequest("POST", fullURL, bytes.NewBuffer(payload))
  109. if err != nil {
  110. return fmt.Errorf("发送企微消息创建请求失败: %v", err)
  111. }
  112. req.Header.Set("Content-Type", "application/json")
  113. // 发送请求
  114. client := &http.Client{}
  115. resp, err := client.Do(req)
  116. if err != nil {
  117. return fmt.Errorf("发送企微消息请求发送失败: %v", err)
  118. }
  119. defer resp.Body.Close()
  120. // 处理响应
  121. if resp.StatusCode != http.StatusOK {
  122. body, _ := ioutil.ReadAll(resp.Body)
  123. return fmt.Errorf("发送企微消息API返回错误: 状态码 %d, 响应: %s", resp.StatusCode, string(body))
  124. }
  125. // 解析响应内容(可选)
  126. var result map[string]interface{}
  127. if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
  128. return fmt.Errorf("发送企微消息响应解析失败: %v", err)
  129. }
  130. if errcode, ok := result["errcode"].(float64); ok && errcode != 0 {
  131. return fmt.Errorf("企业微信API错误: %v", result["errmsg"])
  132. }
  133. return nil
  134. }
  135. func GetTitleByContent(content string) (toTitle string, err error) {
  136. reqUrl := SysConfig.WorkflowsUrl
  137. method := "POST"
  138. param := map[string]interface{}{
  139. "inputs": map[string]interface{}{
  140. "content": content,
  141. },
  142. "response_mode": "blocking",
  143. "user": SysConfig.WorkflowsUser,
  144. }
  145. jsonData, err := json.Marshal(param)
  146. if err != nil {
  147. return
  148. }
  149. payload := strings.NewReader(string(jsonData))
  150. client := &http.Client{}
  151. req, err := http.NewRequest(method, reqUrl, payload)
  152. if err != nil {
  153. return
  154. }
  155. req.Header.Add("Content-Type", "application/json")
  156. req.Header.Add("Authorization", "Bearer app-w7OhSixqrVvZLa3wIEXNeoOh")
  157. res, err := client.Do(req)
  158. if err != nil {
  159. return
  160. }
  161. defer res.Body.Close()
  162. var result map[string]interface{}
  163. if err = json.NewDecoder(res.Body).Decode(&result); err != nil {
  164. return
  165. }
  166. log.Println("result", result)
  167. if data, ok := result["data"].(map[string]interface{}); ok {
  168. if outputs, oks := data["outputs"].(map[string]interface{}); oks {
  169. toTitle = strings.Replace(common.ObjToString(outputs["title"]), "标题:", "", -1)
  170. }
  171. }
  172. //toTitle = string(body)
  173. return
  174. }
  175. func GetQywxUserId() {
  176. userIdMap := map[string]string{}
  177. data := GfastDb.SelectBySql("select userid,email from jy_qywx_user where status = 1")
  178. if data != nil && len(*data) > 0 {
  179. for _, v := range *data {
  180. userIdMap[common.InterfaceToStr(v["email"])] = common.InterfaceToStr(v["userid"])
  181. }
  182. }
  183. QywxUserIdMap = userIdMap
  184. }