public ApiResult WeixinLogin(dynamic data)
        {
            ApiResult apiResult   = new ApiResult();
            var       checkResult = Util.CheckParameters(
                new Parameter {
                Value = data?.code, Msg = "code不能为空"
            },
                new Parameter {
                Value = data?.userinfo.ToString(), Msg = "userInfo不能为空"
            },
                new Parameter {
                Value = data?.appcode, Msg = "appcode不能为空!"
            }
                );

            if (!checkResult.OK)
            {
                apiResult.success = false;
                apiResult.status  = ApiStatusCode.InvalidParam;
                apiResult.message = checkResult.Msg;
                return(apiResult);
            }

            try
            {
                //根据code查找APPID与Secret,获取微信session、openid和unionid
                string appcode = data.appcode.ToString();
                BaseBLL <weixin_applet> weixinAppletBll = new BaseBLL <weixin_applet>();
                weixin_applet           weixinApplet    = weixinAppletBll.Find(x => x.appcode == appcode);
                WeixinXAPI weixinxapi   = new WeixinXAPI(weixinApplet.appid, weixinApplet.secret);
                string     str          = weixinxapi.codeToSession(data.code.ToString());
                JObject    session_json = JObject.Parse(str);
                if (session_json["errcode"].To <string>().IsNotNullAndEmpty())
                {
                    apiResult.success = false;
                    apiResult.status  = ApiStatusCode.NotFound;
                    apiResult.message = str;
                    return(apiResult);
                }
                string openid      = session_json["openid"].ToString();
                string session_key = session_json["session_key"].ToString();
                string unionid     = session_json["unionid"].To <string>();
                if (StringHelper.IsNullOrEmpty(unionid) && !StringHelper.IsNullOrEmpty(data?.encryptedData) && !StringHelper.IsNullOrEmpty(data?.iv))
                {
                    string  info         = DEncrypt.XCXDecrypt(data?.encryptedData.ToString(), session_key, data?.iv.ToString());
                    JObject userInfoJson = JObject.Parse(info);
                    unionid = userInfoJson["unionId"].To <string>();
                }

                weixin_user userInfo = new weixin_user
                {
                    openid           = openid,
                    unionid          = unionid,
                    nickname         = data.userinfo["nickName"],
                    sex              = data.userinfo["gender"],
                    language         = data.userinfo["language"],
                    city             = data.userinfo["city"],
                    province         = data.userinfo["province"],
                    country          = data.userinfo["country"],
                    headimgurl       = data.userinfo["avatarUrl"],
                    source_code      = appcode,
                    weixin_applet_id = weixinApplet.id
                };

                if (Util.isNotNull(userInfo.unionid))
                {
                    //查询当前openid的用户是否存在
                    //如果不存在则要创建,创建时,先创建 iuser ,再创建 weixin_user
                    BaseBLL <weixin_user> weixinUserBll = new BaseBLL <weixin_user>();
                    var  weixinUser  = weixinUserBll.Find(x => x.unionid == unionid);
                    bool first_login = false;
                    //可能是第一次登陆,在网页端登陆
                    if (weixinUser == null)
                    {
                        //微信开发平台的openid与小程序的openid不一致
                        first_login = true;
                        //先存iuser
                        var             iuser    = new iuser();
                        BaseBLL <iuser> iuserBll = new BaseBLL <iuser>();
                        iuser.random     = sys.getRandomStr();
                        iuser.createtime = DateTime.Now;
                        iuser.updatetime = DateTime.Now;
                        iuser            = iuserBll.Add(iuser);
                        //再存weixin_user
                        userInfo.uid            = iuser.id;
                        userInfo.sub_time       = DateTime.Now;
                        userInfo.first_sub_time = DateTime.Now;
                        LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo));
                        weixinUser = weixinUserBll.Add(userInfo);
                    }
                    else
                    {
                        weixinUser.nickname   = userInfo.nickname;
                        weixinUser.headimgurl = userInfo.headimgurl;
                        LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo));
                        weixinUserBll.Update(weixinUser);
                    }

                    apiResult.success = true;
                    apiResult.data    = new { first_login = first_login, weixinUser = weixinUser };
                    apiResult.status  = ApiStatusCode.OK;
                }
                else
                {
                    return(new ApiResult()
                    {
                        success = false,
                        message = "unionid不能为空,小程序必须绑定开放平台"
                    });
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
                apiResult.success = false;
                apiResult.status  = ApiStatusCode.Error;
            }

            return(apiResult);
        }
