package services import ( "encoding/json" "errors" "fmt" "github.com/rdlucklib/rdluck_tools/http" "hongze/hongze_cygxzs/models" "hongze/hongze_cygxzs/utils" "time" ) func GetWxUsersSubscribeCygx() { list, _ := models.GetCygxUserRecordyList() unionIdMap := make(map[string]string) for _, v := range list { unionIdMap[v.OpenId] = v.OpenId } i := 0 openIdList := WxUsersGetV1() fmt.Println("openIdList:", len(openIdList)) time.Sleep(3 * time.Second) for _, v := range openIdList { //if v != "oIwGm5mDnpMyur-v5LHdqcdRkfBU" { // continue //} err := models.SetUserSubscribeByOpenid(v) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("SetUserSubscribeByOpenid Err:" + err.Error()) return } detail, err := models.GetUserRecordByUserOpenId(v) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("GetUserRecordByUserOpenId Err:" + err.Error()) return } if detail != nil { err = models.SetUserSubscribeByMobile(detail.BindAccount) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("SetUserSubscribeByMobile Err:" + err.Error()) fmt.Println(detail.BindAccount) return } } openId := v i++ fmt.Println(openId, i) //total, err := models.GetCygxUserRecordCount(openId) //if err != nil { // utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error()) //} //var unionId string //if unionIdMap[v] == "" { // fmt.Println("添加") // accessToken, err := GetWxAccessTokenByXzs() // if err != nil { // utils.FileLog.Info("accessToken:" + err.Error()) // } // if accessToken == "" { // utils.FileLog.Info("access_token 为空 openId:" + openId) // } // wxUserInfo, err := WxGetUserInfo(openId, accessToken) // if err != nil { // utils.FileLog.Info("accessToken:" + err.Error()) // return // } // if wxUserInfo.Errcode != 0 { // userInfoJson, _ := json.Marshal(wxUserInfo) // utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson)) // } // unionId = wxUserInfo.Unionid // if unionId != "" { // AddCygxUserRecord(wxUserInfo) // } //} } } type WxUsers struct { WxBase Total int Count int Data struct { Openid []string } NextOpenid string `json:"next_openid"` } // 微信接口基础返回数据 type WxBase struct { Errcode int Errmsg string } func WxUsersGetV1() (openIdList []string) { accessToken := "68_Vybgr0jXdiqCJW7NjEROyrSAGT-txiflR9QSKn-9BRo-dAWsuDwwh-Adn2MUqRgrWDwusQx4F_IdB5aWCtFKAvJ6roWrSbP0F98PIMX1drK0Dv9xpSbKsS3PCkUMGNcAIABVC" //关注的openid切片 openIdList = make([]string, 0) //下一个openid nextOpenid := "" QUERY_WX: url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=" + nextOpenid result, err := http.Get(url) if err != nil { utils.FileLog.Info("GetUser Err:", err.Error()) return } item := new(WxUsers) err = json.Unmarshal(result, &item) if err != nil { fmt.Println("Unmarshal Err:", err.Error()) return } if item.Errcode != 0 { utils.FileLog.Info("微信接口返回异常 Err:", item.Errmsg) return } err = errors.New(item.Errmsg) openIdList = append(openIdList, item.Data.Openid...) //如果数量大于10000条,同时下个openid不为空,那么继续查询 if item.Count >= 10000 && item.NextOpenid != "" { nextOpenid = item.NextOpenid goto QUERY_WX } return openIdList } func init231() { list, err := models.GetCygxUserRecordyList() if err != nil { fmt.Println(err) return } var unionIds []string var condition string var pars []interface{} for _, v := range list { unionIds = append(unionIds, v.UnionId) } condition = ` AND union_id IN (` + utils.GetOrmInReplace(len(unionIds)) + `) AND create_platform = 4 ` pars = append(pars, unionIds) listUserRecord, err := models.GetUserRecordList(condition, pars) if err != nil { fmt.Println(err) return } mapMobil := make(map[string]string) mapUserId := make(map[string]int) for _, v := range listUserRecord { mapMobil[v.UnionId] = v.BindAccount mapUserId[v.UnionId] = v.UserId } fmt.Println(len(list)) fmt.Println(len(listUserRecord)) for _, v := range listUserRecord { fmt.Println(mapUserId[v.UnionId], "————", mapMobil[v.UnionId]) err = models.UpdateCygxUserRecordMobile(mapUserId[v.UnionId], mapMobil[v.UnionId], v.UnionId) if err != nil { fmt.Println(err) return } } }