public ActionResult WeChart(string id)
 {
     wx_userweixin wxuser = wx_userweixinService.instance().SingleByCompanyID(new Guid(id));
     if (wxuser == null)
         wxuser = new wx_userweixin() { CompanyID = new Guid(id) };
     return View(wxuser);
 }
 public JsonResult WeChart(FormCollection form)
 {
     ResultBase_form result = new ResultBase_form();
     wx_userweixin m = new wx_userweixin();
     TryUpdateModel<wx_userweixin>(m, form);
     if (m.ID == Guid.Empty)
         result.status = wx_userweixinService.instance().Insert(m);
     else
         result.status = wx_userweixinService.instance().Update(m);
     result.msg = result.status == 0 ? "操作失败" : "操作成功";
     result.ResultURL = "/Company/Index";
     return Json(result, JsonRequestBehavior.AllowGet);
 }
        // GET: Info
        public ActionResult Index()
        {
            ViewBag.CompanyID = UserDateTicket.Company.ID;
            ViewBag.open_sAppid = UserDateTicket.wx_open.open_sAppID;



            TimeSpan chajun = DateTime.Now - UserDateTicket.wx_open.ModifyTime;

            if (string.IsNullOrEmpty(UserDateTicket.wx_open.open_pre_auth_code) || chajun.TotalSeconds >= UserDateTicket.wx_open.pre_auth_code_wxpires_in)
            {
                var result = ComponentApi.GetComponentAccessToken(UserDateTicket.wx_open.open_sAppID, UserDateTicket.wx_open.open_sAppSecret, UserDateTicket.wx_open.open_ticket);
                if (result.errcode == Entity.Weixin.ReturnCode.请求成功)
                {
                    UserDateTicket.wx_open.open_access_token = result.component_access_token;
                    UserDateTicket.wx_open.access_token_expires_in = result.expires_in;

                    var coderesult = ComponentApi.GetPreAuthCode(UserDateTicket.wx_open.open_sAppID, result.component_access_token);
                    if (coderesult.errcode == Entity.Weixin.ReturnCode.请求成功)
                    {


                        UserDateTicket.wx_open.open_pre_auth_code = coderesult.pre_auth_code;
                        UserDateTicket.wx_open.pre_auth_code_wxpires_in = coderesult.expires_in;
                        UserDateTicket.wx_open.ModifyTime = DateTime.Now;
                        wx_openInfoService.instance().Update(UserDateTicket.wx_open);
                        ViewBag.pre_auth_code = UserDateTicket.wx_open.open_pre_auth_code;
                    }
                }
            }
            else {
                ViewBag.pre_auth_code = UserDateTicket.wx_open.open_pre_auth_code;
            }



            wx_userweixin m;
            ViewBag.webSite = System.Configuration.ConfigurationManager.AppSettings["webSite"];
            if (UserDateTicket.wx_user.ID == Guid.Empty)
                m = new wx_userweixin() { DR = true };
            else
                m = wx_userweixinService.instance().SingleByCompanyID(UserDateTicket.Company.ID);
            return View(m);
        }
        public void Callback(string id)
        {
            //string cid = Request["cid"];
            //http://weixin.com/Open/Callback?id=02a07495-5484-4162-a70d-b7341096a1d4&auth_code=queryauthcode@@@zQaA25MwNP71mnGlwrwuRnx2lxw3NvCpK7n-BM01BJlBIkGIZaSluaGzAvdz_oX8sI4kc4wRx7MfLfaEqa8gJA&expires_in=3600
            Guid CID;
            int dbresult = 0;
            wx_openInfo wx_open = wx_openInfoService.instance().Single(new Guid(System.Configuration.ConfigurationManager.AppSettings["openID"]));
            if (Guid.TryParse(id, out CID))
            {
                string auth_code = Request["auth_code"];
                string expires_in = Request["expires_in"];
                var result = ComponentApi.GetComponentAccessToken(wx_open.open_sAppID, wx_open.open_sAppSecret, wx_open.open_ticket);
                if (result.errcode == Entity.Weixin.ReturnCode.请求成功)
                {
                    string open_access_token = result.component_access_token;
                    string open_sAppID = wx_open.open_sAppID;
                    var infoRes = ComponentApi.QueryAuth(open_access_token, open_sAppID, auth_code);
                    if (infoRes.errcode == Entity.Weixin.ReturnCode.请求成功)
                    {

                        wx_userweixin entity = wx_userweixinService.instance().SingleByCompanyID(CID);
                        if (entity == null)
                            entity = new wx_userweixin();
                        entity.expires_in = infoRes.authorization_info.expires_in;
                        entity.AppId = infoRes.authorization_info.authorizer_appid;
                        entity.Access_Token = infoRes.authorization_info.authorizer_access_token;
                        entity.refresh_token = infoRes.authorization_info.authorizer_refresh_token;

                        if (entity.ID == Guid.Empty)
                        {
                            entity.CompanyID = CID;
                            entity.ID = Guid.NewGuid();
                            dbresult = wx_userweixinService.instance().Insert(entity);
                        }
                        else {
                            dbresult = wx_userweixinService.instance().Update(entity);
                        }

                    }
                }
                if (dbresult > 0)
                {
                    UserDateTicket.wx_user = wx_userweixinService.instance().SingleByCompanyID(CID);
                    Response.Redirect("/Info/Index");
                }
                else
                {
                    Response.Write("<script>alert('拉去信息错误');</script>");
                }
            }


        }