Esempio n. 1
0
        public static HeaderViewModle GetHeader(this HtmlHelper helper)
        {
            var result = new HeaderViewModle();

            try
            {
                IUserProfileRepository repo = Bootstrapper.Initialise().Resolve<IUserProfileRepository>();
                var user = repo.FindAll().SingleOrDefault(s => s.UserId == WebSecurity.CurrentUserId);
                if (string.IsNullOrEmpty(user.Header))
                {
                    var token = AccessTokenContainer.TryGetToken(user.AppId, user.AppSecret);
                    var qrResult = QrCodeApi.Create(token, "1", 0);
                    var header = QrCodeApi.GetShowQrCodeUrl(qrResult.ticket);
                    var data = new WeixinShortLinkSend() { access_token = token, long_url = header };
                    header = CommonApi.GetShortLink(token, data).short_url;
                    user.Header = header;
                    repo.Update(user);
                    repo.Context.Commit();
                }

                result.Header = user.Header;

            }
            catch (ErrorJsonResultException e)
            {
                result.Error = e.Message;
            }
            catch (Exception)
            {

            }
            return result;
        }
Esempio n. 2
0
        public static WeixinShortLinkResult GetShortLink(string accessToken, WeixinShortLinkSend data)
        {
            var jsonString = JsonConvert.SerializeObject(data);
            CookieContainer cookieContainer = null;//
            using (MemoryStream ms = new MemoryStream())
            {
                var bytes = Encoding.UTF8.GetBytes(jsonString);
                ms.Write(bytes, 0, bytes.Length);
                ms.Seek(0, SeekOrigin.Begin);

                var url = string.Format("https://api.weixin.qq.com/cgi-bin/shorturl?access_token={0}", accessToken);
                var result = Post.PostGetJson<WeixinShortLinkResult>(url, cookieContainer, ms);
                return result;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 调用微信api获取短链接,一天限制使用1000条所以不建议使用
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="longUrl"></param>
        /// <returns></returns>
        public static string GetShortUrlWx(this HtmlHelper helper, string longUrl)
        {
            WeixinShortLinkResult result = new WeixinShortLinkResult();

            var userId = WebSecurity.CurrentUserId;
            IUserProfileRepository repo = Bootstrapper.Initialise().Resolve<IUserProfileRepository>();
            var user = repo.FindAll().SingleOrDefault(s => s.UserId == userId);
            string token = AccessTokenContainer.TryGetToken(user.AppId, user.AppSecret);

            var data = new WeixinShortLinkSend() { access_token = token, long_url = longUrl };

            result = CommonApi.GetShortLink(token, data);
            return result.short_url;
        }