(借款项目)Jwt+API方法+IOC+ajax调用

 

****项目引用一个JWT帮助类

----------接口类-------------------

public interface IBusiness
{
/// <summary>
/// 登录 根据用户名和密码查询用户所有信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
UserModel Login(UserModel info);
/// <summary>
/// 通过用户id找到用户的所有还款信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
List<RepayModel> ShowList(int id);
/// <summary>
/// 用户id和还款id进行还款
/// </summary>
/// <param name="uid"></param>
/// <param name="rid"></param>
/// <returns></returns>
int Repay(int uid, int rid);
/// <summary>
/// 通过用户id 对余额进行充值
/// </summary>
/// <param name="id"></param>
/// <param name="money"></param>
/// <returns></returns>
int Chonzhi(int id, Decimal money);
}

 

-----------------------数据访问层------------------------

/// <summary>
/// 登录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public UserModel Login(UserModel model)
{

using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
return connection.Query<UserModel>($"select * from Base_UserInfo where UserName='{model.UserName}' and PassWord='{model.PassWord}'").FirstOrDefault();
}

}
/// <summary>
/// 获取还款信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public List<RepayModel> ShowList(int id)
{
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=Month;Integrated Security=True"))
{
return connection.Query<RepayModel>($"select * from Base_RepayInfo where UID={id}").ToList();
}
}
/// <summary>
/// 还款
/// </summary>
/// <param name="uid"></param>
/// <param name="rid"></param>
/// <returns></returns>
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;//还款失败
}
}

}
}
/// <summary>
/// 充值
/// </summary>
/// <returns></returns>
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}");
}
}

--------------------------控制器-----------------------

/// <summary>
/// 登录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public APIResult<string> Login([FromForm]UserModel model)
{
UserModel user = _business.Login(model);
APIResult<string> result = new APIResult<string>();
if (user != null)
{
JWTHelper jwt = new JWTHelper();
Dictionary<string, object> keys = new Dictionary<string, object>();
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;
}
/// <summary>
/// 显示还款信息
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
public List<RepayModel> Show(string token)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject<UserModel>(json);
if (model!=null)
{
//通过id 获取用户的还款记录
return _business.ShowList(model.ID);
}

return null;
}

/// <summary>
/// 还款
/// </summary>

/// <param name="rid"></param>

/// <param name="token"></param>

/// <returns></returns>
[HttpGet]
public int ReMoney(int rid, string token)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject<UserModel>(json);
if (model!=null)
{
//用户id和还款id
return _business.Repay(model.ID,rid);
}
return 0;
}

/// <summary>
/// 根据用户id充值
/// </summary>
/// <param name="token"></param>
/// <param name="money"></param>
/// <returns></returns>
[HttpGet]
public int Chongzhi(string token,Decimal money)
{
JWTHelper jWT = new JWTHelper();
//解码
string json = jWT.GetPayload(token);
//转化为对象
UserModel model = JsonConvert.DeserializeObject<UserModel>(json);
if (model != null)
{
//用户id和充值钱数
return _business.Chonzhi(model.ID, money);
}
return 0;
}

 -----------------------显示页面-----------------------

<h1>Show</h1>
<div>
余额<div id="yue"></div>
<input id="money" type="text" /><input id="btnCZ" type="button" value="充值" />
</div>
<table class="table">
<tr class="active">
<td>期数</td>
<td>应还本金</td>
<td>应还利息</td>
<td>还款总额</td>
<td>还款日期</td>
<td>还款状态</td>
</tr>
<tbody id="tb"></tbody>
</table>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
$(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 = "<tr>" +
"<td>" + item.deadLine + "</td>" +
"<td>" + item.reTime+ "</td>" +
"<td>" + item.capital + "</td>" +
"<td>" + item.accrual + "</td>" +
"<td>" + item.total + "</td>" +
"<td>" + (item.statu == 0 ? "已还款" : (item.statu==1 )? "<a onclick='Repay(" + item.id + ")'>还款</a>" : "<a disable=true>待还款</a>") + "</td>" +
"</tr>";
$("#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("还款失败");
}
}
});
}

</script>

----------登录页面--------------------

<h1>Index</h1>

<table>
<tr>
<td>用户名</td>
<td><input id="name" type="text" /></td>
</tr>
<tr>
<td>密码</td>
<td><input id="psd" type="text" /></td>
</tr>
<tr>
<td><input id="btnLog" type="button" value="登录" /></td>
<td><input id="btnReg" type="button" value="注册" /></td>
</tr>
</table>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
$("#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);
}
}
});

});
</script>

注:数据根据实际改变

posted @ 2020-06-10 13:49  奇怪问题  阅读(280)  评论(0编辑  收藏  举报