示例#1
0
        /// <summary>
        /// 更新小程序用户信息
        /// </summary>
        /// <returns></returns>
        public WxResponseResultModel UpdateSessionKey(WxMiniprogramUserModel model)
        {
            WxResponseResultModel result = new WxResponseResultModel();
            bool flag = false;

            try
            {
                string querySql = string.Format(@"UPDATE kyk_miniprogram_user SET session_key='{0}' WHERE openid='{1}'", model.Session_key, model.OpenId);
                flag = Convert.ToInt32(DBCommon.MYSQLDB.ExecuteNonQuery(querySql)) > 0 ? true : false;
                if (flag)
                {
                    result.Code    = "ok";
                    result.Message = "更新用户信息成功";
                }
                else
                {
                    result.Message = "更新用户信息失败";
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                Logging.Error("更新用户信息出错:" + ex.Message);
            }
            return(result);
            //return new WxResponseResultModel { Code = "", Message = "" };
        }
示例#2
0
        public WxResponseResultModel GetLocalAccessToken()
        {
            string resString            = File.ReadAllText(@"C: \Users\龙隐\source\repos\WeChatTransferBack\WeChatTransferBack\Access_Token\access_token.json");
            WxResponseResultModel model = JsonConvert.DeserializeObject <WxResponseResultModel>(resString);

            return(model);
        }
示例#3
0
        /// <summary>
        /// 新增小程序用户信息
        /// </summary>
        /// <returns></returns>
        public WxResponseResultModel InsertMiniprogramUserInfo(WxMiniprogramUserModel model)
        {
            WxResponseResultModel result = new WxResponseResultModel();
            bool flag = false;

            try
            {
                string querySql = string.Format(@"INSERT INTO kyk_miniprogram_user (openid,session_key,unionid)
VALUES ('{0}', '{1}','{2}');", model.OpenId, model.Session_key, model.Unionid);
                flag = Convert.ToInt32(DBCommon.MYSQLDB.ExecuteNonQuery(querySql)) > 0 ? true : false;
                if (flag)
                {
                    result.Code    = "ok";
                    result.Message = "保存用户信息成功";
                }
                else
                {
                    result.Message = "保存用户信息失败";
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                Logging.Error("保存用户信息出错:" + ex.Message);
            }
            return(result);
            //return new WxResponseResultModel { Code = "", Message = "" };
        }
示例#4
0
        /// <summary>
        /// 获取access_token
        /// </summary>
        public string GetAccessToken()
        {
            string access_token = string.Empty;
            string resString;
            WeChatAccessTokenDAL  dal   = new WeChatAccessTokenDAL();
            WxResponseResultModel model = dal.GetLocalAccessToken();

            if (string.IsNullOrWhiteSpace(model.Access_Token)) //尚未保存过access_token
            {
                resString = Untils.GetToken(AppConsts.WxOpenAppId, AppConsts.WxOpenAppSecret);
                WxResponseResultModel newModel = JsonConvert.DeserializeObject <WxResponseResultModel>(resString);
                UpdateAccessToken(newModel);
                access_token = newModel.Access_Token;
            }
            else
            {
                if (Untils.TokenExpired(model)) //access_token过期
                {
                    resString = Untils.GetToken(AppConsts.WxOpenAppId, AppConsts.WxOpenAppSecret);
                    WxResponseResultModel newModel = JsonConvert.DeserializeObject <WxResponseResultModel>(resString);
                    UpdateAccessToken(newModel);
                    access_token = newModel.Access_Token;
                }
                else
                {
                    return(model.Access_Token);
                }
            }
            return(access_token);
        }
示例#5
0
        //public string Test()
        public WxResponseResultModel Test()
        {
            WxResponseResultModel rsEntity = new WxResponseResultModel();

            rsEntity.Code    = "200";
            rsEntity.Message = "这是后台传的测试方法";
            //return "这是后台传的测试方法";
            return(rsEntity);
        }
示例#6
0
        /// <summary>
        /// 验证Token是否过期
        /// </summary>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public static bool TokenExpired(WxResponseResultModel model)
        {
            double createTime = Convert.ToDouble(GetTimeStamp(model.CreateTime)); //"1586837219832.68"
            double nowTime    = Convert.ToDouble(GetTimeStamp(DateTime.Now));     //"1586848867742.19"

            if (model.ErrCode == "42001" || (nowTime - createTime) / 3600000 > 2)
            {
                return(true);
            }
            return(false);
        }
示例#7
0
        ///// <summary>
        ///// 将获取的access_token写入本地文件
        ///// </summary>
        public void UpdateAccessToken(WxResponseResultModel model)
        {
            model.CreateTime = DateTime.Now;
            WeChatAccessTokenDAL dal = new WeChatAccessTokenDAL();

            if (model.ErrCode == "45009")
            {
                throw new Exception("当天接口调用次数达到上限" + model.ErrMsg);
            }
            dal.UpdateLocalAccessToken(model);
        }
示例#8
0
        public WxResponseResultModel Login(string code)
        {
            WxResponseResultModel rsEntity = new WxResponseResultModel();

            StringBuilder urlStr = new StringBuilder();

            urlStr.AppendFormat(@"https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code",
                                AppConsts.WxOpenAppId,
                                AppConsts.WxOpenAppSecret,
                                code
                                );
            string resString          = Utils.httpGetStr(urlStr.ToString());
            WxAuthenticateModel model = Newtonsoft.Json.JsonConvert.DeserializeObject <WxAuthenticateModel>(resString);

            if (model != null)
            {
                WxMiniProgramUserBLL   bll  = new WxMiniProgramUserBLL();
                WxMiniprogramUserModel user = new WxMiniprogramUserModel();
                user.OpenId      = model.OpenId;
                user.Unionid     = model.Unionid;
                user.Session_key = model.Session_key;
                //先判断用户是否存在,返回true则未存在用户,需添加
                if (!string.IsNullOrEmpty(user.OpenId) && !string.IsNullOrEmpty(user.Session_key))
                {
                    if (bll.IfExistsMiniprogramUserInfo(user.OpenId))
                    {
                        //添加用户

                        rsEntity = bll.InsertMiniprogramUserInfo(user);
                    }
                    else
                    {
                        //存在用户,因session_key过期,需要更新
                        rsEntity = bll.UpdateSessionKey(user);
                        //rsEntity.Message = "已存在该用户信息";
                    }
                }
            }
            else
            {
                rsEntity.Message = "转换用户数据出错!";
            }

            return(rsEntity);
        }
示例#9
0
        public void UpdateLocalAccessToken(WxResponseResultModel model)
        {
            string resString = JsonConvert.SerializeObject(model);

            File.WriteAllText(@"C:\Users\龙隐\source\repos\WeChatTransferBack\WeChatTransferBack\Access_Token\access_token.json", resString, Encoding.UTF8);
        }