public async Task <MiniprogramLoginResult> Login([FromBody] LoginDTO login) { jscode2session data = await GetOpenIdAsync(login.Code); if (data == null) { return(new MiniprogramLoginResult { StatusCode = MiniProgramResultCode.error }); } var dbuser = await _userService.GetByOpenIdAsync(data.openid); if (dbuser == null) { return(new MiniprogramLoginResult() { StatusCode = MiniProgramResultCode.unregistered, Message = "用户未注册" }); } else { return(new MiniprogramLoginResult { StatusCode = MiniProgramResultCode.ok, Data = new MiniprogramLoginResultData { Id = dbuser.Id, SessionId = data.session_key } }); } }
public async Task <MiniprogramRegisterResult> Register([FromBody] RegisterDTO register) { jscode2session data = await GetOpenIdAsync(register.Code); var res = await _userService.RegisterAsync(data, register.RawData); return(res); }
private async Task <jscode2session> GetOpenIdAsync(string code) { var minprogram = _configuration.GetSection("WxMiniProgram"); string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={minprogram["AppId"]}&secret={minprogram["AppSecret"]}&js_code={code}&grant_type=authorization_code"; var responseString = await _clientFactory.CreateClient().GetStringAsync(url); jscode2session data = JsonConvert.DeserializeObject <jscode2session>(responseString); return(data); }
public async Task <MiniprogramRegisterResult> Register([FromBody] RegisterDTO register) { jscode2session data = await GetOpenIdAsync(register.Code); if (data == null) { return(new MiniprogramRegisterResult { StatusCode = MiniProgramResultCode.error }); } var res = await _userService.RegisterAsync(data, register.RawData); return(res); }
public async Task <MiniprogramRegisterResult> RegisterAsync(jscode2session data, string rowData) { var dbuser = await databaseFixture.Db.WxMiniprogramUser.FindAsync(m => m.OpenId == data.openid); if (dbuser != null) { var res = new MiniprogramRegisterResult() { StatusCode = MiniProgramResultCode.error }; } RegisterUserModel userModel = JsonConvert.DeserializeObject <RegisterUserModel>(rowData); WxMiniprogramUser user = new WxMiniprogramUser { CreateTime = DateTime.Now.ToTimeStamp(), OpenId = data.openid, UnionId = data.unionid, AvatarUrl = userModel.AvatarUrl, City = userModel.City, Country = userModel.Country, Gender = userModel.Gender, Language = userModel.Language, NickName = userModel.NickName, Province = userModel.Province }; long userid = await databaseFixture.Db.WxMiniprogramUser.InsertReturnIdAsync(user); return(new MiniprogramRegisterResult { Data = new MiniprogramRegisterData { Id = userid, SessionId = data.session_key }, StatusCode = MiniProgramResultCode.ok }); }