Browse Source

用户信息表新增字段,添加公众号模板推送功能

develop
liujiaqiang 2 years ago
parent
commit
09e9877b1d
  1. 13
      24Hour/Controllers/Common/LawyerservicesController.cs
  2. 38
      24Hour/Controllers/Common/WebSocketController.cs
  3. 49
      24Hour/Controllers/LoginController.cs
  4. 53
      24Hour/Controllers/system/SystemControllerController.cs
  5. 117
      24Hour/WechatMessagerClient.cs
  6. 4
      24Hour/appsettings.json
  7. 2
      Dto/obj/Debug/net6.0/Dto.csproj.CoreCompileInputs.cache
  8. 5
      Elight.Entity/SystemDto/App_Sys_UserDto.cs
  9. 5
      Elight.Entity/SystemModel/App_Sys_UserModel.cs

13
24Hour/Controllers/Common/LawyerservicesController.cs

@ -128,6 +128,19 @@ namespace _24Hour.Controllers.Common
_db.CommitTran();
if (num > 0)
{
var jcguserdata = await _db.Queryable<App_Sys_UserModel>()
.Where(q => q.usertype == 0 && q.messages ==true && q.unitCode == Lawyerdata.unitCode).ToListAsync();
if (jcguserdata != null)
{
foreach (var item in jcguserdata)
{
if (!string.IsNullOrEmpty(item.wechatId))
{
//给检察官发送审核消息
_wechatMessagerClient.gzhwxts(item.wechatId, Lawyerdata?.name, Lawyerdata?.phone, Lawyerdata?.objectstr + "-" + Lawyerdata?.matter, Lawyerdata?.reason);
}
}
}
result.IsSucceed = true;
result.result = "添加成功";
}

38
24Hour/Controllers/Common/WebSocketController.cs

@ -546,24 +546,26 @@ namespace _24Hour.Controllers.Common
return true;
}
///// <summary>
///// 微信推送
///// </summary>
///// <param name="Id"></param>
///// <returns></returns>
//[HttpGet]
//[Route("wxts")]
//public async Task<bool> wxts()
//{
// try
// {
// _wechatMessagerClient.wxts();
// }
// catch (Exception)
// {
// }
// return true;
//}
/// <summary>
/// 微信推送
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet]
[Route("gzhwxts")]
[AllowAnonymous]
public async Task<bool> gzhwxts(string openId)
{
try
{
//ozRTw0uB1w-Puu73araMIHrdKlSQ
_wechatMessagerClient.gzhwxts(openId, "律师","19536521444","查阅xxx人卷宗","");
}
catch (Exception)
{
}
return true;
}
}

49
24Hour/Controllers/LoginController.cs

@ -309,7 +309,7 @@ namespace _24Hour.Controllers
}
/// <summary>
/// 微信Code 登陆
/// 微信小程序Code 登陆
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
@ -327,7 +327,45 @@ namespace _24Hour.Controllers
return opendata;
}
}
/// <summary>
/// 微信公众号Code 登陆
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet]
[Route("WeChatLoginByGzhCode")]
public async Task<Result> WeChatLoginByGzhCode(string code)
{
var opendata = await GetGzhOpenId(code);
if (opendata.IsSucceed == true)
{
return await WeChatLogin(opendata.result);
}
else
{
return opendata;
}
}
///// <summary>
///// 获取微信公众号Code
///// </summary>
///// <param name="code"></param>
///// <returns></returns>
//[HttpGet]
//[Route("GetCode")]
//public async Task<Result> GetCode(string code,string state)
//{
// var opendata = await GetGzhOpenId(code);
// if (opendata.IsSucceed == true)
// {
// return await WeChatLogin(opendata.result);
// }
// else
// {
// return opendata;
// }
//}
/// <summary>
/// ΢ÐÅÊÚȨµÇ¼
@ -677,12 +715,19 @@ namespace _24Hour.Controllers
/// </summary>
private const string secid = "e752e4fba67526eca21313a18c96a58f";
/// <summary>
/// 获取微信openid
/// 获取微信小程序openid
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet("getOpenid")]
public Task<Result> GetOpenId(string code) => wechatMessagerClient.GetOpenId(code);
/// <summary>
/// 获取微信公众号openid
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet("GetGzhOpenId")]
public Task<Result> GetGzhOpenId(string code) => wechatMessagerClient.GetGzhOpenId(code);
}

