using BizCom; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Utils.ImageUtils; namespace SiteCore.Handler { public partial class app { public void user_login() { if (UrlPostParmsCheck("code")) { string code = GetPostString("code"); //string uname = GetPostString("uname"); //string pwd = GetPostString("pwd"); string nickname = GetPostString("nickname"); string avatarUrl = GetPostString("avatarUrl"); int gender = GetPostInt("gender"); //向微信服务端 使用登录凭证 code 获取 session_key 和 openid string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid + "&secret=" + mini_Secret + "&js_code=" + code + "&grant_type=" + grant_type; string type = "utf-8"; string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串 //微信服务器验证成功 JObject jo = (JObject)JsonConvert.DeserializeObject(json); try { string openid = jo["openid"].ToString(); string session_key = jo["session_key"].ToString(); if (openid == "" || session_key == "") { conError("无法登录!"); return; } //pwd = SecurityHelper.EncryptMD5(pwd);//加密 SUser entity = SUser.GetByWeixinMiniOpenId(openid); bool isnew = false; if (entity == null) { entity = new SUser(); entity.miniopenid = openid; entity.openid = openid; isnew = true; } entity.Sex = gender; entity.NickName = nickname; if (isnew) { entity.CreateAndFlush(); entity.UserPic = entity.ID + ".jpg"; entity.Update(); } else { object sid = RedisHelper.StringGet("xy_"+entity.ID.ToString()); if (sid != null) { RedisHelper.StringDelete(sid.ToString()); } entity.UserPic = entity.ID + ".jpg"; entity.Update(); } if (avatarUrl != "") { Thread oThread = new Thread(delegate () { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { KeepAlive = true, Accept = "image/webp,image/*,*/*;q=0.8", URL = avatarUrl, ResultType = ResultType.Byte }; HttpResult hResult = http.GetHtml(item); using (MemoryStream ms = new MemoryStream(hResult.ResultByte)) { Bitmap bm = new Bitmap(ms); //Graphics g = Graphics.FromImage(bm);//实例一个画板的对象,就用上面的图像的画板 //g.DrawImage(bm, 0, 0); bm.Save(webConfig.userPicPath + "\\b\\" + entity.ID + ".jpg", ImageFormat.Jpeg); using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(ms)) { ImageMaker.ToThumbnailImages(imgThumb, webConfig.userPicPath + "\\" + entity.ID + ".jpg", 100, "", 9, 3); //result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 600, "", 9, 3); } //bm.Save(webConfig.userPicPath + "\\" + entity.ID + ".jpg", ImageFormat.Jpeg); } }); oThread.Start(); } //封装成对象 string session_id = con.Session.SessionID; UserObj uObj = new UserObj() { session_key = session_key, openid = openid, userid = entity.ID }; //存入内存中 RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj)); RedisHelper.StringSet("xy_"+entity.ID, session_id); //返回数据给小程序 StringBuilder userStr = new StringBuilder(); userStr.Append("{"); userStr.AppendFormat("\"session3\":\"{0}\"", session_id); userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic); //userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile); userStr.AppendFormat(",\"username\":\"{0}\"", entity.NickName); userStr.Append("}"); conSuccess("登录成功", userStr.ToString()); return; } catch (Exception) { //微信服务器验证失败 string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString(); conError(msg); } return; } conError("错误的参数"); } public void user_expertlogin() { if (UrlPostParmsCheck("code")) { string code = GetPostString("code"); string uname = GetPostString("uname"); string pwd = GetPostString("pwd"); string nickname = GetPostString("nickname"); string avatarUrl = GetPostString("avatarUrl"); int gender = GetPostInt("gender"); //向微信服务端 使用登录凭证 code 获取 session_key 和 openid string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid + "&secret=" + mini_Secret + "&js_code=" + code + "&grant_type=" + grant_type; string type = "utf-8"; string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串 //微信服务器验证成功 JObject jo = (JObject)JsonConvert.DeserializeObject(json); try { string openid = jo["openid"].ToString(); string session_key = jo["session_key"].ToString(); if (openid == "" || session_key == "") { conError("无法登录!"); return; } //pwd = SecurityHelper.EncryptMD5(pwd);//加密 SUser entity = SUser.Get(uname,pwd); bool isnew = false; if (entity == null) { entity = new SUser(); entity.miniopenid = openid; //entity.openid = openid; isnew = true; } entity.Sex = gender; entity.NickName = nickname; if (isnew) { entity.CreateAndFlush(); entity.UserPic = entity.ID + ".jpg"; entity.Update(); } else { object sid = RedisHelper.StringGet("xy_" + entity.ID.ToString()); if (sid != null) { RedisHelper.StringDelete(sid.ToString()); } entity.UserPic = entity.ID + ".jpg"; entity.Update(); } if (avatarUrl != "") { Thread oThread = new Thread(delegate () { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { KeepAlive = true, Accept = "image/webp,image/*,*/*;q=0.8", URL = avatarUrl, ResultType = ResultType.Byte }; HttpResult hResult = http.GetHtml(item); using (MemoryStream ms = new MemoryStream(hResult.ResultByte)) { Bitmap bm = new Bitmap(ms); //Graphics g = Graphics.FromImage(bm);//实例一个画板的对象,就用上面的图像的画板 //g.DrawImage(bm, 0, 0); bm.Save(webConfig.userPicPath + "\\b\\" + entity.ID + ".jpg", ImageFormat.Jpeg); using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(ms)) { ImageMaker.ToThumbnailImages(imgThumb, webConfig.userPicPath + "\\" + entity.ID + ".jpg", 100, "", 9, 3); //result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 600, "", 9, 3); } //bm.Save(webConfig.userPicPath + "\\" + entity.ID + ".jpg", ImageFormat.Jpeg); } }); oThread.Start(); } //封装成对象 string session_id = con.Session.SessionID; UserObj uObj = new UserObj() { session_key = session_key, openid = openid, userid = entity.ID, expertid = entity.ExpertID }; //存入内存中 RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj)); RedisHelper.StringSet("xy_" + entity.ID.ToString(), session_id); //返回数据给小程序 StringBuilder userStr = new StringBuilder(); userStr.Append("{"); userStr.AppendFormat("\"session3\":\"{0}\"", session_id); userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic); //userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile); userStr.AppendFormat(",\"username\":\"{0}\"", entity.NickName); userStr.Append("}"); conSuccess("登录成功", userStr.ToString()); return; } catch (Exception) { //微信服务器验证失败 string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString(); conError(msg); } return; } conError("错误的参数"); } } }