Skip to content

Commit

Permalink
完成转账操作、交易记录查询的相关操作api,简单修改了数据库的model
Browse files Browse the repository at this point in the history
  • Loading branch information
LeChatelier-Lenz committed Jun 11, 2024
1 parent eb59011 commit 976bca5
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 155 deletions.
18 changes: 10 additions & 8 deletions backend/bank_site/bank_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@


class employee(models.Model):
employee_id = models.AutoField(primary_key = True)
employee_name = models.CharField(max_length = 20, null = False, default = "Unknown")
identity_card = models.CharField(max_length = 18, null = False, default = "Unknown")
employee_sex = models.IntegerField(null = False, default = 0)
phone_number = models.CharField(max_length = 20, null = False, default = "Unknown")
occupation_name = models.CharField(max_length = 50, null = False, default = "Unknown")
is_employeed = models.BooleanField(null = False, default = "False")
employee_id = models.AutoField(primary_key=True)
employee_name = models.CharField(max_length=20, null = False, default = "Unknown")
identity_card = models.CharField(max_length=18, null = False, default = "Unknown")
employee_sex = models.IntegerField(null=False, default = 0)
phone_number = models.CharField(max_length=20, null = False, default = "Unknown")
occupation_name = models.CharField(max_length=50, null = False, default = "Unknown")
is_employeed = models.BooleanField(null=False, default = "False")
other_information = models.CharField(max_length = 1021, default = "Unknown")


Expand Down Expand Up @@ -54,7 +54,9 @@ class account(models.Model):
objects = models.Manager()
account_id = models.AutoField(primary_key=True)
password = models.CharField(max_length=20, null=False)
identity_card = models.ForeignKey(online_user, on_delete=models.PROTECT, related_name="accounts")
user_id = models.ForeignKey(online_user, on_delete=models.SET_NULL, related_name="accounts", null=True)
identity_card = models.CharField(max_length=18, null=False)
phone_num = models.CharField(max_length=20, null=False,default = "10086")
card_type = models.IntegerField(null=False)
balance = models.FloatField(null=False, default=0.0)
current_deposit = models.FloatField(null=False, default=0.0)
Expand Down
3 changes: 1 addition & 2 deletions backend/bank_site/bank_app/user_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
path('sign_in/', user_views.user_log_in),
path('change_password/', user_views.user_change_password),
path('list_cards/', user_views.list_cards),
path('bind_card', user_views.bind_card),
path('card_lost/', user_views.card_lost),
path('user_info/',user_views.get_user_info),
path('user_info/', user_views.get_user_info),
path('money_transfer/', user_views.user_account_transfer),
path('list_records/', user_views.user_account_all_records),
]
Expand Down
145 changes: 81 additions & 64 deletions backend/bank_site/bank_app/user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def get_user_info(request):
return JsonResponse({"error": "Method not allowed"}, status=405)



def list_cards(request):
# user_id -> List[Card]
if request.method == 'GET':
Expand All @@ -48,30 +47,23 @@ def list_cards(request):
return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
def bind_card(request):
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
print(f'获取到的post数据为:{data}')
filter_accounts = account.objects.filter(account_id=data.get('account_id'))
filter_users = online_user.objects.filter(user_id=data.get('user_id'))
if filter_accounts.exists() and filter_users.exists():
if (filter_accounts[0].password == data.get('password')
and filter_users[0].identity_card == data.get('identity_card'))\
and filter_users[0].phone_num == data.get('phone_num'):
if filter_accounts[0].is_lost:
return JsonResponse({"error": "The card has been lost", 'state': False}, status=400)
filter_accounts.update(identity_card=data.get('identity_card'))
return_data = {"success": "The binding is successful", 'state': True}
return JsonResponse(return_data, status=200)
else:
return JsonResponse({"error": "Password is Wrong", 'state': False}, status=400)
else:
return JsonResponse({"error": "Account not found or User not Found", 'state': False}, status=400)
elif request.method == 'OPTION':
return JsonResponse({"success": "OPTION operation"}, status=200)
else:
return JsonResponse({"error": "Method not allowed", 'state': True}, status=405)
# @csrf_exempt
# def bind_cards(request):
# if request.method == 'POST':
# data = json.loads(request.body.decode('utf-8'))
# print(f'获取到的post数据为:{data}')
# filter_accounts = account.objects.filter(identity_card=data.get('identity_card'), phone_num=data.get('phone_num'))
# filter_users = online_user.objects.filter(identity_card=data.get('identity_card'), phone_num=data.get('phone_num'))
# if filter_accounts.exists() and filter_users.exists():
# filter_accounts.update(user_id=filter_users[0].user_id)
# return_data = {"success": "The auto-binding is successful", 'state': True}
# return JsonResponse(return_data, status=200)
# else:
# return JsonResponse({"success": "No accounts now for this user", 'state': False}, status=200)
# elif request.method == 'OPTION':
# return JsonResponse({"success": "OPTION operation"}, status=200)
# else:
# return JsonResponse({"error": "Method not allowed", 'state': True}, status=405)


