using Newtonsoft.Json.Linq;
using System.Net;
using System.Text;

namespace Utils
{
    /// <summary>
    /// 微信公众号工具类
    /// </summary>
    public class VxinUtils
    {

        /// <summary>
        /// 微信token
        /// </summary>
        public static string WeiXinToken { get { return AppSettingsHelper.Configuration["WxPay:WeiXinToken"]; } }

        /// <summary>
        /// 微信appID
        /// </summary>
        public static string AppID { get { return AppSettingsHelper.Configuration["WxPay:AppID"]; } }


        /// <summary>
        /// 微信Appsecret
        /// </summary>
        public static string Appsecret { get { return AppSettingsHelper.Configuration["WxPay:Appsecret"]; } }


        /// <summary>
        /// 获得access_token地址
        /// </summary>
        public static string Access_token_URL { get { return string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppID, Appsecret); } }

        /// <summary>
        /// 通过code换取网页授权access_token地址
        /// </summary>
        public static string Web_Access_token_URL { get { return string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&grant_type=authorization_code&code=", AppID, Appsecret); } }


        /// <summary>
        /// 微信菜单创建提交地址
        /// </summary>
        public static string MENU_POST_URL { get { return "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="; } }


        /// <summary>
        /// 微信获取用户分组地址
        /// </summary>
        public static string Group_Get_Url { get { return "https://api.weixin.qq.com/cgi-bin/groups/get?access_token="; } }


        /// <summary>
        /// 微信修改用户分组地址
        /// </summary>
        public static string Group_Update_Url { get { return "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="; } }



        /// <summary>
        /// 微信获取用户所属分组地址
        /// </summary>
        public static string Group_GetUserGroup_Url { get { return "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token="; } }

        /// <summary>
        /// 拉取用户信息(需scope为 snsapi_userinfo)
        /// 如果网页授权作用域为snsapi_userinfo，则此时开发者可以通过access_token和openid拉取用户信息了。
        /// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        /// </summary>
        public static string GetUserUserinfo_Url { get { return "https://api.weixin.qq.com/sns/userinfo?lang=zh_CN&access_token="; } }


        /// <summary>
        /// 获取用户的OpenId
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetOpenID(string code)
        {
            var openid = "";
            using (var wl = new WebClient())
            {
                wl.Headers.Add(HttpRequestHeader.Accept, "json");
                wl.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=UTF-8");
                wl.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0");
                wl.Encoding = Encoding.UTF8;
                openid = wl.DownloadString(Web_Access_token_URL + code);
            }
            if (!string.IsNullOrEmpty(openid))
            {
                var token = JObject.Parse(openid).SelectToken("openid");
                if (token != null)
                {
                    openid = token.ToString();
                }
                else
                {
                    openid = "";
                }

            }
            return openid;
        }

    }
}
