ajax jwt请求,(借款项目)Jwt+API方法+ajax调用

本文展示了如何在API接口中使用JWT进行身份验证,并结合数据访问层和控制器实现用户登录、还款信息展示、还款处理及充值功能。用户通过登录获取JWT令牌,该令牌用于后续接口调用的身份验证。还款流程包括检查余额、减少余额和更新还款状态。整个系统使用了SQL数据库存储用户信息和还款记录。
摘要由CSDN通过智能技术生成

****项目引用一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值