user_index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>水果商城 - 个人中心</title>
<link rel="stylesheet" href="myaccount_style.css">
<script src="../common/jquery-3.3.1.min.js"></script>
<script src="myaccount_scripts.js"></script>
</head>
<main>
<aside>
<nav>
<ul>
<li><a href="#" data-page="my_coupons">我的优惠券</a></li>
<li><a href="#" data-page="my_addresses">我的地址</a></li>
</ul>
</nav>
</aside>
<div id="content-area">
</div>
<button id="addNewAddressBtn" class="add-address-btn" >添加新地址</button>
<div id="content-area-address">
<form id="newAddressForm" style="display: none;">
<input type="hidden" id="userId" name="userId" value="">
<input type="hidden" id="receiveAddressId" name="receiveAddressId" value="">
<div class="form-group">
<label for="receiverName">收件人姓名:</label>
<input type="text" id="receiverName" name="receiverName" placeholder="请输入收件人姓名" required>
</div>
<div class="form-group">
<label for="receiverPhone">收件人电话号码:</label>
<input type="tel" id="receiverPhone" name="receiverPhone" placeholder="请输入收件人电话号码" required>
</div>
<div class="form-group">
<label for="receiverProvince">省份:</label>
<select id="receiverProvince" name="receiverProvince" required>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverCity">城市:</label>
<select id="receiverCity" name="receiverCity" required>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverArea">县区:</label>
<select id="receiverArea" name="receiverArea" required disabled>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverStreet">街道:</label>
<select id="receiverStreet" name="receiverStreet" required disabled>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverDetail">详细地址:</label>
<textarea id="receiverDetail" name="receiverDetail" placeholder="请输入详细地址" required></textarea>
</div>
<div class="form-group">
<label>
<input type="radio" id="isDefaultYes" name="isDefault" value="1">
是默认收件地址
</label>
<label>
<input type="radio" id="isDefaultNo" name="isDefault" value="0" checked>
不是默认收件地址
</label>
</div>
<div class="form-actions">
<input type="submit" value="保存">
<button type="button" class="cancel-new-address-btn">取消</button>
</div>
</form>
</div>
<div id="content-area-review">
</div>
</main>
<div id="my_coupons-list-template" style="display: none;">
<div class="content-block coupon-list">
<h2>我的优惠券列表</h2>
<div class="coupon-item"><p>优惠券1</p></div>
<div class="coupon-item"><p>优惠券2</p></div>
</div>
</div>
<div id="my_addresses-list-template" style="display: none;">
<div class="content-block address-list">
<h2>我的地址列表</h2>
<div class="address-item">
<p>地址1</p>
<button class="edit-btn">修改</button>
<button class="delete-btn">删除</button>
</div>
</div>
</div>
</body>
</html>
myaccount_style.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #f2f2f2;
padding: 15px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
main {
display: flex;
margin-top: 20px;
}
aside {
width: 20%;
background-color: #fff;
border-right: 1px solid #ddd;
box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
padding: 20px;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li a {
display: block;
padding: 10px;
text-decoration: none;
color: #333;
transition: background-color 0.3s ease;
border-radius: 5px;
}
nav ul li a.active,
nav ul li a:hover {
color: #fff;
background-color: #f00;
}
.address-item {
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
transition: all 0.3s ease;
}
.address-item button {
padding: 5px 10px;
font-size: 12px;
margin-right: 5px;
}
.address-item:hover {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transform: translateY(-2px);
}
.edit-form {
display: none;
margin-top: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f5f5f5;
position: absolute;
width: 100%;
z-index: 10;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.edit-form input[type="text"] {
width: 100%;
padding: 5px;
margin-bottom: 10px;
}
.edit-form button {
padding: 5px 10px;
margin-right: 5px;
background-color: #4CAF50;
border: none;
color: white;
cursor: pointer;
}
.edit-form .cancel-new-address-btn {
padding: 5px 10px;
margin-right: 5px;
background-color: #f44336;
border: none;
color: white;
cursor: pointer;
}
#content-area {
padding: 30px;
}
#content-area div {
display: none;
}
#content-area div.active {
display: block;
}
nav ul li a.active {
border-bottom-color: #f00;
}
#addNewAddressBtn {
margin-top: 20px;
}
.address-list h2 {
font-size: 18px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
text-align: left;
}
.address-list {
background-color: #fff;
border: 1px solid #ddd;
padding: 10px;
}
.add-address-btn {
padding: 5px 10px;
font-size: 14px;
position: absolute;
top: 20px;
right: 20px;
z-index: 10;
display: inline-block;
color: white;
background-color: #4CAF50;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.add-address-btn:hover {
background-color: #45a049;
}
.add-address-btn.active {
opacity: 0.6;
cursor: not-allowed;
}
#newAddressForm {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 20px;
width: 100%;
max-width: 600px;
}
#newAddressForm label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}
#newAddressForm input[type="text"],
#newAddressForm input[type="tel"],
#newAddressForm textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
#newAddressForm textarea {
height: 100px;
resize: vertical;
}
#newAddressForm select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
#newAddressForm .form-group {
margin-bottom: 20px;
}
#newAddressForm .form-actions {
text-align: right;
margin-top: 20px;
}
#newAddressForm .form-actions button {
padding: 10px 20px;
margin-left: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
}
#newAddressForm .form-actions .save-btn {
background-color: #4CAF50;
color: #fff;
}
#newAddressForm .form-actions .cancel-btn {
background-color: #f44336;
color: #fff;
}
@media (max-width: 768px) {
aside, #content-area {
width: 100%;
border-right: none;
}
main {
flex-direction: column;
}
aside {
margin-bottom: 20px;
}
.add-address-btn {
top: 10px;
right: 10px;
padding: 3px 8px;
font-size: 12px;
}
.address-list h2 {
font-size: 16px;
}
.address-item {
margin-bottom: 10px;
}
}
myaccount_scripts.js
$(document).ready(function() {
var currentPage = '未赋值';
var provinces = [
{
name: '省份1',
cities: [
{ name: '城市1', areas: [{ name: '县区1', streets: ['街道1-1', '街道1-2'] }, { name: '县区2', streets: ['街道2-1', '街道2-2'] }] },
]
},
{
name: '省份2',
cities: [
{ name: '城市3', areas: [{ name: '县区3', streets: ['街道3-1', '街道3-2'] }] },
]
},
];
function initProvinceSelect() {
var $provinceSelect = $('#receiverProvince');
$provinceSelect.empty();
$provinceSelect.append($('<option>').val('').text('请选择'));
$.each(provinces, function(index, province) {
$provinceSelect.append($('<option>').val(province.name).text(province.name));
});
}
function initCitySelect(provinceName) {
var $citySelect = $('#receiverCity');
$citySelect.empty();
$citySelect.append($('<option>').val('').text('请选择'));
var $areaSelect = $('#receiverArea');
$areaSelect.empty().append($('<option>').val('').text('请选择'));
$areaSelect.prop('disabled', true);
var $streetSelect = $('#receiverStreet');
$streetSelect.empty().append($('<option>').val('').text('请选择'));
$streetSelect.prop('disabled', true);
$.each(provinces, function(index, province) {
if (province.name === provinceName) {
$.each(province.cities, function(cityIndex, city) {
$citySelect.append($('<option>').val(city.name).text(city.name));
});
return false;
}
});
}
function initAreaSelect(cityName) {
var $areaSelect = $('#receiverArea');
$areaSelect.empty();
$areaSelect.append($('<option>').val('').text('请选择'));
var $streetSelect = $('#receiverStreet');
$streetSelect.empty().append($('<option>').val('').text('请选择'));
$streetSelect.prop('disabled', true);
$.each(provinces, function(index, province) {
$.each(province.cities, function(cityIndex, city) {
if (city.name === cityName) {
$.each(city.areas, function(areaIndex, area) {
$areaSelect.append($('<option>').val(area.name).text(area.name));
});
$areaSelect.prop('disabled', false);
return false;
}
});
});
}
function initStreetSelect(areaName) {
var $streetSelect = $('#receiverStreet');
$streetSelect.empty();
$streetSelect.append($('<option>').val('').text('请选择'));
$.each(provinces, function(index, province) {
$.each(province.cities, function(cityIndex, city) {
$.each(city.areas, function(areaIndex, area) {
if (area.name === areaName) {
$.each(area.streets, function(streetIndex, street) {
$streetSelect.append($('<option>').val(street).text(street));
});
$streetSelect.prop('disabled', false);
return false;
}
});
if (!$streetSelect.prop('disabled')) return false;
});
});
}
$('#receiverProvince').on('change', function() {
var selectedProvince = $(this).val();
if (selectedProvince) {
initCitySelect(selectedProvince);
}
});
$('#receiverCity').on('change', function() {
var selectedCity = $(this).val();
if (selectedCity) {
initAreaSelect(selectedCity);
}
});
$('#receiverArea').on('change', function() {
var selectedArea = $(this).val();
if (selectedArea) {
initStreetSelect(selectedArea);
}
});
$('nav ul li a').on('click', function(e) {
e.preventDefault();
var page = $(this).data('page');
console.log("变量"+currentPage);
console.log("传入的"+page);
if ((page !== currentPage) || page == '未赋值') {
loadContent(page);
currentPage = page;
}
$('#addNewAddressBtn').toggle(page === 'my_addresses');
});
function loadContent(page) {
console.log("加载");
$('nav ul li a').removeClass('active');
$('nav ul li a[data-page="' + page + '"]').addClass('active');
if (page === 'my_addresses') {
$('#content-area-address').show();
let token = localStorage.getItem("token");
$.ajax({
url: 'http://localhost:8080/fshop/receiverAddress',
headers:{'token': token},
type: 'GET',
dataType: 'json',
success: function(addresses) {
console.log(addresses.data);
var addressListHtml = '<div class="address-list active">';
$.each(addresses.data, function(index, address) {
addressListHtml += '<div class="address-item active" data-address-id="' + address.receiveAddressId + '" data-user-id="' + address.userId + '">' +
'<p><strong>姓名:</strong> ' + address.receiverName + '</p>' +
'<p><strong>电话:</strong> ' + address.receiverPhone + '</p>' +
'<p><strong>省份:</strong> ' + address.receiverProvince + '</p>' +
'<p><strong>城市:</strong> ' + address.receiverCity + '</p>' +
'<p><strong>县区:</strong> ' + address.receiverArea + '</p>' +
'<p><strong>街道:</strong> ' + address.receiverStreet + '</p>' +
'<p><strong>详细地址:</strong> ' + address.receiverDetail + '</p>' +
'<button class="edit-btn">修改</button>' +
'<button class="delete-btn">删除</button>' +
'</div>';
});
addressListHtml += '</div>';
$('#content-area').html(addressListHtml);
$('#addNewAddressBtn').show();
},
error: function(jqXHR, textStatus, errorThrown) {
alert('加载地址列表失败!错误: ' + textStatus + ', 详细信息: ' + errorThrown);
}
});
} else if (page === 'my_coupons') {
$('#content-area').text('我的优惠券页面');
$('#content-area-address').hide();
}
$('#content-area .content-block').removeClass('active');
$('#content-area .content-block').addClass('active');
$('#content-area .address-item').addClass('active');
}
function bindAddNewAddressEvents() {
$('#addNewAddressBtn').on('click', function() {
$('#newAddressForm').show().find('input[type="text"]').val('');
$('#newAddressForm').removeData('edit-mode');
});
$('#newAddressForm').on('submit', function(e) {
e.preventDefault();
var formData = {
userId: $('#userId').val(),
receiverName: $('#receiverName').val(),
receiverPhone: $('#receiverPhone').val(),
receiverProvince: $('#receiverProvince').val(),
receiverCity: $('#receiverCity').val(),
receiverArea: $('#receiverArea').val(),
receiverStreet: $('#receiverStreet').val(),
receiverDetail: $('#receiverDetail').val(),
isDefault: $('input[name="isDefault"]:checked').val()
};
if (!receiverName || !receiverPhone || !receiverProvince || !receiverCity || !receiverArea || !receiverStreet) {
alert('请填写所有必填字段!');
return;
}
let token = localStorage.getItem("token");
$.ajax({
url: 'http://localhost:8080/fshop/receiverAddress/saveAddr',
contentType: 'application/json; charset=utf-8',
headers:{'token': token},
type: 'POST',
data: JSON.stringify(formData),
dataType: 'json',
success: function(response) {
console.log(response);
if (response.code == 1) {
console.log("成功"+response.data);
var newAddressHtml = createAddressItemHtml(response.data);
$('#content-area .address-list').append(newAddressHtml);
$('#newAddressForm').hide();
alert('地址添加成功!');
} else {
console.log("失败"+response.data);
alert('地址添加失败:' + response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert('添加地址时发生错误:' + textStatus + ', 详细信息: ' + errorThrown);
}
});
});
$('#newAddressForm').on('click', '.cancel-new-address-btn', function() {
$('#newAddressForm').hide();
});
}
function createAddressItemHtml(addressData) {
return '<div class="address-item active" data-address-id="' + addressData.receiveAddressId + '" data-user-id="' + addressData.userId + '">' +
'<p><strong>姓名:</strong> ' + addressData.receiverName + '</p>' +
'<p><strong>电话:</strong> ' + addressData.receiverPhone + '</p>' +
'<p><strong>省份:</strong> ' + addressData.receiverProvince + '</p>' +
'<p><strong>城市:</strong> ' + addressData.receiverCity + '</p>' +
'<p><strong>县区:</strong> ' + addressData.receiverArea
+ '</p>' +
'<p><strong>街道:</strong> ' + addressData.receiverStreet + '</p>' +
'<p><strong>详细地址:</strong> ' + addressData.receiverDetail + '</p>' +
'<button class="edit-btn">修改</button>' +
'<button class="delete-btn">删除</button>' +
'</div>';
}
function bindEditAddressEvents() {
$('#content-area').on('click', '.edit-btn', function() {
var addressItem = $(this).closest('.address-item');
var addressId = addressItem.data('address-id');
var editUrl = 'address_edit.html?addressId=' + addressId;
window.location.href = editUrl;
});
}
function bindDeleteAddressEvents() {
$('#content-area').on('click', '.delete-btn', function() {
var addressItem = $(this).closest('.address-item');
var addressId = addressItem.data('address-id');
if (confirm('确定要删除这个地址吗?')) {
$.ajax({
url: 'http://localhost:8080/fshop/receiverAddress/' + addressId,
headers: { 'token': localStorage.getItem("token") },
type: 'DELETE',
dataType: 'json',
success: function(response) {
if (response.code == 1) {
addressItem.remove();
alert('地址删除成功!');
} else {
alert('地址删除失败:' + response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert('删除地址时发生错误:' + textStatus + ', 详细信息: ' + errorThrown);
}
});
}
});
}
loadContent('my_addresses');
initProvinceSelect();
bindAddNewAddressEvents();
bindEditAddressEvents();
bindDeleteAddressEvents();
});
address_edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../common/jquery-3.3.1.min.js"></script>
<script>
function getQueryParam(name) {
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(window.location.href);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var addressId = getQueryParam('addressId');
console.log(addressId);
</script>
<style>
#newAddressForm {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input[type="text"],
.form-group input[type="tel"],
.form-group textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.form-group select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-group input[type="radio"] {
margin-right: 5px;
}
.form-actions {
text-align: right;
margin-top: 20px;
}
.form-actions input[type="button"],
.form-actions button {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.form-actions input[type="button"]:hover,
.form-actions button:hover {
background-color: #ddd;
}
#submitBtn {
background-color: #4CAF50;
color: white;
}
.cancel-new-address-btn {
background-color: #f44336;
color: white;
}
#loading-indicator {
text-align: center;
padding: 10px;
background-color: rgba(255, 255, 255, 0.8);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 9999;
}
</style>
</head>
<body>
<div id="loading-indicator" style="display: none;">加载中...</div>
<form id="newAddressForm" style="display: none;">
<input type="hidden" id="userId" name="userId" value="">
<input type="hidden" id="receiveAddressId" name="receiveAddressId" value="">
<div class="form-group">
<label for="receiverName">收件人姓名:</label>
<input type="text" id="receiverName" name="receiverName" placeholder="请输入收件人姓名" required>
</div>
<div class="form-group">
<label for="receiverPhone">收件人电话号码:</label>
<input type="tel" id="receiverPhone" name="receiverPhone" placeholder="请输入收件人电话号码" required>
</div>
<div class="form-group">
<label for="receiverProvince">省份:</label>
<select id="receiverProvince" name="receiverProvince" required>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverCity">城市:</label>
<select id="receiverCity" name="receiverCity" required>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverArea">县区:</label>
<select id="receiverArea" name="receiverArea" required disabled>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverStreet">街道:</label>
<select id="receiverStreet" name="receiverStreet" required disabled>
<option value="">请选择</option>
</select>
</div>
<div class="form-group">
<label for="receiverDetail">详细地址:</label>
<textarea id="receiverDetail" name="receiverDetail" placeholder="请输入详细地址" required></textarea>
</div>
<div class="form-group">
<label>
<input type="radio" id="isDefaultYes" name="isDefault" value="1">
是默认收件地址
</label>
<label>
<input type="radio" id="isDefaultNo" name="isDefault" value="0" checked>
不是默认收件地址
</label>
</div>
<div class="form-actions">
<input type="button" id="submitBtn" value="保存">
<button type="button" id="cancelBtn" class="cancel-new-address-btn">取消</button>
</div>
</form>
</body>
<script>
$(document).ready(function() {
$('#loading-indicator').show();
var provinces = [
{
name: '省份1',
cities: [
{ name: '城市1', areas: [{ name: '县区1', streets: ['街道1-1', '街道1-2'] }, { name: '县区2', streets: ['街道2-1', '街道2-2'] }] },
]
},
{
name: '省份2',
cities: [
{ name: '城市3', areas: [{ name: '县区3', streets: ['街道3-1', '街道3-2'] }] },
]
},
{
name: '江苏省',
cities: [
{ name: '宿迁市', areas: [{ name: '灌南县', streets: ['五老村街道', '五老村街道3'] },{ name: '2县', streets: ['2街道', '3街道'] }] },
]
}
];
function initProvinceSelect() {
var $provinceSelect = $('#receiverProvince');
$provinceSelect.empty();
$provinceSelect.append($('<option>').val('').text('请选择'));
$.each(provinces, function(index, province) {
$provinceSelect.append($('<option>').val(province.name).text(province.name));
});
}
function initCitySelect(provinceName) {
var $citySelect = $('#receiverCity');
$citySelect.empty();
$citySelect.append($('<option>').val('').text('请选择'));
var $areaSelect = $('#receiverArea');
$areaSelect.empty().append($('<option>').val('').text('请选择'));
$areaSelect.prop('disabled', true);
var $streetSelect = $('#receiverStreet');
$streetSelect.empty().append($('<option>').val('').text('请选择'));
$streetSelect.prop('disabled', true);
$.each(provinces, function(index, province) {
if (province.name === provinceName) {
$.each(province.cities, function(cityIndex, city) {
$citySelect.append($('<option>').val(city.name).text(city.name));
});
return false;
}
});
}
function initAreaSelect(cityName) {
var $areaSelect = $('#receiverArea');
$areaSelect.empty();
$areaSelect.append($('<option>').val('').text('请选择'));
var $streetSelect = $('#receiverStreet');
$streetSelect.empty().append($('<option>').val('').text('请选择'));
$streetSelect.prop('disabled', true);
$.each(provinces, function(index, province) {
$.each(province.cities, function(cityIndex, city) {
if (city.name === cityName) {
$.each(city.areas, function(areaIndex, area) {
$areaSelect.append($('<option>').val(area.name).text(area.name));
});
$areaSelect.prop('disabled', false);
return false;
}
});
});
}
function initStreetSelect(areaName) {
var $streetSelect = $('#receiverStreet');
$streetSelect.empty();
$streetSelect.append($('<option>').val('').text('请选择'));
$.each(provinces, function(index, province) {
$.each(province.cities, function(cityIndex, city) {
$.each(city.areas, function(areaIndex, area) {
if (area.name === areaName) {
$.each(area.streets, function(streetIndex, street) {
$streetSelect.append($('<option>').val(street).text(street));
});
$streetSelect.prop('disabled', false);
return false;
}
});
if (!$streetSelect.prop('disabled')) return false;
});
});
}
$('#receiverProvince').on('change', function() {
var selectedProvince = $(this).val();
if (selectedProvince) {
initCitySelect(selectedProvince);
}
});
$('#receiverCity').on('change', function() {
var selectedCity = $(this).val();
if (selectedCity) {
initAreaSelect(selectedCity);
}
});
$('#receiverArea').on('change', function() {
var selectedArea = $(this).val();
if (selectedArea) {
initStreetSelect(selectedArea);
}
});
initProvinceSelect();
});
var addressId = getQueryParam('addressId');
if (addressId) {
$.ajax({
url: 'http://localhost:8080/fshop/receiverAddress/' + addressId,
headers: { 'token': localStorage.getItem("token") },
type: 'GET',
dataType: 'json',
success: function(address) {
let addressData = address.data;
$('#loading-indicator').hide();
$('#receiveAddressId').val(addressData.receiveAddressId);
$('#userId').val(addressData.userId);
$('#receiverName').val(addressData.receiverName);
$('#receiverPhone').val(addressData.receiverPhone);
$('#receiverProvince').val(addressData.receiverProvince).trigger('change');
$('#receiverDetail').val(addressData.receiverDetail);
var defaultValueFromBackend = addressData.isDefault;
if (defaultValueFromBackend === 1) {
$('#isDefaultYes').prop('checked', true);
} else if (defaultValueFromBackend === 0) {
$('#isDefaultNo').prop('checked', true);
}
$('#newAddressForm').show();
},
error: function(jqXHR, textStatus, errorThrown) {
$('#loading-indicator').hide();
alert('获取地址详情失败:' + textStatus);
}
});
}
$('#submitBtn').on('click', function(event) {
event.preventDefault();
var formData = {
userId: $('#userId').val(),
receiveAddressId: $('#receiveAddressId').val(),
receiverName: $('#receiverName').val(),
receiverPhone: $('#receiverPhone').val(),
receiverProvince: $('#receiverProvince').val(),
receiverCity: $('#receiverCity').val(),
receiverArea: $('#receiverArea').val(),
receiverStreet: $('#receiverStreet').val(),
receiverDetail: $('#receiverDetail').val(),
isDefault: $('input[name="isDefault"]:checked').val()
};
$.ajax({
url: 'http://localhost:8080/fshop/receiverAddress/updateAddr',
contentType: 'application/json; charset=utf-8',
headers:{'token': localStorage.getItem("token")},
type: 'POST',
data: JSON.stringify(formData),
headers: { 'token': localStorage.getItem("token") },
dataType: 'json',
success: function(response) {
console.log(response);
if (response.code == 1 ) {
window.location.href = 'user_index.html';
} else {
alert('地址保存失败:' + response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert('保存地址时发生错误:' + textStatus + ', 详细信息: ' + errorThrown);
}
});
});
$('#cancelBtn').on('click', function(event) {
event.preventDefault();
window.location.href = 'user_index.html';
});
</script>
</html>
ReceiverAddress
package com.fshop.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("receiver_address")
public class ReceiverAddress implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "receive_address_id", type = IdType.AUTO)
private Integer receiveAddressId;
private Integer userId;
private String receiverName;
private String receiverPhone;
private String receiverProvince;
private String receiverCity;
private String receiverArea;
private String receiverStreet;
private String receiverDetail;
private Byte isDefault;
private Integer status;
private Integer version;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
private String other1;
private String other2;
}
ReceiverAddressController
package com.fshop.controller;
import com.fshop.common.R;
import com.fshop.common.ResponseCode;
import com.fshop.entity.ReceiverAddress;
import com.fshop.service.IReceiverAddressService;
import com.fshop.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/receiverAddress")
@RequiredArgsConstructor
@Slf4j
public class ReceiverAddressController {
private final IReceiverAddressService receiverAddressService;
@GetMapping()
public R<List<ReceiverAddress>> getById(HttpServletRequest request){
String token = request.getHeader("token");
log.info("根据id查询地址的token:{}",token);
if (JwtUtil.checkToken(token)){
log.info("查询地址的token正确:{}",token);
R<List<ReceiverAddress>> listR = receiverAddressService.getById(token);
log.info("查询地址返回的数据:{}",listR);
return listR;
}
return R.error(ResponseCode.TOKEN_ERROR);
}
@GetMapping("{addrId}")
public R<ReceiverAddress> getByAddrId(@PathVariable("addrId") Integer addrId){
log.info("地址id:{}",addrId);
R<ReceiverAddress> result = receiverAddressService.getByAddrId(addrId);
return result;
}
@PostMapping("/saveAddr")
public R<ReceiverAddress> saveAddr(@RequestBody ReceiverAddress receiverAddress, HttpServletRequest request){
log.info("地址信息:{}",receiverAddress);
String token = request.getHeader("token");
log.info("地址信息token:{}",token);
if (JwtUtil.checkToken(token)){
R<ReceiverAddress> receiverAddressR = receiverAddressService.saveAddr(receiverAddress, token);
return receiverAddressR;
}
return R.error(ResponseCode.TOKEN_ERROR);
}
@PostMapping("/updateAddr")
public R<ReceiverAddress> updateAddr(@RequestBody ReceiverAddress receiverAddress, HttpServletRequest request){
String token = request.getHeader("token");
if (JwtUtil.checkToken(token)){
R<ReceiverAddress> receiverAddressR = receiverAddressService.updateAddr(receiverAddress, token);
return receiverAddressR;
}
return R.error(ResponseCode.TOKEN_ERROR);
}
@DeleteMapping("{addrId}")
public R<ReceiverAddress> deleteByAddrId(@PathVariable("addrId") Integer addrId, HttpServletRequest request){
String token = request.getHeader("token");
if (JwtUtil.checkToken(token)){
R<ReceiverAddress> receiverAddressR = receiverAddressService.deleteByAddrId(addrId, token);
return receiverAddressR;
}
return R.error(ResponseCode.TOKEN_ERROR);
}
}
ReceiverAddressServiceImpl
package com.fshop.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fshop.common.R;
import com.fshop.dto.LoginUserDto;
import com.fshop.entity.ReceiverAddress;
import com.fshop.mapper.ReceiverAddressMapper;
import com.fshop.service.IReceiverAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fshop.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
@Slf4j
public class ReceiverAddressServiceImpl extends ServiceImpl<ReceiverAddressMapper, ReceiverAddress> implements IReceiverAddressService {
private final ReceiverAddressMapper receiverAddressMapper;
@Override
public R<List<ReceiverAddress>> getById(String token) {
log.info("根据用户id查询地址:{}",token);
LoginUserDto loginUserDto = JwtUtil.parseToken(token);
QueryWrapper<ReceiverAddress> wrapper = new QueryWrapper<>();
wrapper.eq("user_id",loginUserDto.getUserId()).eq("status",1);
List<ReceiverAddress> receiverAddresses = receiverAddressMapper.selectList(wrapper);
log.info("根据用户id查询到的地址信息:{}",receiverAddresses);
return R.ok(receiverAddresses);
}
@Override
public R<ReceiverAddress> getByAddrId(Integer addrId) {
ReceiverAddress address = receiverAddressMapper.selectById(addrId);
if (address != null)
return R.ok(address);
return R.error("地址不存在");
}
@Override
public R<ReceiverAddress> saveAddr(ReceiverAddress receiverAddress, String token) {
LoginUserDto loginUserDto = JwtUtil.parseToken(token);
if (loginUserDto.getUserId() != null){
receiverAddress.setUserId(loginUserDto.getUserId());
receiverAddress.setStatus(1);
receiverAddress.setVersion(1);
receiverAddress.setCreateTime(LocalDateTime.now());
receiverAddress.setUpdateTime(LocalDateTime.now());
Integer insert = receiverAddressMapper.insert(receiverAddress);
if (insert > 0){
return R.ok(receiverAddress);
}
return R.error("添加失败");
}
return R.error("token错误,未登录");
}
@Override
public R<ReceiverAddress> updateAddr(ReceiverAddress receiverAddress, String token) {
LoginUserDto loginUserDto = JwtUtil.parseToken(token);
if (loginUserDto.getUserId() != null && receiverAddress.getUserId().equals(loginUserDto.getUserId())){
ReceiverAddress receiverAddress1 = receiverAddressMapper.selectById(receiverAddress.getReceiveAddressId());
receiverAddress.setVersion(receiverAddress1.getVersion()+1);
receiverAddress.setUpdateTime(LocalDateTime.now());
Integer update = receiverAddressMapper.updateById(receiverAddress);
if (update > 0){
return R.ok(receiverAddress);
}
return R.error("修改失败");
}
return R.error("token错误,未登录");
}
@Override
public R<ReceiverAddress> deleteByAddrId(@PathVariable("addrId") Integer addrId,String token){
LoginUserDto loginUserDto = JwtUtil.parseToken(token);
ReceiverAddress receiverAddress = receiverAddressMapper.selectById(addrId);
receiverAddress.setStatus(0);
if(receiverAddress != null && receiverAddress.getUserId().equals(loginUserDto.getUserId())){
receiverAddressMapper.updateById(receiverAddress);
return R.ok("删除成功");
}
return R.error("删除失败");
}
}
IReceiverAddressService
package com.fshop.service;
import com.fshop.common.R;
import com.fshop.entity.ReceiverAddress;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
public interface IReceiverAddressService extends IService<ReceiverAddress> {
R<List<ReceiverAddress>> getById(String token);
R<ReceiverAddress> getByAddrId(Integer addrId);
R<ReceiverAddress> saveAddr(ReceiverAddress receiverAddress, String token);
public R<ReceiverAddress> deleteByAddrId(@PathVariable("addrId") Integer addrId, String token);
public R<ReceiverAddress> updateAddr(ReceiverAddress receiverAddress, String token);
}
R
package com.fshop.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import java.io.Serializable;
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> implements Serializable {
private static final long serialVersionUID = 7735505903525411467L;
private static final int SUCCESS_CODE = 1;
private static final int ERROR_CODE = 0;
private final int code;
private String msg;
private T data;
private R(int code) {
this.code = code;
}
private R(int code, T data) {
this.code = code;
this.data = data;
}
private R(int code, String msg) {
this.code = code;
this.msg = msg;
}
private R(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> R<T> ok() {
return new R<T>(SUCCESS_CODE, "success");
}
public static <T> R<T> ok(String msg) {
return new R<T>(SUCCESS_CODE, msg);
}
public static <T> R<T> ok(T data) {
return new R<T>(SUCCESS_CODE, data);
}
public static <T> R<T> ok(String msg, T data) {
return new R<T>(SUCCESS_CODE, msg, data);
}
public static <T> R<T> error() {
return new R<T>(ERROR_CODE, "error");
}
public static <T> R<T> error(String msg) {
return new R<T>(ERROR_CODE, msg);
}
public static <T> R<T> error(int code, String msg) {
return new R<T>(code, msg);
}
public static <T> R<T> error(ResponseCode res) {
return new R<T>(res.getCode(), res.getMessage());
}
@Override
public String toString() {
return "R{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
FshopAppApplication
package com.fshop;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@MapperScan("com.fshop.mapper")
public class FshopAppApplication {
public static void main(String[] args) {
SpringApplication.run(FshopAppApplication.class, args);
}
}
ServletInitializer
package com.fshop;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(FshopAppApplication.class);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/>
</parent>
<groupId>com.fshop</groupId>
<artifactId>fshop-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fshop-app</name>
<description>fshop-app</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.13.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.6</version>
</plugin>
</plugins>
</build>
</project>
ReceiverAddressMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fshop.mapper.ReceiverAddressMapper">
</mapper>
ReceiverAddressMapper.java
package com.fshop.mapper;
import com.fshop.entity.ReceiverAddress;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface ReceiverAddressMapper extends BaseMapper<ReceiverAddress> {
}