Esempio n. 1
0
    /// <summary>
    /// 获取微信openid
    /// </summary>
    /// <param name="isbase"></param>
    /// <returns></returns>
    public static WxResultMsg GetOpenId(string acid, bool isbase = true)
    {
        acid = acid.ToLower();

        WxResultMsg result = new WxResultMsg();

        string code = HttpContext.Current.Request["code"];

        if (string.IsNullOrEmpty(code) == false)
        {
            if (isbase)
            {
                string paramsstr = "getresult=snsapi_base&acid={0}&code={1}";
                paramsstr = string.Format(paramsstr, acid, code);
                string postresult = doPost(wxUrl, paramsstr);

                result = JsonDeserialize <WxResultMsg>(postresult);

                if (result.states == 0)
                {
                    return(result);
                }
                //LogTool.LogCommon.WebFramework.GeneralMethodBase.WebDebugLog(result.error); //错误信息
            }
            else
            {
                string paramsstr = "getresult=snsapi_userinfo&acid={0}&code={1}";
                paramsstr = string.Format(paramsstr, acid, code);
                string postresult = doPost(wxUrl, paramsstr);
                result = JsonDeserialize <WxResultMsg>(postresult);
                if (result.states == 0)
                {
                    return(result);
                }
            }
            return(result);
        }

        if (isbase)
        {
            HttpContext.Current.Response.Redirect(wxUrl + "?getresult=code&acid=" + acid + "&redirecturl=" + HttpUtility.UrlEncode(RedirectURL));
        }
        else
        {
            HttpContext.Current.Response.Redirect(wxUrl + "?getresult=code&isbase=false&acid=" + acid + "&redirecturl=" + HttpUtility.UrlEncode(RedirectURL));
        }
        return(null);
    }
Esempio n. 2
0
    /// <summary>
    /// 获取OpenId
    /// </summary>
    /// <param name="istest">true-返回固定OpenId false-返回获取OpenId</param>
    /// <param name="isbase">true-获取OpenId false-获取微信昵称</param>
    /// <returns></returns>
    public string GetOpenId(bool istest = false, bool isbase = true)
    {
        string Codes = string.IsNullOrEmpty(Request["codes"]) ? "" : Request["codes"];

        if (istest)
        {
            SessionMethod.SessionInstance.SetSession("倘若", "otzXTjqt6B6xXVjozCblakccTUyw", "...", Codes);
            return("otzXTjqt6B6xXVjozCblakccTUyw");
        }

        Model.SessionModel session = SessionMethod.SessionInstance.GetSession();

        if (session != null && Common.ValidateHelper.IsOpenid(session.OpenId))
        {
            if (Codes.Length > 0)
            {
                SessionMethod.SessionInstance.SetSession("", session.OpenId, "", Codes);
            }
            return(session.OpenId);
        }

        WxResultMsg baseopid = WeiXinOpenId.GetOpenId(WebFramework.GeneralMethodBase.GetHost().Replace("demo", "").ToLower(), isbase) as WxResultMsg;

        if (isbase && baseopid.snsapibase != null && Common.ValidateHelper.IsOpenid(baseopid.snsapibase.openid))
        {
            SessionMethod.SessionInstance.SetSession("", baseopid.snsapibase.openid, "", Codes);
            return(baseopid.snsapibase.openid);
        }
        else if (isbase == false && baseopid.userinfo != null && Common.ValidateHelper.IsOpenid(baseopid.userinfo.openid))
        {
            SessionMethod.SessionInstance.SetSession(baseopid.userinfo.nickname.Replace("'", ""), baseopid.userinfo.openid, baseopid.userinfo.headimgurl, Codes);
            return(baseopid.userinfo.openid);
        }
        else
        {
            HttpContext.Current.Response.End();
            return("");
        }
    }