53
24Hour/Controllers/system/SystemControllerController.cs

@ -367,7 +367,7 @@ namespace _24Hour.Controllers.system
}
/// <summary>
/// 用户绑定微信openid
/// 用户绑定微信小程序openid
/// </summary>
/// <param name="openid"></param>
/// <returns></returns>
@ -417,6 +417,57 @@ namespace _24Hour.Controllers.system
return result;
}
/// <summary>
/// 用户绑定微信公众号openid
/// </summary>
/// <param name="openid"></param>
/// <returns></returns>
[HttpGet]
[Route("UserBindGzhWechat")]
public async Task<Result> UserBindGzhWechat(string code)
{
try
{
var getopenid = await wechatMessagerClient.GetGzhOpenId(code);
if (getopenid.IsSucceed == false)
{
return getopenid;
}
string openid = getopenid.result;
if (await _db.Queryable<App_Sys_UserModel>().AnyAsync(x => x.wechatId == openid && x.IsDeleted == 0))
{
result.IsSucceed = false;
result.Message = "当前微信已与其他账号绑定";
return result;
}
var model = await _db.Queryable<App_Sys_UserModel>().Where(x => x.Id == _userdata.Id && x.IsDeleted == 0).FirstAsync();
if (model != null)
{
model.wechatId = getopenid.result;
}
_db.BeginTran();
var num = await _db.Updateable(model).UpdateColumns(it => new { it.wechatId }).ExecuteCommandAsync();
//var num = await _db.Updateable(UserModel).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
_db.CommitTran();
if (num > 0)
{
result.IsSucceed = true;
result.result = getopenid.result;
}
}
catch (System.Exception ex)
{
_db.RollbackTran();
result.IsSucceed = false;
result.Message = ex.Message;
}
_logs.WriteSysLogadd("用户管理", "修改人员信息", result, _db);
return result;
}
/// <summary>
/// 删除用户
/// </summary>

117
24Hour/WechatMessagerClient.cs

