Пример #1
0
        public MyResult <object> Register(WxRegisterDto model)
        {
            MyResult result = new MyResult();

            if (string.IsNullOrEmpty(model.Code))
            {
                return(result.SetStatus(ErrorCode.InvalidData, "code 无效"));
            }
            if (string.IsNullOrEmpty(model.EncryptedData))
            {
                return(result.SetStatus(ErrorCode.InvalidData, "EncryptedData 无效"));
            }
            if (string.IsNullOrEmpty(model.Iv))
            {
                return(result.SetStatus(ErrorCode.InvalidData, "Iv 无效"));
            }
            //调登陆获取key
            var code2SessionUrl  = $"https://api.weixin.qq.com/sns/jscode2session?appid={Constants.WxAppId}&secret={Constants.WxSecret}&js_code={model.Code}&grant_type=authorization_code";
            var rep              = HttpUtil.GetString(code2SessionUrl);
            var repObj           = rep.GetModel <Code2SessionRep>();
            var session_key      = repObj.Session_Key;
            var deCryptedData    = SecurityUtil.AES_128_CBC_Decrypt(model.EncryptedData, session_key, model.Iv);
            var deCryptedDataObj = deCryptedData.GetModel <Code2SessionRep>();
            var hasUser          = base.First <User>(predicate => predicate.OpenId.Equals(deCryptedDataObj.OpenId));

            if (hasUser != null)
            {
                return(result.SetStatus(ErrorCode.HasValued, "数据已存在"));
            }
            User user = new User
            {
                OpenId     = deCryptedDataObj.OpenId,
                NickName   = deCryptedDataObj.NickName,
                SessionKey = session_key,
                Pic        = deCryptedDataObj.AvatarUrl
            };

            if (model.referrer.HasValue)
            {
                user.RefId = (int)model.referrer;
            }
            base.Add(user, true);
            return(result);
        }
Пример #2
0
 public MyResult <object> Register([FromBody] WxRegisterDto model)
 {
     return(WxService.Register(model));
 }