日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

.Net Core 實(shí)現(xiàn)賬戶充值,還款,用戶登錄(WebApi的安全)

 怡紅公子0526 2021-03-28

個(gè)人未開通網(wǎng)站: http://justin1107.pc./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,給他失效時(shí)間
                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">還款I(lǐng)D</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;
            }
        }
    }
}
View Code

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;
                <input id="btn_TX" type="button" value="提現(xiàn)" />
            </th>
        </tr>
    </table>
</div>
<div style="margin-left:0px;margin-top:15px;">
    <table>
        <tr>
            <th>還款期數(shù)</th>
            <th>還款日期</th>
            <th>應(yīng)還本金</th>
            <th>還款利息</th>
            <th>還款總額</th>
            <th>還款狀態(tài)</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("網(wǎng)絡(luò)不可用,無法充值");
                    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>
View Code

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)
                    {
                        //修改還款狀態(tài)
                        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();
            }
        }
    }
View Code

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約