Exemple #1
0
        public ActionResult GetPhoneNumberFromWeChat(string userID, string iv, string encryptedData)
        {
            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", "该用户已经个人名片已存在"), JsonRequestBehavior.AllowGet));
            }
            //把数据中的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不存在"), JsonRequestBehavior.AllowGet));
            }
            string session = null;

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

            string mobile = null;

            try
            {
                mobile = Jscode2sessionResultList.AESDecryptPhoneNumber(encryptedData, session, iv);
            }
            catch (Exception)
            {
                Comm.WriteLog("CreateByWeChatPhoneDecrypt", JsonConvert.SerializeObject(new { encryptedData, session, iv }), Common.Enums.DebugLogLevel.Error);
                return(Json(Comm.ToJsonResult("DecryptFail", "解密失败,SessionKey过期,需要重新调用登录接口")));
            }
            return(Json(Comm.ToJsonResult("Success", "成功", mobile), JsonRequestBehavior.AllowGet));
        }
Exemple #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 })));
        }