@ -28,6 +28,14 @@ namespace _24Hour
/// </summary>
private string secret = "wx48108a0c98a3dab0";
/// <summary>
/// 微信公众号AppID
/// </summary>
private string Gzhappid = "wx48108a0c98a3dab0";
/// <summary>
/// 微信公众号密码
/// </summary>
private string Gzhsecret = "wx48108a0c98a3dab0";
/// <summary>
/// 小程序secretID
/// </summary>
private string secid = "e752e4fba67526eca21313a18c96a58f";
@ -43,7 +51,10 @@ namespace _24Hour
appid = $"{configuration.GetSection("Wechat:appid").Value}";
secret = $"{configuration.GetSection("Wechat:secret").Value}";
secid = $"{configuration.GetSection("Wechat:secid").Value}";
Gzhappid = $"{configuration.GetSection("WechatGzh:appid").Value}";
Gzhsecret = $"{configuration.GetSection("WechatGzh:secret").Value}";
}
//小程序获取openId
public async Task<Result> GetOpenId(string code)
{
var ret = new Result();
@ -69,6 +80,36 @@ namespace _24Hour
}
return ret;
}
/// <summary>
/// 微信公众号获取openId
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<Result> GetGzhOpenId(string code)
{
var ret = new Result();
var reponse = HttpHelper.Get($" https://api.weixin.qq.com/sns/oauth2/access_token?appid={Gzhappid}&secret={Gzhsecret}&code={code}&grant_type=authorization_code");
var opendata = reponse;
var data = JsonConvert.DeserializeAnonymousType(opendata, new
{
openid = default(string),
session_key = default(string),
unionid = default(string),
errcode = default(int),
errmsg = default(string),
});
if (data.errcode == 0)
{
ret.IsSucceed = true;
ret.result = data.openid;
}
else
{
ret.IsSucceed = false;
ret.Message = data.errmsg;
}
return ret;
}
/// <summary>
/// 律师预约微信消息提醒,成功
@ -156,7 +197,7 @@ namespace _24Hour
if (string.IsNullOrEmpty(token))
{
string geturl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx48108a0c98a3dab0&secret=e752e4fba67526eca21313a18c96a58f";
string geturl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
var re1 = HttpHelper.Get(geturl);
if (re1.Jsonstr("errcode") == null)
{
@ -211,5 +252,79 @@ namespace _24Hour
return false;
}
/// <summary>
/// 微信公众号消息提醒,推送检察官
/// </summary>
/// <returns></returns>
public bool gzhwxts(string? useropenId, string? name,string? phone, string? matter, string? attention)
{
try
{
var token = "";
// 获取缓存
token = cache.Get("gzhtoken")?.ToString();
if (string.IsNullOrEmpty(token))
{
string geturl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={Gzhappid}&secret={Gzhsecret}";
var re1 = HttpHelper.Get(geturl);
if (re1.Jsonstr("errcode") == null)
{
token = re1.Jsonstr("access_token");
// 移除缓存
cache.Remove("gzhtoken");
// 添加缓存
cache.Add("gzhtoken", token, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(120) });
}
}
string url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";//发送地址
try
{
object msg = null;
string posturl = $"{url}{token}";//发送地址
var thing4 = matter;
var thing5 = attention;
if (!string.IsNullOrEmpty(matter) && matter.Length > 20)
thing4 = matter.Substring(0, 17) + "...";
if (!string.IsNullOrEmpty(attention) && attention.Length > 20)
thing5 = attention.Substring(0, 17) + "...";
msg = new
{
touser = useropenId,
template_id = "SmCAFFqHSnGC4FnRBFgkmuBvTBWkPnWi4zkjshrpz_8",
//miniprogram =new{
// appid= appid,
// pagepath= "pages/selfService/reception/reception"
//},
//miniprogram_state = $"{_configuration.GetSection("Wechat:miniprogram_state").Value}",
//lang = "zh_CN",
data = new
{
//name1 = new { value = $"{name}"},//
thing1 = new { value = $"{name}" },//
phone_number4 = new { value = $"{phone}" },//
thing5 = new { value = $"{thing4}" },//
},
};
var re = HttpHelper.Post(posturl, msg);
if (re.Jsonstr("errcode") == "0")
return true;
return false;
}
catch (Exception ex)
{
}
return true;
}
catch (Exception ex)
{
}
return false;
}
}
}

4
24Hour/appsettings.json

@ -51,6 +51,10 @@
"secid": "e752e4fba67526eca21313a18c96a58f",
"miniprogram_state": "trial"
},
"WechatGzh": { //
"appid": "wx1f5c662edbe3b627",
"secret": "a892658a8d21f4e97cbb1ec1880b78fd",
},
"CaseTwenty": {
"SysAddress": "http://127.0.0.1:5194",
"UseOfflineData": "true",

2
Dto/obj/Debug/net6.0/Dto.csproj.CoreCompileInputs.cache

@ -1 +1 @@
6045bad44ea6ca22bfa9bf856af27786e542aa85
cc7e514c4ce4ef06a7f82aece20a70c7d8114bfe

5
Elight.Entity/SystemDto/App_Sys_UserDto.cs

@ -142,6 +142,11 @@ namespace Elight.Entity
/// </summary>
[DataMember]
public DateTime? createtime { get; set; } = DateTime.Now;
/// <summary>
/// 是否接收消息
/// </summary>
[DataMember]
public bool? messages { get; set; } = false;
}
}

5
Elight.Entity/SystemModel/App_Sys_UserModel.cs

@ -162,6 +162,11 @@ namespace Elight.Entity
/// </summary>
[DataMember]
public DateTime? createtime { get; set; } = DateTime.Now;
/// <summary>
/// 是否接收消息
/// </summary>
[DataMember]
public bool? messages { get; set; } = false;
}
}

Loading…
Cancel
Save