****项目引用一个JWT帮助类
----------接口类-------------------
public interface IBusiness
{
///
/// 登录 根据用户名和密码查询用户所有信息
///
///
///
UserModel Login(UserModel info);
///
/// 通过用户id找到用户的所有还款信息
///
///
///
List ShowList(int id);
///
/// 用户id和还款id进行还款
///
///
///
///
int Repay(int uid, int rid);
///
/// 通过用户id 对余额进行充值
///
///
///
///
int Chonzhi(int id, Decimal money);
}
-----------------------数据访问层------------------------
///
/// 登录
///
///
///
public UserModel Login(UserModel model)
{
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
return connection.Query($"select * from Base_UserInfo where UserName='{model.UserName}' and PassWord='{model.PassWord}'").FirstOrDefault();
}
}
///
/// 获取还款信息
///
///
///
public List ShowList(int id)
{
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
return connection.Query($"select * from Base_RepayInfo where UID={id}").ToList();
}
}
///
/// 还款
///
///
///
///
public int Repay(int uid, int rid)
{
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
//通过还款id 求出还款总金额
object total = connection.ExecuteScalar($"select Total from Base_RepayInfo where ID ={rid} ");
//通过用户id求余额
object yue = connection.ExecuteScalar($"select Balance from Base_Balance where UID={uid} ");
//判断余额是否足够
if (Convert.ToDecimal(total) > Convert.ToDecimal(yue))
{
//余额不足
return -1;
}
else
{
//根据用户id减少余额
int code = connection.Execute($"update Base_Balance set Balance=Balance-{total} where UID={uid}");
//判断是否修改成功
if (code > 0)
{
//修改成功,根据还款id改变状态
return connection.Execute($"update Base_RepayInfo set Statu=0 where ID={rid}");
}
else
{
return 0;//还款失败
}
}
}
}
///
/// 充值
///
///
public int Chonzhi(int id, Decimal money)
{
//通过用户id 查找到用户的余额对余额进行充值
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
return connection.Execute($"update Base_Balance set Balance+={money} where UID={id}");
}
}
--------------------------控制器-----------------------
///
/// 登录
///
///
///
[HttpPost]
public APIResult Login([FromForm]UserModel model)
{
UserModel user = _business.Login(model);
APIResult result = new APIResult();
if (user != null)
{
JWTHelper jwt = new JWTHelper();
Dictionary keys = new Dictionary();
keys.Add("UserName", user.UserName);
keys.Add("ID", user.ID);
keys.Add("PassWord", user.PassWord);
string token = jwt.GetToken(keys, 300000);
result.code = 1;
result.mes = "登陆成功";
result.token = token;
}
else
{
result.code = 0;
result.mes = "登陆失败";
}
return result;
}
///
/// 显示还款信息
///
///
///
[HttpGet]
public List Show(string token)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject(json);
if (model!=null)
{
//通过id 获取用户的还款记录
return _business.ShowList(model.ID);
}
return null;
}
///
/// 还款
///
///
///
///
[HttpGet]
public int ReMoney(int rid, string token)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject(json);
if (model!=null)
{
//用户id和还款id
return _business.Repay(model.ID,rid);
}
return 0;
}
///
/// 根据用户id充值
///
///
///
///
[HttpGet]
public int Chongzhi(string token,Decimal money)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject(json);
if (model != null)
{
//用户id和充值钱数
return _business.Chonzhi(model.ID, money);
}
return 0;
}
-----------------------显示页面-----------------------
Show
余额
期数 | 应还本金 | 应还利息 | 还款总额 | 还款日期 | 还款状态 |
$(function () {
$.ajax({
url: "https://localhost:44380/api/Default/Show",
data: { token: localStorage["user"] },
dataType: "json",
type: "get",
success: function (data) {
$.each(data, function (index, item) {
var time = new Date();
var tr = "
" +"
" + item.deadLine + "" +"
" + item.reTime+ "" +"
" + item.capital + "" +"
" + item.accrual + "" +"
" + item.total + "" +"
" + (item.statu == 0 ? "已还款" : (item.statu==1 )? " 还款" : " 待还款") + "" +"
";$("#tb").append(tr);
});
}
});
$("#btnCZ").click(function () {
$.ajax({
url: "https://localhost:44380/api/Default/Chongzhi",
data: { token: localStorage["user"], money: money },
dataType: "json",
type: "get",
success: function (data) {
if (data > 0) {
alert("充值成功")
//显示余额
}
else {
alert("充值失败");
}
}
});
});
});
function Repay(rid) {
$.ajax({
url: "https://localhost:44380/api/Default/ReMoney",
data: {rid:rid, token: localStorage["user"] },
dataType: "json",
type: "get",
success: function (data) {
if (data > 0) {
alert("还款成功")
//刷新页面
window.location.reload();
}
else {
alert("还款失败");
}
}
});
}
----------登录页面--------------------
Index
用户名 | |
密码 | |
$("#btnLog").click(function () {
var obj = {};
obj.UserName = $("#name").val();
obj.PassWord = $("#psd").val();
$.ajax({
url: "https://localhost:44380/api/Default/Login",
data: obj,
dataType: "json",
accepts: "application/x-www-form-urlencoded",
contentType: "application/x-www-form-urlencoded",
type: "POST",
success: function (data) {
if (data.code > 0) {
alert(data.mes);
localStorage["user"] = data.token;
window.location.href = "Show";
}
else {
alert(data.mes);
}
}
});
});
注:数据根据实际改变
标签:return,int,Jwt,ajax,API,还款,model,data,id
来源: https://www.cnblogs.com/xr0818/p/13084620.html