Example #1
0
        /// <summary>
        /// 通过appID和appsecret获取Access_token
        /// </summary>
        /// <returns></returns>
        private static MAccessToken GetAccesstokenFromWX()
        {
            try
            {
                //// 微信API地址
                string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

                //// 微信平台的APPID
                string appid = WebConfigeOpert.GetWXappid();

                //// 微信平台的密码
                string secret = WebConfigeOpert.GetWXAppSecret();

                string       strUrl = wxAPIURL + "cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
                MAccessToken mode   = PublicTools.HttpGetRequest <MAccessToken>(strUrl);
                LogOpert.AddWeiXinMessage("获取wxAccesstoken结果:" + mode.access_token);
                return(mode);
            }
            catch (Exception ex)
            {
                LogOpert.AddWeiXinMessage("获取wxAccesstoken异常:" + ex);
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// 根据code获取openid(一个code只能查询一次)
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public String GetOpenIdByCode(String code)
        {
            //// 微信API地址
            string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

            //// 微信平台的APPID
            string appid = WebConfigeOpert.GetWXappid();

            //// 微信平台的密码
            string secret = WebConfigeOpert.GetWXAppSecret();

            secret = string.IsNullOrEmpty(secret) ? "7233eefb8657eb0f6a1ed7224fdc0e7f" : secret;

            // 换取access_token 其中包含了openid
            // 这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。
            String strUrl = string.Format("{0}sns/oauth2/access_token?appid={1}&secret={2}&code={3}&grant_type=authorization_code", wxAPIURL, appid, secret, code);

            MWXUserSampleInfo model = null;

            model = PublicTools.HttpGetRequest <MWXUserSampleInfo>(strUrl);

            if (model != null)
            {
                return(model.openid);
            }

            return(string.Empty);
        }
Example #3
0
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public string GetMenu()
        {
            //// 微信API地址
            string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

            string strUrl = wxAPIURL + "cgi-bin/menu/get?access_token=" + WXTokenOpert.GetAccessToken();

            return(PublicTools.HttpGetRequestAsString(strUrl));
        }
Example #4
0
        /// <summary>
        /// 删除所有菜单
        /// </summary>
        /// <returns></returns>
        public MwxResult DeletMenu()
        {
            //// 微信API地址
            string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

            string strUrl = wxAPIURL + "cgi-bin/menu/delete?access_token=" + WXTokenOpert.GetAccessToken();

            return(PublicTools.HttpGetRequest <MwxResult>(strUrl));
        }
Example #5
0
        /// <summary>
        /// 向微信服务器请求创建自定义菜单
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public MwxResult AddMenu(string jsonStr)
        {
            //// 处理结果对象
            MwxResult model = null;

            //// 微信API地址
            string interfaceUrl = WebConfigeOpert.GetWXAPIURL();

            interfaceUrl = string.IsNullOrEmpty(interfaceUrl) ? "https://api.weixin.qq.com/" : interfaceUrl;

            //声明一个HttpWebRequest请求
            interfaceUrl = string.Format("{0}cgi-bin/menu/create?access_token={1}", interfaceUrl, WXTokenOpert.GetAccessToken());

            model = PublicTools.HttpPostRequest <MwxResult>(interfaceUrl, jsonStr);

            return(model);
        }
Example #6
0
        /// <summary>
        /// 修改用户备注信息
        /// </summary>
        /// <param name="openid">被修改这者的openid</param>
        /// <param name="remark">修改后的备注信息</param>
        /// <returns></returns>
        public MwxResult CheckUserRemark(string openid, string remark)
        {
            //// 处理结果对象
            MwxResult model = null;

            //// 微信API地址
            string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

            //声明一个HttpWebRequest请求
            string interfaceUrl = string.Format("{0}cgi-bin/user/info/updateremark?access_token={1}", wxAPIURL, WXTokenOpert.GetAccessToken());

            string jsonStr = "{" + "\"openid\";" + openid + ",\"remark\":" + remark + "}";

            model = PublicTools.HttpPostRequest <MwxResult>(interfaceUrl, jsonStr);

            return(model);
        }
Example #7
0
        /// <summary>
        /// 根据OpenID 获取用户基本信息(需关注公众号)
        /// </summary>
        /// <param name="openId"></param>
        public MWXUserInfo GetUserInfo(string openId)
        {
            //// 微信API地址
            string wxAPIURL = WebConfigeOpert.GetWXAPIURL();

            String      strUrl = string.Format("{0}cgi-bin/user/info?access_token={1}&openid={2}&lang=zh_CN", wxAPIURL, WXTokenOpert.GetAccessToken(), openId);
            MWXUserInfo model  = PublicTools.HttpGetRequest <MWXUserInfo>(strUrl);

            //// 对获取大的用户信息的关注日期和头像做处理
            if (model != null)
            {
                model.subscribe_time = PublicTools.GetDateTimeWithSecond(Convert.ToInt64(model.subscribe_time)).ToString("yyyy-MM-dd HH:mm:ss");
                model.headimgurl     = model.headimgurl.Substring(0, model.headimgurl.LastIndexOf('/') + 1);
            }

            return(model);
        }
Example #8
0
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message"></param>
        public static bool AddMessage(string message)
        {
            try
            {
                //// 记录接收到的微信消息日志
                //// 获取微信消息日志存储位置

                //// 是否记录微信消息日志 1:记录 0:不记录 默认为0
                string isAddWXMessageLog = WebConfigeOpert.GetIsAddWXMessageLog();

                //// 如果不记录日志,那么直接返回
                if (isAddWXMessageLog != "1")
                {
                    return(true);
                }

                //// 微信消息日志记录路径
                string wxMessageLogPath = WebConfigeOpert.GetWXMessageLogPath();

                if (string.IsNullOrEmpty(wxMessageLogPath))
                {
                    return(true);
                }

                DateTime dateNow = System.DateTime.Now;

                //// 检查创建文件夹
                CreateDirectoryByMoth(dateNow, wxMessageLogPath);

                string filePath = string.Format("{0}//{1}//{2}//{3}.txt", wxMessageLogPath, dateNow.ToString("yyyy"), dateNow.ToString("MM"), dateNow.ToString("yyyyMMdd"));
                //// 按照日期创建文件
                CreateFile(filePath);

                //// 写文件
                FileOpert.WriteFile(filePath, message, true);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }