Esempio n. 1
0
        //post方法调用接口获取token
        public CommonMethod.RootObject GetAuthToken(string appid, string authCode, string componentToken)
        {
            var obj = new
            {
                component_appid    = appid,
                authorization_code = authCode
            };
            string responseStr =
                WebService.PostFunction("https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentToken, obj);

            CommonMethod.RootObject authInfoModel = CommonMethod.JsonHelper.ParseFromJson <CommonMethod.RootObject>(responseStr);
            //CommonMethod.RootObject rb = JsonConvert.DeserializeObject<CommonMethod.RootObject>(responseStr);//这种方法也可以解析
            if (authInfoModel != null)
            {
                return(authInfoModel);
            }
            else
            {
                return(new CommonMethod.RootObject());
            }
        }
Esempio n. 2
0
        public DataTable DT = new DataTable(); //平台配置
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                comID = OperUtil.Get("companyID");
                this._operaID.Value = OperUtil.Get("operaID");
                if (string.IsNullOrEmpty(comID))
                {
                    Response.Write("<script>alert('您尚未登录或已长时间未进行操作,请重新登录!');top.location.href='../../index.aspx';</script>");
                    return;
                }
                this._companyID.Value = comID;
                Session.Timeout       = 360;

                if (!IsPostBack)
                {
                    //初始化页面的时候回去数据库校验当前登录用户(企业账号)是否“授权过”,是-展示企业公众号信息,否-展示授权二维码
                    //1-获取登录用户信息
                    string sql = "select * from asm_company where id=" + this._companyID.Value;

                    DC = DbHelperSQL.Query(sql).Tables[0];
                    DT = DbHelperSQL.Query("select * from asm_platformInfo").Tables[0];
                    this.HF_userInfoId.Value = comID;
                    //获取授权成功后回调的参数:
                    string auth_code  = Request.QueryString["auth_code"];
                    string expires_in = Request.QueryString["expires_in"];
                    Util.Debuglog("auth_code=" + auth_code + ";expires_in=" + expires_in, "_授权.txt");
                    if (!string.IsNullOrEmpty(auth_code))
                    {
                        //授权后的回调
                        auth_code = auth_code.Split(new string[] { "@@@" }, StringSplitOptions.RemoveEmptyEntries)[1];
                        string comToken = Util.getComToken();
                        //获取微信公众号接口“调用凭据authorizer_access_token”,用这个参数来调用微信公众平台接口
                        CommonMethod.RootObject authModel = GetAuthToken(OpenPFConfig.Appid, auth_code, comToken);
                        //将authorizer_refresh_token(这个是不会改变的)存入数据库,这是当token过期的时候用来刷新token的。如果authorizer_refresh_token丢失了,则需要重新授权
                        //获取“授权公众号详细信息”
                        authInfoModel = GetAuthInfo(OpenPFConfig.Appid, authModel.authorization_info.authorizer_appid, comToken);
                        string funcinfoIdStr = "";
                        //更新authorizer_refresh_token到userinfo表中
                        if (authInfoModel != null && authInfoModel.authorization_info != null && authInfoModel.authorization_info.func_info != null && authInfoModel.authorization_info.func_info.Count > 0)
                        {
                            UpdateUserInfo(authInfoModel, authModel.authorization_info.authorizer_refresh_token, DC.Rows[0]["id"].ToString());
                            //设置行业为消费品
                            string    sqlC    = "select * from asm_company where id=" + comID;
                            DataTable dt      = DbHelperSQL.Query(sql).Tables[0];
                            wxHelper  wx      = new wxHelper(comID);
                            string    token   = Util.GetTokenInfo(OpenPFConfig.Appid, dt.Rows[0]["appId"].ToString(), Util.getComToken(), dt.Rows[0]["refresh_token"].ToString()).authorizer_access_token;
                            string    result  = wx.setIndustry(token);
                            JObject   jo      = (JObject)JsonConvert.DeserializeObject(result);
                            string    errcode = jo["errcode"].ToString();
                        }
                        authAppid    = authModel.authorization_info.authorizer_appid;
                        refreshToken = authModel.authorization_info.authorizer_refresh_token;
                    }
                    else
                    {
                        //用户主动点击进来的(用户对象中含有Refresh_token属性)
                        if (DC != null && !string.IsNullOrEmpty(DC.Rows[0]["refresh_token"].ToString()))
                        {
                            //有这个值证明已经授权过,且在数据库中有信息
                            authAppid    = DC.Rows[0]["appId"].ToString();
                            refreshToken = DC.Rows[0]["refresh_token"].ToString();
                        }
                        else
                        {
                            //从Redis中取token
                            string comToken = Util.getComToken();
                            if (string.IsNullOrEmpty(comToken))
                            {
                                string ticket = DT.Rows[0]["ticket"].ToString();
                                comToken = GetToken(OpenPFConfig.Appid, OpenPFConfig.Appsecret, ticket);
                            }
                            //获取预授权码
                            string pac = GetPre_Auth_Code(OpenPFConfig.Appid, comToken);
                            //这里要判断下pac是否有值,无值证明comToken过期了,要重新获取
                            if (string.IsNullOrEmpty(pac))
                            {
                                comToken = GetToken(OpenPFConfig.Appid, OpenPFConfig.Appsecret, DT.Rows[0]["ticket"].ToString());
                                pac      = GetPre_Auth_Code(OpenPFConfig.Appid, comToken);
                            }
                            pac = pac.Split(new string[] { "@@@" }, StringSplitOptions.RemoveEmptyEntries)[1];
                            //拼接授权页面地址(用户扫描后跳转的扫码页面,前端必须用一个连接去进行跳转)
                            PhoneAuthPageUrl = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" + OpenPFConfig.Appid + "&pre_auth_code=" + pac + "&redirect_uri=http://nq.bingoseller.com/main/enterprise/authPage.aspx&auth_type=3";
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Util.Debuglog("auth ee=" + ee.ToString(), "_授权.txt");
            }
        }