@csrf_exempt
Expand Down Expand Up @@ -121,8 +113,8 @@ def user_add(request):
print('看看data:{}'.format(data))
filter_online_user = online_user.objects.filter(identity_card=data.get('identity_card'))
# print('看看filter_online_user:{}'.format(filter_online_user))
if (not filter_online_user.exists()):
if(online_user.objects.count == 0):
if not filter_online_user.exists():
if online_user.objects.count == 0:
cur_id = 1
new_user = online_user(
user_id = cur_id,
Expand All @@ -144,9 +136,18 @@ def user_add(request):
is_lost=False
)
new_user.save()
# print(new_user)
return_data = {'state': True}
return JsonResponse(return_data, status=200)
# 注册成功后自动开始相关联的卡片(账户绑定)
filter_accounts = account.objects.filter(identity_card=data.get('identity_card'), phone_num=data.get('phone_num'))
print(f"看看这个{filter_accounts}")
if filter_accounts.exists():
filter_accounts.update(user_id=new_user.user_id)
return_data = {"success": "The auto-binding is successful", 'state': True}
return JsonResponse(return_data, status=200)
else:
return JsonResponse({"success": "No accounts now for this user", 'state': True}, status=200)
# # print(new_user)
# return_data = {'state': True}
# return JsonResponse(return_data, status=200)
else:
return JsonResponse({"error": "User with this identity_card has been exist",'state': False}, status=403)
elif request.method == 'OPTION':
Expand All @@ -164,8 +165,12 @@ def user_log_in(request):
filter_online_user = online_user.objects.filter(user_name=data.get('user_name'))
# print('看看filter_online_user:{}'.format(filter_online_user))
if filter_online_user.exists():
if filter_online_user[0].is_blacklisted:
return JsonResponse({"error": "this user is blacklisted", 'state': False}, status=400)
if filter_online_user[0].is_frozen:
return JsonResponse({"error": "this user is frozen", 'state': False}, status=400)
# 用户存在开始对照密码
cur_user = online_user.objects.get(user_name = data.get('user_name'))
cur_user = online_user.objects.get(user_name = data.get('user_name'))
print(f"文豪说看看这个密码: {cur_user.password}")
if data.get('password') == cur_user.password:
return_data = {'user_id': cur_user.user_id, 'state': True}
Expand Down Expand Up @@ -214,35 +219,46 @@ def user_account_all_records(request):
# print("Invalid account_id")
return JsonResponse({"error": "查询的账户不存在"}, status=403)
else:
result = []
queryset = []
filter_deposit_records = deposit_record.objects.filter(account_id=int(request.GET.get('account_id')))
if filter_deposit_records.exists():
queryset.extend(filter_deposit_records)
queryset = queryset.sort(key=lambda x: x.deposit_start_date)
result.append({"deposit": queryset})
else:
result.append({"deposit": []})
queryset = []

