diff --git a/COMCMS.Common/Utils.cs b/COMCMS.Common/Utils.cs
index 0212de9..8bd9b67 100644
--- a/COMCMS.Common/Utils.cs
+++ b/COMCMS.Common/Utils.cs
@@ -5,6 +5,8 @@ using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Reflection;
using System.Configuration;
+using System.Net;
+using System.IO;
namespace COMCMS.Common
{
@@ -791,6 +793,134 @@ namespace COMCMS.Common
}
return str;
}
+
+ ///
+ /// HTTP GET方式请求数据.
+ ///
+ /// URL.
+ ///
+ public static string HttpGet(string url)
+ {
+ HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
+ request.Method = "GET";
+ //request.ContentType = "application/x-www-form-urlencoded";
+ request.Accept = "*/*";
+ request.Proxy = null;
+ request.Timeout = 15000;
+ request.AllowAutoRedirect = false;
+
+ WebResponse response = null;
+ string responseStr = null;
+
+ try
+ {
+ response = request.GetResponse();
+
+ if (response != null)
+ {
+ StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
+ responseStr = reader.ReadToEnd();
+ reader.Close();
+ }
+ }
+ catch (Exception ex)
+ {
+ NewLife.Log.XTrace.WriteException(ex);
+ throw;
+ }
+ finally
+ {
+ request = null;
+ response = null;
+ }
+
+ return responseStr;
+ }
+
+ ///
+ /// 后台发送POST请求
+ ///
+ /// 服务器地址
+ /// 发送的数据
+ ///
+ public static Stream HttpPost(string url, string data)
+ {
+ try
+ {
+ //创建post请求
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
+ request.Method = "POST";
+ request.ContentType = "application/json;charset=UTF-8";
+ byte[] payload = Encoding.UTF8.GetBytes(data);
+ request.ContentLength = payload.Length;
+
+
+ //发送post的请求
+ Stream writer = request.GetRequestStream();
+ writer.Write(payload, 0, payload.Length);
+ writer.Close();
+
+ //接受返回来的数据
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+ Stream stream = response.GetResponseStream();
+ return stream;
+ //StreamReader reader = new StreamReader(stream, Encoding.UTF8);
+ //string value = reader.ReadToEnd();
+
+ //reader.Close();
+ //stream.Close();
+ //response.Close();
+
+ //return value;
+ }
+ catch (Exception ex)
+ {
+ NewLife.Log.XTrace.WriteException(ex);
+ return null;
+ }
+ }
+
+ ///
+ /// 后台发送POST请求
+ ///
+ /// 服务器地址
+ /// 发送的数据
+ ///
+ public static string HttpPostAndReturnString(string url, string data)
+ {
+ try
+ {
+ //创建post请求
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
+ request.Method = "POST";
+ request.ContentType = "application/json;charset=UTF-8";
+ byte[] payload = Encoding.UTF8.GetBytes(data);
+ request.ContentLength = payload.Length;
+
+
+ //发送post的请求
+ Stream writer = request.GetRequestStream();
+ writer.Write(payload, 0, payload.Length);
+ writer.Close();
+
+ //接受返回来的数据
+ HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+ Stream stream = response.GetResponseStream();
+ StreamReader reader = new StreamReader(stream, Encoding.UTF8);
+ string value = reader.ReadToEnd();
+
+ reader.Close();
+ stream.Close();
+ response.Close();
+
+ return value;
+ }
+ catch (Exception ex)
+ {
+ NewLife.Log.XTrace.WriteException(ex);
+ return "";
+ }
+ }
#endregion
#region COMCMS类型
diff --git a/COMCMS.Web/Controllers/api/UserController.cs b/COMCMS.Web/Controllers/api/UserController.cs
index bdc163c..4982c06 100644
--- a/COMCMS.Web/Controllers/api/UserController.cs
+++ b/COMCMS.Web/Controllers/api/UserController.cs
@@ -8,16 +8,201 @@ using COMCMS.Common;
using COMCMS.Core;
using COMCMS.Core.Models;
using XCode;
+using System.Web;
+using NewLife.Log;
+using static COMCMS.Web.Models.APIModels;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace COMCMS.Web.Controllers.api
{
public class UserController : APIBaseController
{
#region 用户登录
+ ///
+ /// 使用code直接登录,如果登录返回信息,如果没有则注册
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
[HttpGet]
- public object Test()
+ public object UserLogin(string code, string random = "", string timeStamp = "", string signature = "")
{
- return "abc";
+ if (!AutoCheckQueryStringSignature())
+ {
+ return reJson;
+ }
+ if (string.IsNullOrEmpty(code))
+ {
+ reJson.message = "请允许用户授权信息!";
+ return reJson;
+ }
+ Config cfg = Config.GetSystemConfig();
+ string appid = cfg.WXAppId;// ConfigurationManager.AppSettings["wxappAppId"];
+ string appsecrect = cfg.WXAppSecret; //ConfigurationManager.AppSettings["wxappAppSecrect"];
+ //请求地址
+ string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={appsecrect}&js_code={code}&grant_type=authorization_code";
+ string result = Utils.HttpGet(url);
+ XTrace.WriteLine("用户Code登录回调结果:" + result);
+ //反序列化为json
+ dynamic json = JToken.Parse(result) as dynamic;
+ if (!string.IsNullOrEmpty(json.errcode))
+ {
+ XTrace.WriteLine("微信服务器回调失败:" + json.errcode);
+ reJson.message = "注册失败,请联系管理员。" + json.errcode;
+ return reJson;
+ }
+ if (Member.FindCount(Member._.WeixinAppOpenId == json.openid, null, null, 0, 0) > 0)
+ {
+ Member my = Member.Find(Member._.WeixinAppOpenId == json.openid);
+ if (my == null)
+ {
+ XTrace.WriteLine("登录成功,但是系统找不到用户:");
+ reJson.code = 5003;
+ reJson.message = "系统找不到本用户!";
+ return reJson;
+ }
+
+ //如果存在,则返回个人信息
+ WXAppSession wxas = WXAppSession.Find(WXAppSession._.SessionKey == json.session_key & WXAppSession._.OpenId == json.openid);
+ string mysessionkey = "";
+ if (wxas == null)
+ {
+ //登录过期了。重新写一个登录和session key
+ string openid = json.openid;
+ string session_key = json.session_key;
+ WXAppSession entity = new WXAppSession();
+ entity.OpenId = openid;
+ entity.SessionKey = session_key;
+ entity.Key = Utils.GetRandomChar(20);
+ entity.AddTime = DateTime.Now;
+ entity.UId = my.Id;
+ entity.Insert();
+ XTrace.WriteLine("登录成功,过期重写session");
+ mysessionkey = entity.Key;
+ //return new { code = 0, message = "登录成功", sessionkey = entity.Key };
+ }
+ else
+ {
+ mysessionkey = wxas.Key;
+ }
+ string nickName = my.Nickname;
+ if (!string.IsNullOrEmpty(nickName)) nickName = HttpUtility.UrlDecode(nickName);
+ reJson.code = 0;
+ reJson.message = "登录成功!";
+ reJson.detail = new { sessionkey = mysessionkey, username = nickName, userimg = my.UserImg };
+ return reJson;
+ //return new { code = 0, message = "登录成功", sessionkey = mysessionkey, username = nickName, userimg = my.UserImg };
+ }
+ else
+ {
+ XTrace.WriteLine($"用户{json.openid} 开始执行注册{DateTime.Now}...");
+ //执行注册
+ string salt = Utils.GetRandomChar(10);
+ Member m = new Member();
+ m.UserName = "wx" + Utils.GetRandomChar(12);//user.nickName;
+ m.PassWord = Utils.MD5(salt + Utils.GetRandomChar(20));
+ m.Salt = salt;
+ m.UserImg = "";
+ m.Nickname = "";
+ m.RoleId = 3;//默认是普通会员
+ m.Sex = 0;
+ m.RegIP = Utils.GetIP();
+ m.RegTime = DateTime.Now;
+ m.WeixinAppOpenId = json.openid;
+ //如果邀请人id不为空,则处理下级
+ int parent = 0;
+ int grandfather = 0;
+ m.Parent = parent;
+ m.Grandfather = grandfather;
+ m.Insert();
+
+ //然后执行登录
+ string openid = json.openid;
+ string session_key = json.session_key;
+ WXAppSession entity = new WXAppSession();
+ entity.OpenId = openid;
+ entity.SessionKey = session_key;
+ entity.Key = Utils.GetRandomChar(20);
+ entity.AddTime = DateTime.Now;
+ entity.UId = m.Id;
+ entity.Insert();
+ reJson.code = 0;
+ reJson.message = "注册成功!";
+ reJson.detail = new { sessionkey = entity.Key, username = "", userimg = m.UserImg };
+ return reJson;
+ //return new { code = 0, message = "注册成功!", sessionkey = entity.Key, username = "", userimg = m.UserImg };
+ }
+ }
+ #endregion
+
+ #region 用户授权信息同步
+ ///
+ /// 同步微信信息
+ ///
+ ///
+ public object SyncWeixinInfo(string sessionkey, string userInfo, int inviteid = 0, string random = "", string timeStamp = "", string signature = "")
+ {
+ if (!AutoCheckQueryStringSignature())
+ {
+ return reJson;
+ }
+ if (string.IsNullOrEmpty(sessionkey))
+ {
+ reJson.code = 5003;
+ reJson.message = "登录状态失败";
+ return reJson;
+ }
+ WXAppSession entity = WXAppSession.Find(WXAppSession._.Key == sessionkey);
+ if (entity == null)
+ {
+ reJson.message = "未找到登录状态!";
+ return reJson;
+ }
+ if (string.IsNullOrEmpty(userInfo))
+ {
+ reJson.message = "用户信息错误!";
+ return reJson;
+ }
+ WXAppUserInfo info = JsonConvert.DeserializeObject(userInfo);
+ if (string.IsNullOrEmpty(info.avatarUrl))
+ {
+ reJson.message = "微信用户信息错误!";
+ return reJson;
+ }
+ //获取用户
+ Member my = Member.FindById(entity.UId);
+ if (my == null)
+ {
+ reJson.code = 40000;
+ reJson.message = "系统找不到本用户!";
+ return reJson;
+ }
+ //同步用户信息
+ my.Country = info.country;
+ my.UserImg = info.avatarUrl;
+ my.City = info.city;
+ my.Province = info.province;
+ my.Sex = info.gender;
+ my.Nickname = HttpUtility.UrlEncode(info.nickName);
+ my.LastLoginTime = DateTime.Now;
+ if (inviteid > 0 & my.Parent == 0)
+ {
+ Member inviter = Member.Find(Member._.Id == inviteid);
+ if (inviter != null)
+ {
+ my.Parent = inviter.Id;
+ my.Grandfather = inviter.Parent;
+ }
+ }
+ my.Update();
+
+ reJson.code = 0;
+ reJson.message = "同步信息成功!";
+ reJson.detail = new { sessionkey = entity.Key, username = info.nickName, userimg = info.avatarUrl };
+ return reJson;
}
#endregion
}
diff --git a/COMCMS.Web/Models/APIModels.cs b/COMCMS.Web/Models/APIModels.cs
new file mode 100644
index 0000000..e899a0a
--- /dev/null
+++ b/COMCMS.Web/Models/APIModels.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace COMCMS.Web.Models
+{
+ public class APIModels
+ {
+ #region 修改我的个人资料Model
+ public class APIModMyInfo
+ {
+ public string RealName { get; set; }
+ public string Tel { get; set; }
+ public string Weixin { get; set; }
+ public string Alipay { get; set; }
+ public string BankCardNO { get; set; }
+ public string Bank { get; set; }
+ public string SessionKey { get; set; }
+ }
+ #endregion
+
+ #region 小程序获得用户信息
+ ///
+ /// 小程序获得用户信息
+ ///
+ public class WXAppUserInfo
+ {
+ ///
+ /// 用户名
+ ///
+ public string nickName { get; set; }
+ ///
+ /// 性别 0 未知,1 男,2女
+ ///
+ public int gender { get; set; }
+ ///
+ /// 语言
+ ///
+ public string language { get; set; }
+ ///
+ /// 城市
+ ///
+ public string city { get; set; }
+ ///
+ /// 省份
+ ///
+ public string province { get; set; }
+ ///
+ /// 国家
+ ///
+ public string country { get; set; }
+ ///
+ /// 用户头像
+ ///
+ public string avatarUrl { get; set; }
+ }
+
+ #endregion
+ }
+}