/// <summary> /// 注册检查 /// </summary> /// <returns></returns> private string CheckSignature() { if (!string.IsNullOrEmpty(Request["echoStr"])) { var signature = Request["signature"]; var timestamp = Request["timestamp"]; var nonce = Request["nonce"]; var token = WebConfigeOpert.GetWXtoken(); bool checkReslut = new WXApiOpert().CheckSignature(signature, timestamp, nonce, token); return(checkReslut ? Request["echoStr"] : string.Empty); } return(string.Empty); }
/// <summary> /// 根据code获取用户信息 /// </summary> /// <param name="code"></param> /// <returns></returns> public MWXUserInfo GetWXUserInfoByCode(string code) { //// 实现逻辑步骤 //// 1、首先判断在session中是否已经存在对应的cdoe,根据实际情况获取openid //// 2、如果存在说明不是第一次跳转过来的,那么直接用session中对应的openid做操作 //// 3、如果不存在,那么需要根据code,到微信获取对应的openid //// 4、根据openid 到数据库中查询用户数据信息 //// 5、如果数据库未查询到数据信息,那么根据openid到微信查询对应的用户信息 //// 6、对步骤5获取的用户数据信息入库 //// 7、返回最终查询到的用户信息对象 //// 用户信息模型 MWXUserInfo model = null; string openid = string.Empty; //// 1、首先判断在session中是否已经存在对应的cdoe,根据实际情况获取openid HttpContext httpContext = HttpContext.Current; if (string.IsNullOrEmpty(httpContext.Session["code"] + string.Empty)) { //// 3、如果不存在,那么需要根据code,到支付获取对应的openid openid = new WXApiOpert().GetOpenIdByCode(code); //// LogOpert.AddWeiXinMessage("1根据code到微信获取openid:" + openid); } else { //// 取session中的openid openid = httpContext.Session["openid"] + string.Empty; //// LogOpert.AddWeiXinMessage("在内存中获取openid:" + openid); if (string.IsNullOrEmpty(openid)) { openid = new WXApiOpert().GetOpenIdByCode(code); //// LogOpert.AddWeiXinMessage("2根据code到微信获取openid:" + openid); } } //// LogOpert.AddWeiXinMessage("最终的openid值为:" + openid); //// 方法封装了 model = this.GetWXUserInfoByOpenid(openid); httpContext.Session["loginuserId"] = model.wxuserid; httpContext.Session["loginuserName"] = model.nickname; //// 7、返回最终查询到的用户信息对象 return(model); }
/// <summary> /// 根据code获取用户信息 /// </summary> /// <param name="code"></param> /// <returns></returns> public MWXUserInfo GetWXUserInfoByOpenid(string openid) { //// 实现逻辑步骤 //// 1、根据openid 到数据库中查询用户数据信息 //// 2、如果数据库未查询到数据信息,那么根据openid到微信查询对应的用户信息 //// 3、对步骤5获取的用户数据信息入库 //// 4、返回最终查询到的用户信息对象 //// 用户信息模型 MWXUserInfo model = null; if (!string.IsNullOrEmpty(openid)) { //// 1、根据openid 到数据库中查询用户数据信息 model = new WXuserService().GetWXUserInfoByOpenid(openid); if (model == null) { //// 2、如果数据库未查询到数据信息,那么根据openid到微信查询对应的用户信息 model = new WXApiOpert().GetUserInfo(openid); if (model != null) { model.great_time = System.DateTime.Now; model.modify_time = System.DateTime.Now; } //// LogOpert.AddWeiXinMessage("微信中根据openid获取到的用户信息为:" + JsonHelper.GetJson<MWXUserInfo>(model)); if (model != null) { //// 3、对步骤5获取的用户数据信息入库 异步入库 AddWXUser handler = new AddWXUser(new WXuserService().AddWXuser); model.wxuserid = PublicTools.GetRandomNumberByTime(); handler.BeginInvoke(model, null, null); //// LogOpert.AddWeiXinMessage("用户信息异步数据入库:" + JsonHelper.GetJson<MWXUserInfo>(model)); } } else { //// LogOpert.AddWeiXinMessage("数据库中根据openid获取到的用户信息为:" + JsonHelper.GetJson<MWXUserInfo>(model)); } } //// 4、返回最终查询到的用户信息对象 return(model); }
/// <summary> /// 微信API入口 /// </summary> /// <returns></returns> public string Index() { //// 只有在第一次服务搭建的时候开启,后面注释掉该代码 /// CheckSignature(); try { MwxMessage wx = GetWxMessage(); string res = ""; string sendMessage = string.Empty; if (wx.MsgType == "text") { if (wx.Content == "待送货订单") { ///// 查询待送货订单 //// 包括3条数据 //// 1、总待送货 //// 2、半小时内需要送货的订单数 //// 3、今天内需要送货的订单数 sendMessage = this.GetSendGoodesCount(); } else { //// 记录一条日志信息 sendMessage = string.Format("您好, {0}已收到你消息。!", WebConfigeOpert.GetPlatformName()); } } if (!string.IsNullOrEmpty(sendMessage)) { res = new WXApiOpert().GetSendMessage(wx, sendMessage); } return(res); } catch (Exception) { return(string.Empty); } }
/// <summary> /// 发布微信菜单 /// </summary> /// <returns></returns> public string FabuMenuToWX() { MwxResult mwxResult = new MwxResult() { errcode = -1 }; try { List <Mwxmenu> menuList = this.GetAllMenu(); if (menuList != null && menuList.Count > 0) { ///// 构建微信菜单对象 MwxMenu <MwxMenuBase> wxMenu = new MwxMenu <MwxMenuBase>(); wxMenu.button = new List <MwxMenuBase>(); //// 获取所有主菜单 List <Mwxmenu> mianMenuList = menuList.FindAll(p => string.IsNullOrEmpty(p.superId)); if (mianMenuList != null && mianMenuList.Count > 0) { mianMenuList = mianMenuList.OrderBy(p => p.sortNum).ToList <Mwxmenu>(); foreach (var item in mianMenuList) { if (menuList.Exists(p => p.superId == item.id)) { List <MwxMenuView> listSubMent = new List <MwxMenuView>(); List <Mwxmenu> subMenu = menuList.FindAll(p => p.superId == item.id); subMenu = subMenu.OrderBy(p => p.sortNum).ToList <Mwxmenu>();; foreach (var item2 in subMenu) { MwxMenuView model2 = new MwxMenuView(); model2.type = item2.type; model2.name = item2.menuName; model2.url = item2.url; listSubMent.Add(model2); } ///// 存在子菜单 MminWxMenuView <MwxMenuView> model = new MminWxMenuView <MwxMenuView>(); model.name = item.menuName; model.sub_button = listSubMent; wxMenu.button.Add(model); } else { MwxMenuView model = new MwxMenuView(); model.type = item.type; model.name = item.menuName; model.url = item.url; wxMenu.button.Add(model); } } } if (wxMenu != null) { string menuHtml = JsonConvert.SerializeObject(wxMenu); //// 开始调用接口生成菜单 mwxResult = new WXApiOpert().AddMenu(menuHtml); } } } catch (Exception ex) { mwxResult.errmsg = "操作失败:系统异常!"; } return(JsonHelper.GetJson <MwxResult>(mwxResult)); }