Compare commits

..

2 Commits

Author SHA1 Message Date
tim
92ba475f3b fix: 更新分类选择 2025-09-07 13:38:09 +08:00
tim
6fffdb0fd6 feat: 侧边栏按钮样式逻辑修改 2025-09-07 13:20:53 +08:00
9 changed files with 50 additions and 85 deletions

View File

@@ -10,7 +10,6 @@ import java.util.List;
public interface PointHistoryRepository extends JpaRepository<PointHistory, Long> {
List<PointHistory> findByUserOrderByIdDesc(User user);
List<PointHistory> findByUserOrderByIdAsc(User user);
long countByUser(User user);
List<PointHistory> findByUserAndCreatedAtAfterOrderByCreatedAtDesc(User user, LocalDateTime createdAt);

View File

@@ -225,20 +225,17 @@ public class PointService {
*/
public int recalculateUserPoints(User user) {
// 获取用户所有的积分历史记录(由于@Where注解已删除的记录会被自动过滤
List<PointHistory> histories = pointHistoryRepository.findByUserOrderByIdAsc(user);
List<PointHistory> histories = pointHistoryRepository.findByUserOrderByIdDesc(user);
int totalPoints = 0;
for (PointHistory history : histories) {
totalPoints += history.getAmount();
// 重新计算每条历史记录的余额
history.setBalance(totalPoints);
}
// 批量更新历史记录及用户积分
pointHistoryRepository.saveAll(histories);
// 更新用户积分
user.setPoint(totalPoints);
userRepository.save(user);
return totalPoints;
}

View File

@@ -1,7 +1,6 @@
package com.openisle.service;
import com.openisle.exception.FieldException;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
/**
@@ -18,11 +17,6 @@ public class UsernameValidator {
if (username == null || username.isEmpty()) {
throw new FieldException("username", "Username cannot be empty");
}
if (NumberUtils.isDigits(username)) {
throw new FieldException("username", "Username cannot be pure number");
}
}
}

View File

@@ -1,67 +0,0 @@
package com.openisle.service;
import com.openisle.model.PointHistory;
import com.openisle.model.PointHistoryType;
import com.openisle.model.Role;
import com.openisle.model.User;
import com.openisle.repository.PointHistoryRepository;
import com.openisle.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DataJpaTest
@Import(PointService.class)
class PointServiceRecalculateUserPointsTest {
@Autowired
private PointService pointService;
@Autowired
private UserRepository userRepository;
@Autowired
private PointHistoryRepository pointHistoryRepository;
@Test
void recalculatesBalanceAfterDeletion() {
User user = new User();
user.setUsername("u");
user.setEmail("u@example.com");
user.setPassword("p");
user.setRole(Role.USER);
userRepository.save(user);
PointHistory h1 = new PointHistory();
h1.setUser(user);
h1.setType(PointHistoryType.POST);
h1.setAmount(30);
h1.setBalance(30);
h1.setCreatedAt(LocalDateTime.now().minusMinutes(2));
pointHistoryRepository.save(h1);
PointHistory h2 = new PointHistory();
h2.setUser(user);
h2.setType(PointHistoryType.COMMENT);
h2.setAmount(10);
h2.setBalance(40);
h2.setCreatedAt(LocalDateTime.now().minusMinutes(1));
pointHistoryRepository.save(h2);
user.setPoint(40);
userRepository.save(user);
pointHistoryRepository.delete(h1);
int total = pointService.recalculateUserPoints(user);
assertEquals(10, total);
assertEquals(10, userRepository.findById(user.getId()).orElseThrow().getPoint());
assertEquals(10, pointHistoryRepository.findById(h2.getId()).orElseThrow().getBalance());
}
}

View File

@@ -2,11 +2,12 @@
<div class="article-category-container" v-if="category">
<div class="article-info-item" @click="gotoCategory">
<BaseImage
v-if="category.smallIcon"
v-if="isImageIcon(category.smallIcon)"
class="article-info-item-img"
:src="category.smallIcon"
:alt="category.name"
/>
<component v-else :is="category.smallIcon || category.icon" class="article-info-item-img" />
<div class="article-info-item-text">{{ category.name }}</div>
</div>
</div>
@@ -22,6 +23,11 @@ const gotoCategory = async () => {
const value = encodeURIComponent(props.category.id ?? props.category.name)
await navigateTo({ path: '/', query: { category: value } }, { replace: true })
}
const isImageIcon = (icon) => {
if (!icon) return false
return /^https?:\/\//.test(icon) || icon.startsWith('/')
}
</script>
<style scoped>

View File

@@ -7,11 +7,17 @@
@click="gotoTag(tag)"
>
<BaseImage
v-if="tag.smallIcon"
v-if="isImageIcon(tag.smallIcon)"
class="article-info-item-img"
:src="tag.smallIcon"
:alt="tag.name"
/>
<component
v-else-if="tag.smallIcon || tag.icon"
:is="tag.smallIcon || tag.icon"
class="article-info-item-img"
/>
<tag-one v-else class="article-info-item-img" />
<div class="article-info-item-text">{{ tag.name }}</div>
</div>
</div>
@@ -26,6 +32,11 @@ const gotoTag = async (tag) => {
const value = encodeURIComponent(tag.id ?? tag.name)
await navigateTo({ path: '/', query: { tags: value } }, { replace: true })
}
const isImageIcon = (icon) => {
if (!icon) return false
return /^https?:\/\//.test(icon) || icon.startsWith('/')
}
</script>
<style scoped>

View File

@@ -15,7 +15,7 @@
class="option-icon"
:alt="option.name"
/>
<!-- <i v-else :class="['option-icon', option.icon]"></i> -->
<component v-else :is="option.smallIcon || option.icon" class="option-icon" />
</template>
<span>{{ option.name }}</span>
<span class="option-count" v-if="option.count > 0"> x {{ option.count }}</span>

View File

@@ -122,6 +122,11 @@
class="section-item-icon"
:alt="t.name"
/>
<component
v-else-if="t.smallIcon || t.icon"
:is="t.smallIcon || t.icon"
class="section-item-icon"
/>
<tag-one v-else class="section-item-icon" />
<span class="section-item-text"
>{{ t.name }} <span class="section-item-text-count">x {{ t.count }}</span></span

View File

@@ -63,6 +63,16 @@ import {
History,
Lightning,
PeoplesTwo,
Code,
GoodTwo,
Twitter,
Bitcoin,
Fire,
Communication,
WaterLevel,
RobotOne,
Server,
Protection,
} from '@icon-park/vue-next'
export default defineNuxtPlugin((nuxtApp) => {
@@ -129,4 +139,14 @@ export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.component('HistoryIcon', History)
nuxtApp.vueApp.component('Lightning', Lightning)
nuxtApp.vueApp.component('PeoplesTwo', PeoplesTwo)
nuxtApp.vueApp.component('CodeIcon', Code)
nuxtApp.vueApp.component('GoodTwo', GoodTwo)
nuxtApp.vueApp.component('Twitter', Twitter)
nuxtApp.vueApp.component('Bitcoin', Bitcoin)
nuxtApp.vueApp.component('Fire', Fire)
nuxtApp.vueApp.component('Communication', Communication)
nuxtApp.vueApp.component('WaterLevel', WaterLevel)
nuxtApp.vueApp.component('RobotOne', RobotOne)
nuxtApp.vueApp.component('ServerIcon', Server)
nuxtApp.vueApp.component('Protection', Protection)
})