filter_withdrawal_records = withdrawal_record.objects.filter(account_id=int(request.GET.get('account_id')))
if filter_withdrawal_records.exists():
queryset = []
queryset.extend(filter_withdrawal_records)
queryset = queryset.sort(key=lambda x: x.withdrawal_date)
result.append({"withdrawal": queryset})
else:
result.append({"withdrawal": []})
queryset = []
filter_transfer_records = transfer_record.objects.filter(account_in_id=int(request.GET.get('account_id')))
if filter_transfer_records.exists():
queryset = []
queryset.extend(filter_transfer_records)
queryset = queryset.sort(key=lambda x: x.transfer_date)
result.append({"transfer": queryset})
records = []
record_type = int(request.GET.get('record_type'))
if record_type == 1:
filter_deposit_records = deposit_record.objects.filter(account_id=int(request.GET.get('account_id')))
if filter_deposit_records.exists():
records =[{
"deposit_record_id": record.deposit_record_id,
"account_id": record.account_id,
"deposit_type": record.deposit_type,
"auto_renew_status": record.auto_renew_status,
"deposit_start_date": record.deposit_start_date,
"deposit_end_date": record.deposit_end_date,
"deposit_amount": record.deposit_amount,
"cashier_id": record.cashier_id,
} for record in filter_deposit_records]
elif record_type == 2:
filter_withdrawal_records = withdrawal_record.objects.filter(account_id=int(request.GET.get('account_id')))
if filter_withdrawal_records.exists():
records = [{
"withdrawal_record_id": record.withdrawal_record_id,
"account_id": record.account_id,
"withdrawal_date": record.withdrawal_date,
"withdrawal_amount": record.withdrawal_amount,
"cashier_id": record.cashier_id,
} for record in filter_withdrawal_records]
elif record_type == 3:
filter_transfer_records = transfer_record.objects.filter(account_out_id=int(request.GET.get('account_id')))
if filter_transfer_records.exists():
records = [{
"transfer_record_id": record.transfer_record_id,
"account_in_id": record.account_in_id,
"account_out_id": record.account_out_id,
"transfer_date": record.transfer_date,
"transfer_amount": record.transfer_amount,
"cashier_id": record.cashier_id,
} for record in filter_transfer_records]
else:
result.append({"transfer": []})
return JsonResponse(result, safe=False, status=200)
return JsonResponse({"error":"Wrong record type"},status=400)
print(f"look records: {records}")
return JsonResponse(records, safe=False, status=200)
else:
return JsonResponse({"error": "Method not allowed"}, status=405)

