public async Task <ContentResult> Post(string signature, string timestamp, string nonce, string openid) { //验证微信签名 if (!WxBase.CheckSignature(signature, timestamp, nonce)) { Console.WriteLine($"receive error check signature:{signature} with {timestamp},{nonce}"); return(new ContentResult() { StatusCode = 401 }); } //初始化菜单 await WxBase.InitMenu(); using (Stream stream = Request.Body) { byte[] buffer = new byte[Request.ContentLength.Value]; await stream.ReadAsync(buffer, 0, buffer.Length); string xml = Encoding.UTF8.GetString(buffer); Console.WriteLine($"收到:{xml}"); var msg = WxTextMsg.FromXml(xml); if (msg.MsgType.Contains("text") && msg.Content.Contains("磁芯大战")) { WxUser user = null; if (await DbEntityManager.Exist <WxUser>("openid", openid)) { //get user from db user = await DbEntityManager.SelectOne <WxUser>("openid", openid); //update salt only user.ResetSalt(); await DbEntityManager.Update(user); } else { // var info = await WxBase.GetUserInfo(openid); Console.WriteLine($"获取:{info.nickname} 信息"); user = WxUser.CreateFromWxUserInfo(info); await DbEntityManager.Insert(user); } // var article = new WxArticle("MagCore - 磁芯大战", "进入房间创建游戏", HelperConfig.Current.WxInterfaceHost + "assets/images/icon.png", HelperConfig.Current.WxInterfaceHost + "pages/creator.html" + "?t=" + DateTime.Now.Ticks.ToString() + "&oid=" + openid + $"&name={user.nickname}({HashManager.Md5(openid).Substring(12, 4)})" + "&s=" + user.salt); var reply = new WxArticleMsg(msg.FromUserName, msg.ToUserName, msg.CreateTime, new WxArticle[] { article }); string text = reply.ToXml(); Console.WriteLine($"回复:{text}"); return(new ContentResult() { StatusCode = 200, Content = text }); } else { // var defaultReply = new WxTextMsg(msg.FromUserName, msg.ToUserName, msg.CreateTime, "未正确识别,磁芯大战比赛期间,其他回复暂时停止服务"); return(new ContentResult() { StatusCode = 200, Content = defaultReply.ToXml() }); } } }
public async Task <ContentResult> New([FromBody] dynamic json) { if (json == null) { return new ContentResult() { StatusCode = 404 } } ; string openid = json.oid.ToString(); string salt = json.s.ToString(); int color = Convert.ToInt32(json.color.ToString()); string map = json.map.ToString(); if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(salt) || color < 0 || string.IsNullOrEmpty(map)) { return new ContentResult() { StatusCode = 404 } } ; else { var user = await DbEntityManager.SelectOne <WxUser>("openid", openid); if (user.salt != salt) { return new ContentResult() { StatusCode = 401 } } ; else { string name = $"{user.nickname}({HashManager.Md5(openid).Substring(12, 4)})"; //init integral UserIntegral userIntegral = null; if (await DbEntityManager.Exist <UserIntegral>("wx_openid", openid)) { userIntegral = await DbEntityManager.SelectOne <UserIntegral>("wx_openid", openid); } else { userIntegral = new UserIntegral() { wx_openid = openid, mc_integral = 0, total_integral = 0 }; await DbEntityManager.Insert(userIntegral); } try { //join or creat a game bool isMatch = userIntegral.mc_integral >= HelperConfig.Current.IntegralToJoin; string match = isMatch ? "true" : "false"; var result = await GameManager.JoinOneGame( isMatch, map, name, color, openid); return(new ContentResult() { StatusCode = 200, Content = $"{{\"gid\":\"{result.gid}\",\"pid\":\"{result.pid}\",\"pidx\":{result.pidx},\"match\":{match}}}" }); } catch (GameJoinedException) { return(new ContentResult() { StatusCode = 400, Content = "请等待上局结束" }); } } } }