示例#1
0
        /// <summary>
        /// 创建vip用户及上下级关系
        /// </summary>
        /// <param name="vUserID"></param>
        /// <param name="inviteCode"></param>
        /// <returns></returns>
        public RequestResult CreateVipRelation(string vUserID, string inviteCode)
        {
            #region
            RequestResult result = new RequestResult()
            {
                retCode = ReqResultCode.failed,
                retMsg  = "计算佣金失败"
            };
            #region 数据校验
            if (string.IsNullOrWhiteSpace(vUserID))
            {
                result.retMsg = "操作用户ID不能为空";
                return(result);
            }
            #endregion
            int rows = 0;
            try
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Vip uVip = db.Vips.FirstOrDefault(p => p.UserID == vUserID);
                    if (uVip == null)
                    {
                        CardPersonal card = db.CardPersonals.FirstOrDefault(p => p.UserID == vUserID);
                        if (card != null)
                        {
                            uVip = new Vip()
                            {
                                Amount               = 0,
                                CardID               = card.ID,
                                CreateDateTime       = DateTime.Now,
                                FreeChildCount       = 0,
                                State                = Common.Enums.VipState.Enable,
                                TotalAmount          = 0,
                                TotalMonthAmountRank = 0,
                                TotalAmountRank      = 0,
                                TotalWeekAmountRank  = 0,
                                UserID               = vUserID,
                                Type             = Common.Enums.VipRank.Default,
                                VipChild2ndCount = 0,
                                VipChild3rdCount = 0
                            };
                            db.Vips.Add(uVip);
                            rows = db.SaveChanges();
                            if (rows <= 0)
                            {
                                result.retMsg = "创建vip用户失败";
                                return(result);
                            }
                        }
                        else
                        {
                            result.retMsg = "尚未创建该用户的名片";
                            return(result);
                        }
                    }
                    //有邀请码则建立关系
                    if (!string.IsNullOrEmpty(inviteCode))
                    {
                        Vip inviteVip = db.Vips.FirstOrDefault(p => p.Code == inviteCode);
                        if (inviteVip != null)
                        {
                            VipRelationship relation = db.VipRelationships.FirstOrDefault(p => p.UserID == vUserID && p.ParentUserID == inviteVip.UserID);
                            if (relation == null)
                            {
                                relation = new VipRelationship()
                                {
                                    CreateDateTime = DateTime.Now,
                                    ParentID       = inviteVip.ID,
                                    ParentUserID   = inviteVip.UserID,
                                    VipID          = uVip.ID,
                                    UserID         = vUserID
                                };
                                db.VipRelationships.Add(relation);
                                rows = db.SaveChanges();
                                if (rows <= 0)
                                {
                                    result.retMsg = "创建vip用户与上级关系时失败";
                                    return(result);
                                }
                            }
                        }
                    }
                }
                //计算佣金
                RequestResult ret = bll.CalculateVIPAmount(vUserID, 0);
                if (ret.retCode != ReqResultCode.success)
                {
                    result.retMsg = ret.retMsg;
                    return(result);
                }
            }
            catch (Exception ex)
            {
                result.retMsg = $"创建vip用户关系时发生异常:{ex.Message}";
                return(result);
            }

            result.retCode = ReqResultCode.success;
            result.retMsg  = "操作成功";
            return(result);

            #endregion
        }
示例#2
0
        /// <summary>
        /// 注册VIP
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="iv">手机号</param>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult CreateByWeChatPhone(string userID, string iv, string encryptedData, string code)
        {
            var user = db.Users.FirstOrDefault(s => s.Id == userID);

            if (user == null)
            {
                return(Json(Comm.ToJsonResult("UserNoFound", "用户不存在")));
            }
            if (db.CardPersonals.Any(s => s.UserID == userID))
            {
                return(Json(Comm.ToJsonResult("CardPersonalHadCreate", "该用户已经个人名片已存在")));
            }
            //把数据中的OpenID取出
            var     userOpenIDs = new Bll.Users.UserOpenID(user);
            IConfig config      = new ConfigMiniPersonal();
            var     openID      = userOpenIDs.SearchOpenID(config.AppID);

            if (openID == null)
            {
                return(Json(Comm.ToJsonResult("OpenIDIsNull", "OpenID不存在")));
            }
            string session = null;

            try
            {
                session = Jscode2sessionResultList.GetSession(openID);
            }
            catch (Exception ex)
            {
                return(Json(Comm.ToJsonResult("GetSessionFail", ex.Message)));
            }

            string mobile = null;

            try
            {
                mobile = Jscode2sessionResultList.AESDecryptPhoneNumber(encryptedData, session, iv);
            }
            catch (Exception)
            {
                return(Json(Comm.ToJsonResult("DecryptFail", "解密失败,SessionKey过期,需要重新调用登录接口")));
            }

            if (db.Users.Any(s => s.PhoneNumber == mobile))
            {
                return(Json(Comm.ToJsonResult("MobileHadUsed", "手机号已被使用")));
            }

            //if (!Reg.IsMobile(mobile))
            //{
            //    return Json(Comm.ToJsonResult("Moblie Error", "手机号不正确"));
            //}


            Vip parentVip = null;

            if (!string.IsNullOrWhiteSpace(code))
            {
                //判断是否邀请码是否存在
                parentVip = db.Vips.FirstOrDefault(s => s.State == Common.Enums.VipState.Enable && s.Code == code);
                if (parentVip == null)
                {
                    return(Json(Comm.ToJsonResult("CodeNoFound", "邀请码不存在")));
                }
            }
            //保存用户手机号到用户表
            user.PhoneNumber = mobile;

            //把名片已知信息填到个人名片
            var card = new CardPersonal
            {
                UserID = userID,
                Avatar = user.Avatar,
                Enable = true,
                Gender = Common.Enums.Gender.NoSet,
                Name   = user.NickName,
                Mobile = mobile
            };

            db.CardPersonals.Add(card);
            db.SaveChanges();
            try
            {
                card.WeChatMiniQrCode = GetWeChatQrCode(card.ID);
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Comm.WriteLog(this.GetType().ToString(), ex.Message, Common.Enums.DebugLogLevel.Error);
            }
            var result = new Bll.VipBLL().CreateVipRelation(userID, code);

            if (result.retCode == Comm.ReqResultCode.failed)
            {
                //回滚
                db.CardPersonals.Remove(card);
                db.SaveChanges();
                return(Json(Comm.ToJsonResult("Error", result.retMsg)));
            }
            return(Json(Comm.ToJsonResult("Success", "成功", new { PCardID = card.ID })));
        }