robot.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package app
  2. /*
  3. #cgo LDFLAGS: -L../ -lsdk
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. extern int WxInitSDK(bool, int);
  7. extern int WxDestroySDK();
  8. */
  9. import "C"
  10. import (
  11. "bytes"
  12. "client/wcf"
  13. "io"
  14. "log"
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. "time"
  19. "github.com/go-resty/resty/v2"
  20. "github.com/google/uuid"
  21. )
  22. var WxClient *wcf.Client
  23. // Message 组装成一个结构体展示消息
  24. type Message struct {
  25. IsSelf bool `json:"is_self,omitempty"`
  26. IsGroup bool `json:"is_group,omitempty"`
  27. MessageId uint64 `json:"message_id,omitempty"`
  28. Type uint32 `json:"type,omitempty"`
  29. Ts uint32 `json:"ts,omitempty"`
  30. RoomId string `json:"room_id,omitempty"`
  31. Content string `json:"content,omitempty"`
  32. WxId string `json:"wx_id,omitempty"`
  33. Sign string `json:"sign,omitempty"`
  34. Thumb string `json:"thumb,omitempty"`
  35. Extra string `json:"extra,omitempty"`
  36. Xml string `json:"xml,omitempty"`
  37. }
  38. // WechatFerryInit 调用sdk.dll中的WxInitSdk 进行启动微信并注入
  39. func WechatFerryInit() {
  40. // 调试模式 端口
  41. initSuccess := C.WxInitSDK(C.bool(false), C.int(10086))
  42. if initSuccess == 0 {
  43. log.Println("SDK 初始化成功")
  44. } else {
  45. log.Println("SDK 初始化失败")
  46. }
  47. time.Sleep(time.Millisecond * 5000)
  48. // 连接服务器
  49. client, errs := wcf.NewWCF("")
  50. if errs != nil {
  51. return
  52. }
  53. // 一定要在这里判断是否登录成功 否则会导致用户列表获取失败
  54. for true {
  55. if client.IsLogin() == true {
  56. log.Println("登录成功...等待初始化中...")
  57. time.Sleep(2000 * time.Millisecond)
  58. break
  59. }
  60. time.Sleep(1000 * time.Millisecond)
  61. }
  62. WxClient = client
  63. ContactsInit()
  64. log.Println("初始化完成")
  65. }
  66. // ContactsInit 通讯录初始化
  67. func ContactsInit() {
  68. var contactsMap []map[string]string
  69. contacts := WxClient.GetContacts()
  70. for _, v := range contacts {
  71. gender := ""
  72. if v.Gender == 1 {
  73. gender = "男"
  74. }
  75. if v.Gender == 2 {
  76. gender = "女"
  77. }
  78. contactsMaps := map[string]string{
  79. "wxId": v.Wxid,
  80. "code": v.Code,
  81. "remark": v.Remark,
  82. "name": v.Name,
  83. "country": v.Country,
  84. "province": v.Province,
  85. "city": v.City,
  86. "gender": gender,
  87. }
  88. contactsMap = append(contactsMap, contactsMaps)
  89. }
  90. WxClient.ContactsMap = contactsMap
  91. }
  92. // DownloadFile 下载文件
  93. func DownloadFile(url string, fileType string, suffix string) (string, error) {
  94. log.Println(url)
  95. // 发送HTTP请求获取文件
  96. resp, err := resty.New().R().Get(url)
  97. if err != nil {
  98. return "", err
  99. }
  100. // 获取当前日期
  101. currentTime := time.Now()
  102. datePath := filepath.Join("./resource/static/"+fileType, currentTime.Format("2006-01-02"))
  103. // 创建目录
  104. if err := os.MkdirAll(datePath, os.ModePerm); err != nil {
  105. return "", err
  106. }
  107. // 生成唯一的文件名
  108. fileName := uuid.New().String() + "." + suffix
  109. filePath := filepath.Join(datePath, fileName)
  110. // 创建文件
  111. file, err := os.Create(filePath)
  112. if err != nil {
  113. return "", err
  114. }
  115. defer file.Close()
  116. // 将HTTP响应的Body复制到文件
  117. _, err = io.Copy(file, bytes.NewBuffer(resp.Body()))
  118. if err != nil {
  119. return "", err
  120. }
  121. currentDir, err := os.Getwd()
  122. if err != nil {
  123. return "", err
  124. }
  125. filePath = currentDir + "/" + filePath
  126. filePath = strings.Replace(filePath, "\\", "/", -1)
  127. return filePath, nil
  128. }