Example #1
0
        public ActionResult Test()
        {
            WeHelper.GetUserInfo("orbpLt2PcsB8gIbkkR7f4exf1Hb8");
            return(View());

            //var ret1 = WeHelper.AddKfAccount("jckf1", "客服人员", accessToken);
            //var ret2 = WeHelper.SendMessage("orbpLt2PcsB8gIbkkR7f4exf1Hb8", "取消订单 50165465446 成功!", accessToken);
            //return Content("");
        }
Example #2
0
        /// <summary>
        /// 微信用户oauth方式认证
        /// </summary>
        public ActionResult WeOAuth(out Customer customer, string actionUrl)
        {
            customer = null;
            if (!this.IsAuthenticated)
            {
                //var code = Request["code"];
                Lgr.Log.Info(string.Format("用户IP:{0} HttpContext.Request.Cookies.Count:{1},UserAgent:{2},UserHostName:{3}", HttpContext.Request.UserHostAddress, HttpContext.Request.Cookies.Count, HttpContext.Request.UserAgent, HttpContext.Request.UserHostName));
                string code = HttpContext.Request["code"];
                Lgr.Log.Info(string.Format("WeOAuth认证时 HttpContext.Request.Cookie:{0},用户IP:{1}", code, HttpContext.Request.UserHostAddress));
                if (string.IsNullOrEmpty(code))
                {
                    return(Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=1#wechat_redirect",
                                                  Constants.AppId, Constants.SiteDomain + actionUrl)));
                }

                #region 获取微信用户信息
                try
                {
                    //获取微信用户openid
                    Lgr.Log.Info(string.Format("WeOAuth获取微信用户信息时,code:{0}", code));
                    var openId = WeHelper.GetOpenId(code);
                    if (string.IsNullOrEmpty(openId))
                    {
                        throw new Exception("微信用户认证失败");
                    }

                    //检查openid是否已经在数据库中存在
                    customer = CustomerService.GetByopenId(openId);
                    if (customer == null)
                    {
                        #region 如果关注的用户未保存到数据库,重新获取用户信息并保存
                        //获取用户详细信息
                        var userInfo = WeHelper.GetUserInfo(openId);
                        if (userInfo.Subscribe) //只有用户关注了公众号才能获取到用户其他信息
                        {
                            //数据库中检查微信用户
                            customer = CustomerService.CheckNewWeAccount(userInfo);
                        }
                        else
                        {
                            return(RedirectToAction("FollowQrCode"));
                        }
                        #endregion
                    }

                    //设置当前登录的微信用户
                    SetCurAccount(openId);
                }
                #region 2015-11-16 遇到不合法的oauth_code 再次请求获取Code
                catch (KeyNotFoundException ex)
                {
                    Lgr.Log.Error(ex.Message, ex);
                    Lgr.Log.Info(string.Format("URL:{0}", HttpContext.Request.Url));
                    return(Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=1#wechat_redirect",
                                                  Constants.AppId, Constants.SiteDomain + actionUrl)));
                }
                #endregion
                catch (Exception ex)
                {
                    Lgr.Log.Error(ex.Message, ex);
                }
                #endregion
            }
            else
            {
                Lgr.Log.Debug(string.Format("Session[WE_ACCOUNT]:{0},actionUrl:{1}", this.CurAccount, actionUrl));
                customer = CustomerService.GetByopenId(this.CurAccount);
            }

            if (!this.IsAuthenticated)
            {
                Lgr.Log.Debug("BaseController:IsAuthenticated:" + this.IsAuthenticated);
                throw new Exception("用户认证失败");
            }
            if (customer == null)
            {
                Lgr.Log.Debug("BaseController:customer == null" + customer == null);
                throw new Exception("用户认证失败");
            }

            return(null);
        }
        /// <summary>
        /// 小程序下单
        /// </summary>
        /// <param name="oIds">订单Id</param>
        /// <param name="code">临时登录凭证</param>
        /// <returns></returns>
        public ActionResult WeXcxPay(int[] oIds, string code)
        {
            #region 验证订单是否有效,并合计价格

            //订单价格
            decimal payPrice = 0;

            //订单描述
            string detail = "";

            //验证订单.....


            #endregion

            #region 统一下单

            try
            {
                //支付回调通知地址
                var        address = WebConfigurationManager.AppSettings["WxXcxNotifyUrl"].ToString();
                XcxPayData data    = new XcxPayData();
                data.SetValue("body", "商品购买");

                //可以将用户Id和订单Id同时封装在attach中
                data.SetValue("attach", String.Join(",", oIds).ToString());
                Random rd = new Random();

                //外部商户订单号
                var payNum = DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(0, 1000).ToString().PadLeft(3, '0');
                data.SetValue("out_trade_no", payNum);
                data.SetValue("detail", detail.Substring(0, detail.Length - 1));
                data.SetValue("total_fee", Convert.ToInt32(payPrice * 100));
                data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
                data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
                data.SetValue("notify_url", address);
                //data.SetValue("goods_tag", "test");
                data.SetValue("trade_type", "JSAPI");
                data.SetValue("openid", WeHelper.Code2Session(code).openid);

                XcxPayData result   = XcxPayApi.UnifiedOrder(data);
                var        flag     = true;
                var        msg      = "";
                var        nonceStr = "";
                var        appId    = "";
                var        package  = "";
                var        mch_id   = "";
                if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
                {
                    flag = false;
                    msg  = "下单失败";
                    return(Json(new { Result = false, Msg = "下单失败!" }));
                }
                else
                {
                    //统一下单

                    ///TO Do......
                    /// 修改订单状态

                    nonceStr = result.GetValue("nonce_str").ToString();
                    appId    = result.GetValue("appid").ToString();
                    mch_id   = result.GetValue("mch_id").ToString();
                    package  = "prepay_id=" + result.GetValue("prepay_id").ToString();
                }
                var        signType  = "MD5";
                var        timeStamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000).ToString();
                XcxPayData applet    = new XcxPayData();
                applet.SetValue("appId", appId);
                applet.SetValue("nonceStr", nonceStr);
                applet.SetValue("package", package);
                applet.SetValue("signType", signType);
                applet.SetValue("timeStamp", timeStamp);
                var appletSign = applet.MakeSign();
                return(Json(new { timeStamp, nonceStr, package, signType, paySign = appletSign, Result = flag, msg }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = false, msg = "缺少参数" }));
            }
            #endregion
        }