Exemple #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));
            }
        }
Exemple #2
0
        public async System.Threading.Tasks.Task <APIResult <CodeResultJson> > ReleaseAsync([FromBody] ShopIdArgsModel args)
        {
            if (!args.ShopId.HasValue)
            {
                throw new ArgumentNullException("ShopId");
            }
            CheckShopActor(args.ShopId.Value, ShopActorType.超级管理员);

            var authorizerAccessToken = GetAuthorizerAccessToken(args.ShopId.Value);
            var result = await CodeApi.ReleaseAsync(authorizerAccessToken.authorizer_access_token);


            //如果请求成功,则将请求的结果写入到数据库中
            if (result.errcode == Senparc.Weixin.ReturnCode.请求成功)
            {
                var authorizer = db.Query <ShopWechatOpenAuthorizer>()
                                 .Where(m => !m.IsDel)
                                 .Where(m => m.ShopId == args.ShopId.Value)
                                 .FirstOrDefault();

                authorizer.IsRelease = true;
                await db.SaveChangesAsync();

                //这里尝试添加二唯码规则
                try
                {
                    CodeApiExt.QRCodeJumpAddPublish(args.ShopId.Value, authorizerAccessToken.authorizer_access_token, hostingEnvironment);
                }
                catch (Exception)
                {
                }
            }
            return(Success(result));
        }
Exemple #3
0
        /// <summary>
        /// 发布已通过版本
        /// </summary>
        /// <param name="authorizerAccessToken"></param>
        /// <param name="shopId"></param>
        /// <param name="logDbContext"></param>
        public APIResult ReleaseVersion(string authorizerAccessToken, ShopWechatOpenAuthorizer item, TemplateInfo tempInfo)
        {
            var releaseResult = CodeApi.Release(authorizerAccessToken);

            //如果发布成功,则将请求的结果写入到数据库中
            if (releaseResult.errcode != Senparc.Weixin.ReturnCode.请求成功)
            {
                return(Error("发布失败," + releaseResult.errmsg));
            }

            item.IsRelease = true;
            item.CurrentTemplateUserVersion = item.ReleaseTemplateUserVersion;
            item.CurrentTemplateId          = tempInfo.template_id;
            item.CurrentTemplateUserDesc    = tempInfo.user_desc;
            db.SaveChanges();
            //这里尝试添加二唯码规则
            if (hostingEnvironment != null)
            {
                var qrCodeResult = CodeApiExt.QRCodeJumpAddPublish(item.ShopId, authorizerAccessToken, hostingEnvironment);
                if (qrCodeResult.errcode != ReturnCode.请求成功)
                {
                    return(Error("添加二维码规则失败 ," + qrCodeResult.errmsg));
                }
            }
            return(Success());
        }
Exemple #4
0
        public APIResult QRCodeJumpAddPublish([FromBody] IdArgsModel args)
        {
            CheckShopActor(args.Id, ShopActorType.超级管理员);

            var authorizerAccessToken = GetAuthorizerAccessToken(args.Id);

            var result = CodeApiExt.QRCodeJumpAddPublish(args.Id, authorizerAccessToken.authorizer_access_token, hostingEnvironment);

            return(Success(result));
        }
Exemple #5
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));
            }
        }