.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;
        }
        /// 
        /// 登录
        /// 
        /// 
        /// 
        [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;
        }
        /// 
        /// 显示还款列表
        /// 
        /// 
        /// 
        [HttpGet]
        [Route("list")]
        public async Task> Select(string token)
        {
            List list = new List();
            string json = jwt.GetPayload(token);
            //序列化
            UserHK user = JsonConvert.DeserializeObject(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;
        }
        /// 
        /// 显示账户信息
        /// 
        /// 
        /// 
        [HttpGet]
        [Route("zhanghu")]
        public async Task> ZhangHus(string token)
        {
            List zhanghu = new List();
            string json = jwt.GetPayload(token);
            ZhangHuInfo zhang = JsonConvert.DeserializeObject(json);
            if (zhang != null)
            {
                zhanghu = await Task.Run(() => { return _dal.GetZhang(zhang.ID); });
            }
            return zhanghu;
        }
        /// 
        /// 还款
        /// 
        /// 
        /// 
        /// 
        [HttpPost]
        [Route("hk")]
        public int HK([FromForm]HK_Model model)
        {
            string json = jwt.GetPayload(model.token);
            UserInfo user = JsonConvert.DeserializeObject(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(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
    {
        /// 
        /// 充值
        /// 
        /// 
        /// 
        /// 
        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}");
            }
        }

        /// 
        /// 显示账户余额信息
        /// 
        /// 
        /// 
        public List 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(sql).ToList();
            }
        }
        /// 
        /// 环框功能
        /// 
        /// 
        /// 
        /// 
        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;
                }
            }
        }

        /// 
        /// 登录
        /// 
        /// 
        /// 
        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(sql).FirstOrDefault();
            }
        }
        /// 
        /// 显示
        /// 
        /// 
        /// 
        public List 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(sql).ToList();
            }
        }
    }
}

Login.cshtml

@{
    ViewData["Title"] = "Login";
}


    
class="box"> "text" id="userName" placeholder="用户名/手机号" /> "password" id="userPwd" placeholder="密码" />
"button" id="btn_save" value="登录" class="btn btn-primary" />

Show.cshtml

@{
    ViewData["Title"] = "Show";
}



    
class="box">
class="right">
class="up"> "margin-left:150px">可用余额
"color:red;margin-left:155px">¥ "color:red" id="balance">
"height:25px;width:120px;margin-top:20px" type="text" hidden="hidden" id="money" /> "button" id="cz" value="充值" class="btn btn-warning" style="width:60px;height:30px;line-height:18px;margin-top:15px" /> "button" value="提现" style="width:60px;height:30px;line-height:18px;margin-top:15px" class="btn btn-secondary" />
class="down"> "tb">
还款期数 还款日期 应还本金 应还利息 还款总额 还款状态

 

你可能感兴趣的:(.net core用户管理登录传值方式)