コード例 #1
0
        /**
         *
         * 通过code换取网页授权access_token和openid的返回数据,正确时返回的JSON数据包如下:
         * {
         *  "access_token":"ACCESS_TOKEN",
         *  "expires_in":7200,
         *  "refresh_token":"REFRESH_TOKEN",
         *  "openid":"OPENID",
         *  "scope":"SCOPE",
         *  "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
         * }
         * 其中access_token可用于获取共享收货地址
         * openid是微信支付jsapi支付接口统一下单时必须的参数
         * 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID);
                data.SetValue("secret", WxPayConfig.APPSECRET);
                data.SetValue("code", code);
                data.SetValue("grant_type", "authorization_code");
                string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();

                //请求url以获取数据
                string result = HttpService.Get(url);

                Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];

                Log.Debug(this.GetType().ToString(), "Get openid : " + openid);
                Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
コード例 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IsLoad = false;
                JsApiPay jsApiPay = new JsApiPay(this);
                try
                {
                    //定义一个回调地址
                    //ViewState["callbackurl"] = "http://keaideyibaobao.com/charactertest.aspx";

                    //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                    jsApiPay.GetOpenidAndAccessToken();

                    //获取收货地址js函数入口参数
                    //wxEditAddrParam = jsApiPay.GetEditAddressParameters();
                    ViewState["openid"] = jsApiPay.openid;
                    //ViewState["access_token"] = jsApiPay.access_token;
                    string resToken = HttpService.Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxcaf17959e7a40461&secret=d6e05480d9cebeecd672e08d84dd3737");

                    LitJson.JsonData jdata = LitJson.JsonMapper.ToObject(resToken);

                    WxPayData data = new WxPayData();
                    data.SetValue("access_token", jdata["access_token"]);
                    data.SetValue("openid", jsApiPay.openid);

                    string url    = "https://api.weixin.qq.com/cgi-bin/user/info?" + data.ToUrl();
                    string result = HttpService.Get(url);
                    jdata = LitJson.JsonMapper.ToObject(result);
                    if (!string.IsNullOrEmpty(jdata["subscribe"].ToString()))
                    {
                        if (jdata["subscribe"].ToString() == "1")
                        {
                            Session["openid"] = jsApiPay.openid;
                            //关注该公众账号,进行推荐处理
                            if (Request["reference"] != null)
                            {
                                string reference = Request["reference"].ToString();
                            }
                            //Response.Write("<span style='color:#FF0000;font-size:20px'>" + "您已关注本公众号" + "</span>");
                        }
                        else
                        {
                            Response.Write("<script>alert('您尚未关注该公众号')</script>");
                            // Response.Write("<script>window.location.href='weixin://weixin.qq.com/r/dz8fB6zE8fGOrehk92pl';</script>");
                            Server.Transfer("./pic.aspx");
                            return;
                        }
                    }

                    //Response.Write("<span style='color:#FF0000;font-size:20px'>" + result + jsApiPay.openid + "</span>");
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('系统出了点小问题,请您稍后再试')");
                }
            }
        }
コード例 #3
0
        /**
         *
         * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9
         * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用
         */
        public string GetEditAddressParameters()
        {
            string parameter = "";

            try
            {
                string host        = page.Request.Url.Host;
                string path        = page.Request.Path;
                string queryString = page.Request.Url.Query;
                //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url
                string url = "http://" + host + path + queryString;

                //构造需要用SHA1算法加密的数据
                WxPayData signData = new WxPayData();
                signData.SetValue("appid", WxPayConfig.APPID);
                signData.SetValue("url", url);
                signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
                signData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
                signData.SetValue("accesstoken", access_token);
                string param = signData.ToUrl();

                Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param);
                //SHA1加密
                string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1");
                Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign);

                //获取收货地址js函数入口参数
                WxPayData afterData = new WxPayData();
                afterData.SetValue("appId", WxPayConfig.APPID);
                afterData.SetValue("scope", "jsapi_address");
                afterData.SetValue("signType", "sha1");
                afterData.SetValue("addrSign", addrSign);
                afterData.SetValue("timeStamp", signData.GetValue("timestamp"));
                afterData.SetValue("nonceStr", signData.GetValue("noncestr"));

                //转为json格式
                parameter = afterData.ToJson();
                Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }

            return(parameter);
        }
