コード例 #1
0
        public static string TryWeappAutoLogin(this IAccessControl ac, JObject data, out string errMsg)
        {
            errMsg = null;

            var app     = ac.Context.RequestServices.GetService <IWeChatContainer>().GetApp();
            var session = data.GetValue("sessionid", key => SessionContainer.GetSession(key));

            if (session == null)
            {
                // session未建立,通过code登录换取session
                var result = WeChatApi.WxLogin(app, data.GetValue("code", ""));
                if (result.ErrorCodeValue == 0)
                {
                    // success,成功时更新session
                    session = SessionContainer.UpdateSession(null, result.openid, result.session_key, result.unionid);
                }
                else
                {
                    errMsg = result.errmsg;
                }
            }

            if (session != null)
            {
                // 根据OpenID自动登录
                var service = ac.Context.RequestServices.GetService <IUserService>();
                var user    = service.GetByAuth(app.Id, session.OpenId);

                if (user == null)
                {
                    // OpenID对应的用户不存在,解密客户端数据
                    var cryptedData = data.GetValue("encryptedData", "");
                    var iv          = data.GetValue("iv", "");
                    var userInfo    = EncryptHelper.DecodeUserInfoBySessionId(session.Key, cryptedData, iv);

                    // 自动创建用户
                    var mobile        = data.GetValue <string>("mobile");
                    var wechatService = ac.Context.RequestServices.GetService <IWechatService>();
                    user = wechatService.WeappRegist(app, userInfo, mobile);
                }

                return($"{user.HashId}-{session.SessionKey}");
            }

            return(null);
        }
コード例 #2
0
        public IActionResult WxLogin([FromBody] JObject data)
        {
            SessionBag sessionBag = null;
            var        sessionId  = data.GetValue("sessionid", "");

            if (sessionId.HasValue())
            {
                sessionBag = SessionContainer.GetSession(sessionId);
            }

            string message = null;

            if (sessionBag == null)
            {
                var code   = data.GetValue("code", "");
                var result = WeChatApi.WxLogin(app, code);
                if (result.ErrorCodeValue == 0)
                {
                    sessionBag = SessionContainer.UpdateSession(null, result.openid, result.session_key, result.unionid);
                }
                else
                {
                    message = result.errmsg;
                }
            }
            else
            {
                var obj = AppCtx.Session.ExecuteScalar(
                    new RequestContext("wx", "exec_wx_login")
                    .SetParam(new
                {
                    appid    = app.Id,
                    authtype = "wechat",
                    authid   = sessionBag.OpenId
                })
                    );
                if (obj == null)
                {
                    var cryptedData     = data.GetValue("encryptedData", "");
                    var iv              = data.GetValue("iv", "");
                    var decodedUserInfo = EncryptHelper.DecodeUserInfoBySessionId(sessionBag.Key, cryptedData, iv);
                    obj = AppCtx.Session.ExecuteScalar(
                        new RequestContext("wx", "exec_wx_regist")
                        .SetParam(new
                    {
                        newid    = App.IdWorker.NextId(),
                        nickname = decodedUserInfo.nickName,
                        photo    = decodedUserInfo.avatarUrl,
                        province = decodedUserInfo.province,
                        city     = decodedUserInfo.city,
                        country  = decodedUserInfo.country,
                        appid    = app.Id,
                        authtype = "wechat",
                        authid   = decodedUserInfo.openId,
                        unionid  = decodedUserInfo.unionId
                    })
                        );
                }
                return(Json(new
                {
                    success = true,
                    token = $"{sessionBag.Key}",
                    message = "ok"
                }));
            }

            return(Json(new
            {
                success = false,
                message
            }));
        }