public ActionResult GetUserQrCode() { var uqr = UserInfo.user.qr_code; dynamic data; string link = $"http://{Request.Url.Host}:{Request.Url.Port}/mcac/index.html?pid={UserInfo.id}"; //首页地址 if (uqr == null) { string path = $"/UploadFiles/ExtensionQR/{UserInfo.id}.png"; Common.ImageClass.Generate2Code2(Server.MapPath(path), link); data = new { link, qr = path }; UserInfo.user.qr_code = path; Wx_userService.EditEntity(UserInfo); } else { data = new { link, qr = uqr }; } return(Json(SysEnum.成功, data, "成功")); }
/// <summary> /// 分页获取用户数据 /// </summary> /// <param name="page">第几页</param> /// <param name="limit">多少条</param> /// <param name="searchKey">搜索关键字</param> /// <param name="state">用户状态</param> /// <returns></returns> public ActionResult GetData(int page, int limit, string searchKey, int state = -1) { List <wx_user> wxuserList; int totalCount; if (string.IsNullOrEmpty(searchKey)) { wxuserList = Wx_userService.LoadPageEntities(page, limit, out totalCount, s => true, s => s.add_time, false).ToList(); } else { wxuserList = Wx_userService.LoadPageEntities(page, limit, out totalCount, s => true && s.nickname.Contains(searchKey), s => s.add_time, false).ToList(); } if (state != -1) { wxuserList = Wx_userService.LoadPageEntities(page, limit, out totalCount, s => true && s.state == state, s => s.add_time, false).ToList(); } List <dynamic> dataList = new List <dynamic>(); if (wxuserList.Any()) { foreach (var w in wxuserList) { dataList.Add( new { Id = Common.EncryptHelper.Encrypt(w.id.ToString()), w.nickname, Sex = Enum.GetName(typeof(Sex), w.sex), w.city, w.wx_head_protrait, add_time = w.add_time == null ? "-" : w.add_time.ToString("yyyy-MM-dd"), State = Enum.GetName(typeof(WXUserState), w.state), totalCount }); } return(Json(SysEnum.成功, dataList, "获取数据成功", totalCount)); } else { return(Json(SysEnum.失败, "无数据")); } }
//#region 小程序授权入口 ///// <summary> ///// 小程序授权入口 ///// </summary> ///// <param name="code">The code.</param> ///// <returns>返回会话ID</returns> //public ActionResult OnLogin(string code) //{ // var jsonResult = SnsApi.JsCode2Json(wxOpenAppId, wxOpenAppSecret, code); // if (jsonResult.errcode == ReturnCode.请求成功) // { // var sessionBag = SessionContainer.UpdateSession(null, jsonResult.openid, jsonResult.session_key); // dynamic data = new // { // sessionId = sessionBag.Key // }; // return Json(SysEnum.成功, data, "登录成功"); // } // else // { // return Json(SysEnum.失败); // } //} //#endregion #region 获取小程序用户token /// <summary> /// 解密用户数据(获取用户token) /// </summary> /// <param name="sessionId">会话ID</param> /// <param name="encryptedData">要解密的数据</param> /// <param name="iv">解密向量</param> /// <returns>返回Token</returns> public ActionResult DecodeEncryptedData(string sessionId, string encryptedData, string iv) { var userInfoJsonStr = Senparc.Weixin.WxOpen.Helpers.EncryptHelper.DecodeEncryptedDataBySessionId(sessionId, encryptedData, iv); var wxUser = Common.SerializeHelper.SerializeToObject <DecodedUserInfo>(userInfoJsonStr); var tempUser = Wx_userService.LoadEntities(u => u.unionid == wxUser.unionId || u.xcx_openid == wxUser.openId).FirstOrDefault(); if (tempUser != null) { if (tempUser.state == (int)WXUserState.取消关注) { tempUser.state = (int)WXUserState.未关注; tempUser.add_time = DateTime.Now; } tempUser.unionid = wxUser.unionId; tempUser.xcx_openid = wxUser.openId; tempUser.nickname = wxUser.nickName; tempUser.wx_head_protrait = wxUser.avatarUrl; tempUser.sex = wxUser.gender; tempUser.city = wxUser.city; tempUser.province = wxUser.province; tempUser.country = wxUser.country; Wx_userService.EditEntity(tempUser); } else { var newUser = new wx_user() { city = wxUser.city, country = wxUser.country, wx_head_protrait = wxUser.avatarUrl, nickname = wxUser.nickName, xcx_openid = wxUser.openId, province = wxUser.province, sex = wxUser.gender, add_time = DateTime.Now, unionid = wxUser.unionId, state = (int)WXUserState.未关注, //未关注 }; Wx_userService.AddEntity(newUser); } dynamic data = new { token = Common.EncryptHelper.Encrypt(string.Format("{0}|{1}|{2}", wxUser.openId, Request.UserHostAddress, RequestCategory.微信小程序)) }; return(Json(SysEnum.成功, data, "获取成功")); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult UserTeamDetail(int id) { var item = Wx_userService.LoadEntities(n => n.id == id).FirstOrDefault(); if (item != null) { var data = new { item.nickname, add_time = item.add_time.ToString(), item.user.total_score, item.user.total_product_count, item.user.total_pay, item.wx_head_protrait, }; return(Json(SysEnum.成功, data, "获取数据成功", 1)); } return(Json(SysEnum.失败, "未找到对象")); }
/// <summary> /// 获取用户下级用户信息 /// </summary> /// <returns></returns> public ActionResult UserTeam() { var user_first = Wx_userService.LoadEntities(n => n.user.pid == UserInfo.id).ToList().Select(n => new { n.id, n.nickname, n.user.isbuy, }).ToList(); var user_second = new List <dynamic>(); if (user_first.Any()) { foreach (var item in user_first) { var second = Wx_userService.LoadEntities(n => n.user.pid == item.id).ToList().Select(n => new { n.id, nickname = $"{n.nickname}({item.nickname})", n.user.isbuy, }).ToList(); user_second.AddRange(second); } } return(Json(SysEnum.成功, new { UserInfo.nickname, totalCount = user_first.Count + user_second.Count, user_first, user_second }, "获取数据成功", 1)); }
/// <summary> /// 公众号授权回调 /// </summary> /// <param name="code">The code.</param> /// <param name="state">The state.</param> /// <param name="goUrl">要跳转的url地址</param> /// <returns>跳转的url地址会带上Token</returns> public ActionResult UserInfoCallback(string code, string state, string goUrl) { goUrl = Common.EncryptHelper.Decrypt(goUrl); var temp = Request; string parms = string.Empty; string strpid = string.Empty; if (goUrl.IndexOf('?') > -1) { parms = goUrl.Substring(goUrl.IndexOf('?')); if (parms.Contains("pid")) { strpid = parms.Substring(parms.IndexOf("pid")).Split('=')[1]; } } //var strpid = goUrl.Substring(goUrl.IndexOf('?'), goUrl.Length).Split('=')[1]; if (string.IsNullOrEmpty(code)) { return(Content("您拒绝了授权!")); } OAuthAccessTokenResult result = null; string token = string.Empty; //通过,用code换取access_token try { result = OAuthApi.GetAccessToken(appId, secret, code); token = Common.EncryptHelper.Encrypt(string.Format("{0}|{1}|{2}", result.openid, Request.UserHostAddress, RequestCategory.微信公众号)); } catch (Exception ex) { if (ex.Message.Contains("40163")) { return(Redirect(goUrl)); } return(Content(ex.Message)); } if (result.errcode != ReturnCode.请求成功) { return(Redirect(goUrl)); } try { var wxUser = OAuthApi.GetUserInfo(result.access_token, result.openid); var tempUser = Wx_userService.LoadEntities(u => u.unionid == wxUser.unionid || u.gzh_openid == wxUser.openid).FirstOrDefault(); if (!int.TryParse(strpid, out int pid)) { pid = 0; } if (tempUser != null) { if (tempUser.state == (int)WXUserState.取消关注) { tempUser.state = (int)WXUserState.未关注; tempUser.add_time = DateTime.Now; } //if (tempUser.unsubscribe_time==null||tempUser.unsubscribe_time<=DateTime.Now.AddDays(-7)) if (true) { if (tempUser.user == null) { //新用户 var user = new user(); user.pid = pid; user.name = wxUser.nickname; user.sex = wxUser.sex; user.state = (int)User_state.正常; user.isbuy = (int)Isbuy.未购买; //首次关注赠送积分 var config_ruleItem = Config_ruleService.LoadEntities(n => n.name == "首次关注" && n.state == (int)State.可用).FirstOrDefault(); if (config_ruleItem != null) { var usr = new user_score_record(); usr.id = Guid.NewGuid(); usr.score = config_ruleItem.value != null?Convert.ToDecimal(config_ruleItem.value) : 0; usr.type = (int)Score_type.收益; usr.state = (int)User_score_record_state.已完成; usr.source_name = "首次关注系统赠送"; usr.add_time = DateTime.Now; usr.remark = ((int)Score_source_remark.系统赠送).ToString(); user.total_score += usr.score; user.usable_score += user.total_score; user.user_score_record.Add(usr); } tempUser.user = user; } tempUser.unionid = wxUser.unionid; tempUser.gzh_openid = wxUser.openid; tempUser.nickname = wxUser.nickname; tempUser.wx_head_protrait = wxUser.headimgurl; tempUser.sex = wxUser.sex; tempUser.city = wxUser.city; tempUser.province = wxUser.province; tempUser.country = wxUser.country; tempUser.unsubscribe_time = DateTime.Now; Wx_userService.EditEntity(tempUser); } } else { //新用户 var user = new user(); user.pid = pid; user.name = wxUser.nickname; user.sex = wxUser.sex; user.state = (int)User_state.正常; user.isbuy = (int)Isbuy.未购买; //首次关注赠送积分 var config_ruleItem = Config_ruleService.LoadEntities(n => n.name == "首次关注" && n.state == (int)State.可用).FirstOrDefault(); if (config_ruleItem != null) { var usr = new user_score_record(); usr.id = Guid.NewGuid(); usr.score = config_ruleItem.value != null?Convert.ToDecimal(config_ruleItem.value) : 0; usr.type = (int)Score_type.收益; usr.state = (int)User_score_record_state.已完成; usr.source_name = "首次关注系统赠送"; usr.add_time = DateTime.Now; usr.remark = ((int)Score_source_remark.系统赠送).ToString(); user.total_score += usr.score; user.usable_score += user.total_score; user.user_score_record.Add(usr); } var newUser = new wx_user() { city = wxUser.city, country = wxUser.country, wx_head_protrait = wxUser.headimgurl, nickname = wxUser.nickname, gzh_openid = wxUser.openid, province = wxUser.province, sex = wxUser.sex, add_time = DateTime.Now, unionid = wxUser.unionid, state = (int)WXUserState.未关注,//未关注 user = user, }; Wx_userService.AddEntity(newUser); //SaveSyslog($"{wxUser.nickname}=>({Url})加入系统,pid={pid}", SysLogType.前台日志, "授权系统"); } var url = $"{goUrl}?state={Guid.NewGuid().ToString().Substring(0, 4)}#token={token}"; return(Redirect(url)); } catch (ErrorJsonResultException ex) { return(Content(ex.Message)); } }