Exemple #1
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));
            }
        }