コード例 #4
0
 /**
  *
  * 网页授权获取用户基本信息的全部过程
  * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
  * 第一步:利用url跳转获取code
  * 第二步:利用code去获取openid和access_token
  *
  */
 public void GetOpenidAndAccessToken()
 {
     if (!string.IsNullOrEmpty(page.Request.QueryString["code"]))
     {
         //获取code码,以获取openid和access_token
         string code = page.Request.QueryString["code"];
         Log.Debug(this.GetType().ToString(), "Get code : " + code);
         GetOpenidAndAccessTokenFromCode(code);
     }
     else
     {
         //构造网页授权获取code的URL
         string    host         = page.Request.Url.Host;
         string    path         = page.Request.Path;
         string    query        = page.Request.Url.Query;
         string    redirect_uri = HttpUtility.UrlEncode("http://www.keaideyibaobao.com" + path + query);
         WxPayData data         = new WxPayData();
         data.SetValue("appid", WxPayConfig.APPID);
         data.SetValue("redirect_uri", redirect_uri);
         data.SetValue("response_type", "code");
         data.SetValue("scope", "snsapi_base");
         data.SetValue("state", "STATE" + "#wechat_redirect");
         string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl();
         Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url);
         try
         {
             //触发微信返回code码
             page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
         }
         catch (System.Threading.ThreadAbortException ex)
         {
         }
     }
 }
コード例 #5
0
        //public static string wxEditAddrParam { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            Log.Info(this.GetType().ToString(), "page load");
            #region 预加载处理
            if (!IsPostBack)
            {
                IsLoad = false;
                JsApiPay jsApiPay = new JsApiPay(this);
                try
                {
                    //定义一个回调地址
                    //ViewState["callbackurl"] = "http://keaideyibaobao.com/charactertest.aspx";

                    //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                    jsApiPay.GetOpenidAndAccessToken();

                    //获取收货地址js函数入口参数
                    //wxEditAddrParam = jsApiPay.GetEditAddressParameters();
                    ViewState["openid"] = jsApiPay.openid;
                    //ViewState["access_token"] = jsApiPay.access_token;
                    string resToken = HttpService.Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxcaf17959e7a40461&secret=d6e05480d9cebeecd672e08d84dd3737");

                    LitJson.JsonData jdata = LitJson.JsonMapper.ToObject(resToken);

                    WxPayData data = new WxPayData();
                    data.SetValue("access_token", jdata["access_token"]);
                    data.SetValue("openid", jsApiPay.openid);

                    string url    = "https://api.weixin.qq.com/cgi-bin/user/info?" + data.ToUrl();
                    string result = HttpService.Get(url);
                    jdata = LitJson.JsonMapper.ToObject(result);
                    if (!string.IsNullOrEmpty(jdata["subscribe"].ToString()))
                    {
                        if (jdata["subscribe"].ToString() == "1")
                        {
                            //Response.Write("<span style='color:#FF0000;font-size:20px'>" + "您已关注本公众号" + "</span>");
                        }
                        else
                        {
                            Response.Write("<script>alert('您尚未关注该公众号')</script>");
                            // Response.Write("<script>window.location.href='weixin://weixin.qq.com/r/dz8fB6zE8fGOrehk92pl';</script>");
                            Server.Transfer("./pic.aspx");
                            return;
                        }
                    }

                    //Response.Write("<span style='color:#FF0000;font-size:20px'>" + result + jsApiPay.openid + "</span>");
                }
                catch (Exception ex)
                {
                    //Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试" + ex.Message + "</span>");
                }
            }
            #endregion
            else
            {
                Random r = new Random();
                if (Request["user-sex"] == null || Request["user-name"] == null)
                {
                    // Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错~~~" + "</span>");
                    return;
                }
                string test = "";
                if (!string.IsNullOrEmpty(Request["user-sex"]) || !string.IsNullOrEmpty(Request["user-name"]))
                {
                    string sex  = Request["user-sex"];
                    string name = Request["user-name"];
                    for (int i = 0; i < name.Length; i++)
                    {
                        if (business.SqlServer.IsContain(name[i] + sex))
                        {
                            business.SqlServer.Search(name[i] + sex);
                            int[] temp = business.SqlServer.Search(name[i] + sex);
                            for (int j = 0; j < list1.Length; j++)
                            {
                                list1[j] += temp[j];
                            }
                        }
                        else
                        {
                            int[] temp = new int[9];
                            for (int j = 0; j < list1.Length; j++)
                            {
                                temp[j] = r.Next(40, 120);
                                if (temp[j] >= 100)
                                {
                                    temp[j] = 100;
                                }
                                list1[j] += temp[j];
                            }
                            business.SqlServer.Add(name[i] + sex, temp);
                        }
                    }

                    sum  = 0;
                    list = list1;
                    for (int j = 0; j < list1.Length; j++)
                    {
                        list[j] = list1[j] / name.Length;
                        sum    += list[j];
                    }
                    sum    = sum / 9;
                    IsLoad = true;
                    if (name == "纪春光")
                    {
                        for (int j = 0; j < list.Length; j++)
                        {
                            list[j] = r.Next(98, 101);
                            sum     = 99;
                        }
                    }
                }
            }
        }