예제 #1
0
        /// <summary>
        /// 获取全局唯一票据
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public WXAccessTokenCache AccessToken(string appid, string appsecret)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                WXAccessTokenCache token = s.Get <WXAccessTokenCache>(" where type = @0 and id != @1 ", "access_token", 0);
                if (token == null || token.expires_time <= DateTime.Now)
                {
                    if (token == null)
                    {
                        token = new WXAccessTokenCache();
                    }

                    //从API获取access_token
                    AccessToken access_token = WeChatBaseHelper.GetAccessToken(appid, appsecret);

                    //access_token的有效期目前为2个小时
                    token.expires_time = (DateTime)DateTime.Now.AddSeconds(access_token.expires_in -= 600);
                    token.token        = access_token.access_token;
                    token.type         = "access_token";

                    if (token.id == 0)
                    {
                        s.Insert <WXAccessTokenCache>(token);
                    }
                    else
                    {
                        s.Update <WXAccessTokenCache>(token);
                    }
                }
                return(token);
            }
        }
예제 #2
0
        /// <summary>
        /// 删除微信菜单
        /// </summary>
        /// <returns></returns>
        public JsonResult DelWeChatMenu()
        {
            try
            {
                string        data;
                WeChatAccount wxAccount = ServiceIoc.Get <WeChatAccountService>().Get();
                AccessToken   token     = WeChatBaseHelper.GetAccessToken(wxAccount.appid, wxAccount.app_secret);
                if (token.access_token == null)
                {
                    return(Json(GetResult(StateCode.State_152)));
                }
                data = WeChatBaseHelper.DeleteWXMenu(token.access_token);
                var errcode = JsonConvert.DeserializeObject <WXErrCode>(data);
                if (errcode.errcode > 0)
                {
                    return(Json(GetResult(StateCode.State_152, token.errcode)));
                }

                //errcode 等于0时候成功
                return(Json(GetResult(StateCode.State_200)));
            }
            catch
            {
                return(Json(GetResult(StateCode.State_500)));
            }
        }
예제 #3
0
        /// <summary>
        /// 生成微信自定义菜单
        /// </summary>
        /// <returns></returns>
        public JsonResult BuildWeChatMenu()
        {
            try
            {
                ComplexButton complexBtn;
                WXDefineMenu  wxDefiMenu = new WXDefineMenu();
                wxDefiMenu.button = new List <Button>();
                List <DefineMenu> Menus     = ServiceIoc.Get <DefineMenuService>().GetEnableList();
                List <DefineMenu> mainMenus = Menus.Where(m => m.parent_id == 0).ToList();
                if (mainMenus.Count > 0)
                {
                    foreach (var menu in mainMenus)
                    {
                        var subMenus = Menus.Where(m => m.parent_id == menu.id).ToList();
                        if (subMenus.Count > 0)
                        {
                            complexBtn            = new ComplexButton();
                            complexBtn.sub_button = new List <Button>();
                            complexBtn.name       = menu.name;
                            foreach (var submenu in subMenus)
                            {
                                complexBtn.sub_button.Add(GetButton(submenu));
                            }
                            wxDefiMenu.button.Add(complexBtn);
                        }
                        else
                        {
                            wxDefiMenu.button.Add(GetButton(menu));
                        }
                    }

                    string        postData  = JsonConvert.SerializeObject(wxDefiMenu).Replace("%26", "&");
                    WeChatAccount wxAccount = ServiceIoc.Get <WeChatAccountService>().Get();
                    AccessToken   token     = WeChatBaseHelper.GetAccessToken(wxAccount.appid, wxAccount.app_secret);
                    if (token.access_token == null)
                    {
                        return(Json(GetResult(StateCode.State_152, token.errcode)));
                    }
                    string data    = WeChatBaseHelper.CreateWXMenu(token.access_token, postData);
                    var    errcode = JsonConvert.DeserializeObject <WXErrCode>(data);
                    if (errcode.errcode > 0)
                    {
                        return(Json(GetResult(StateCode.State_152, token.errcode)));
                    }
                }
                return(Json(GetResult(StateCode.State_200)));
            }
            catch
            {
                return(Json(GetResult(StateCode.State_500)));
            }
        }
예제 #4
0
        /// <summary>
        /// 保存公众账号
        /// </summary>
        /// <param name="user"></param>
        /// <param name="account"></param>
        /// <param name="account_ent"></param>
        /// <param name="merchant"></param>
        /// <param name="imgmsg"></param>
        /// <returns></returns>
        public JsonResult SaveAccount(SysUser user, WeChatAccount account, WeChatAccountEnt account_ent, WeChatMerchant merchant, string imgmsg)
        {
            //微信公众号类型验证
            if (account.type != WXAccountType.Subscription)
            {
                AccessToken token = WeChatBaseHelper.GetAccessToken(account.appid, account.app_secret);
                if (token.access_token == null)
                {
                    return(Json(GetResult(StateCode.State_11001, token.errcode.ToString())));
                }
            }

            StateCode state = ServiceIoc.Get <WeChatAccountService>().Save(user.id, account, account_ent, merchant, imgmsg);

            return(Json(GetResult(state)));
        }