/***********************************************************************
* Project: CoreCms
* ProjectName: 核心内容管理系统
* Web: https://www.corecms.net
* Author: 大灰灰
* Email: jianweie@163.com
* CreateTime: 2021/1/31 21:45:10
* Description: 暂无
***********************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Auth.Policys;
using CoreCms.Net.Caching.AccressToken;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.WeChat.Service.Enums;
using CoreCms.Net.WeChat.Service.HttpClients;
using CoreCms.Net.WeChat.Service.Models;
using CoreCms.Net.WeChat.Service.Options;
using CoreCms.Net.WeChat.Service.Utilities;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Nito.AsyncEx;
using NLog;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using SqlSugar;
using DotLiquid.Util;
using System.IO;
using System.Text.RegularExpressions;
using CoreCms.Net.Model.ViewModels.User;
using CoreCms.Net.Repository.UnitOfWork;
using CoreCms.Net.IRepository.UnitOfWork;
using Newtonsoft.Json.Linq;
namespace CoreCms.Net.Web.WebApi.Controllers
{
///
/// 用户操作事件
///
[Route("api/[controller]/[action]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly ICoreCmsUserWeChatInfoServices _userWeChatInfoServices;
private readonly ICoreCmsUserServices _userServices;
private readonly PermissionRequirement _permissionRequirement;
private readonly ICoreCmsSmsServices _smsServices;
private readonly ICoreCmsUserGradeServices _userGradeServices;
private readonly IHttpContextUser _user;
private readonly ICoreCmsUserLogServices _userLogServices;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ICoreCmsGoodsServices _goodsServices;
private readonly ICoreCmsGoodsBrowsingServices _goodsBrowsingServices;
private readonly ICoreCmsCartServices _cartServices;
private readonly ICoreCmsGoodsCollectionServices _goodsCollectionServices;
private readonly ICoreCmsUserShipServices _userShipServices;
private readonly ICoreCmsAreaServices _areaServices;
private readonly ICoreCmsBillPaymentsServices _billPaymentsServices;
private readonly ICoreCmsGoodsCommentServices _goodsCommentServices;
private readonly ICoreCmsUserBankCardServices _userBankCardServices;
private readonly ICoreCmsUserTocashServices _userTocashServices;
private readonly ICoreCmsUserBalanceServices _userBalanceServices;
private readonly ICoreCmsInvoiceServices _invoiceServices;
private readonly ICoreCmsUserPointLogServices _userPointLogServices;
private readonly ICoreCmsShareServices _shareServices;
private readonly ICoreCmsSettingServices _settingServices;
private readonly ICoreCmsServicesServices _servicesServices;
private readonly ICoreCmsUserServicesOrderServices _userServicesOrderServices;
private readonly ICoreCmsUserServicesTicketServices _userServicesTicketServices;
private readonly ICoreCmsStoreServices _storeServices;
private readonly ICoreCmsCouponServices _couponServices;
private readonly ICoreCmsOrderServices _orderServices;
private readonly IToolsServices _toolsServices;
private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
private readonly WeChatOptions _weChatOptions;
private readonly ICoreCmsUserBlacklistServices _coreCmsUserBlacklistServices;
private readonly SqlSugarScope _dbBase;
private readonly AsyncLock _mutex = new AsyncLock();
///
/// 构造函数
///
public UserController(
IHttpContextUser user
, ICoreCmsUserWeChatInfoServices userWeChatInfoServices
, ICoreCmsUserServices userServices
, PermissionRequirement permissionRequirement
, ICoreCmsSmsServices smsServices
, ICoreCmsUserGradeServices userGradeServices
, ICoreCmsUserLogServices userLogServices
, IHttpContextAccessor httpContextAccessor
, ICoreCmsGoodsServices goodsServices
, ICoreCmsGoodsBrowsingServices goodsBrowsingServices
, ICoreCmsCartServices cartServices
, ICoreCmsGoodsCollectionServices goodsCollectionServices
, ICoreCmsUserShipServices userShipServices
, ICoreCmsAreaServices areaServices
, ICoreCmsBillPaymentsServices billPaymentsServices
, ICoreCmsGoodsCommentServices goodsCommentServices
, ICoreCmsUserBankCardServices userBankCardServices
, ICoreCmsUserTocashServices userTocashServices
, ICoreCmsUserBalanceServices userBalanceServices
, ICoreCmsInvoiceServices invoiceServices
, ICoreCmsUserPointLogServices userPointLogServices
, ICoreCmsShareServices shareServices
, ICoreCmsSettingServices settingServices
, ICoreCmsServicesServices servicesServices
, IOptions weChatOptions
, ICoreCmsUserServicesOrderServices userServicesOrderServices, ICoreCmsUserServicesTicketServices userServicesTicketServices, ICoreCmsStoreServices storeServices, ICoreCmsCouponServices couponServices, ICoreCmsOrderServices orderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IToolsServices toolsServices, ICoreCmsUserBlacklistServices coreCmsUserBlacklistServices, IUnitOfWork unitOfWork)
{
_user = user;
_userWeChatInfoServices = userWeChatInfoServices;
_userServices = userServices;
_permissionRequirement = permissionRequirement;
_smsServices = smsServices;
_userGradeServices = userGradeServices;
_userLogServices = userLogServices;
_httpContextAccessor = httpContextAccessor;
_goodsServices = goodsServices;
_goodsBrowsingServices = goodsBrowsingServices;
_cartServices = cartServices;
_goodsCollectionServices = goodsCollectionServices;
_userShipServices = userShipServices;
_areaServices = areaServices;
_billPaymentsServices = billPaymentsServices;
_goodsCommentServices = goodsCommentServices;
_userBankCardServices = userBankCardServices;
_userTocashServices = userTocashServices;
_userBalanceServices = userBalanceServices;
_invoiceServices = invoiceServices;
_userPointLogServices = userPointLogServices;
_shareServices = shareServices;
_settingServices = settingServices;
_servicesServices = servicesServices;
_userServicesOrderServices = userServicesOrderServices;
_userServicesTicketServices = userServicesTicketServices;
_storeServices = storeServices;
_couponServices = couponServices;
_orderServices = orderServices;
_weChatApiHttpClientFactory = weChatApiHttpClientFactory;
_weChatOptions = weChatOptions.Value;
_toolsServices = toolsServices;
_coreCmsUserBlacklistServices = coreCmsUserBlacklistServices;
_dbBase = unitOfWork.GetDbClient();
}
///
/// wx.login登陆成功之后发送的请求
///
///
///
[HttpPost]
public async Task UseAnonymousLogin([FromBody] FMWxPost entity)
{
var jm = new WebApiCallBack();
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
var request = new SnsJsCode2SessionRequest();
request.JsCode = entity.code;
request.AccessToken = accessToken;
var response = await client.ExecuteSnsJsCode2SessionAsync(request, HttpContext.RequestAborted);
if (response.ErrorCode == (int)WeChatReturnCode.ReturnCode.请求成功)
{
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == response.OpenId);
if (userInfo != null)
{
if (userInfo.sessionKey != response.SessionKey)
{
await _userWeChatInfoServices.UpdateAsync(
p => new CoreCmsUserWeChatInfo() { sessionKey = response.SessionKey, updateTime = DateTime.Now },
p => p.openid == userInfo.openid);
}
return new WebApiDto { Code = 0, Msg = "OK", Data = new { user = userInfo.userId, isMobile = userInfo.mobile?.Length > 0, sessionAuthId = response.OpenId } };
}
userInfo = new CoreCmsUserWeChatInfo();
userInfo.openid = response.OpenId;
userInfo.type = (int)GlobalEnumVars.UserAccountTypes.微信小程序;
userInfo.sessionKey = response.SessionKey;
userInfo.gender = 1;
userInfo.createTime = DateTime.Now;
await _userWeChatInfoServices.InsertAsync(userInfo);
return new WebApiDto { Code = 0, Msg = "OK", Data = new { user = userInfo.userId, isMobile = false, sessionAuthId = response.OpenId } };
}
return new WebApiDto { Code = -2, Msg = "", Data = new { user = 0, isMobile = false, sessionAuthId = "" } };
}
///
/// 微信小程序授权拉取手机号码并登录
///
///
///
[HttpPost]
public async Task UseWxPhoneNumberLogin([FromBody] FMWxLoginPhoneNumber entity)
{
var jm = new WebApiCallBack();
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId);
if (userInfo == null)
{
jm.code = -1;
jm.msg = "用户信息获取失败";
return jm;
}
string phoneNumber = "";
try
{
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
//phoneNumber = EncryptHelper.DecryptPhoneNumber(userInfo.sessionKey, entity.encryptedData, entity.iv);
var response = await client.ExecuteWxaBusinessGetUserPhoneNumberAsync(new SKIT.FlurlHttpClient.Wechat.Api.Models.WxaBusinessGetUserPhoneNumberRequest()
{
AccessToken = accessToken,
Code = entity.code
}, HttpContext.RequestAborted);
if (response.ErrorCode == (int)WeChatReturnCode.ReturnCode.请求成功)
{
phoneNumber = response.PhoneInfo.PhoneNumber;
}
}
catch (Exception ex)
{
jm.code = 500;
NLogUtil.WriteAll(LogLevel.Error, LogType.Web, "小程序接口", "微信小程序授权拉取手机号码", ex);
return jm;
}
Random rnd = new Random();
var data = new FMWxAccountCreate
{
mobile = phoneNumber,
invitecode = entity.invitecode,
sessionAuthId = entity.sessionAuthId,
nickname = "微信用户" + rnd.Next(1000, 9999),
};
jm = await _userServices.SmsLogin(data);
return jm;
}
///
/// 微信小程序匿名转正式登录
///
///
///
[HttpPost]
public async Task UseWxAnonymousLogin([FromBody] FMWxLoginPhoneNumber entity)
{
var jm = new WebApiCallBack();
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId);
if (userInfo == null)
{
jm.code = -1;
jm.msg = "用户信息获取失败";
return jm;
}
string phoneNumber = userInfo.mobile;
Random rnd = new Random();
var data = new FMWxAccountCreate
{
mobile = phoneNumber,
invitecode = entity.invitecode,
sessionAuthId = entity.sessionAuthId,
nickname = "微信用户" + rnd.Next(1000, 9999),
};
jm = await _userServices.SmsLogin(data);
return jm;
}
///
/// 匿名登录
///
///
[HttpPost]
[Authorize]
public async Task AnonymousLogin()
{
var userId = _user.ID;
var jm = new WebApiCallBack();
jm = await _userServices.AnonymousLogin(userId);
return jm;
}
#region wx.login登陆成功之后发送的请求=========================================================
///
/// wx.login登陆成功之后发送的请求
///
///
///
[HttpPost]
public async Task OnLogin([FromBody] FMWxPost entity)
{
var jm = new WebApiCallBack();
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
var request = new SnsJsCode2SessionRequest();
request.JsCode = entity.code;
request.AccessToken = accessToken;
var response = await client.ExecuteSnsJsCode2SessionAsync(request, HttpContext.RequestAborted);
//wxa/business/getuserphonenumber
//client.ExecuteWxaBusinessGetUserPhoneNumberAsync(new SKIT.FlurlHttpClient.Wechat.Api.Models.WxaBusinessGetUserPhoneNumberRequest()
//{
// AccessToken = accessToken,
// Code = ""
//}, HttpContext.RequestAborted).GetAwaiter().GetResult();
if (response.ErrorCode == (int)WeChatReturnCode.ReturnCode.请求成功)
{
using (await _mutex.LockAsync())
{
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == response.OpenId);
if (userInfo == null)
{
userInfo = new CoreCmsUserWeChatInfo();
userInfo.openid = response.OpenId;
userInfo.type = (int)GlobalEnumVars.UserAccountTypes.微信小程序;
userInfo.sessionKey = response.SessionKey;
userInfo.gender = 1;
userInfo.createTime = DateTime.Now;
await _userWeChatInfoServices.InsertAsync(userInfo);
}
else
{
if (userInfo.sessionKey != response.SessionKey)
{
await _userWeChatInfoServices.UpdateAsync(
p => new CoreCmsUserWeChatInfo() { sessionKey = response.SessionKey, updateTime = DateTime.Now },
p => p.openid == userInfo.openid);
}
}
if (userInfo is { userId: > 0 })
{
var user = await _userServices.QueryByClauseAsync(p => p.id == userInfo.userId);
if (user != null)
{
var claims = new List {
new Claim(ClaimTypes.Name, user.nickName),
new Claim(JwtRegisteredClaimNames.Jti, user.id.ToString()),
new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_permissionRequirement.Expiration.TotalSeconds).ToString(CultureInfo.InvariantCulture)) };
//用户标识
var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
identity.AddClaims(claims);
jm.status = true;
jm.data = new
{
auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement),
user
};
jm.otherData = response.OpenId;
//录入登录日志
var log = new CoreCmsUserLog();
log.userId = user.id;
log.state = (int)GlobalEnumVars.UserLogTypes.登录;
log.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1";
log.createTime = DateTime.Now;
log.parameters = GlobalEnumVars.UserLogTypes.登录.ToString();
await _userLogServices.InsertAsync(log);
return jm;
}
}
}
//注意:生产环境下SessionKey属于敏感信息,不能进行传输!
//return new JsonResult(new { success = true, msg = "OK", sessionAuthId = sessionBag.Key, sessionKey = sessionBag.SessionKey, data = jsonResult, sessionBag = sessionBag });
jm.status = true;
jm.data = response.OpenId;
jm.otherData = response.OpenId;
//jm.methodDescription = JsonConvert.SerializeObject(sessionBag);
jm.msg = "OK";
}
else
{
jm.msg = response.ErrorMessage;
}
return jm;
}
#endregion
#region 微信核验数据并获取用户详细资料=====================================================
///
/// 核验数据并获取用户详细资料
///
///
///
[HttpPost]
public async Task DecodeEncryptedData([FromBody] FMWxLoginDecodeEncryptedData entity)
{
var jm = new WebApiCallBack();
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId);
if (userInfo == null)
{
jm.status = false;
jm.msg = "用户信息获取失败";
return jm;
}
var decodedEntity = EncryptHelper.DecodeUserInfoBySessionId(userInfo.sessionKey, entity.encryptedData, entity.iv);
var token = string.Empty;
var userWxId = entity.sessionAuthId;
//检验水印
if (decodedEntity != null)
{
var checkWatermark = decodedEntity.CheckWatermark(_weChatOptions.WxOpenAppId);
jm.status = checkWatermark;
//保存用户信息(可选)
if (checkWatermark && decodedEntity is { } decodedUserInfo)
{
//更新数据库讯息
userInfo.gender = decodedUserInfo.gender;
userInfo.city = decodedUserInfo.city;
userInfo.avatar = decodedUserInfo.avatarUrl;
userInfo.country = decodedUserInfo.country;
userInfo.nickName = decodedUserInfo.nickName;
userInfo.province = decodedUserInfo.province;
userInfo.unionId = decodedUserInfo.unionId;
userInfo.updateTime = DateTime.Now;
await _userWeChatInfoServices.UpdateAsync(userInfo);
if (userInfo.userId > 0)
{
var user = await _userServices.QueryByClauseAsync(p => p.id == userInfo.userId);
if (user != null)
{
var claims = new List {
new Claim(ClaimTypes.Name, user.nickName),
new Claim(JwtRegisteredClaimNames.Jti, user.id.ToString()),
new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_permissionRequirement.Expiration.TotalSeconds).ToString(CultureInfo.InvariantCulture)) };
//用户标识
var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
identity.AddClaims(claims);
jm.status = true;
jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement);
//录入登录日志
var log = new CoreCmsUserLog();
log.userId = user.id;
log.state = (int)GlobalEnumVars.UserLogTypes.登录;
log.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1";
log.createTime = DateTime.Now;
log.parameters = GlobalEnumVars.UserLogTypes.登录.ToString();
await _userLogServices.InsertAsync(log);
//更新手机号码标识
if (!string.IsNullOrEmpty(userInfo.mobile))
{
await _userWeChatInfoServices.UpdateAsync(p => new CoreCmsUserWeChatInfo() { mobile = user.mobile }, p => p.id == userInfo.id);
}
return jm;
}
}
}
}
jm.data = new
{
token,
sessionAuthId = userWxId
};
return jm;
}
#endregion
#region 用户短信发送===================================================================
///
/// 用户短信发送
///
///
///
[HttpPost]
public async Task SendSms([FromBody] FMWxSendSMS entity)
{
var jm = new WebApiCallBack();
if (!CommonHelper.IsMobile(entity.mobile))
{
jm.msg = "请输入合法的手机号码";
return jm;
}
if (entity.code == "login")
{
var shave = await _userServices.ExistsAsync(p => p.mobile == entity.mobile && p.userWx > 0);
if (shave)
{
jm.msg = "手机号码已被绑定,请更换";
return jm;
}
}
jm = await _smsServices.DoSendSms(entity.code, entity.mobile);
return jm;
}
#endregion
#region 手机短信验证码登陆,同时兼有手机短信注册的功能,还有第三方账户绑定的功能======================================================
///
/// 手机短信验证码登陆,同时兼有手机短信注册的功能,还有第三方账户绑定的功能
///
///
///
[HttpPost]
public async Task SmsLogin([FromBody] FMWxAccountCreate entity)
{
var jm = await _userServices.SmsLogin(entity, 2, entity.platform);
return jm;
}
#endregion
#region 微信小程序授权拉取手机号码
///
/// 微信小程序授权拉取手机号码
///
///
///
[HttpPost]
public async Task DecryptPhoneNumber([FromBody] FMWxLoginDecryptPhoneNumber entity)
{
var jm = new WebApiCallBack();
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId);
if (userInfo == null)
{
jm.status = false;
jm.msg = "用户信息获取失败";
return jm;
}
DecodedPhoneNumber phoneNumber;
try
{
phoneNumber = EncryptHelper.DecryptPhoneNumber(userInfo.sessionKey, entity.encryptedData, entity.iv);
}
catch (Exception ex)
{
jm.status = false;
jm.code = 500;
NLogUtil.WriteAll(LogLevel.Error, LogType.Web, "小程序接口", "微信小程序授权拉取手机号码", ex);
return jm;
}
var data = new FMWxAccountCreate
{
mobile = phoneNumber.phoneNumber,
invitecode = entity.invitecode,
sessionAuthId = entity.sessionAuthId
};
jm = await _userServices.SmsLogin(data);
return jm;
}
#endregion
#region 用户短信注册并返回jwt token(弃用)======================================================
///
/// 用户短信注册并返回jwt token(弃用)
///
///
///
[Obsolete]
[HttpPost]
public async Task SmsLogin2([FromBody] FMWxAccountCreate entity)
{
var jm = new WebApiCallBack();
if (!CommonHelper.IsMobile(entity.mobile))
{
jm.msg = "请输入合法的手机号码";
return jm;
}
var user = await _userServices.QueryByClauseAsync(p => p.mobile == entity.mobile);
if (user != null)
{
jm.msg = "此号码已经绑定,请更换";
return jm;
}
var wxUserInfo = new CoreCmsUserWeChatInfo();
//1就是h5登陆(h5端和微信公众号端),2就是微信小程序登陆,3是支付宝小程序,4是app,5是pc
if (entity.platform == 2)
{
if (string.IsNullOrEmpty(entity.sessionAuthId))
{
jm.msg = "用户未正确登陆";
return jm;
}
wxUserInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == entity.sessionAuthId);
}
var sms = await _smsServices.QueryByClauseAsync(p => p.parameters == entity.code && p.mobile == entity.mobile);
if (sms == null)
{
jm.msg = "验证码核验失败";
return jm;
}
if (sms.isUsed)
{
jm.msg = "验证码已被使用";
return jm;
}
var dt = DateTime.Now;
var endDt = sms.createTime.AddMinutes(10);
if (dt > endDt)
{
jm.msg = "验证码已过期,请重新获取";
return jm;
}
user = new CoreCmsUser();
user.mobile = entity.mobile;
user.sex = wxUserInfo?.gender ?? 3;
user.avatarImage = wxUserInfo != null ? wxUserInfo.avatar : "";
user.nickName = wxUserInfo != null ? wxUserInfo.nickName : entity.mobile;
user.balance = 0;
user.parentId = 0;
user.point = 0;
//获取用户等级
var userGrade = await _userGradeServices.QueryByClauseAsync(p => p.isDefault);
user.grade = userGrade?.id ?? 0;
user.createTime = DateTime.Now;
user.status = 1;
user.userWx = wxUserInfo?.id ?? 0;
user.isDelete = false;
if (entity.invitecode > 0)
{
var parentId = UserHelper.GetUserIdByShareCode(entity.invitecode);
if (parentId > 0 && await _userServices.ExistsAsync(p => p.id == parentId))
{
user.parentId = parentId;
}
}
var id = await _userServices.InsertAsync(user);
if (id > 0)
{
var claims = new List {
new Claim(ClaimTypes.Name, user.nickName),
new Claim(JwtRegisteredClaimNames.Jti, id.ToString()),
new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_permissionRequirement.Expiration.TotalSeconds).ToString(CultureInfo.InvariantCulture)) };
//用户标识
var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
identity.AddClaims(claims);
jm.status = true;
jm.msg = "注册成功";
jm.data = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement);
//录入登录日志
var log = new CoreCmsUserLog();
log.userId = id;
log.state = (int)GlobalEnumVars.UserLogTypes.注册;
log.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ?
_httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1";
log.createTime = DateTime.Now;
log.parameters = GlobalEnumVars.UserLogTypes.注册.ToString();
await _userLogServices.InsertAsync(log);
//标识短信是否可用
sms.isUsed = true;
await _smsServices.UpdateAsync(sms);
}
else
{
jm.msg = "注册失败";
}
return jm;
}
#endregion
#region 获取区域ID
///
/// 获取区域ID
///
///
///
[HttpPost]
public async Task GetAreaId([FromBody] GetAreaIdPost entity)
{
var jm = await _areaServices.GetAreaId(entity.provinceName, entity.cityName, entity.countyName, entity.postalCode);
return jm;
}
#endregion
#region 注销登录
///
/// 注销登录
///
///
[HttpPost]
public WebApiCallBack LogOut()
{
var jm = new WebApiCallBack
{
status = true,
data = new
{
token = "", //直接前端删除token-无为而治
}
};
return jm;
}
#endregion
#region 判断是否开启积分
///
/// 判断是否开启积分
///
///
[HttpPost]
public async Task IsPoint()
{
var jm = new WebApiCallBack { status = true, msg = "获取成功" };
var allConfigs = await _settingServices.GetConfigDictionaries();
jm.data = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointSwitch).ObjectToInt(2);
return jm;
}
#endregion
#region 统一分享url处理
///
/// 统一分享url处理
/// 新的分享,不管是二维码,还是地址,都走这个
/// page 场景值 1店铺首页,2商品详情页,3拼团详情页,4邀请好友(店铺页面,params里需要传store),5文章页面,6参团页面,7自定义页面,8智能表单,9团购,10秒杀,11代理
/// url:前端地址
/// params:参数,根据场景值不一样而内容不一样
/// 1
/// 2 goodsId:商品ID
/// 3 goodsId:商品ID,teamId:拼团ID
/// 4 store:店铺code
/// 5 articleId:文章ID,articleType:文章类型
/// 6 goodsId:商品ID,groupId:参团ID,teamId:拼团ID
/// 7 pageCode:自定义页面code
/// 8 id:智能表单ID
/// 9 goodsId:商品ID,groupId:团购秒杀ID
/// type 类型,1url,2二维码,3海报
/// token 可以保存推荐人的信息
/// client 终端,1普通h5,2微信小程序,3微信公众号(h5),4头条系小程序,5pc,6阿里小程序
/// 10 store:店铺code
///
///
/// array
[HttpPost]
public async Task Share([FromBody] FMShare entity)
{
var jm = new WebApiCallBack();
var userShareCode = 0;
if (_user != null && _user.ID > 0)
{
userShareCode = UserHelper.GetShareCodeByUserId(_user.ID);
}
if (entity.type == (int)GlobalEnumVars.ShareType.Url) //链接分享
{
jm = _shareServices.UrlShare(entity.client, entity.page, userShareCode, entity.url, entity.@params);
}
else if (entity.type == (int)GlobalEnumVars.ShareType.QrCode) //二维码
{
jm = await _shareServices.QrShare(entity.client, entity.page, userShareCode, entity.url, entity.@params);
}
else if (entity.type == (int)GlobalEnumVars.ShareType.Poster) //海报
{
jm = await _shareServices.PosterShare(entity.client, entity.page, userShareCode, entity.url, entity.@params);
}
return jm;
}
#endregion
#region 统一分享解码
///
/// 统一分享解码
///
///
///
[HttpPost]
public WebApiCallBack DeShare([FromBody] FMDeShare entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.code))
{
jm.msg = GlobalErrorCodeVars.Code10000;
}
jm = _shareServices.de_url(entity.code);
return jm;
}
#endregion
//验证接口====================================================================================================
#region 同步微信用户数据=====================================================
///
/// 同步微信用户数据
///
///
///
[Authorize]
[HttpPost]
public async Task SyncWeChatInfo([FromBody] FMWxSync entity)
{
var jm = new WebApiCallBack();
var user = await _userServices.QueryByClauseAsync(p => p.id == _user.ID);
if (user != null)
{
user.avatarImage = entity.avatarUrl;
user.nickName = entity.nickName;
user.sex = entity.gender;
//更新
await _userServices.UpdateAsync(p => new CoreCmsUser()
{
avatarImage = entity.avatarUrl,
nickName = entity.nickName,
sex = entity.gender,
}, p => p.id == user.id);
}
else
{
jm.msg = "用户信息获取失败";
}
if (user is { userWx: > 0 })
{
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.id == user.userWx);
if (userInfo != null)
{
userInfo.avatar = entity.avatarUrl;
userInfo.city = entity.city;
userInfo.country = entity.country;
userInfo.gender = entity.gender;
userInfo.nickName = entity.nickName;
userInfo.province = entity.province;
userInfo.updateTime = DateTime.Now;
await _userWeChatInfoServices.UpdateAsync(userInfo);
}
}
jm.status = true;
jm.data = user;
return jm;
}
#endregion
#region 获取用户信息
///
/// 获取用户信息
///
///
[HttpPost]
[Authorize]
public async Task GetUserInfo()
{
var jm = new WebApiCallBack() { status = true };
var user = await _userServices.QueryByIdAsync(_user.ID);
if (user == null)
{
jm.status = false;
jm.msg = "用户信息获取失败";
jm.code = 14007;
return jm;
}
////获取用户等级
//var userGrade = await _userGradeServices.QueryByClauseAsync(p => p.id == user.grade);
////获取优惠券
//var userCouponCount = await _couponServices.GetMyCouponCount(user.id);
////订单数量
//var orderCount = await _orderServices.OrderCount(0, user.id);
////足迹
//var footPrintCount = await _goodsBrowsingServices.GetCountAsync(p => p.userId == user.id);
////收藏
//var collectionCount = await _goodsCollectionServices.GetCountAsync(p => p.userId == user.id);
jm.data = new
{
user.id,
user.userName,
user.mobile,
user.sex,
birthday = user.birthday != null ? user.birthday?.ToString("yyyy-MM-dd") : "",
user.avatarImage,
user.nickName,
user.balance,
user.point,
user.grade,
user.play_level,
user.dove_count,
user.skills_level,
user.credit_score,
age = 30,
user.createTime,
user.updataTime,
user.status,
user.parentId,
};
return jm;
}
#endregion
#region 获取购物车商品数量
///
/// 获取购物车商品数量
///
///
[HttpPost]
[Authorize]
public async Task GetCartNumber()
{
var jm = new WebApiCallBack();
var count = await _cartServices.GetCountAsync(_user.ID);
jm.status = true;
jm.msg = jm.status ? GlobalConstVars.GetDataSuccess : GlobalConstVars.GetDataFailure;
jm.data = count;
return jm;
}
#endregion
#region 商品取消/添加收藏
///
/// 商品取消/添加收藏
///
///
[HttpPost]
[Authorize]
public async Task GoodsCollectionCreateOrDelete([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
var collection = await _goodsCollectionServices.QueryByClauseAsync(p => p.goodsId == entity.id && p.userId == _user.ID);
if (collection == null)
{
var goods = await _goodsServices.QueryByIdAsync(entity.id);
if (goods == null)
{
jm.msg = GlobalErrorCodeVars.Code17001;
return jm;
}
collection = new CoreCmsGoodsCollection()
{
goodsId = goods.id,
userId = _user.ID,
goodsName = goods.name,
createTime = DateTime.Now,
};
await _goodsCollectionServices.InsertAsync(collection);
jm.msg = GlobalErrorCodeVars.Code17002;
}
else
{
await _goodsCollectionServices.DeleteAsync(collection);
jm.msg = GlobalErrorCodeVars.Code17003;
}
jm.status = true;
return jm;
}
#endregion
#region 获取用户获取用户默认收货地址
///
/// 获取用户获取用户默认收货地址
///
///
[HttpPost]
[Authorize]
public async Task GetUserDefaultShip()
{
var jm = new WebApiCallBack();
var ship = await _userShipServices.QueryByClauseAsync(p => p.isDefault && p.userId == _user.ID) ?? await _userShipServices.QueryByClauseAsync(p => p.userId == _user.ID, p => p.id, OrderByType.Desc);
if (ship != null)
{
var fullName = await _areaServices.GetAreaFullName(ship.areaId);
if (fullName.status)
{
ship.areaName = fullName.data.ToString();
}
}
jm.status = true;
jm.data = ship;
return jm;
}
#endregion
#region 设置默认地址
///
/// 设置默认地址
///
///
[HttpPost]
[Authorize]
public async Task SetDefShip([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
var ship = await _userShipServices.QueryByClauseAsync(p => p.id == entity.id && p.userId == _user.ID);
if (ship != null)
{
//没有默认的直接设置为默认
ship.isDefault = true;
var result = await _userShipServices.UpdateAsync(ship);
jm.status = result.code == 0;
jm.msg = jm.status ? "保存成功" : "保存失败";
}
else
{
jm.msg = "该地址不存在";
}
return jm;
}
#endregion
#region 判断用户下单可以使用多少积分
///
/// 判断用户下单可以使用多少积分
///
///
[HttpPost]
[Authorize]
public async Task GetUserPoint([FromBody] GetUserPointPost entity)
{
var jm = new WebApiCallBack();
var ship = await _userServices.GetUserPoint(_user.ID, entity.orderMoney);
jm.status = true;
jm.data = ship;
return jm;
}
#endregion
#region 获取用户的收货地址列表
///
/// 获取用户的收货地址列表
///
///
[HttpPost]
[Authorize]
public async Task GetUserShip()
{
var jm = new WebApiCallBack();
var ship = await _userShipServices.QueryListByClauseAsync(p => p.userId == _user.ID, p => p.isDefault, OrderByType.Desc);
if (ship.Any())
{
ship.ForEach(Action);
}
jm.status = true;
jm.data = ship;
return jm;
}
private async void Action(CoreCmsUserShip p)
{
var fullName = await _areaServices.GetAreaFullName(p.areaId);
if (fullName.status)
{
p.areaName = fullName.data.ToString();
}
}
#endregion
#region 保存用户地址
///
/// 保存用户地址
///
///
///
[HttpPost]
[Authorize]
public async Task SaveUserShip([FromBody] SaveUserShipPost entity)
{
var jm = new WebApiCallBack();
if (entity.id > 0)
{
//判断是否存在默认数据
if (entity.isDefault != 1)
{
if (await _userShipServices.ExistsAsync(p => p.userId == _user.ID && p.isDefault == true && p.id != entity.id) == false) entity.isDefault = 1;
}
var userShip = new CoreCmsUserShip();
userShip.id = entity.id;
userShip.userId = _user.ID;
userShip.areaId = entity.areaId;
userShip.isDefault = entity.isDefault == 1;
userShip.name = entity.name;
userShip.address = entity.address;
userShip.mobile = entity.mobile;
userShip.updateTime = DateTime.Now;
var ship = await _userShipServices.UpdateAsync(userShip);
jm.status = true;
jm.data = ship;
jm.msg = "地址保存成功";
}
else
{
//判断是否存在默认数据
if (entity.isDefault != 1)
{
if (await _userShipServices.ExistsAsync(p => p.userId == _user.ID && p.isDefault == true) == false) entity.isDefault = 1;
}
var userShip = new CoreCmsUserShip();
userShip.userId = _user.ID;
userShip.areaId = entity.areaId;
userShip.isDefault = entity.isDefault == 1;
userShip.name = entity.name;
userShip.address = entity.address;
userShip.mobile = entity.mobile;
userShip.createTime = DateTime.Now;
var ship = await _userShipServices.InsertAsync(userShip);
jm.status = true;
jm.data = ship;
jm.msg = "地址保存成功";
}
return jm;
}
#endregion
#region 获取用户单个地址详情
///
/// 获取用户单个地址详情
///
///
[HttpPost]
[Authorize]
public async Task GetShipDetail([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
var ship = await _userShipServices.QueryByClauseAsync(p => p.userId == _user.ID && p.id == entity.id);
if (ship != null)
{
//var areas = _areaServices.FindListAsync();
var fullName = await _areaServices.GetAreaFullName(ship.areaId);
if (fullName.status)
{
ship.areaName = fullName.data.ToString();
}
}
jm.status = true;
jm.data = ship;
return jm;
}
#endregion
#region 收货地址删除
///
/// 收货地址删除
///
///
[HttpPost]
[Authorize]
public async Task RemoveShip([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
jm.status = await _userShipServices.DeleteAsync(p => p.userId == _user.ID && p.id == entity.id);
jm.msg = jm.status ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
if (jm.status)
{
//如果只有一个地址了,默认将最后一个剩余的地址设置为默认。
var anySum = await _userShipServices.GetCountAsync(p => p.userId == _user.ID);
if (anySum == 1)
{
await _userShipServices.UpdateAsync(p => new CoreCmsUserShip() { isDefault = true }, p => p.userId == _user.ID);
}
}
return jm;
}
#endregion
#region 支付
///
/// 支付
///
///
[HttpPost]
[Authorize]
public async Task Pay([FromBody] PayPost entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.ids))
{
jm.code = 13100;
jm.msg = GlobalErrorCodeVars.Code13100;
}
else if (string.IsNullOrEmpty(entity.payment_code))
{
jm.code = 10055;
jm.msg = GlobalErrorCodeVars.Code10055;
}
else if (entity.payment_type == 0)
{
jm.code = 10051;
jm.msg = GlobalErrorCodeVars.Code10051;
}
//生成支付单,并发起支付
jm = await _billPaymentsServices.Pay(entity.ids, entity.payment_code, _user.ID, entity.payment_type,
entity.@params);
return jm;
}
///
/// 支付
///
///
[HttpPost]
[Authorize]
public async Task UsePay([FromBody] JObject @params)
{
var jm = new WebApiCallBack();
var userId = _user.ID;
if (@params == null || !@params.ContainsKey("money"))
{
jm.status = false;
jm.msg = "参数不全";
return jm;
}
if (@params.ContainsKey("nonce"))
{
@params.Remove("nonce");
}
if (@params.ContainsKey("sign"))
{
@params.Remove("sign");
}
if (!@params.ContainsKey("trade_type"))
{
@params.Add("trade_type", "JSAPI_OFFICIAL");
}
if (@params["money"].ToString() != "5" && @params["money"].ToString() != "10")
{
jm.status = false;
jm.msg = "参数不正确";
return jm;
}
@params["money"] = "0.01";
//生成支付单,并发起支付
jm = await _billPaymentsServices.Pay("", "wechatpay", _user.ID, 6, @params);
return jm;
}
#endregion
#region 订单评价
///
/// 订单评价
///
///
[HttpPost]
[Authorize]
public async Task OrderEvaluate([FromBody] OrderEvaluatePost entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.orderId))
{
jm.code = 13100;
jm.msg = GlobalErrorCodeVars.Code13100;
}
else if (entity.items == null || entity.items.Count == 0)
{
jm.code = 10051;
jm.msg = GlobalErrorCodeVars.Code10051;
}
jm = await _goodsCommentServices.AddComment(entity.orderId, entity.items, _user.ID);
jm.otherData = entity;
return jm;
}
#endregion
#region 我的银行卡列表
///
/// 我的银行卡列表
///
///
[HttpPost]
[Authorize]
public async Task GetMyBankcardsList()
{
var jm = await _userBankCardServices.GetMyBankcardsList(_user.ID);
return jm;
}
#endregion
#region 添加银行卡
///
/// 添加银行卡
///
///
[HttpPost]
[Authorize]
public async Task AddBankCards([FromBody] CoreCmsUserBankCard entity)
{
entity.userId = _user.ID;
var jm = await _userBankCardServices.AddBankCards(entity);
return jm;
}
#endregion
#region 设置默认银行卡
///
/// 设置默认银行卡
///
///
[HttpPost]
[Authorize]
public async Task SetDefaultBankCard([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
if (entity.id == 0)
{
jm.msg = GlobalErrorCodeVars.Code10051;
return jm;
}
jm = await _userBankCardServices.SetDefault(_user.ID, entity.id);
return jm;
}
#endregion
#region 获取银行卡信息
///
/// 获取银行卡信息
///
///
[HttpPost]
[Authorize]
public async Task GetBankCardInfo([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
if (entity.id == 0)
{
jm.msg = GlobalErrorCodeVars.Code10051;
return jm;
}
jm = await _userBankCardServices.GetBankcardInfo(_user.ID, entity.id);
return jm;
}
#endregion
#region 获取用户默认银行卡信息
///
/// 获取用户默认银行卡信息
///
///
[HttpPost]
[Authorize]
public async Task GetDefaultBankCard()
{
var jm = await _userBankCardServices.GetDefaultBankCard(_user.ID);
return jm;
}
#endregion
#region 删除银行卡信息
///
/// 删除银行卡信息
///
///
[HttpPost]
[Authorize]
public async Task Removebankcard([FromBody] FMIntId entity)
{
var jm = await _userBankCardServices.Removebankcard(entity.id, _user.ID);
return jm;
}
#endregion
#region 获取银行卡组织信息
///
/// 获取银行卡组织信息
///
///
[HttpPost]
[Authorize]
public WebApiCallBack GetBankCardsOrganization([FromBody] FMStringId entity)
{
var jm = _userBankCardServices.BankCardsOrganization(entity.id);
return jm;
}
#endregion
#region 提现申请
///
/// 提现申请
///
///
[HttpPost]
[Authorize]
public async Task Cash([FromBody] FMIntId entity)
{
var money = entity.data.ObjectToDecimal(0);
var jm = await _userTocashServices.Tocash(_user.ID, money, entity.id);
return jm;
}
#endregion
#region 提现记录列表
///
/// 提现记录列表
///
///
[HttpPost]
[Authorize]
public async Task CashList([FromBody] FMPageByIntId entity)
{
var jm = await _userTocashServices.UserToCashList(_user.ID, entity.page, entity.limit, entity.id);
return jm;
}
#endregion
#region 获取我的余额明细列表
///
/// 获取我的余额明细列表
///
///
[HttpPost]
[Authorize]
public async Task UserBalance([FromBody] FMGetBalancePost entity)
{
var jm = new WebApiCallBack();
var where = PredicateBuilder.True();
where = where.And(p => p.userId == _user.ID);
if (entity.id > 0)
{
where = where.And(p => p.type == entity.id);
}
if (!string.IsNullOrEmpty(entity.propsDate))
{
if (entity.propsDate.Contains("-"))
{
var dts = entity.propsDate.Split("-");
if (dts.Length == 2)
{
var dt = dts[0].ObjectToDate(DateTime.Now);
var startTime = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
var dt2 = dts[1].ObjectToDate(DateTime.Now);
var endTime = new DateTime(dt2.Year, dt2.Month, dt2.Day, 23, 59, 59);
where = where.And(p => p.createTime > startTime && p.createTime < endTime);
}
}
else
{
var dt = entity.propsDate.ObjectToDate(DateTime.Now);
var startTime = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
var endTime = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
where = where.And(p => p.createTime > startTime && p.createTime < endTime);
}
}
var data = await _userBalanceServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc,
entity.page, entity.limit);
if (data.Any())
{
foreach (var item in data)
{
item.typeName = EnumHelper.GetEnumDescriptionByValue(item.type);
}
}
var sunMoney = await _userBalanceServices.GetSumAsync(where, p => p.money);
jm.status = true;
jm.data = data;
jm.otherData = new
{
data.TotalPages,
sunMoney
};
return jm;
}
#endregion
#region 我的发票列表
///
/// 我的发票列表
///
///
[HttpPost]
[Authorize]
public async Task UserInvoiceList([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var where = PredicateBuilder.True();
where = where.And(p => p.userId == _user.ID);
if (entity.id > 0)
{
where = where.And(p => p.id == entity.id);
}
var status = entity.otherData.ObjectToInt(0);
if (status > 0)
{
where = where.And(p => p.status == status);
}
var data = await _invoiceServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc,
entity.page, entity.limit);
if (data.Any())
{
foreach (var item in data)
{
item.categoryName = EnumHelper.GetEnumDescriptionByValue(item.category);
item.typeName = EnumHelper.GetEnumDescriptionByValue(item.type);
item.statusName = EnumHelper.GetEnumDescriptionByValue(item.status);
}
}
jm.status = true;
jm.data = data;
jm.otherData = new
{
data.TotalCount,
data.TotalPages
};
return jm;
}
#endregion
#region 我的积分列表
///
/// 我的积分列表
///
///
[HttpPost]
[Authorize]
public async Task UserPointLog([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var where = PredicateBuilder.True();
where = where.And(p => p.userId == _user.ID);
var data = await _userPointLogServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);
if (data.Any())
{
foreach (var item in data)
{
item.typeName = EnumHelper.GetEnumDescriptionByValue(item.type);
}
}
jm.status = true;
jm.data = data;
jm.otherData = new
{
data.TotalCount,
data.TotalPages
};
return jm;
}
#endregion
#region 取得商品收藏记录(关注)
///
/// 取得商品收藏记录(关注)
///
///
[HttpPost]
[Authorize]
public async Task GoodsCollectionList([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var data = await _goodsCollectionServices.QueryPageAsync(p => p.userId == _user.ID, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);
jm.status = true;
jm.data = new
{
list = data,
count = data.TotalCount,
};
return jm;
}
#endregion
#region 添加商品收藏(关注)
///
/// 添加商品收藏(关注)
///
///
[HttpPost]
[Authorize]
public async Task GoodsCollection([FromBody] FMIntId entity)
{
var jm = await _goodsCollectionServices.ToAdd(_user.ID, entity.id);
return jm;
}
#endregion
#region 取得商品浏览足迹
///
/// 取得商品浏览足迹
///
///
[HttpPost]
[Authorize]
public async Task Goodsbrowsing([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var data = await _goodsBrowsingServices.QueryPageAsync(p => p.userId == _user.ID, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);
jm.status = true;
jm.data = new
{
list = data,
count = data.TotalCount,
};
return jm;
}
#endregion
#region 添加商品浏览足迹
///
/// 添加商品浏览足迹
///
///
[HttpPost]
[Authorize]
public async Task AddGoodsBrowsing([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
//获取数据
var goods = await _goodsServices.QueryByIdAsync(entity.id);
if (goods == null)
{
jm.msg = GlobalConstVars.DataisNo;
return jm;
}
var goodsBrowsing = new CoreCmsGoodsBrowsing
{
goodsId = goods.id,
userId = _user.ID,
goodsName = goods.name,
createTime = DateTime.Now,
isdel = false
};
jm.status = await _goodsBrowsingServices.InsertAsync(goodsBrowsing) > 0;
jm.msg = jm.status ? GlobalConstVars.InsertSuccess : GlobalConstVars.InsertFailure;
return jm;
}
#endregion
#region 删除商品浏览足迹
///
/// 删除商品浏览足迹
///
///
[HttpPost]
[Authorize]
public async Task DelGoodsBrowsing([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
jm.status = await _goodsBrowsingServices.DeleteAsync(p => p.userId == _user.ID && p.goodsId == entity.id);
jm.msg = jm.status ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
return jm;
}
#endregion
#region 更换头像
///
/// 更换头像
///
///
[HttpPost]
[Authorize]
public async Task ChangeAvatar([FromBody] FMStringId entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.id))
{
jm.data = GlobalErrorCodeVars.Code11003;
return jm;
}
var up = await _userServices.UpdateAsync(p => new CoreCmsUser() { avatarImage = entity.id },
p => p.id == _user.ID);
jm.status = up;
jm.msg = jm.status ? "设置头像成功" : "设置头像失败";
jm.data = entity.id;
return jm;
}
#endregion
#region 编辑用户信息
///
/// 编辑用户信息
///
///
[HttpPost]
[Authorize]
public async Task EditInfo([FromBody] EditInfoPost entity)
{
var jm = new WebApiCallBack();
if (entity.birthday == null)
{
jm.msg = GlobalErrorCodeVars.Code11027;
return jm;
}
if (string.IsNullOrEmpty(entity.nickname))
{
jm.msg = GlobalErrorCodeVars.Code11028;
return jm;
}
if (entity.sex <= 0)
{
jm.msg = GlobalErrorCodeVars.Code11029;
return jm;
}
var up = await _userServices.UpdateAsync(p => new CoreCmsUser() { birthday = entity.birthday, nickName = entity.nickname, sex = entity.sex },
p => p.id == _user.ID);
jm.status = up;
jm.msg = jm.status ? "资料保存成功" : "资料保存失败";
return jm;
}
///
/// 编辑用户信息
///
///
///
[HttpPost]
public async Task EditUserInfo([FromBody] EditLoginUserInfo entity)
{
var jm = new WebApiCallBack();
if (entity.birthday == null)
{
jm.msg = GlobalErrorCodeVars.Code11027;
return jm;
}
if (string.IsNullOrEmpty(entity.nickName))
{
jm.msg = GlobalErrorCodeVars.Code11028;
return jm;
}
if (entity.sex <= 0)
{
jm.msg = GlobalErrorCodeVars.Code11029;
return jm;
}
if (string.IsNullOrEmpty(entity.avatar))
{
jm.msg = GlobalErrorCodeVars.Code10003;
return jm;
}
if (entity.avatar.IndexOf("http") == -1)
{
//entity.avatar # data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4
//IFormFile file
var filesStorageOptions = await _settingServices.GetFilesStorageOptions();
//初始化上传参数
var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M
Random random = new Random();
string fileName = _user.ID + "_" + random.Next(1000, 9999) + ".png";
string fileExt = Path.GetExtension(fileName).ToLowerInvariant();
//检查文件扩展名
if (string.IsNullOrEmpty(fileExt) ||
Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
{
jm.msg = "上传文件扩展名是不允许的扩展名,请上传后缀名为:" + filesStorageOptions.FileTypes;
return jm;
}
var file = Base64ToFormFile(entity.avatar, fileName);
//// 使用StreamReader来读取文件内容
//using (var reader = new StreamReader(file.OpenReadStream(), Encoding.UTF8))
//{
// var content = await reader.ReadToEndAsync(); // 注意:这可能会消耗大量内存对于大文件,所以需要限制上传大小
// // 检查内容是否合法
// if (CommonHelper.CheckData(content))
// {
// jm.msg = "请勿提交非法数据。";
// return jm;
// }
//}
filesStorageOptions.Path = "users";
string url = string.Empty;
if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString())
{
url = await _toolsServices.UpLoadFileForLocalStorage(filesStorageOptions, fileExt, file, (int)GlobalEnumVars.FilesStorageLocation.API);
}
else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString())
{
url = await _toolsServices.UpLoadFileForAliYunOSS(filesStorageOptions, fileExt, file);
}
else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString())
{
url = await _toolsServices.UpLoadFileForQCloudOSS(filesStorageOptions, fileExt, file);
}
else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString())
{
url = await _toolsServices.UpLoadFileForQiNiuKoDo(filesStorageOptions, fileExt, file);
}
entity.avatar = url;
}
var up = await _userServices.UpdateAsync(p => new CoreCmsUser()
{
avatarImage = entity.avatar,
birthday = entity.birthday,
nickName = entity.nickName,
sex = entity.sex
},
p => p.id == _user.ID);
await _userWeChatInfoServices.UpdateAsync(p => new CoreCmsUserWeChatInfo()
{
nickName = entity.nickName,
avatar = entity.avatar,
gender = entity.sex
}, p => p.userId == _user.ID);
jm.status = up;
jm.msg = jm.status ? "资料保存成功" : "资料保存失败";
return jm;
}
private IFormFile Base64ToFormFile(string base64, string fileName)
{
// 匹配并去除 data:image/png;base64, 前缀
var match = Regex.Match(base64, @"data:image/(?.+?);base64,(?.+)");
if (!match.Success)
throw new ArgumentException("base64格式不正确");
var base64Data = match.Groups["data"].Value;
var bytes = Convert.FromBase64String(base64Data);
var stream = new MemoryStream(bytes);
// 构造 IFormFile
return new FormFile(stream, 0, bytes.Length, "file", fileName)
{
Headers = new HeaderDictionary(),
ContentType = $"image/{match.Groups["type"].Value}"
};
}
#endregion
#region 修改用户密码
///
/// 修改用户密码
///
///
[HttpPost]
[Authorize]
public async Task EditPwd([FromBody] EditPwdPost entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.repwd))
{
jm.msg = GlobalErrorCodeVars.Code11014;
return jm;
}
if (string.IsNullOrEmpty(entity.newpwd))
{
jm.msg = GlobalErrorCodeVars.Code11013;
return jm;
}
if (entity.repwd != entity.newpwd)
{
jm.msg = GlobalErrorCodeVars.Code11025;
return jm;
}
jm = await _userServices.ChangePassword(_user.ID, entity.newpwd, entity.pwd);
return jm;
}
#endregion
#region 邀请好友(获取我的要求相关信息)
///
/// 邀请好友(获取我的要求相关信息)
///
///
[HttpPost]
[Authorize]
public async Task MyInvite()
{
var jm = new WebApiCallBack();
jm.status = true;
//我的邀请码
var code = UserHelper.GetShareCodeByUserId(_user.ID);
//我邀请的人数
var number = await _userServices.GetCountAsync(p => p.parentId == _user.ID);
//邀请赚的佣金
var money = await _userBalanceServices.GetInviteCommission(_user.ID);
//是否有上级
var userInfo = await _userServices.QueryByIdAsync(_user.ID);
bool isSuperior = userInfo != null && userInfo.parentId > 0;
jm.data = new
{
code,
number,
money,
isSuperior
};
return jm;
}
#endregion
#region 设置我的上级邀请人
///
/// 设置我的上级邀请人
///
///
[HttpPost]
[Authorize]
public async Task SetMyInvite([FromBody] FMIntId entity)
{
var jm = new WebApiCallBack();
if (entity.id == 0)
{
jm.msg = "请输入推荐人邀请码!";
return jm;
}
var code = UserHelper.GetUserIdByShareCode(entity.id);
jm = await _userServices.SetMyInvite(code, _user.ID);
return jm;
}
#endregion
#region 获取我的上级邀请人
///
/// 获取我的上级邀请人
///
///
[HttpPost]
[Authorize]
public async Task GetMyInvite()
{
return await _userServices.GetMyInvite(_user.ID);
}
#endregion
#region 获取我的下级用户数量
///
/// 获取我的下级用户数量
///
///
[HttpPost]
[Authorize]
public async Task GetMyChildSum()
{
var jm = new WebApiCallBack();
var first = await _userServices.QueryChildCountAsync(_user.ID);
var second = await _userServices.QueryChildCountAsync(_user.ID, 2);
var monthFirst = await _userServices.QueryChildCountAsync(_user.ID, 1, true);
var monthSecond = await _userServices.QueryChildCountAsync(_user.ID, 2, true);
jm.status = true;
jm.data = new
{
count = first + second,
first,
second,
monthCount = monthFirst + monthSecond,
monthFirst,
monthSecond
};
return jm;
}
#endregion
#region 获取用户推荐列表
///
/// 获取用户推荐列表
///
///
[HttpPost]
[Authorize]
public async Task Recommend([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var where = PredicateBuilder.True();
where = where.And(p => p.parentId == _user.ID);
var data = await _userServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);
jm.status = true;
jm.data = data.Select(p => new
{
p.nickName,
p.avatarImage,
mobile = UserHelper.FormatMobile(p.mobile),
p.createTime,
p.childNum
});
jm.otherData = new
{
data.TotalCount,
data.TotalPages
};
return jm;
}
#endregion
#region 获取用户邀请码
///
/// 获取用户邀请码
///
///
[HttpPost]
[Authorize]
public WebApiCallBack ShareCode()
{
var jm = new WebApiCallBack();
jm.status = true;
jm.data = UserHelper.GetShareCodeByUserId(_user.ID);
return jm;
}
#endregion
#region 判断是否签到
///
/// 判断是否签到
///
///
[HttpPost]
[Authorize]
public async Task IsSign()
{
var jm = await _userPointLogServices.IsSign(_user.ID);
return jm;
}
#endregion
#region 用户签到
///
/// 用户签到
///
///
[HttpPost]
[Authorize]
public async Task Sign()
{
var jm = await _userPointLogServices.Sign(_user.ID);
return jm;
}
#endregion
#region 用户找回密码
///
/// 用户找回密码
///
///
[HttpPost]
[Authorize]
public async Task ForgetPwd([FromBody] FMForgetPwdPost entity)
{
var jm = new WebApiCallBack();
if (string.IsNullOrEmpty(entity.mobile))
{
jm.msg = GlobalErrorCodeVars.Code10051;
return jm;
}
if (string.IsNullOrEmpty(entity.code))
{
jm.msg = GlobalErrorCodeVars.Code10013;
return jm;
}
if (string.IsNullOrEmpty(entity.newpwd))
{
jm.msg = GlobalErrorCodeVars.Code11013;
return jm;
}
if (string.IsNullOrEmpty(entity.repwd))
{
jm.msg = GlobalErrorCodeVars.Code11014;
return jm;
}
if (entity.newpwd != entity.repwd)
{
jm.msg = GlobalErrorCodeVars.Code11025;
return jm;
}
jm = await _userServices.ForgetPassword(entity.mobile, entity.code, entity.newpwd);
return jm;
}
#endregion
#region 取得服务卡列表信息
///
/// 取得服务卡列表信息
///
///
[HttpPost]
[Authorize]
public async Task GetServicesPageList([FromBody] FMPageByIntId entity)
{
var jm = new WebApiCallBack();
var where = PredicateBuilder.True();
//where = where.And(p => p.status == (int)GlobalEnumVars.ServicesStatus.Shelve);
where = where.And(p => p.userId == _user.ID);
where = where.And(p => p.isPay == true);
var orders = await _userServicesOrderServices.QueryPageAsync(where, p => p.payTime, OrderByType.Desc, entity.page, entity.limit);
if (orders.Any())
{
var services = await _servicesServices.QueryAsync();
foreach (var item in orders)
{
item.service = services.Find(p => p.id == item.servicesId);
item.statusStr =
EnumHelper.GetEnumDescriptionByValue(item.status);
}
}
jm.status = true;
jm.data = new
{
list = orders,
count = orders.TotalCount,
};
return jm;
}
#endregion
#region 取得服务卡列表信息
///
/// 取得服务卡列表信息
///
///
[HttpPost]
[Authorize]
public async Task GetServicesTickets([FromBody] FMPageByStringId entity)
{
var jm = new WebApiCallBack();
var order = await _userServicesOrderServices.QueryByClauseAsync(p => p.serviceOrderId == entity.id);
if (order == null)
{
jm.msg = "订单信息获取失败";
return jm;
}
var model = await _servicesServices.QueryByClauseAsync(p => p.id == order.servicesId);
if (model != null)
{
var dt = DateTime.Now;
TimeSpan ts = model.endTime.Subtract(dt);
model.timestamp = (int)ts.TotalSeconds;
if (!string.IsNullOrEmpty(model.consumableStore))
{
var consumableStoreStr = CommonHelper.GetCaptureInterceptedText(model.consumableStore, ",");
var consumableStoreIds = CommonHelper.StringToIntArray(consumableStoreStr);
if (consumableStoreIds.Any())
{
var stores = await _storeServices.QueryListByClauseAsync(p => consumableStoreIds.Contains(p.id));
model.consumableStores = stores.Select(p => p.storeName).ToList();
}
}
if (!string.IsNullOrEmpty(model.allowedMembership))
{
var allowedMembershipStr = CommonHelper.GetCaptureInterceptedText(model.allowedMembership, ",");
var allowedMembershipIds = CommonHelper.StringToIntArray(allowedMembershipStr);
if (allowedMembershipIds.Any())
{
var userGrades = await _userGradeServices.QueryListByClauseAsync(p => allowedMembershipIds.Contains(p.id));
model.allowedMemberships = userGrades.Select(p => p.title).ToList();
}
}
}
var orders = await _userServicesTicketServices.QueryPageAsync(p => p.serviceOrderId == entity.id, p => p.createTime, OrderByType.Asc, entity.page, entity.limit);
if (orders.Any())
{
foreach (var item in orders)
{
item.statusStr =
EnumHelper.GetEnumDescriptionByValue(item.status);
}
}
jm.status = true;
jm.data = new
{
model,
list = orders,
count = orders.TotalCount,
};
return jm;
}
#endregion
///
/// 获取黑名单列表
///
///
[HttpGet]
[Authorize]
public async Task GetMyBlackList()
{
var userId = _user.ID;
var list = await _dbBase.Ado.SqlQueryAsync($"select b.*,u.nickName as BlockedNickName,u.avatarImage as BlockedAvatarImage from CoreCmsUserBlacklist b left join CoreCmsUser u on b.BlockedUserId=u.id where b.UserId={userId}");
return new WebApiDto()
{
Code = 0,
Data = list
};
}
///
/// 添加黑名单
///
///
///
[HttpPost]
[Authorize]
public async Task AddUserBlack([FromBody] FMIntId entity)
{
var userId = _user.ID;
var toUserId = entity.id;
var isSuccess = await _coreCmsUserBlacklistServices.UserBlackUser(userId, toUserId, 0, "用户主动拉黑");
return new WebApiDto()
{
Code = 0,
Msg = isSuccess ? "已添加黑名单" : "已在黑名单中"
};
}
///
/// 取消拉黑名单
///
///
///
[HttpPost]
[Authorize]
public async Task CancelUserBlack([FromBody] FMIntId entity)
{
var userId = _user.ID;
var toUserId = entity.id;
await _coreCmsUserBlacklistServices.UserBlackUser(userId, toUserId, 1);
return new WebApiDto()
{
Code = 0,
Msg = "已取消拉黑"
};
}
//
}
}