个人未开通网站: http://justin1107.pc.evyundata.cn/vip_justin1107.html


Api

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Iot.Loan.Exam.Dapper;
using Iot.Loan.Exam.Models;
using JWT.Exceptions;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; namespace Iot.Loan.Exam.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[EnableCors("any")]
public class LoanController : ControllerBase
{
JWTHelper helper = new JWTHelper();
private IDapper dapper;
public LoanController(IDapper _dapper)
{
dapper = _dapper;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
[HttpPost]
public string Login([FromForm]UserInfo info)
{
//得到登录model
UserInfo model = dapper.Login(info);
if (model != null)
{
//定义字典存放用户登录的信息
Dictionary<string, object> keys = new Dictionary<string, object>();
keys.Add("User_Name", model.User_Name);
keys.Add("User_ID", model.User_ID);
keys.Add("User_Pwd", model.User_Pwd);
//得到toekn,给他失效时间
string token = helper.GetToken(keys, 30000);
return token;
}
else
{
return null;
}
}
/// <summary>
/// 还款信息列表
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
public async Task<List<HuK_UserInfo>> Select(string token)
{
//token解码
string json = helper.GetPayload(token);
//反序列化
UserInfo model = JsonConvert.DeserializeObject<UserInfo>(json);
if (model != null)
{
return await Task.Run(() => { return dapper.Select(model.User_ID); });
}
else
{
return null;
}
}
/// <summary>
/// 账户信息列表
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
public async Task<List<ZhuanHu_UserInfo>> ZhuanHuSelect(string token)
{
//token解码
string json = helper.GetPayload(token);
//反序列化
UserInfo model = JsonConvert.DeserializeObject<UserInfo>(json);
if (model != null)
{
return await Task.Run(() => { return dapper.YSelect(model.User_ID); });
}
else
{
return null;
}
}
/// <summary>
/// 还款
/// </summary>
/// <param name="HkId">还款ID</param>
/// <param name="token">用户登录的token,</param>
/// <returns></returns>
[HttpPost]
public int HK(int HkId, string token)
{
string json = helper.GetPayload(token);
UserInfo model = JsonConvert.DeserializeObject<UserInfo>(json);
if (model != null)
{
return dapper.HunKuan(model.User_ID, HkId);
}
else
{
return 0;
}
}
/// <summary>
/// 充值
/// </summary>
/// <param name="money">充值金额</param>
/// <param name="token">用户登录保存的Token</param>
/// <returns></returns>
[HttpPost]
public int CZ(decimal money, string token)
{
//token解码
string json = helper.GetPayload(token);
//反序列化
UserInfo info = JsonConvert.DeserializeObject<UserInfo>(json);
if (info != null)
{
return dapper.CzMoney(info.User_ID, money);
}
else
{
return 0;
}
}
}
}

cshtml