Expand All @@ -259,15 +275,15 @@ def user_account_transfer(request):
if not filter_in_account.exists():
return JsonResponse({"error": "接收转账用户不存在"}, status=403)
if not filter_out_account.exists():
return JsonResponse({"error": "存款不足"}, status=403)
return JsonResponse({"error": "转出账户不存在或密码错误"}, status=403)
filter_in_account = filter_in_account.first()
filter_out_account = filter_out_account.first()
if filter_out_account.is_frozen or filter_out_account.is_lost:
return JsonResponse({"error": "转出账户挂失/冻结"}, status=403)
return JsonResponse({"error": "转出账户被挂失/冻结"}, status=403)
if filter_in_account.is_frozen or filter_in_account.is_lost:
return JsonResponse({"error": "转入账户挂失/冻结"}, status=403)
return JsonResponse({"error": "转入账户被挂失/冻结"}, status=403)
# 判断用户存款是否满足取出条件
if filter_out_account.uncredited_deposit >= data.get('transfer_amount'):
if filter_out_account.uncredited_deposit >= float(data.get('transfer_amount')):
# 更新用户存款情况
filter_out_account.uncredited_deposit -= data.get('transfer_amount')
filter_out_account.balance -= data.get('transfer_amount')
Expand All @@ -283,11 +299,12 @@ def user_account_transfer(request):
transfer_date=datetime.datetime.now(),
# -----
transfer_amount=data.get('transfer_amount'),
cashier_id=0 # 0 作为默认互联网转账的cashier_id
)
new_transfer_record.save()
return JsonResponse({"success": "successful operation"}, status=200)
else:
return JsonResponse({"error": "转账用户不存在"}, status=403)
return JsonResponse({"error": "转出账户余额不足"}, status=403)
elif request.method == 'OPTION':
return JsonResponse({"success": "OPTION operation"}, status=200)
else:
Expand Down
5 changes: 2 additions & 3 deletions backend/bank_site/bank_site/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": 'fse_db',
"NAME": 'online_bank',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'root',
'PASSWORD': 'ww71304002ww'
# 'PASSWORD': '031716cqp0222'
'PASSWORD': '031716cqp0222'
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/login/components/signup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
identity_card: this.IDCard,
phone_num: this.phone
}).then(response => {
window.location.href = "/login"; // 函数内部进行超链接跳转
}).catch(error => {
ElMessage.error(error.response.data.error);
Expand Down
26 changes: 12 additions & 14 deletions frontend/src/online_user/components/account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
<p style="font-weight: bold;font-size: larger;margin: 10px" >用户名 : {{user_info.user_name}}</p>
<p style="font-weight: bold;font-size: larger;margin: 10px" >电话号码 : {{user_info.phone_num}}</p>
</div>
<!-- 存款操作 -->

<!-- 存款记录 -->
<!-- <div class="cashierBox" v-for="record in records" :key="record.deposit_record_id"> -->
<!-- </div> -->


<div style="margin-top: 40px; margin-left: 40px; font-size: 1.5em; font-weight: bold;">
账户列表/卡包
</div>
<div style="display: flex;flex-wrap: wrap; justify-content: start;">
<!-- 借书证卡片 -->
<div class="cardBox" v-for="card in cards" :key="card.account_id">
Expand Down Expand Up @@ -49,16 +46,16 @@

</div>
</div>

<!--
<el-button class="newCardBox"
@click="newCardInfo.account_id = '', newCardInfo.identity_card = '', newCardInfo.password = '', newCardInfo.phone_num = '',newCardVisible = true">
<el-icon style="height: 50px; width: 50px;">
<Plus style="height: 100%; width: 100%;" />
</el-icon>
</el-button>
</el-button>-->

<!-- 新建借书证对话框 -->
<el-dialog v-model="newCardVisible" title="绑定新账户" width="30%" align-center>
<!-- 绑定新账户对话框 -->
<!-- <el-dialog v-model="newCardVisible" title="绑定新账户" width="30%" align-center>
<div style="margin-left: 2vw; font-weight: bold; font-size: 1rem; margin-top: 20px; ">
卡号:
<el-input v-model="newCardInfo.account_id" style="width: 12.5vw;" clearable />
Expand All @@ -83,7 +80,7 @@
:disabled="newCardInfo.identity_card.length === 0 || newCardInfo.phone_num.length === 0 || newCardInfo.account_id.length === 0 || newCardInfo.password.length === 0">确定</el-button>
</span>
</template>
</el-dialog>
</el-dialog>-->


<!-- 挂失账户对话框 -->
Expand Down Expand Up @@ -178,7 +175,7 @@ export default{
}
},
methods: {
ConfirmNewCard(){
/*ConfirmNewCard(){
// 发出POST请求
axios.post("http:https://127.0.0.1:8000/user/bind_card",
{ // 请求体
Expand All @@ -195,7 +192,7 @@ export default{
}).catch(error=>{
ElMessage.error(error.response.data.error);
})
},
},*/
ReportLostCard(){
axios.post("http:https://127.0.0.1:8000/user/card_lost/",
{ // 请求体
Expand All @@ -205,7 +202,8 @@ export default{
})
.then(response => {
ElMessage.success("操作成功") // 显示消息提醒
this.newCardVisible = false // 将对话框设置为不可见
this.reportLostVisible = false // 将对话框设置为不可见
this.toReportLost.password = "" // 清空密码
this.QueryCards() // 重新查询借书证以刷新页面
}).catch(error=>{
ElMessage.error(error.response.data.error);
Expand Down
Loading

0 comments on commit 976bca5

Please sign in to comment.