func (c *AdminImportUserController) Get() {
	op := c.Input().Get("op")
	if op == "import" {
		beego.Debug("AdminImportUserController get import")
		openid := c.Input().Get("openid")
		errcode, token := getToken()
		if errcode == 0 && len(openid) != 0 {
			user, err := getWxUser(openid, token)
			if err == nil {
				if user.ErrCode == 0 {
					err = models.AddWxUserInfo(user)
					if err != nil {
						beego.Error(err)
					} else {
						err = models.AddWxUserMoney(user.OpenId, 4)
						if err != nil {
							beego.Error(err)
						} else {
							_, err = models.AddUserMoneyRecord(user.OpenId, MONEY_SUBSCRIBE_SUM, MONEY_SUBSCRIBE)
						}
					}

				}
			}
		}
		url := "/admin/importuser"
		c.Redirect(url, 302)
		return
	}
	wxusers, err := models.GetAllWxUsers()
	if err != nil {
		beego.Error("err :", err)
	}
	// var wxusermap map[string]int64
	wxusermap := make(map[string]int64)
	for i := 0; i < len(wxusers); i++ {
		wxusermap[wxusers[i].OpenId] = wxusers[i].Id
	}

	errcode, token := getToken()
	beego.Debug("errcode :", errcode, "token :", token)
	if errcode == 0 {
		openids := getWxUserList(token, c)
		importUsers := make([]*ImportUser, len(openids))
		for i := 0; i < len(openids); i++ {
			importUser := &ImportUser{OpenId: openids[i]}
			if _, ok := wxusermap[openids[i]]; ok {
				//存在
				importUser.IsImport = true
			}
			// beego.Debug(" openids ", i, importUser)
			importUsers[i] = importUser
		}
		c.Data["ImportUsers"] = importUsers
	}
	c.TplName = "adminimportuser.html"
}
Exemple #2
0
func getHomeUserInfo(access_toke, openid string, c *WxHomeController) {
	isdebug := "true"
	iniconf, err := config.NewConfig("json", "conf/myconfig.json")
	if err != nil {
		beego.Debug(err)
	} else {
		isdebug = iniconf.String("qax580::isdebug")
	}
	wx_url := "[REALM]?access_token=[ACCESS_TOKEN]&openid=[OPENID]&lang=zh_CN"
	// if beego.AppConfig.Bool("qax580::isdebug") {
	realm_name := ""
	if isdebug == "true" {
		realm_name = "http://localhost:9091"
	} else {
		realm_name = "https://api.weixin.qq.com/sns/userinfo"
	}
	wx_url = strings.Replace(wx_url, "[REALM]", realm_name, -1)
	wx_url = strings.Replace(wx_url, "[ACCESS_TOKEN]", access_toke, -1)
	wx_url = strings.Replace(wx_url, "[OPENID]", openid, -1)
	beego.Debug("----------------get UserInfo --------------------")
	beego.Debug(wx_url)

	resp, err := http.Get(wx_url)
	if err != nil {
		beego.Debug(err)
	}

	defer resp.Body.Close()
	beego.Debug("----------------get UserInfo body--------------------")
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		beego.Debug(err)
	} else {
		beego.Debug(string(body))
	}
	var uij models.Wxuserinfo
	if err := json.Unmarshal(body, &uij); err == nil {
		beego.Debug("----------------get UserInfo json--------------------")
		beego.Debug(uij)
		if uij.ErrCode == 0 {
			c.Data["Uij"] = uij
		}
		err = models.AddWxUserInfo(uij)
		if err != nil {
			beego.Error(err)
		} else {
			// wx_home := "/?logtype=wx&openid=[OPENID]"
			// wx_home = strings.Replace(wx_home, "[OPENID]", uij.OpenId, -1)
			// beego.Debug("----------------wx_home--------------------")
			// beego.Debug(wx_home)
			// c.Redirect(wx_home, 302)
			maxAge := 1<<31 - 1
			c.Ctx.SetCookie(COOKIE_WX_OPENID, uij.OpenId, maxAge, "/")
			c.Redirect("/", 302)
		}
		return
	} else {
		beego.Debug("----------------get UserInfo json error--------------------")
		beego.Debug(err)
	}
}