<script src="~/lib/jquery/dist/jquery.js"></script>
<div style="width:150px;height:100px;background-color:aqua">
<table>
<tr>
<th style="width:200px;">可用余额<br /></th>
<th id="th"></th>
</tr>
<tr>
<th colspan="2">
<input hidden="hidden" id="cz_money" type="text" />
<input id="btn_CZ" type="button" value="充值" />&nbsp;&nbsp;&nbsp;&nbsp;
<input id="btn_TX" type="button" value="提现" />
</th>
</tr>
</table>
</div>
<div style="margin-left:0px;margin-top:15px;">
<table>
<tr>
<th>还款期数</th>
<th>还款日期</th>
<th>应还本金</th>
<th>还款利息</th>
<th>还款总额</th>
<th>还款状态</th> </tr>
<tbody id="tb"></tbody>
</table>
</div>
<script>
$('#btn_CZ').click(function () {
$.ajax({
url: 'http://localhost:53048/Api/Loan/CZ?money=' + $('#cz_money').val() + '&token=' + localStorage["User_Name"],
type: 'post',
contentType: 'application/x-www-form-urlencoded',
accepts: 'application/x-www-form-urlencoded',
success: function (data) {
if (data > 0) {
$('#cz_money').attr('hidden', 'hidden');
window.location.reload();
} else {
alert("网络不可用,无法充值");
return;
}
}
})
})
$.ajax({
url: 'http://localhost:53048/Api/Loan/Select?token=' + localStorage["User_Name"],
type: 'post',
contentType: 'application/x-www-form-urlencoded',
accepts: 'application/x-www-form-urlencoded',
success: function (data) {
var tr = '';
var state = '';
$.each(data, function (i, t) {
$('#tb').empty();
if (t.hk_State == 1) {
state = '已还清';
} else if (t.hk_State == 0) {
state = '<a href="#" onclick="HuK(' + t.huanK_id + ')">还款</a>';
} else if (t.hk_State == 2) {
state = '还款';
}
tr += '<tr>';
tr += '<th>' + t.hK_QiShu + '</th>';
tr += '<th>' + t.hk_DataTime + '</th>';
tr += '<th>' + t.hk_BenJin + '</th>';
tr += '<th>' + t.hk_LiXi + '</th>';
tr += '<th>' + (t.hk_BenJin + t.hk_LiXi) + '</th>';
tr += '<th>' + state + '</th>';
tr += '</tr>';
})
$('#tb').append(tr);
}
})
$.ajax({
url: 'http://localhost:53048/Api/Loan/ZhuanHuSelect?token=' + localStorage["User_Name"],
type: 'post',
contentType: 'application/x-www-form-urlencoded',
accepts: 'application/x-www-form-urlencoded',
success: function (data) {
var th = '';
$.each(data, function (i, t) {
$('#th').empty();
th += '<th>' + t.zhuHu_Money + '</th>';
})
$('#th').append(th);
}
})
function HuK(hkid) {
$.ajax({
url: 'http://localhost:53048/Api/Loan/HK?HkId=' + hkid + '&token=' + localStorage["User_Name"],
type: 'post',
contentType: 'application/x-www-form-urlencoded',
accepts: 'application/x-www-form-urlencoded',
success: function (data) {
if (data > 0) {
alert("还款成功");
window.location.reload();
} else if (data == 0) {
alert("还款失败");
} else {
alert("余额不足");
$('#cz_money').removeAttr('hidden');
}
}
})
}
</script>

DapperHelper(我使用的是接口)

    public class DapperHelper : IDapper
{
/// <summary>
/// 充值
/// </summary>
/// <param name="UserId"></param>
/// <param name="money"></param>
/// <returns></returns>
public int CzMoney(int UserId, decimal money)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Iot.Loan_DB;Integrated Security=True"))
{
return conn.Execute($"update ZhuanHu_UserInfo set ZhuHu_Money=ZhuHu_Money+{money} where User_Id={UserId}");
}
}
/// <summary>
/// 还款
/// </summary>
/// <param name="UserId"></param>
/// <param name="HkId"></param>
/// <returns></returns>
public int HunKuan(int UserId, int HkId)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Iot.Loan_DB;Integrated Security=True"))
{
object Zhmoney = conn.ExecuteScalar($"select ZhuHu_Money from ZhuanHu_UserInfo where User_Id={UserId}"); object HkMoney = conn.ExecuteScalar($"select Hk_BenJin+Hk_LiXi from HuK_UserInfo where HuanK_id={HkId}");
if (Convert.ToDouble(Zhmoney) >= Convert.ToDouble(HkMoney))
{
//开始把账户余额减少
int h = conn.Execute($"update ZhuanHu_UserInfo set ZhuHu_Money=ZhuHu_Money-{HkMoney} where User_Id={UserId}");
if (h > 0)
{
//修改还款状态
return conn.Execute($"update HuK_UserInfo set Hk_State=1 where HuanK_id={HkId}");
}
else
{
return 0;
}
}
else
{
//余额不足
return -1;
} }
} /// <summary>
/// 登录
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public UserInfo Login(UserInfo user)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Iot.Loan_DB;Integrated Security=True"))
{
string sql = $"select * from UserInfo where User_Name='{user.User_Name}' and User_Pwd='{user.User_Pwd}'";
return conn.Query<UserInfo>(sql).FirstOrDefault();
} }
/// <summary>
/// 还款信息列表
/// </summary>
/// <param name="UserId"></param>
/// <returns></returns>
public List<HuK_UserInfo> Select(int UserId)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Iot.Loan_DB;Integrated Security=True"))
{
string sql = $"select * from HuK_UserInfo where User_Id={UserId}";
return conn.Query<HuK_UserInfo>(sql).ToList();
}
}
/// <summary>
/// 账户信息列表
/// </summary>
/// <param name="UserId"></param>
/// <returns></returns>
public List<ZhuanHu_UserInfo> YSelect(int UserId)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Iot.Loan_DB;Integrated Security=True"))
{
string sql = $"select * from ZhuanHu_UserInfo where User_Id={UserId}";
return conn.Query<ZhuanHu_UserInfo>(sql).ToList();
}
}
}