Exemple #2
0
        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="code">请求微信返回的code</param>
        /// <param name="state">请求微信的参数state</param>
        /// <returns></returns>
        public ApiResult LoginReturn(string code, string state)
        {
            ApiResult apiResult = new ApiResult();

            LogHelper.Info("code:" + code + ",state:" + state);
            ////必须用cookie或者session
            //var session = HttpContext.Current.Session["session_weixin_login_state"];
            //string session_state = session == null ? "" : session.ToString();
            //string cookie_state = CookieHelper.GetCookieValue("cookie_weixin_login_state");
            //LogHelper.Info("session_state:" + session_state + ",cookie_state:" + cookie_state);
            //if (state == _state)
            //{
            BaseBLL <weixin_open> bll = new BaseBLL <weixin_open>();
            var    weixin_open        = bll.Find(o => o.appid != null && o.secret != null);
            string appid  = weixin_open.appid;
            string secret = weixin_open.secret;

            WeixinOpenAPI api = new WeixinOpenAPI(appid, secret);

            //string access_token = weixin_open.access_token;
            //string access_token_time = weixin_open.access_token_time == null ? "" : weixin_open.access_token_time.Value.ToString();

            api.GetAccessToken(code);
            LogHelper.Info("access_token:" + api.access_token);
            string user_json = api.GetUserInfo(api.openid);

            LogHelper.Info("user_json:" + user_json);
            JObject obj    = JObject.Parse(user_json);
            string  openid = obj["openid"] == null ? "" : obj["openid"].ToString();

            LogHelper.Info("openid:" + openid);
            BaseBLL <weixin_applet> weixinAppletBll = new BaseBLL <weixin_applet>();
            weixin_applet           weixinApplet    = weixinAppletBll.Find(x => x.appcode == "ZHIYIN");
            weixin_user             userInfo        = new weixin_user
            {
                openid           = obj["openid"] == null ? "" : obj["openid"].ToString(),
                unionid          = obj["unionid"] == null ? "" : obj["unionid"].ToString(),
                nickname         = obj["nickname"] == null ? "" : obj["nickname"].ToString(),
                sex              = obj["sex"] == null ? 0 : int.Parse(obj["sex"].ToString()),
                language         = obj["language"] == null ? "" : obj["language"].ToString(),
                city             = obj["city"] == null ? "" : obj["city"].ToString(),
                province         = obj["province"] == null ? "" : obj["province"].ToString(),
                country          = obj["country"] == null ? "" : obj["country"].ToString(),
                headimgurl       = obj["headimgurl"] == null ? "" : obj["headimgurl"].ToString(),
                source_code      = weixinApplet.appcode,
                weixin_applet_id = weixinApplet.id
            };

            if (!Util.isNotNull(openid))
            {
                return(new ApiResult()
                {
                    success = false,
                    message = "openid为空"
                });
            }
            #region 微信登陆,保存信息
            //如果不存在则要创建,创建时,先创建 iuser ,再创建 weixin_user
            bool first_login = false;
            BaseBLL <weixin_user> weixinUserBll = new BaseBLL <weixin_user>();
            if (Util.isNotNull(userInfo.unionid))
            {
                var weixinUser = weixinUserBll.Find(o => o.unionid == userInfo.unionid);
                //可能是第一次登陆,在网页端登陆
                if (weixinUser == null)
                {
                    //微信开发平台的openid与小程序的openid不一致
                    //var _weixin_user = weixinUserBll.Find(o => o.nickname == userInfo.nickname);
                    first_login = true;
                    //先存iuser
                    var             iuser    = new iuser();
                    BaseBLL <iuser> iuserBll = new BaseBLL <iuser>();
                    iuser.random     = sys.getRandomStr();
                    iuser.createtime = DateTime.Now;
                    iuser.updatetime = DateTime.Now;
                    iuser            = iuserBll.Add(iuser);
                    //再存weixin_user
                    userInfo.uid            = iuser.id;
                    userInfo.sub_time       = DateTime.Now;
                    userInfo.first_sub_time = DateTime.Now;
                    LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo));
                    weixinUser = weixinUserBll.Add(userInfo);
                }
                else
                {
                    weixinUser.nickname   = userInfo.nickname;
                    weixinUser.headimgurl = userInfo.headimgurl;
                    LogHelper.Info("first_login:"******",userInfo:" + Newtonsoft.Json.JsonConvert.SerializeObject(userInfo));
                    weixinUserBll.Update(weixinUser);
                }

                apiResult.success = true;
                apiResult.data    = new { first_login = first_login, weixinUser = weixinUser };
                apiResult.status  = ApiStatusCode.OK;
            }
            else
            {
                return(new ApiResult()
                {
                    success = false,
                    message = "微信开发平台未获取到unionid"
                });
            }
            #endregion
            //}
            //else
            //{
            //    return new ApiResult()
            //    {
            //        success = false,
            //        message = "请求超时"
            //    };
            //}
            return(apiResult);
        }