public IHttpActionResult Getaccess_token(string authorizationCode, string membershipCardStore) { SimpleResult result = new SimpleResult(); try { var store = storeService.GetStore(membershipCardStore); if (store != null) { LogHelper.WriteLog("微信authorizationCode:" + authorizationCode + "membershipCardStore:" + membershipCardStore); var wxAccessToken = Common.wxCertification(authorizationCode, store); if (wxAccessToken != null && wxAccessToken.access_token != null) { Certification certification = new Certification(); //①请求判断是否用户已经存在认证②存在了更新 LogHelper.WriteLog("微信openid:" + wxAccessToken.openid + "membershipCardStore:" + membershipCardStore); if (certificationService.CheckCertification(wxAccessToken.openid)) { certification.CertificationCode = Guid.NewGuid().ToString(); certification.OpenID = wxAccessToken.openid; certification.AccessToken = Guid.NewGuid().ToString(); certification.CreateTime = DateTime.Now; certification.ReAccessToken = Guid.NewGuid().ToString(); certificationService.UpdateUserCertification(certification); } else { //③不存在 微信authorizationCode转化自己的authorizationCode certification.CertificationCode = Guid.NewGuid().ToString(); certification.OpenID = wxAccessToken.openid; certification.CreateTime = DateTime.Now; certification.AccessToken = Guid.NewGuid().ToString(); certification.ReAccessToken = Guid.NewGuid().ToString(); //3.1 创建认证信息 certificationService.CreateUserCertification(certification); } IUserTaskService _userTaskService = new UserTaskService(); //3.2用OpenID检查用户 没有的话创建用户信息待写 //using (var scope = new TransactionScope())//创建事务 //{ User user = new User(); var userStore = userStoreService.GetUserStoreby(wxAccessToken.openid); if (userStore != null) { user = userInfo.GetUserByCode(userStore.UserCode); if (_userTaskService.GetUserTaskList(user.Code).Count() != 2) { _userTaskService.InsertUserTask(user.Code, "1"); _userTaskService.InsertUserTask(user.Code, "2"); } } // 用OpenID获取用户信息然后创建用户信息 else { var wxuserinfo = Common.wxuserInfo(wxAccessToken.access_token, wxAccessToken.openid); if (wxuserinfo != null) { user.Code = Guid.NewGuid().ToString(); if (wxuserinfo.sex == 2) { user.Sex = true; } else { user.Sex = false; } user.UserIamgeUrl = wxuserinfo.headimgurl; user.UserName = wxuserinfo.nickname; user.CreateTime = DateTime.Now; userInfo.CreateUser(user); UserStore userStorew = new UserStore(); userStorew.OpenID = wxAccessToken.openid; userStorew.UserStoreCode = Guid.NewGuid().ToString(); userStorew.UserCode = user.Code; userStorew.MembershipCardStore = membershipCardStore; userStoreService.CreateUserStore(userStorew); _userTaskService.InsertUserTask(user.Code, "1"); _userTaskService.InsertUserTask(user.Code, "2"); } else { result.Msg = "请使用微信获取用户信息失败"; } } result.Status = Result.SUCCEED; FristModel fristModel = new FristModel(); fristModel.certification = certification; fristModel.userInfo = user; result.Resource = fristModel; result.Msg = "请使用AccessToken请求认证"; // scope.Complete();//这是最后提交事务 //} } else { result.Msg = "认证失败"; result.Msg = "请求微信认证失败重新获取Code"; } } else { result.Msg = "店铺没有配置"; } } catch (Exception e) { result.Msg = e.Message + e.StackTrace; LogHelper.WriteLog("Getaccess_token ", e); } LogHelper.WriteLog("Getaccess_token result" + Json(result)); return(Json(result)); }