kbService.go 3.7 KB

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