public ActionResult ShopReceiverOAuth(string code, int state) { var result = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(wechatTemplateSendOptions.AppId, wechatTemplateSendOptions.AppSecret, code); string message = "faile"; ShopOrderReceiver model = null; Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo userInfo = null; try { if (result != null && !string.IsNullOrEmpty(result.openid)) { string openId = result.openid; string access_token = result.access_token; userInfo = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetUserInfo(access_token, openId); if (userInfo != null && !string.IsNullOrEmpty(userInfo.openid)) { model = db.Query <ShopOrderReceiver>() .Where(m => !m.IsDel) .Where(m => m.ShopId == state) .Where(m => m.ReceiverOpenId == openId) .FirstOrDefault(); if (model == null) { bool canUsed = db.Query <ShopOrderReceiver>() .Where(m => !m.IsDel) .Where(m => m.ShopId == state) .Where(m => m.IsUsed) .Count() < 5; model = new ShopOrderReceiver() { ShopId = state, ReceiverOpenId = openId, NickName = userInfo.nickname, Headimgurl = userInfo.headimgurl, IsDel = false, IsUsed = canUsed }; db.AddTo(model); message = "success"; } else { //已成功授权过 model.Headimgurl = userInfo.headimgurl; model.NickName = userInfo.nickname; message = "authored"; } db.SaveChanges(); } } } catch (Exception e) { message = NickNameTryAgain(model, state, userInfo); logger.LogError("授权绑定失败:{0}", e.Message); } ViewData["message"] = message; return(View()); }
/// <summary> /// 获取用户的信息 /// </summary> /// <param name="Code"></param> /// <returns></returns> public WeChatUser GetUserInfo(string AppId, string Code) { WeChatUser UModel = new WeChatUser(); Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthAccessTokenResult TokenResult = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(AppId, WeChatDeploy.GetDeploy()[AppId], Code); Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo Model = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetUserInfo(TokenResult.access_token, TokenResult.openid); UModel.HeadImgUrl = Model.headimgurl ?? ""; UModel.NickName = Model.nickname ?? ""; UModel.OpenID = Model.openid; UModel.Sex = Model.sex; UModel.City = Model.city ?? ""; UModel.Country = Model.country ?? ""; UModel.Privilege = Model.privilege ?? new string[0]; UModel.Province = Model.province ?? ""; UModel.UnionID = Model.unionid ?? ""; return(UModel); }
public async Task <ActionResult> CncelPickupCode(int orderId, string pickupCode, string code, int state) { _logger.LogInformation($"=========================开始核销CncelPickupCode方法开始============================"); _logger.LogInformation($"orderId ={orderId} shopid={state}"); var result = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(wechatTemplateSendOptions.AppId, wechatTemplateSendOptions.AppSecret, code); Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo userInfo = null; if (result != null && !string.IsNullOrEmpty(result.openid)) { string openId = result.openid; string access_token = result.access_token; userInfo = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetUserInfo(access_token, openId); if (userInfo != null && !string.IsNullOrEmpty(userInfo.openid)) { var shopServiceUserInfo = await db.ShopServiceUserInfo.FirstOrDefaultAsync( m => m.Openid.Equals(userInfo.openid) && m.Unionid.Equals(userInfo.unionid) && m.ShopId.Equals(state) && !m.IsDel ); _logger.LogInformation($"userInfo ={userInfo.nickname}"); if (shopServiceUserInfo == null) { _logger.LogInformation($"=========================开始核销CncelPickupCode方法结束============================"); return(Json("无核销权限")); } var isOk = await ShopConglomerationOrderOptions.TakeTheirFinishOrderAsync(db, orderId, pickupCode, _logger); if (isOk) { _logger.LogInformation($"=========================开始核销CncelPickupCode方法结束============================"); return(Json("核销成功")); } } } _logger.LogInformation($"=========================开始核销CncelPickupCode方法结束============================"); return(Json("核销失败")); }
//字符集问题,临时解决方案 string NickNameTryAgain(ShopOrderReceiver model, int state, Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo userInfo) { string message = "faile"; try { if (model == null) { bool canUsed = db.Query <ShopOrderReceiver>() .Where(m => !m.IsDel) .Where(m => m.ShopId == state) .Where(m => m.IsUsed) .Count() < 5; model = new ShopOrderReceiver() { ShopId = state, ReceiverOpenId = userInfo.openid, NickName = userInfo.nickname, Headimgurl = userInfo.headimgurl, IsDel = false, IsUsed = canUsed }; db.AddTo(model); message = "success"; } else { //已成功授权过 model.Headimgurl = userInfo.headimgurl; model.NickName = UniCodeToUTF8(userInfo.nickname); message = "authored"; } db.SaveChanges(); } catch (Exception) { } return(message); }
public ActionResult AddServerUserInfo(string code, int state) { string message = "faile"; try { _logger.LogInformation($"==================AddServerUserInfo方法开始=================="); var result = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetAccessToken(wechatTemplateSendOptions.AppId, wechatTemplateSendOptions.AppSecret, code); ShopOrderReceiver model = null; Senparc.Weixin.MP.AdvancedAPIs.OAuth.OAuthUserInfo userInfo = null; if (result != null && !string.IsNullOrEmpty(result.openid)) { string openId = result.openid; string access_token = result.access_token; userInfo = Senparc.Weixin.MP.AdvancedAPIs.OAuthApi.GetUserInfo(access_token, openId); if (userInfo != null && !string.IsNullOrEmpty(userInfo.openid)) { var shopServiceUserInfo = db.ShopServiceUserInfo.FirstOrDefault( m => m.Openid.Equals(userInfo.openid) && m.Unionid.Equals(userInfo.unionid) && m.ShopId.Equals(state) && !m.IsDel ); if (shopServiceUserInfo != null) { //已添加授权 shopServiceUserInfo.Headimgurl = userInfo.headimgurl; shopServiceUserInfo.Nickname = userInfo.nickname; db.SaveChanges(); message = "authored"; } else { shopServiceUserInfo = new ShopServiceUserInfo(); shopServiceUserInfo.Headimgurl = userInfo.headimgurl; shopServiceUserInfo.Nickname = userInfo.nickname; shopServiceUserInfo.Openid = userInfo.openid; shopServiceUserInfo.Sex = userInfo.sex == 1 ? "男" : "女"; shopServiceUserInfo.Province = userInfo.province; shopServiceUserInfo.City = userInfo.city; shopServiceUserInfo.Country = userInfo.country; shopServiceUserInfo.Headimgurl = userInfo.headimgurl; shopServiceUserInfo.Unionid = userInfo.unionid; shopServiceUserInfo.AddTime = DateTime.Now; shopServiceUserInfo.ShopId = state; db.ShopServiceUserInfo.Add(shopServiceUserInfo); db.SaveChanges(); message = "success"; } } } _logger.LogInformation($"==================AddServerUserInfo方法结束=================="); } catch (Exception e) { message = e.Message; _logger.LogError($"添加授权客服失败:{ e.Message} | { e.InnerException.Message}"); } //ViewData["message"] = message; return(Json(new { Message = message })); }