Ejemplo n.º 1
0
        public void GenerateQRCodeForMarketingOrders(int activityTaocanId)
        {
            AppSettings settings = AppSettings.GetAppSettings();
            string      qrfolder = System.IO.Path.Combine(settings.RootDirectory, settings.QRFolder);

            using (chargebitEntities db = new chargebitEntities())
            {
                Marketing_Activities    activity = null;
                Customer                customer = null;
                List <Marketing_Orders> orders   = (from o in db.Marketing_Orders where o.ActivityTaocanId == activityTaocanId select o).ToList <Marketing_Orders>();
                if (orders.Count > 0)
                {
                    foreach (Marketing_Orders order in orders)
                    {
                        if (order.Used)
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(order.CodePath))
                        {
                            string tmpPhysicalPath = Path.Combine(settings.RootDirectory, order.CodePath.Replace('/', '\\'));
                            if (File.Exists(tmpPhysicalPath))
                            {
                                continue;
                            }
                        }
                        if (activity == null)
                        {
                            activity = (from a in db.Marketing_Activities where a.Id == order.ActivityId select a).FirstOrDefault <Marketing_Activities>();
                            if (activity == null)
                            {
                                continue;
                            }

                            if (customer == null)
                            {
                                customer = (from c in db.Customer where c.Id == activity.CustomerId select c).FirstOrDefault <Customer>();
                            }
                        }

                        string midPhysicalPath = string.Format("{0}\\{1}", activity.AgentId, activity.CustomerId);
                        string fileName        = Guid.NewGuid().ToString() + ".png";
                        string urlAbsPath      = string.Format("{0}/{1}/{2}", activity.AgentId, activity.CustomerId, fileName);
                        string parameter       = string.Empty; //string.Format("agentId={0}&customerId={1}&activityId={2}&activityOrderId={3}", activity.AgentId, activity.CustomerId,activity.Id,order.Id);
                        SortedDictionary <string, string> ps = new SortedDictionary <string, string>();
                        ps["agentId"]         = activity.AgentId.ToString();
                        ps["customerId"]      = activity.CustomerId.ToString();
                        ps["activityId"]      = activity.Id.ToString();
                        ps["activityOrderId"] = order.Id.ToString();
                        StringBuilder pstr  = new StringBuilder();
                        int           count = 1;
                        foreach (KeyValuePair <string, string> p in ps)
                        {
                            pstr.Append(p.Key);
                            pstr.Append("=");
                            pstr.Append(p.Value);
                            if (count < ps.Count)
                            {
                                pstr.Append("&");
                            }
                            count++;
                        }
                        parameter = pstr.ToString();
                        pstr.Append("&key=");
                        pstr.Append(customer.Token);
                        parameter = parameter + "&signature=" + UrlSignUtil.GetMD5(pstr.ToString());
                        parameter = KMEncoder.Encode(parameter);
                        string codeContent = string.Format("{0}/Product/SaoMa?p={1}", settings.WebURL, parameter);
                        string fullFolder  = Path.Combine(qrfolder, midPhysicalPath);
                        QRCodeUtil.CreateQRCode(fullFolder, fileName, codeContent);
                        if (File.Exists(Path.Combine(fullFolder, fileName)))
                        {
                            order.CodePath = urlAbsPath;
                        }
                    }

                    db.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
        public ActionResult DoSaoMa()
        {
            string p        = Request["p"];
            string number   = Request["mobile_number"];
            string spName   = Request["SPName"];
            string province = Request["Province"];
            string city     = Request["City"];

            if (string.IsNullOrEmpty(p))
            {
                ViewBag.Message = "参数错误,请正确扫码,输入手机号码点充值";
            }
            else
            {
                int    agentId         = 0;
                int    customerId      = 0;
                int    activityId      = 0;
                int    activityOrderId = 0;
                string parameters      = KMEncoder.Decode(p);
                if (!string.IsNullOrEmpty(parameters))
                {
                    string signature = string.Empty;
                    SortedDictionary <string, string> pvs = parseParameters(parameters, out signature);
                    if (string.IsNullOrEmpty(signature))
                    {
                        ViewBag.Message = "URL参数不正确,请重新扫码";
                        return(View("SaoMa"));
                    }
                    System.Text.StringBuilder pBuilder = new System.Text.StringBuilder();
                    if (pvs.Count > 0)
                    {
                        int count = 1;
                        foreach (KeyValuePair <string, string> pair in pvs)
                        {
                            pBuilder.Append(pair.Key);
                            pBuilder.Append("=");
                            pBuilder.Append(pair.Value);
                            if (count < pvs.Count)
                            {
                                pBuilder.Append("&");
                            }
                            count++;
                            switch (pair.Key)
                            {
                            case "agentId":
                                int.TryParse(pair.Value, out agentId);
                                break;

                            case "customerId":
                                int.TryParse(pair.Value, out customerId);
                                break;

                            case "activityId":
                                int.TryParse(pair.Value, out activityId);
                                break;

                            case "activityOrderId":
                                int.TryParse(pair.Value, out activityOrderId);
                                break;
                            }
                        }
                        CustomerManagement customerMgr = new CustomerManagement(0);
                        int total;
                        List <BCustomer> customers = customerMgr.FindCustomers(0, customerId, out total);
                        if (total <= 0 || total > 1)
                        {
                            ViewBag.Message = "URL参数不正确,请重新扫码";
                            return(View("SaoMa"));
                        }
                        pBuilder.Append("&key=");
                        pBuilder.Append(customers[0].Token);
                        string sign = UrlSignUtil.GetMD5(pBuilder.ToString());
                        if (sign != signature)
                        {
                            ViewBag.Message = "URL参数不正确,请重新扫码";
                            return(View("SaoMa"));
                        }
                        ActivityManagement activityMgr = new ActivityManagement(0);
                        BMarketOrderCharge order       = new BMarketOrderCharge()
                        {
                            ActivityId      = activityId,
                            ActivityOrderId = activityOrderId,
                            AgentId         = agentId,
                            CustomerId      = customerId,
                            City            = city,
                            Province        = province,
                            OpenId          = "",
                            Phone           = number,
                            SPName          = spName
                        };
                        KMBit.BL.Charge.ChargeResult result = activityMgr.MarketingCharge(order);
                        ViewBag.Message = result.Message;
                        //if(result.Status == ChargeStatus.FAILED)
                        //{
                        //    ViewBag.Paras = pvs;
                        //    //paras.Add("p", p);
                        //}
                    }
                }
                else
                {
                    ViewBag.Message = "不能重复扫码,或者修改扫码后的URL地址";
                }
            }

            return(View("SaoMa"));
        }
Ejemplo n.º 3
0
        public string GenerateActivityQRCode(int agendId, int customerId, int activityId)
        {
            string codePath = string.Empty;

            agendId = agendId > 0 ? agendId : CurrentLoginUser.User.Id;
            AppSettings settings = AppSettings.GetAppSettings();

            using (chargebitEntities db = new chargebitEntities())
            {
                Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == activityId select a).FirstOrDefault <Marketing_Activities>();
                if (activity == null)
                {
                    throw new KMBitException(string.Format("编号为:{0}的活动不存在", activityId));
                }
                if (activity.CustomerId != customerId)
                {
                    throw new KMBitException(string.Format("编号为{0}的活动不属于编号为{1}的客户", activityId, customerId));
                }
                if (activity.AgentId != agendId)
                {
                    throw new KMBitException(string.Format("编号为{0}的活动不属于编号为{1}的代理的客户的活动", activityId, agendId));
                }

                Customer customer = (from c in db.Customer where c.Id == customerId select c).FirstOrDefault <Customer>();
                codePath = agendId + "\\" + customerId;
                string fileName      = Guid.NewGuid().ToString() + ".png";
                string absPath       = agendId + "/" + customerId + "/" + fileName;
                string fullDirectory = Path.Combine(settings.RootDirectory + "\\" + settings.QRFolder, codePath);
                if (!string.IsNullOrEmpty(activity.CodePath) && File.Exists(Path.Combine(settings.RootDirectory + "\\" + settings.QRFolder, activity.CodePath.Replace('/', '\\'))))
                {
                    return(activity.CodePath);
                }
                string parameter = string.Empty;// string.Format("agentId={0}&customerId={1}&activityId={2}", agendId, customerId, activityId);
                SortedDictionary <string, string> ps = new SortedDictionary <string, string>();
                ps["agentId"]    = activity.AgentId.ToString();
                ps["customerId"] = activity.CustomerId.ToString();
                ps["activityId"] = activity.Id.ToString();

                StringBuilder pstr  = new StringBuilder();
                int           count = 1;
                foreach (KeyValuePair <string, string> p in ps)
                {
                    pstr.Append(p.Key);
                    pstr.Append("=");
                    pstr.Append(p.Value);
                    if (count < ps.Count)
                    {
                        pstr.Append("&");
                    }
                    count++;
                }
                parameter = pstr.ToString();
                pstr.Append("&key=");
                pstr.Append(customer.Token);
                parameter = parameter + "&signature=" + UrlSignUtil.GetMD5(pstr.ToString());
                parameter = KMEncoder.Encode(parameter);
                string codeContent = string.Format("{0}/Product/SaoMa?p={1}", settings.WebURL, parameter);
                QRCodeUtil.CreateQRCode(fullDirectory, fileName, codeContent);
                if (File.Exists(Path.Combine(fullDirectory, fileName)))
                {
                    activity.CodePath = absPath;
                }

                db.SaveChanges();
                return(absPath);
            }
        }
Ejemplo n.º 4
0
        public HttpResponseMessage GetCodeDirect()
        {
            this.IniRequest();
            var        resp       = new HttpResponseMessage(HttpStatusCode.OK);
            ApiMessage message    = new ApiMessage();
            string     openId     = string.Empty;
            string     openPublic = string.Empty;
            string     spName     = string.Empty;
            string     signature  = request["signature"];
            string     once       = request["nonce"];
            string     timestamp  = request["timestamp"];
            string     echostr    = request["echostr"];

            if (string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(once) || string.IsNullOrEmpty(timestamp))
            {
                logger.Info("The request was not sent from weixin");
                resp.Content = new StringContent("false", System.Text.Encoding.UTF8, "text/plain");
                return(resp);
            }
            logger.Info(string.Format("signature:{0},nonce:{1},timestamp:{2},echostr:{3}", signature, once, timestamp, echostr));
            SortedSet <string> paras = new SortedSet <string>();

            paras.Add(weixinToken);
            paras.Add(once);
            paras.Add(timestamp);
            string strKey = "";

            logger.Info(string.Format("signature posted by weixin is {0}", signature));
            foreach (string v in paras)
            {
                strKey += v;
            }
            string sign = KMEncoder.SHA1_Hash(strKey);

            logger.Info(string.Format("signature calculated in platform is {0}", sign));
            if (signature != sign)
            {
                logger.Info("two signatures are different, The request was not sent from weixin");
                resp.Content = new StringContent("false", System.Text.Encoding.UTF8, "text/plain");
                return(resp);
            }
            //For weixin URL verification
            if (!string.IsNullOrEmpty(echostr))
            {
                resp.Content = new StringContent(echostr, System.Text.Encoding.UTF8, "text/plain");
                return(resp);
            }
            try
            {
                ActivityManagement activityMgr = new ActivityManagement(0);
                int agentId    = 0;
                int customerId = 0;
                int activityId = 0;
                int.TryParse(request["agentId"], out agentId);
                int.TryParse(request["customerId"], out customerId);
                int.TryParse(request["activityId"], out activityId);
                System.IO.Stream sream  = context.Request.InputStream;
                StreamReader     sr     = new StreamReader(sream);
                string           strXML = sr.ReadToEnd();
                sream.Close();
                //strXML = "<xml><ToUserName><![CDATA[gh_3f1c1268428a]]></ToUserName><FromUserName><![CDATA[oNEHRsogX4seVvYK5v3S-veFUkEk]]></FromUserName><CreateTime>1446455044</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[+联通]]></Content><MsgId>6212477109514836427</MsgId></xml>";
                logger.Info(string.Format("Message pushed by Weichat server:{0}", strXML));
                WeiChatReceivedContentMessage weiChatMessage = WeiChatMessageUtil.ParseWeichatXML(strXML, logger);
                if (weiChatMessage != null)
                {
                    if (weiChatMessage.Content != null)
                    {
                        spName = weiChatMessage.Content.Trim();
                    }
                    if (weiChatMessage.FromUserName != null)
                    {
                        openId = weiChatMessage.FromUserName;
                    }
                    if (weiChatMessage.ToUserName != null)
                    {
                        openPublic = weiChatMessage.ToUserName;
                    }
                }
                if (string.IsNullOrEmpty(spName))
                {
                    resp.Content = new StringContent("success", System.Text.Encoding.UTF8, "text/plain");
                    return(resp);
                }
                if (!spName.Contains("+联通") && !spName.Contains("+移动") && !spName.Contains("+电信"))
                {
                    resp.Content = new StringContent("success", System.Text.Encoding.UTF8, "text/plain");
                    return(resp);
                }
                string url = activityMgr.GetOneRandomMarketOrderQrCodeUrl(spName, openId, agentId, customerId, activityId);
                message.Item    = url;
                message.Status  = "OK";
                message.Message = "成功获取到二维码";
                logger.Info(url);
            }
            catch (KMBitException kex)
            {
                logger.Warn(kex);
                message.Message = kex.Message;
                message.Status  = "ERROR";
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                message.Message = "未知错误";
                message.Status  = "ERROR";
            }

            logger.Info(string.Format("GetOneRandomMarketOrderQrCodeUrl Status:{0}, Message:{1}", message.Status, message.Message));
            string returnXml = "";

            if (message.Status == "OK")
            {
                logger.Info("二维码获取成功,准备推送二维码到微信");
                WeiChatNewsMessage news = new WeiChatNewsMessage();
                news.ToUserName   = openId;
                news.FromUserName = openPublic;
                news.MsgType      = "news";
                List <WeiChatArticle> articles = new List <WeiChatArticle>();
                news.Articles = articles;
                WeiChatArticle article = new WeiChatArticle()
                {
                    Description = "",
                    Title       = "将二维码保存到相册,微信->发现->扫一扫->相册,选择刚刚保存的二维码",
                    PicUrl      = message.Item.ToString(),
                    Url         = message.Item.ToString()
                };
                articles.Add(article);
                returnXml = WeiChatMessageUtil.PrepareWeiChatNewsXml(news);
            }
            else
            {
                logger.Info("二维码获取失败");
                WeiChatContentMessage msg = new WeiChatContentMessage()
                {
                    Content      = message.Message,
                    CreateTime   = 0,
                    FromUserName = openPublic,
                    MsgType      = "text",
                    ToUserName   = openId
                };
                returnXml = WeiChatMessageUtil.PrepareWeiChatXml(msg);
            }
            //logger.Info(returnXml);
            resp.Content = new StringContent(returnXml, System.Text.Encoding.UTF8, "text/plain");
            return(resp);
        }