• .net core用户管理登录传值方式


    API 代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using DAL;
    using Model;
    using Microsoft.AspNetCore.Cors;
    using Newtonsoft.Json;
    
    namespace Test_Month6.API.Controllers
    {
        [EnableCors("any")]
        [Route("UserHK")]
        [ApiController]
        public class UserHKController : ControllerBase
        {
            JWTHelper jwt = new JWTHelper();
            //依赖注入
            private IDAL _dal;
            public UserHKController(IDAL dal)
            {
                _dal = dal;
            }
            /// <summary>
            /// 登录
            /// </summary>
            /// <param name="info"></param>
            /// <returns></returns>
            [HttpPost]
            [Route("login")]
            public string Login([FromForm]UserInfo info)
            {
                UserInfo model = _dal.Login(info);
                if (model != null)
                {
                    Dictionary<string, object> keys = new Dictionary<string, object>();
                    keys.Add("ID", model.ID);
                    keys.Add("User_Name", model.User_Name);
                    keys.Add("User_PassWord", model.User_PassWord);
                    string token = jwt.GetToken(keys, 300000);
                    return token;
                }
                return null;
            }
            /// <summary>
            /// 显示还款列表
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            [HttpGet]
            [Route("list")]
            public async Task<List<UserHK>> Select(string token)
            {
                List<UserHK> list = new List<UserHK>();
                string json = jwt.GetPayload(token);
                //序列化
                UserHK user = JsonConvert.DeserializeObject<UserHK>(json);
                if (user != null)
                {
                    list = await Task.Run(() => { return _dal.Select(user.ID); });
                }
                foreach (var item in list)
                {
                    item.date = item.HKDate.ToString("yyyy-MM-dd");
                }
                return list;
            }
            /// <summary>
            /// 显示账户信息
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            [HttpGet]
            [Route("zhanghu")]
            public async Task<List<ZhangHuInfo>> ZhangHus(string token)
            {
                List<ZhangHuInfo> zhanghu = new List<ZhangHuInfo>();
                string json = jwt.GetPayload(token);
                ZhangHuInfo zhang = JsonConvert.DeserializeObject<ZhangHuInfo>(json);
                if (zhang != null)
                {
                    zhanghu = await Task.Run(() => { return _dal.GetZhang(zhang.ID); });
                }
                return zhanghu;
            }
            /// <summary>
            /// 还款
            /// </summary>
            /// <param name="token"></param>
            /// <param name="hkId"></param>
            /// <returns></returns>
            [HttpPost]
            [Route("hk")]
            public int HK([FromForm]HK_Model model)
            {
                string json = jwt.GetPayload(model.token);
                UserInfo user = JsonConvert.DeserializeObject<UserInfo>(json);
                if(user != null)
                {
                    return _dal.HK(user.ID,model.ID);
                }
                else
                {
                    return -1;
                }
            }
            [HttpPost]
            [Route("chongzhi")]
            public int ChongZhi([FromForm]chongzhi_Model model)
            {
                string json = jwt.GetPayload(model.token);
                UserInfo user = JsonConvert.DeserializeObject<UserInfo>(json);
                if (user != null)
                {
                    return _dal.ChongZhi(user.ID,model.money);
                }
                else
                {
                    return -1;
                }
            }
        }
    }

    DAL 代码

    using Model;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using Dapper;
    using System.Linq;
    
    namespace DAL
    {
        public class Dal : IDAL
        {
            /// <summary>
            /// 充值
            /// </summary>
            /// <param name="UserId"></param>
            /// <param name="money"></param>
            /// <returns></returns>
            public int ChongZhi(int UserId, decimal money)
            {
                using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-O7U2DEC;Initial Catalog=Test_MonthTest6;User ID=sa;PassWord=990621"))
                {
                    return connection.Execute($"update ZhangHuInfo set ZhangYE=ZhangYE+{money} where User_ID={UserId}");
                }
            }
    
            /// <summary>
            /// 显示账户余额信息
            /// </summary>
            /// <param name="UserId"></param>
            /// <returns></returns>
            public List<ZhangHuInfo> GetZhang(int UserId)
            {
                using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-O7U2DEC;Initial Catalog=Test_MonthTest6;User ID=sa;PassWord=990621"))
                {
                    string sql = $"select * from ZhangHuInfo where User_ID ={UserId}";
                    return connection.Query<ZhangHuInfo>(sql).ToList();
                }
            }
            /// <summary>
            /// 环框功能
            /// </summary>
            /// <param name="UserId"></param>
            /// <param name="hkId"></param>
            /// <returns></returns>
            public int HK(int UserId, int hkId)
            {
                using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-O7U2DEC;Initial Catalog=Test_MonthTest6;User ID=sa;PassWord=990621"))
                {
                    //求需要还多少钱
                    object b = connection.ExecuteScalar($"select HKBenJin+HKLiXi from UserHK where ID = {hkId}");
                    //求余额还有多少钱
                    object yue = connection.ExecuteScalar($"select ZhangYE from ZhangHuInfo where User_ID = {UserId}");
                    if((decimal)b <= (decimal)yue)
                    {
                        //减少余额
                        int code = connection.Execute($"update ZhangHuInfo set ZhangYE = ZhangYE-{b} where User_ID = 1");
                        if(code > 0)
                        {
                            return connection.Execute($"update UserHK set HKState = 0 where ID = {hkId}");
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
    
            /// <summary>
            /// 登录
            /// </summary>
            /// <param name="info"></param>
            /// <returns></returns>
            public UserInfo Login(UserInfo info)
            {
                using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-O7U2DEC;Initial Catalog=Test_MonthTest6;User ID=sa;PassWord=990621"))
                {
                    string sql = $"select * from UserInfo where User_Name = '{info.User_Name}' and User_PassWord = '{info.User_PassWord}'";
                    return connection.Query<UserInfo>(sql).FirstOrDefault();
                }
            }
            /// <summary>
            /// 显示
            /// </summary>
            /// <param name="UserId"></param>
            /// <returns></returns>
            public List<UserHK> Select(int UserId)
            {
                using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-O7U2DEC;Initial Catalog=Test_MonthTest6;User ID=sa;PassWord=990621"))
                {
                    string sql = $"select * from UserHK where User_ID ={UserId}";
                    return connection.Query<UserHK>(sql).ToList();
                }
            }
        }
    }

    Login.cshtml

    @{
        ViewData["Title"] = "Login";
    }
    <style>
        body {
            background-image: url(/Content/Imgs/2001513.jpg);
            background-size: cover;
            background-repeat: no-repeat
        }
    
        .box {
             300px;
            height: 300px;
            margin: 0 auto;
            margin-top: 130px
        }
    
        #userName {
            margin-top: 20px;
             200px;
            height: 35px;
            border-radius: 6px;
        }
    
        #userPwd {
            margin-top: 20px;
             200px;
            height: 35px;
            border-radius: 6px;
        }
    
        #btn_save {
            margin-top: 20px;
             200px;
            font-size: 18px
        }
    </style>
    <body>
        <div class="box">
            <input type="text" id="userName" placeholder="用户名/手机号" />
            <input type="password" id="userPwd" placeholder="密码" /><br />
            <input type="button" id="btn_save" value="登录" class="btn btn-primary" />
        </div>
    </body>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script>
        //加密登录
        $('#btn_save').click(function () {
            var obj = {
                "User_Name" : $('#userName').val(),
                "User_PassWord" : $('#userPwd').val()
            };
            $.ajax({
                url: 'http://localhost:59661/UserHK/login',
                data: obj,
                type:'post',
                contentType: 'application/x-www-form-urlencoded',
                accepts: "application/x-www-form-urlencoded",
                dataType:'text',
                success: function (data) {
                    if (data != null) {
                        alert('登录成功');
                        localStorage["userName"] = data;
                        window.location.href = "/Default/Show";
                    }
                    else {
                        alert('登录失败');
                    }
                }
            });
        });
    </script>

    Show.cshtml

    @{
        ViewData["Title"] = "Show";
    }
    <style>
        body {
            background-image: url(/Content/Imgs/2003368.jpg);
        }
    
        .box {
             950px;
            height: 450px;
            margin: 0 auto;
            background-color: white;
            margin-top: 100px;
            background: rgba(255, 255, 255, 0.40);
        }
    
        .right {
             100%;
            height: 100%;
            float: right;
        }
    
        .up {
             270px;
            height: 130px;
            background-color: aqua;
            margin-bottom: 25px;
            padding-top: 15px;
            background: rgba(0, 255, 255, 0.40);
        }
    
        .down table tr td {
            border: solid 1px;
            height: 20px;
             120px;
            font-size: 15px;
            text-align: center
        }
    
        .down {
             1000px;
        }
    </style>
    
    <body>
        <div class="box">
            <div class="right">
                <div class="up">
                    <span><b style="margin-left:150px">可用余额</b></span><br />
                    <span style="color:red;margin-left:155px">¥ <span style="color:red" id="balance">  </span></span><br />
                    <input style="height:25px;120px;margin-top:20px" type="text" hidden="hidden" id="money" />
                    <input type="button" id="cz" value="充值" class="btn btn-warning" style="60px;height:30px;line-height:18px;margin-top:15px" />
                    <input type="button" value="提现" style="60px;height:30px;line-height:18px;margin-top:15px" class="btn btn-secondary" />
                </div>
                <div class="down">
                    <table>
                        <tr>
                            <td>还款期数</td>
                            <td>还款日期</td>
                            <td>应还本金</td>
                            <td>应还利息</td>
                            <td>还款总额</td>
                            <td>还款状态</td>
                        </tr>
                        <tbody id="tb"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script>
        $(function () {
            show();
        })
        //获取加密的数据
        var token = localStorage["userName"];
        function show() {
            $('#tb').empty();
            $.ajax({
                url: 'http://localhost:59661/UserHK/zhanghu',
                type: 'get',
                dataType: 'json',
                data: { token: token },
                success: function (data) {
                    var tr = '';
                    $.each(data, function (index, item) {
                        tr += '<span style="color:red" id="balance">' + item.zhangYE + '</span>';
                    });
                    $('#balance').html(tr);
                }
            });
            $.ajax({
                url: 'http://localhost:59661/UserHK/list',
                type: 'get',
                dataType: 'json',
                data: { token: token },
                //contentType: 'application/x-www-form-urlencoded',
                //accepts: "application/x-www-form-urlencoded",
                success: function (data) {
                    console.log(data);
                    var tr = '';
                    $.each(data, function (index, item) {
                        tr += '<tr><td>' + item.hkqs + '</td>';
                        tr += '<td>' + item.date + '</td>';
                        tr += '<td>' + item.hkBenJin + '</td>';
                        tr += '<td>' + item.hkLiXi + '</td>';
                        tr += '<td>' + (item.hkLiXi + item.hkBenJin) + '</td>';
                        if (item.hkState == 0) {
                            tr += '<td>已还清</td>';
                        }
                        else if (item.hkState == 1) {
                            tr += "<td><a href='#' onclick='Repay(" + item.id + ")'>还款</a></td>";
                        }
                        else {
                            tr += '<td>还清</td>';
                        }
                    });
                    $('#tb').append(tr);
                }
            });
        }
        function Repay(id) {
            var obj = {
                "ID": id,
                "token": token
            };
            $.ajax({
                url: 'http://localhost:59661/UserHK/hk',
                data: obj,
                type: 'post',
                contentType: 'application/x-www-form-urlencoded',
                accepts: "application/x-www-form-urlencoded",
                dataType: 'text',
                success: function (data) {
                    if (data > 0) {
                        alert('还款成功');
                        show();
                    }
                    else if(data == -1){
                        alert('余额不足请充值');
                        $("#money").removeAttr("hidden");
    
                    }
                    else {
                        alert('还款失败');
                    }
                }
    
            });
        }
        $('#cz').click(function () {
            var obj = {
                "money": $('#money').val(),
                "token": token
            };
            
            $.ajax({
                url: 'http://localhost:59661/UserHK/chongzhi',
                data: obj,
                type: 'post',
                contentType: 'application/x-www-form-urlencoded',
                accepts: "application/x-www-form-urlencoded",
                dataType: 'text',
                success: function (data) {
                    if (data != null) {
                        alert('充值成功');
                        show();
                        $("#money").attr("hidden", "hidden")
                    }
                    else {
                        alert('充值失败');
                    }
                }
            });
        });
    </script>
  • 相关阅读:
    C语言得到当前系统时间
    【solr这四个主题】在Tomcat 部署Solr4.x
    MySQL 一般查询日志(General Query Log)
    iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上
    Java Persistence with MyBatis 3(中国版) 第五章 与Spring集成
    Kaggle入门——使用scikit-learn解决DigitRecognition问题
    Effective C++:规定34:区分接口继承和实现继承
    Critical thinking and Thoughtful writing
    我的时间,GTD做主
    jquery自己主动旋转的登录界面的背景代码登录页背景图
  • 原文地址:https://www.cnblogs.com/lina0621/p/13082622.html
Copyright © 2020-2023  润新知