.Net Core 实现账户充值,还款,用户登录(WebApi的安全)的更多相关文章

  1. ASP.NET Core 2.0 MVC - 获取当前登录用户信息

    一.前言 上篇实战完成后,没想到会有那么多的圈友给了那么多的支持,甚至连只是作为代码仓储的git上也给了一些小星星,真的感觉很惶恐啊,哈哈哈,毕竟代码写的很烂啊.由于上一篇只是大概说了下项目,所以准备 ...

  2. Asp.Net Core 项目实战之权限管理系统(5) 用户登录

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  3. .NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

    在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到 ...

  4. 【京东账户】——Mysql/PHP/Ajax爬坑之用户登录

    一.引言 实现京东的账户项目,功能模块之一,用户登录.要用到的是Apach环境,Mysql.PHP以及Ajax. 二.依据功能创建库.表.记录 创建库:jd 创建表:登录表 添加三条记录 CREATE ...

  5. ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(使用官网提供的UI)

    IdentityServer官方提供web页面,可以根据需求修改样式.具体UI下载跟配置参考官网文档. 文档地址:https://identityserver4.readthedocs.io/en/r ...

  6. ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(通过接口登录,无UI版本)

    官网接口详解文档地址:文档地址 (PS:可通过接口名称搜索相应接口信息.) 源码地址:https://github.com/YANGKANG01/IdentityServer4-IdentityAut ...

  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(19)-权限管理系统-用户登录

    系列目录 我们之前做了验证码,登录界面,却没有登录实际的代码,我们这次先把用户登录先完成了,要不权限是讲不下去了 把我们之前的表更新到EF中去 登录在Account控制器,所以我们要添加Account ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录 我们之前做了验证码,登录界面,却没有登录实际的代码,我们这次先把用户登录先 ...

  9. UWP 应用获取各类系统、用户信息 (1) - 设备和系统的基本信息、应用包信息、用户数据账户信息和用户账户信息

    应用开发中,开发者时常需要获取一些系统.用户信息用于数据统计遥测.问题反馈.用户识别等功能.本文旨在介绍在 Windows UWP 应用中获取一些常用系统.用户信息的方法.示例项目代码可参见 Gith ...

随机推荐

  1. Note -「群论」学习笔记

    目录 前置知识 群 置换 Burnside 引理与 Pólya 定理 概念引入 引例 轨道-稳定子(Orbit-Stabilizer)定理 证明 Burnside 引理 证明 Pólya 定理 证明 ...

  2. 书写高质量sql的一些建议

    It's better to light a candle than to curse the darkness 老生常谈的不要使用select * 如果硬要使用select *,那么就请忍受一下以下 ...

  3. 超详细的Cookie增删改查

    目录 1,什么是 Cookie? 1.1,存储形式 1.2,常用属性 1.3,大小限制 2,增 or 改Cookie 3,查Cookie 4,删Cookie 1,什么是 Cookie? Cookie是 ...

  4. 图解python | 安装与环境设置

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...

  5. 请求XSS攻击原理

    起因 巨人的肩膀 一个神秘URL酿大祸,差点让我背锅! (qq.com)

  6. 快速上手 vue3

    当前为vue3的基础知识点,为总结b站某视频的知识文章,在刚开始学习时自我保存在语雀,现在分享到博客. 目前找不到原视频文章地址了!!!要有兄弟看到原文地址:欢迎在下面评论! Vue3新的特性 Com ...

  7. Activity通过bundle传递数据

    从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...

  8. UnboundLocalError: local variable ‘xxx‘ referenced before assignment

    原因 在Python函数中调用了某个和全局变量同名的局部变量,导致编译器不知道此时使用的是全局变量还是局部变量 a = 3 def func(): a+=3 func() UnboundLocalEr ...

  9. VMware:用Ubuntu创建一个新的虚拟机

    1)进入VMware,选择创建新虚拟机 2)安装ISO文件 3)各种名,密码 这里有可能出错: 原因是你输入的用户名和系统用户名重复了,修改一下就可以了 4)安装位置:这里可以是任意盘,但至少要有4G ...

  10. 使用 kubebuilder 创建并部署 k8s-operator

    一.准备 本文中的示例运行环境及相关软件版本如下: Kubernetes v1.16.3 Go 1.15.6 Kubebuilder 3.1.0 Docker 20.10.7 安装kubebuilde ...