app.xy.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using BizCom;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using Utils.ImageUtils;
  14. namespace SiteCore.Handler
  15. {
  16. public partial class app
  17. {
  18. public void user_login()
  19. {
  20. if (UrlPostParmsCheck("code"))
  21. {
  22. string code = GetPostString("code");
  23. //string uname = GetPostString("uname");
  24. //string pwd = GetPostString("pwd");
  25. string nickname = GetPostString("nickname");
  26. string avatarUrl = GetPostString("avatarUrl");
  27. int gender = GetPostInt("gender");
  28. //向微信服务端 使用登录凭证 code 获取 session_key 和 openid
  29. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid + "&secret=" + mini_Secret + "&js_code=" + code + "&grant_type=" + grant_type;
  30. string type = "utf-8";
  31. string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串
  32. //微信服务器验证成功
  33. JObject jo = (JObject)JsonConvert.DeserializeObject(json);
  34. try
  35. {
  36. string openid = jo["openid"].ToString();
  37. string session_key = jo["session_key"].ToString();
  38. if (openid == "" || session_key == "")
  39. {
  40. conError("无法登录!");
  41. return;
  42. }
  43. //pwd = SecurityHelper.EncryptMD5(pwd);//加密
  44. SUser entity = SUser.GetByWeixinMiniOpenId(openid);
  45. bool isnew = false;
  46. if (entity == null)
  47. {
  48. entity = new SUser();
  49. entity.miniopenid = openid;
  50. entity.openid = openid;
  51. isnew = true;
  52. }
  53. entity.Sex = gender;
  54. entity.NickName = nickname;
  55. if (isnew)
  56. {
  57. entity.CreateAndFlush();
  58. entity.UserPic = entity.ID + ".jpg";
  59. entity.Update();
  60. }
  61. else {
  62. object sid = RedisHelper.StringGet("xy_"+entity.ID.ToString());
  63. if (sid != null)
  64. {
  65. RedisHelper.StringDelete(sid.ToString());
  66. }
  67. entity.UserPic = entity.ID + ".jpg";
  68. entity.Update();
  69. }
  70. if (avatarUrl != "")
  71. {
  72. Thread oThread = new Thread(delegate ()
  73. {
  74. HttpHelper http = new HttpHelper();
  75. HttpItem item = new HttpItem()
  76. {
  77. KeepAlive = true,
  78. Accept = "image/webp,image/*,*/*;q=0.8",
  79. URL = avatarUrl,
  80. ResultType = ResultType.Byte
  81. };
  82. HttpResult hResult = http.GetHtml(item);
  83. using (MemoryStream ms = new MemoryStream(hResult.ResultByte))
  84. {
  85. Bitmap bm = new Bitmap(ms);
  86. //Graphics g = Graphics.FromImage(bm);//实例一个画板的对象,就用上面的图像的画板
  87. //g.DrawImage(bm, 0, 0);
  88. bm.Save(webConfig.userPicPath + "\\b\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  89. using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(ms))
  90. {
  91. ImageMaker.ToThumbnailImages(imgThumb, webConfig.userPicPath + "\\" + entity.ID + ".jpg", 100, "", 9, 3);
  92. //result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 600, "", 9, 3);
  93. }
  94. //bm.Save(webConfig.userPicPath + "\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  95. }
  96. });
  97. oThread.Start();
  98. }
  99. //封装成对象
  100. string session_id = con.Session.SessionID;
  101. UserObj uObj = new UserObj()
  102. {
  103. session_key = session_key,
  104. openid = openid,
  105. userid = entity.ID
  106. };
  107. //存入内存中
  108. RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj));
  109. RedisHelper.StringSet("xy_"+entity.ID, session_id);
  110. //返回数据给小程序
  111. StringBuilder userStr = new StringBuilder();
  112. userStr.Append("{");
  113. userStr.AppendFormat("\"session3\":\"{0}\"", session_id);
  114. userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic);
  115. //userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile);
  116. userStr.AppendFormat(",\"username\":\"{0}\"", entity.NickName);
  117. userStr.Append("}");
  118. conSuccess("登录成功", userStr.ToString());
  119. return;
  120. }
  121. catch (Exception)
  122. {
  123. //微信服务器验证失败
  124. string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString();
  125. conError(msg);
  126. }
  127. return;
  128. }
  129. conError("错误的参数");
  130. }
  131. public void user_expertlogin()
  132. {
  133. if (UrlPostParmsCheck("code"))
  134. {
  135. string code = GetPostString("code");
  136. string uname = GetPostString("uname");
  137. string pwd = GetPostString("pwd");
  138. string nickname = GetPostString("nickname");
  139. string avatarUrl = GetPostString("avatarUrl");
  140. int gender = GetPostInt("gender");
  141. //向微信服务端 使用登录凭证 code 获取 session_key 和 openid
  142. string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mini_Appid + "&secret=" + mini_Secret + "&js_code=" + code + "&grant_type=" + grant_type;
  143. string type = "utf-8";
  144. string json = GetUrltoHtml(url, type);//获取微信服务器返回字符串
  145. //微信服务器验证成功
  146. JObject jo = (JObject)JsonConvert.DeserializeObject(json);
  147. try
  148. {
  149. string openid = jo["openid"].ToString();
  150. string session_key = jo["session_key"].ToString();
  151. if (openid == "" || session_key == "")
  152. {
  153. conError("无法登录!");
  154. return;
  155. }
  156. //pwd = SecurityHelper.EncryptMD5(pwd);//加密
  157. SUser entity = SUser.Get(uname,pwd);
  158. bool isnew = false;
  159. if (entity == null)
  160. {
  161. entity = new SUser();
  162. entity.miniopenid = openid;
  163. //entity.openid = openid;
  164. isnew = true;
  165. }
  166. entity.Sex = gender;
  167. entity.NickName = nickname;
  168. if (isnew)
  169. {
  170. entity.CreateAndFlush();
  171. entity.UserPic = entity.ID + ".jpg";
  172. entity.Update();
  173. }
  174. else {
  175. object sid = RedisHelper.StringGet("xy_" + entity.ID.ToString());
  176. if (sid != null)
  177. {
  178. RedisHelper.StringDelete(sid.ToString());
  179. }
  180. entity.UserPic = entity.ID + ".jpg";
  181. entity.Update();
  182. }
  183. if (avatarUrl != "")
  184. {
  185. Thread oThread = new Thread(delegate ()
  186. {
  187. HttpHelper http = new HttpHelper();
  188. HttpItem item = new HttpItem()
  189. {
  190. KeepAlive = true,
  191. Accept = "image/webp,image/*,*/*;q=0.8",
  192. URL = avatarUrl,
  193. ResultType = ResultType.Byte
  194. };
  195. HttpResult hResult = http.GetHtml(item);
  196. using (MemoryStream ms = new MemoryStream(hResult.ResultByte))
  197. {
  198. Bitmap bm = new Bitmap(ms);
  199. //Graphics g = Graphics.FromImage(bm);//实例一个画板的对象,就用上面的图像的画板
  200. //g.DrawImage(bm, 0, 0);
  201. bm.Save(webConfig.userPicPath + "\\b\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  202. using (System.Drawing.Image imgThumb = System.Drawing.Image.FromStream(ms))
  203. {
  204. ImageMaker.ToThumbnailImages(imgThumb, webConfig.userPicPath + "\\" + entity.ID + ".jpg", 100, "", 9, 3);
  205. //result = ImageMaker.ToThumbnailImages(imgThumb, saveFile, 600, "", 9, 3);
  206. }
  207. //bm.Save(webConfig.userPicPath + "\\" + entity.ID + ".jpg", ImageFormat.Jpeg);
  208. }
  209. });
  210. oThread.Start();
  211. }
  212. //封装成对象
  213. string session_id = con.Session.SessionID;
  214. UserObj uObj = new UserObj()
  215. {
  216. session_key = session_key,
  217. openid = openid,
  218. userid = entity.ID,
  219. expertid = entity.ExpertID
  220. };
  221. //存入内存中
  222. RedisHelper.StringSet(session_id, JsonConvert.SerializeObject(uObj));
  223. RedisHelper.StringSet("xy_" + entity.ID.ToString(), session_id);
  224. //返回数据给小程序
  225. StringBuilder userStr = new StringBuilder();
  226. userStr.Append("{");
  227. userStr.AppendFormat("\"session3\":\"{0}\"", session_id);
  228. userStr.AppendFormat(",\"userpic\":\"{0}\"", entity.UserPic);
  229. //userStr.AppendFormat(",\"mobile\":\"{0}\"", entity.Mobile);
  230. userStr.AppendFormat(",\"username\":\"{0}\"", entity.NickName);
  231. userStr.Append("}");
  232. conSuccess("登录成功", userStr.ToString());
  233. return;
  234. }
  235. catch (Exception)
  236. {
  237. //微信服务器验证失败
  238. string msg = jo["errcode"].ToString() + "," + jo["errmsg"].ToString();
  239. conError(msg);
  240. }
  241. return;
  242. }
  243. conError("错误的参数");
  244. }
  245. }
  246. }