增加微信小程序登录模式
This commit is contained in:
@@ -5,6 +5,8 @@ using System.Text.RegularExpressions;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
|
using System.Net;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace COMCMS.Common
|
namespace COMCMS.Common
|
||||||
{
|
{
|
||||||
@@ -791,6 +793,134 @@ namespace COMCMS.Common
|
|||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP GET方式请求数据.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">URL.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 后台发送POST请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">服务器地址</param>
|
||||||
|
/// <param name="data">发送的数据</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 后台发送POST请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">服务器地址</param>
|
||||||
|
/// <param name="data">发送的数据</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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
|
#endregion
|
||||||
|
|
||||||
#region COMCMS类型
|
#region COMCMS类型
|
||||||
|
|||||||
@@ -8,16 +8,201 @@ using COMCMS.Common;
|
|||||||
using COMCMS.Core;
|
using COMCMS.Core;
|
||||||
using COMCMS.Core.Models;
|
using COMCMS.Core.Models;
|
||||||
using XCode;
|
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
|
namespace COMCMS.Web.Controllers.api
|
||||||
{
|
{
|
||||||
public class UserController : APIBaseController
|
public class UserController : APIBaseController
|
||||||
{
|
{
|
||||||
#region 用户登录
|
#region 用户登录
|
||||||
|
/// <summary>
|
||||||
|
/// 使用code直接登录,如果登录返回信息,如果没有则注册
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code"></param>
|
||||||
|
/// <param name="random"></param>
|
||||||
|
/// <param name="timeStamp"></param>
|
||||||
|
/// <param name="signature"></param>
|
||||||
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[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 用户授权信息同步
|
||||||
|
/// <summary>
|
||||||
|
/// 同步微信信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
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<WXAppUserInfo>(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
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 小程序获得用户信息
|
||||||
|
/// <summary>
|
||||||
|
/// 小程序获得用户信息
|
||||||
|
/// </summary>
|
||||||
|
public class WXAppUserInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 用户名
|
||||||
|
/// </summary>
|
||||||
|
public string nickName { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 性别 0 未知,1 男,2女
|
||||||
|
/// </summary>
|
||||||
|
public int gender { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 语言
|
||||||
|
/// </summary>
|
||||||
|
public string language { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 城市
|
||||||
|
/// </summary>
|
||||||
|
public string city { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 省份
|
||||||
|
/// </summary>
|
||||||
|
public string province { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 国家
|
||||||
|
/// </summary>
|
||||||
|
public string country { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 用户头像
|
||||||
|
/// </summary>
|
||||||
|
public string avatarUrl { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user