wx_init.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hongze_mfyx_gzh/models"
  8. "hongze/hongze_mfyx_gzh/utils"
  9. )
  10. type WxUsers struct {
  11. WxBase
  12. Total int
  13. Count int
  14. Data struct {
  15. Openid []string
  16. }
  17. NextOpenid string `json:"next_openid"`
  18. }
  19. // 微信接口基础返回数据
  20. type WxBase struct {
  21. Errcode int
  22. Errmsg string
  23. }
  24. func WxUsersGetV1() (openIdList []string) {
  25. accessToken := "68_Vybgr0jXdiqCJW7NjEROyrSAGT-txiflR9QSKn-9BRo-dAWsuDwwh-Adn2MUqRgrWDwusQx4F_IdB5aWCtFKAvJ6roWrSbP0F98PIMX1drK0Dv9xpSbKsS3PCkUMGNcAIABVC"
  26. //关注的openid切片
  27. openIdList = make([]string, 0)
  28. //下一个openid
  29. nextOpenid := ""
  30. QUERY_WX:
  31. url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=" + nextOpenid
  32. result, err := http.Get(url)
  33. if err != nil {
  34. utils.FileLog.Info("GetUser Err:", err.Error())
  35. return
  36. }
  37. item := new(WxUsers)
  38. err = json.Unmarshal(result, &item)
  39. if err != nil {
  40. fmt.Println("Unmarshal Err:", err.Error())
  41. return
  42. }
  43. if item.Errcode != 0 {
  44. utils.FileLog.Info("微信接口返回异常 Err:", item.Errmsg)
  45. return
  46. }
  47. err = errors.New(item.Errmsg)
  48. openIdList = append(openIdList, item.Data.Openid...)
  49. //如果数量大于10000条,同时下个openid不为空,那么继续查询
  50. if item.Count >= 10000 && item.NextOpenid != "" {
  51. nextOpenid = item.NextOpenid
  52. goto QUERY_WX
  53. }
  54. return openIdList
  55. }
  56. func init231() {
  57. list, err := models.GetCygxUserRecordyList()
  58. if err != nil {
  59. fmt.Println(err)
  60. return
  61. }
  62. var unionIds []string
  63. var condition string
  64. var pars []interface{}
  65. for _, v := range list {
  66. unionIds = append(unionIds, v.UnionId)
  67. }
  68. condition = ` AND union_id IN (` + utils.GetOrmInReplace(len(unionIds)) + `) AND create_platform = 4 `
  69. pars = append(pars, unionIds)
  70. listUserRecord, err := models.GetUserRecordList(condition, pars)
  71. if err != nil {
  72. fmt.Println(err)
  73. return
  74. }
  75. mapMobil := make(map[string]string)
  76. mapUserId := make(map[string]int)
  77. for _, v := range listUserRecord {
  78. mapMobil[v.UnionId] = v.BindAccount
  79. mapUserId[v.UnionId] = v.UserId
  80. }
  81. fmt.Println(len(list))
  82. fmt.Println(len(listUserRecord))
  83. for _, v := range listUserRecord {
  84. fmt.Println(mapUserId[v.UnionId], "————", mapMobil[v.UnionId])
  85. err = models.UpdateCygxUserRecordMobile(mapUserId[v.UnionId], mapMobil[v.UnionId], v.UnionId)
  86. if err != nil {
  87. fmt.Println(err)
  88. return
  89. }
  90. }
  91. }