Exemplo n.º 1
0
        public APIResult InitShop([FromBody] IdArgsModel args)
        {
            CheckShopActor(args.Id, ShopActorType.超级管理员);

            var authorizerAccessToken = GetAuthorizerAccessToken(args.Id);

            try
            {
                var result = CodeApiExt.QRCodeJumpAddPublish(args.Id, authorizerAccessToken.authorizer_access_token, hostingEnvironment);
                AuthorizerHelper.CreateAndBindOpen(authorizerAccessToken.authorizer_appid);
                if (result.errcode == ReturnCode.请求成功)
                {
                    return(Success("设置成功"));
                }
                else
                {
                    return(Error(result.errmsg));
                }
            }
            catch (Exception ex)
            {
                logger.LogInformation("初始化商店失败。错误路径:{0}", ex.StackTrace);
                return(Error(ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Name")] Household household)
        {
            if (ModelState.IsValid)
            {
                db.Households.Add(household);
                db.SaveChanges();

                //#1 Update user created to reference household
                var userId = User.Identity.GetUserId();
                var user   = db.Users.Find(userId);
                user.HouseholdId = household.Id;

                //#2 whoever creates HH inherits role of HOH
                roleHelper.AddUserToRole(userId, RoleName.HOH);
                db.SaveChanges();

                await AuthorizerHelper.ReauthorizeUserAsync(userId);

                return(RedirectToAction("DashBoard"));
            }



            return(View(household));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 提交审核
        /// </summary>
        /// <param name="authorizerAccessToken"></param>
        /// <param name="item"></param>
        /// <param name="tempInfo"></param>
        public APIResult SubmitVersion(string authorizerAccessToken, ShopWechatOpenAuthorizer item, TemplateInfo tempInfo)
        {
            //先上传代码
            string extJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(new { ext = new { shopFlag = item.Shop.Flag } });
            var    commitResult  = CodeApi.Commit(authorizerAccessToken, tempInfo.template_id, extJsonString, tempInfo.user_version, tempInfo.user_desc);

            //如果请求成功,则将请求的参数写入到数据库进行保存
            if (commitResult.errcode != Senparc.Weixin.ReturnCode.请求成功)
            {
                return(Error("上传代码失败," + commitResult.errmsg));
            }

            //提交审核
            List <SubmitAuditPageInfo> list = new List <SubmitAuditPageInfo>();
            CategroyInfo category           = AuthorizerHelper.GetCategory(authorizerAccessToken);

            list.Add(new SubmitAuditPageInfo()
            {
                first_id     = category.first_id,
                second_id    = category.second_id,
                first_class  = category.first_class,
                second_class = category.second_class,
                third_class  = category.third_class,
                third_id     = category.third_id,
                title        = "点餐",
                tag          = category.first_class,
                address      = "pages/home/home"
            });
            try
            {
                var submitAuditResult = CodeApi.SubmitAudit(authorizerAccessToken, list);

                if (submitAuditResult.errcode != ReturnCode.请求成功)
                {
                    return(Error("提交审核失败," + submitAuditResult.errmsg));
                }
                item.CurrentTemplateExtJson     = extJsonString;
                item.CurrentAuditId             = int.Parse(submitAuditResult.auditid);
                item.CurrentAuditStatus         = 2; //审核状态,其中0为审核成功,1为审核失败,2为审核中
                item.ReleaseTemplateUserVersion = tempInfo.user_version;
                item.IsRelease = false;              //将发布状态设置为未发布
                db.SaveChanges();

                if (item.CurrentAuditStatus == 0) //审核通过则马上发布
                {
                    ReleaseVersion(authorizerAccessToken, item, tempInfo);
                }
            }
            catch (Exception ex)
            {
                return(Error("提交审核失败," + ex.Message));
            }

            return(Success());
        }
Exemplo n.º 4
0
        public APIResult SubmitAuditShop(int shopId)
        {
            var target = db.ShopWechatOpenAuthorizers.Include(p => p.WechatOpenAuthorizer).Include(p => p.Shop).Where(m => m.ShopId == shopId && !m.IsDel).FirstOrDefault();

            if (target == null)
            {
                return(Error("not found ShopWechatOpenAuthorizer by shopid"));
            }
            //取小程序模板列表
            var    accessToken = AuthorizerHelper.GetComponentAccessToken();
            var    tempList    = CodeTemplateApi.GetTemplateListAsync(accessToken).Result;
            var    tempInfo    = tempList.template_list[tempList.template_list.Count - 1];
            string newVersion  = tempInfo.user_version; //最新版本

            AutoUpgrade(target, tempInfo);

            return(Success("ok"));
        }
Exemplo n.º 5
0
        public APIResult SubmitAudit()
        {
            var targets = db.ShopWechatOpenAuthorizers.Include(p => p.WechatOpenAuthorizer).Include(p => p.Shop).Where(m => !m.IsDel).ToList();

            if (targets.Count == 0)
            {
                return(Success("not found ShopWechatOpenAuthorizer"));
            }
            //取小程序模板列表
            var accessToken = AuthorizerHelper.GetComponentAccessToken();//  ComponentContainer.TryGetComponentAccessToken(wechatOptions.AppId, wechatOptions.AppSecret);
            var tempList    = CodeTemplateApi.GetTemplateListAsync(accessToken).Result;
            var tempInfo    = tempList.template_list[tempList.template_list.Count - 1];

            foreach (var item in targets)
            {
                AutoUpgrade(item, tempInfo);
            }
            return(Success("ok"));
        }
Exemplo n.º 6
0
        public APIResult AutoUpgrade(ShopWechatOpenAuthorizer item, TemplateInfo tempInfo)
        {
            var authorizerAccessToken = AuthorizerHelper.GetAuthorizerAccessToken(item.WechatOpenAuthorizer.AuthorizerAppId);

            //检查是否已创建开放平台和绑定,此功能目的是为了登录时获取到unionid
            var bindResult = AuthorizerHelper.CreateAndBindOpen(item.WechatOpenAuthorizer.AuthorizerAppId);

            //当前店铺不是最新版 提交审核
            if (item.ReleaseTemplateUserVersion != tempInfo.user_version)
            {
                return(SubmitVersion(authorizerAccessToken, item, tempInfo));
            }
            else     //已提交审核则查询审核状态
            {
                if (item.CurrentTemplateUserVersion != item.ReleaseTemplateUserVersion)
                {
                    return(QuerySubmit(authorizerAccessToken, item, tempInfo));
                }
            }
            return(Success());
        }
        public async Task <ActionResult> AcceptRegister(AcceptRegisterViewModel model, string keyCode)
        {
            var invite = db.Invitations.FirstOrDefault(i => i.KeyCode.ToString() == keyCode);

            if (model.Keycode.ToString() == keyCode)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    HouseholdId = invite.HouseholdId
                };

                await UserManager.CreateAsync(user, model.Password);

                db.SaveChanges();


                var userId  = UserManager.FindByEmail(model.Email).Id;
                var newUser = db.Users.Find(userId);
                roleHelper.AddUserToRole(userId, RoleName.Member);
                invite.Accepted = DateTime.Now;
                invite.Expired  = true;
                db.SaveChanges();

                await AuthorizerHelper.ReauthorizeUserAsync(userId);


                return(RedirectToAction("Dashboard", "Households"));
            }

            return(View(model));
        }
Exemplo n.º 8
0
        public ActionResult OAuthCallback(string auth_code, int expires_in, int shopId)
        {
            logger.LogInformation($"================================调试开始====================================");
            try
            {
                #region 查询授权信息
                var componentAppId    = wechatOpenOptions.AppId;
                var authorizationCode = auth_code;
                var accessToken       = ZRui.Web.BLL.AuthorizerHelper.GetComponentAccessToken();
                var queryAuthResult   = ComponentApi.QueryAuth(accessToken, componentAppId, authorizationCode);
                logger.LogInformation($"授权返回信息queryAuthResult:{queryAuthResult}");
                var authorizerAppid   = queryAuthResult.authorization_info.authorizer_appid;
                var authorizationInfo = queryAuthResult.authorization_info;
                #endregion

                WechatOpenAuthorizer authorizer = null;
                var authorizers = db.Query <WechatOpenAuthorizer>().
                                  Where(p => p.AuthorizerAppId == authorizationInfo.authorizer_appid);
                if (authorizers.Count() > 0)
                {
                    authorizer = authorizers.FirstOrDefault(p => !p.IsDel);
                    if (authorizer != null)
                    {
                        return(Content("当前店铺绑定的小程序已经存在"));
                    }
                    else
                    {
                        authorizer       = authorizers.OrderByDescending(p => p.Id).FirstOrDefault();
                        authorizer.IsDel = false;
                    }
                }
                else
                {
                    authorizer = new WechatOpenAuthorizer();
                    db.Add(authorizer);
                }


                authorizer.AddIp                  = GetIp();
                authorizer.AddTime                = DateTime.Now;
                authorizer.AddUser                = GetUsername();
                authorizer.AuthorizerAppId        = queryAuthResult.authorization_info.authorizer_appid;
                authorizer.AuthorizerAccessToken  = queryAuthResult.authorization_info.authorizer_access_token;
                authorizer.AuthorizerRefreshToken = queryAuthResult.authorization_info.authorizer_refresh_token;
                authorizer.ExpiresIn              = queryAuthResult.authorization_info.expires_in;
                authorizer.ExpiresTime            = DateTime.Now.AddSeconds(queryAuthResult.authorization_info.expires_in);


                GetAuthorizerInfoResult authorizerInfoResult = ComponentApi.GetAuthorizerInfo(accessToken, componentAppId, authorizerAppid, 0x2710);

                authorizer.AuthorizerNickname = authorizerInfoResult.authorizer_info.nick_name;
                //这里的Username是原始Id
                authorizer.AuthorizerUsername = authorizerInfoResult.authorizer_info.user_name;


                db.SaveChanges();



                ShopWechatOpenAuthorizer shopAuth = null;
                var shopAuths = db.Query <ShopWechatOpenAuthorizer>()
                                .Where(m => m.ShopId == shopId);
                if (shopAuths.Count() > 0)
                {
                    shopAuth = shopAuths.FirstOrDefault(p => !p.IsDel);
                    if (shopAuth == null)
                    {
                        shopAuth = shopAuths.OrderByDescending(p => p.Id).FirstOrDefault();
                    }
                    shopAuth.IsDel = false;
                }
                else
                {
                    shopAuth = new ShopWechatOpenAuthorizer()
                    {
                        ShopId = shopId,
                    };
                    db.Add(shopAuth);
                }
                shopAuth.WechatOpenAuthorizerId = authorizer.Id;
                db.SaveChanges();


                ///初始化
                //复制一份授权信息到auth数据库
                ZRui.Web.BLL.AuthorizerHelper.InsertOrUpdateAuthorizer(authorizer);
                //设置请求域以及添加跳转二维码
                var initShop = CodeApiExt.QRCodeJumpAddPublish(shopId, authorizer.AuthorizerAccessToken, hostingEnvironment);
                //创建开放平台--为了获取授权信息时含有unionid
                AuthorizerHelper.CreateAndBindOpen(authorizer.AuthorizerAppId);


                ViewData["QueryAuthorizationInfo"]  = queryAuthResult.authorization_info;
                ViewData["GetAuthorizerInfoResult"] = authorizerInfoResult.authorizer_info;
                return(View());
            }
            catch (ErrorJsonResultException ex)
            {
                return(Content(ex.Message));
            }
        }