///<summary> ///向数据库中添加一条记录 ///</summary> ///<param name="model">要添加的实体</param> public bool Insert(wechat model) { const string sql = @"INSERT INTO [dbo].[wechat] (objectId,openId,inopenId) VALUES (@objectId,@openId,@inopenId)"; int res = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@objectId", model.objectId.ToDBValue()), new SqlParameter("@openId", model.openId.ToDBValue()), new SqlParameter("@inopenId", model.inopenId.ToDBValue())); return(res > 0); }
/// <summary> /// 查询单个模型实体 /// </summary> /// <param name="id">objectId</param>); /// <returns>实体</returns>); public wechat QuerySingleById(string objectId) { const string sql = "SELECT TOP 1 objectId,openId,inopenId from wechat WHERE [objectId] = @objectId"; using (var reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@objectId", objectId))) { if (reader.HasRows) { reader.Read(); wechat model = SqlHelper.MapEntity <wechat>(reader); return(model); } else { return(null); } } }
///<summary> ///分页查询一个集合 ///</summary> ///<param name="index">页码</param> ///<param name="size">页大小</param> ///<param name="wheres">条件匿名类</param> ///<param name="orderField">排序字段</param> ///<param name="isDesc">是否降序排序</param> ///<returns>实体集合</returns> public IEnumerable <wechat> QueryList(int index, int size, object wheres = null, string orderField = "objectId", bool isDesc = true) { List <SqlParameter> list = null; string where = wheres.parseWheres(out list); orderField = string.IsNullOrEmpty(orderField) ? "objectId" : orderField; var sql = SqlHelper.GenerateQuerySql("wechat", new string[] { "objectId", "openId", "inopenId" }, index, size, where, orderField, isDesc); using (var reader = SqlHelper.ExecuteReader(sql, list.ToArray())) { if (reader.HasRows) { while (reader.Read()) { wechat model = SqlHelper.MapEntity <wechat>(reader); yield return(model); } } } }
/// <summary> /// 根据主键更新一条记录 /// </summary> /// <param name="model">更新后的实体</param> /// <returns>是否成功</returns> public bool Update(wechat model) { const string sql = @"UPDATE [dbo].[wechat] SET openId=@openId,inopenId=@inopenId WHERE [objectId] = @objectId"; return(SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@objectId", model.objectId.ToDBValue()), new SqlParameter("@openid", model.openId.ToDBValue()), new SqlParameter("@inopenId", model.inopenId.ToDBValue())) > 0); }
/// <summary> /// 微信端注册与绑定接口 /// </summary> /// <param name="v1"></param> /// <param name="model"></param> /// <param name="code"></param> /// <param name="wechat"></param> /// <returns></returns> public IHttpActionResult Post(string v1, [FromBody] _User model, string code, bool wechat) { try { //表单验证 if (isNUll(model.username, model.password, code)) { return(invildRequest("参数不能为空")); } //注册流程 wechat chat = new wechat(); //主键 Guid guid = Guid.NewGuid(); //判断是否有openId if (model.authData == null || model.authData.wechat == null || isNUll(model.authData.wechat.openId)) { return(invildRequest("参数有误")); } //微信端短信验证 MvcApplication1.Utility.HttpClient client = new MvcApplication1.Utility.HttpClient("https://webapi.sms.mob.com"); string postUri = "sms/checkcode?appkey=1077112ae0d07&phone=" + model.username + "&zone=86&code=" + code; //string userJson = @"{""appkey"":""1ad08332b2ac0"",""phone"":" + model.username + @",""zone"":""86"",""code"":" + code + "}"; //请求验证 string postResponse = client.Get(postUri); if (!string.IsNullOrEmpty(postResponse)) { JObject jo = JsonHelper.DeserializeObject(postResponse); string status = jo["status"].ToString(); if (!status.Equals("200")) { return(notFound("验证码错误")); } } else { return(notFound("验证码请求验证失败")); } //判断微信号是否绑定过 if (wechat_bll.QueryExitByOpenId(model.authData.wechat.openId)) { return(notFound("此微信号已经绑定过了哦!")); } //注册与绑定逻辑 model.authData.objectId = guid.ToString(); model.authData.wechat.objectId = guid.ToString(); //判断用户是否存在 if (bll.QueryExitByUsername(model.username)) { //用户已存在 //微信绑定操作 //更新openId和inopenId //邀请码选填 if (isNUll(model.authData.wechat.inopenId)) { //邀请码为空,绑定 if (bll.UpdateInsert1(model)) { return(ok(new { msg = "绑定成功" })); } return(notFound("绑定失败")); } //邀请码不为空 if (!bll.QueryExitByUsername(model.authData.wechat.inopenId)) { //inopenId无效 return(notFound("您的邀请用户手机号无效!")); } //判断用户是否在APP端被邀请过 if (invite_bll.QueryExitByUsername(model.username)) { //用户在APP端被邀请过,邀请用户不再获得积分,只进行微信信息绑定,双方均不得积分。 if (bll.UpdateInsert1(model)) { return(ok(new { msg = "绑定成功" })); } return(notFound("绑定失败")); } //绑定只给邀请人积分,不给被邀请人积分 //邀请者记录 //条件 List <Wheres> whs = new List <Wheres>() { new Wheres("username", "=", model.authData.wechat.inopenId) }; _User user = bll.QuerySingleByWheres(whs); CreditsHistory history = new CreditsHistory(); history.objectId = guid.ToString(); history.createdAt = DateTime.Now; history.updatedAt = DateTime.Now; history.change = 30; history.credit = user.credit + 30; history.userId = user.objectId; //微信邀请好友 history.type = 3; if (bll.UpdateInsert(model, history)) { return(ok(new { msg = "绑定成功" })); } else { return(notFound("绑定失败")); } } //用户不存在 //微信注册操作 DateTime dt = DateTime.Now; model.objectId = guid.ToString(); //密码加盐保存 model.password = (model.password + model.objectId).Md5(); //初始化数据 model.nickname = "口袋爆料人"; model.credit = 40; model.overage = 0; model.sign_in = true; model.shake_times = 3; model.createdAt = dt; model.updatedAt = dt; string initPassword = "******"; model.transaction_password = (initPassword.Md5() + model.objectId).Md5(); CreditsHistory history2 = new CreditsHistory(); history2.objectId = guid.ToString(); history2.createdAt = dt; history2.updatedAt = dt; history2.change = 40; history2.credit = 40; history2.type = 4;//注册得积分 bool result = false; if (isNUll(model.authData.wechat.inopenId)) { //没有邀请人 result = bll.Insert(model, history2); } else { //有邀请人 //条件 List <Wheres> whs = new List <Wheres>() { new Wheres("username", "=", model.authData.wechat.inopenId) }; _User user = bll.QuerySingleByWheres(whs); CreditsHistory history1 = new CreditsHistory(); Guid guid1 = Guid.NewGuid(); history1.objectId = guid1.ToString(); history1.createdAt = dt; history1.updatedAt = dt; history1.type = 3;//邀请得积分 history1.change = 30; history1.credit = user.credit + 30; history1.userId = user.objectId; result = bll.Insert(model, history2, history1); } if (result) { return(ok(new { msg = "注册成功" })); } return(notFound("注册失败")); } catch (Exception e) { return(execept(e.Message)); } }
/// <summary> /// 根据主键更新一条记录 /// </summary> /// <param name="model">更新后的实体</param> /// <returns>执行结果受影响行数</returns> public bool Update(wechat model) { return(_dao.Update(model)); }
/// <summary> /// 向数据库中添加一条记录 /// </summary> /// <param name="model">要添加的实体</param> /// <returns>是否成功</returns> public bool Insert(wechat model) { return(_dao.Insert(model)); }