wx_init.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hongze_cygxzs/models"
  8. "hongze/hongze_cygxzs/utils"
  9. "time"
  10. )
  11. func GetWxUsersSubscribeCygx() {
  12. list, _ := models.GetCygxUserRecordyList()
  13. unionIdMap := make(map[string]string)
  14. for _, v := range list {
  15. unionIdMap[v.OpenId] = v.OpenId
  16. }
  17. i := 0
  18. openIdList := WxUsersGetV1()
  19. fmt.Println("openIdList:", len(openIdList))
  20. time.Sleep(3 * time.Second)
  21. for _, v := range openIdList {
  22. //if v != "oIwGm5mDnpMyur-v5LHdqcdRkfBU" {
  23. // continue
  24. //}
  25. err := models.SetUserSubscribeByOpenid(v)
  26. if err != nil && err.Error() != utils.ErrNoRow() {
  27. fmt.Println("SetUserSubscribeByOpenid Err:" + err.Error())
  28. return
  29. }
  30. detail, err := models.GetUserRecordByUserOpenId(v)
  31. if err != nil && err.Error() != utils.ErrNoRow() {
  32. fmt.Println("GetUserRecordByUserOpenId Err:" + err.Error())
  33. return
  34. }
  35. if detail != nil {
  36. err = models.SetUserSubscribeByMobile(detail.BindAccount)
  37. if err != nil && err.Error() != utils.ErrNoRow() {
  38. fmt.Println("SetUserSubscribeByMobile Err:" + err.Error())
  39. fmt.Println(detail.BindAccount)
  40. return
  41. }
  42. }
  43. openId := v
  44. i++
  45. fmt.Println(openId, i)
  46. //total, err := models.GetCygxUserRecordCount(openId)
  47. //if err != nil {
  48. // utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
  49. //}
  50. //var unionId string
  51. //if unionIdMap[v] == "" {
  52. // fmt.Println("添加")
  53. // accessToken, err := GetWxAccessTokenByXzs()
  54. // if err != nil {
  55. // utils.FileLog.Info("accessToken:" + err.Error())
  56. // }
  57. // if accessToken == "" {
  58. // utils.FileLog.Info("access_token 为空 openId:" + openId)
  59. // }
  60. // wxUserInfo, err := WxGetUserInfo(openId, accessToken)
  61. // if err != nil {
  62. // utils.FileLog.Info("accessToken:" + err.Error())
  63. // return
  64. // }
  65. // if wxUserInfo.Errcode != 0 {
  66. // userInfoJson, _ := json.Marshal(wxUserInfo)
  67. // utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
  68. // }
  69. // unionId = wxUserInfo.Unionid
  70. // if unionId != "" {
  71. // AddCygxUserRecord(wxUserInfo)
  72. // }
  73. //}
  74. }
  75. }
  76. type WxUsers struct {
  77. WxBase
  78. Total int
  79. Count int
  80. Data struct {
  81. Openid []string
  82. }
  83. NextOpenid string `json:"next_openid"`
  84. }
  85. // 微信接口基础返回数据
  86. type WxBase struct {
  87. Errcode int
  88. Errmsg string
  89. }
  90. func WxUsersGetV1() (openIdList []string) {
  91. accessToken := "68_Vybgr0jXdiqCJW7NjEROyrSAGT-txiflR9QSKn-9BRo-dAWsuDwwh-Adn2MUqRgrWDwusQx4F_IdB5aWCtFKAvJ6roWrSbP0F98PIMX1drK0Dv9xpSbKsS3PCkUMGNcAIABVC"
  92. //关注的openid切片
  93. openIdList = make([]string, 0)
  94. //下一个openid
  95. nextOpenid := ""
  96. QUERY_WX:
  97. url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=" + nextOpenid
  98. result, err := http.Get(url)
  99. if err != nil {
  100. utils.FileLog.Info("GetUser Err:", err.Error())
  101. return
  102. }
  103. item := new(WxUsers)
  104. err = json.Unmarshal(result, &item)
  105. if err != nil {
  106. fmt.Println("Unmarshal Err:", err.Error())
  107. return
  108. }
  109. if item.Errcode != 0 {
  110. utils.FileLog.Info("微信接口返回异常 Err:", item.Errmsg)
  111. return
  112. }
  113. err = errors.New(item.Errmsg)
  114. openIdList = append(openIdList, item.Data.Openid...)
  115. //如果数量大于10000条,同时下个openid不为空,那么继续查询
  116. if item.Count >= 10000 && item.NextOpenid != "" {
  117. nextOpenid = item.NextOpenid
  118. goto QUERY_WX
  119. }
  120. return openIdList
  121. }
  122. func init231() {
  123. list, err := models.GetCygxUserRecordyList()
  124. if err != nil {
  125. fmt.Println(err)
  126. return
  127. }
  128. var unionIds []string
  129. var condition string
  130. var pars []interface{}
  131. for _, v := range list {
  132. unionIds = append(unionIds, v.UnionId)
  133. }
  134. condition = ` AND union_id IN (` + utils.GetOrmInReplace(len(unionIds)) + `) AND create_platform = 4 `
  135. pars = append(pars, unionIds)
  136. listUserRecord, err := models.GetUserRecordList(condition, pars)
  137. if err != nil {
  138. fmt.Println(err)
  139. return
  140. }
  141. mapMobil := make(map[string]string)
  142. mapUserId := make(map[string]int)
  143. for _, v := range listUserRecord {
  144. mapMobil[v.UnionId] = v.BindAccount
  145. mapUserId[v.UnionId] = v.UserId
  146. }
  147. fmt.Println(len(list))
  148. fmt.Println(len(listUserRecord))
  149. for _, v := range listUserRecord {
  150. fmt.Println(mapUserId[v.UnionId], "————", mapMobil[v.UnionId])
  151. err = models.UpdateCygxUserRecordMobile(mapUserId[v.UnionId], mapMobil[v.UnionId], v.UnionId)
  152. if err != nil {
  153. fmt.Println(err)
  154. return
  155. }
  156. }
  157. }