Beispiel #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));
            }
        }
Beispiel #2
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());
        }
Beispiel #3
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));
            }
        }