| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- 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("错误的参数");
- }
- }
- }
|