用户购买功能实现

This commit is contained in:
xxy
2020-05-13 21:43:07 +08:00
parent 401d23871d
commit 0fa929f9de
12 changed files with 1719 additions and 10 deletions

View File

@ -116,7 +116,11 @@ public class BookController extends BaseController{
public ResultBean queryBookIndexAbout(Long bookId,Long lastBookIndexId) {
Map<String,Object> data = new HashMap<>(2);
data.put("bookIndexCount",bookService.queryIndexCount(bookId));
data.put("lastBookContent",bookService.queryBookContent(lastBookIndexId).getContent().substring(0,42));
String lastBookContent = bookService.queryBookContent(lastBookIndexId).getContent();
if(lastBookContent.length()>42){
lastBookContent=lastBookContent.substring(0,42);
}
data.put("lastBookContent",lastBookContent);
return ResultBean.ok(data);
}
@ -161,4 +165,6 @@ public class BookController extends BaseController{
}

View File

@ -7,6 +7,7 @@ import com.java2nb.novel.entity.*;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.NewsService;
import com.java2nb.novel.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -34,6 +35,8 @@ public class PageController extends BaseController{
private final AuthorService authorService;
private final UserService userService;
@RequestMapping("{url}.html")
public String module(@PathVariable("url") String url) {
@ -121,7 +124,7 @@ public class PageController extends BaseController{
* 内容页
* */
@RequestMapping("/book/{bookId}/{bookIndexId}.html")
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, Model model) {
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request,Model model) {
//查询书籍
Book book = bookService.queryBookDetail(bookId);
model.addAttribute("book",book);
@ -137,6 +140,23 @@ public class PageController extends BaseController{
//查询内容
BookContent bookContent = bookService.queryBookContent(bookIndex.getId());
model.addAttribute("bookContent",bookContent);
//判断该目录是否收费
if(bookIndex.getIsVip() == 1){
UserDetails user = getUserDetails(request);
if(user == null){
//未登录
return "redirect:/user/login.html?originUrl="+request.getRequestURI();
}
//收费,判断用户是否购买过该目录
boolean isBuy = userService.queryIsBuyBookIndex(user.getId(),bookIndexId);
if(!isBuy){
//没有购买过,需要购买
bookContent.setContent(null);
model.addAttribute("needBuy",true);
return "book/book_content";
}
}
model.addAttribute("needBuy",false);
return ThreadLocalUtil.getTemplateDir()+"book/book_content";
}

View File

@ -7,6 +7,7 @@ import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
import com.java2nb.novel.entity.User;
import com.java2nb.novel.entity.UserBuyRecord;
import com.java2nb.novel.form.UserForm;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.UserService;
@ -264,6 +265,21 @@ public class UserController extends BaseController {
}
/**
* 购买小说章节
* */
@PostMapping("buyBookIndex")
public ResultBean buyBookIndex(UserBuyRecord buyRecord, HttpServletRequest request) {
UserDetails userDetails = getUserDetails(request);
if (userDetails == null) {
return ResultBean.fail(ResponseStatus.NO_LOGIN);
}
userService.buyBookIndex(userDetails.getId(),buyRecord);
return ResultBean.ok();
}
}

View File

@ -2,6 +2,7 @@ package com.java2nb.novel.service;
import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.entity.UserBuyRecord;
import com.java2nb.novel.form.UserForm;
import com.java2nb.novel.vo.BookReadHistoryVO;
import com.java2nb.novel.vo.BookShelfVO;
@ -122,4 +123,19 @@ public interface UserService {
* @param userId 用户ID
* @param amount 增加的余额 */
void addAmount(Long userId, int amount);
/**
* 判断用户是否购买过该小说章节
* @param userId 用户ID
* @param bookIndexId 章节目录ID
* @return true:购买过false:没购买
* */
boolean queryIsBuyBookIndex(Long userId, Long bookIndexId);
/**
* 购买小说章节
* @param userId 用户ID
* @param buyRecord 购买信息
* */
void buyBookIndex(Long userId, UserBuyRecord buyRecord);
}

View File

@ -3,14 +3,12 @@ package com.java2nb.novel.service.impl;
import com.github.pagehelper.PageHelper;
import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.entity.*;
import com.java2nb.novel.entity.User;
import com.java2nb.novel.form.UserForm;
import com.java2nb.novel.service.UserService;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException;
import com.java2nb.novel.entity.User;
import com.java2nb.novel.entity.UserBookshelf;
import com.java2nb.novel.entity.UserFeedback;
import com.java2nb.novel.entity.UserReadHistory;
import com.java2nb.novel.mapper.*;
import com.java2nb.novel.vo.BookReadHistoryVO;
import com.java2nb.novel.vo.BookShelfVO;
@ -31,6 +29,8 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.book;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.id;
import static com.java2nb.novel.mapper.UserBookshelfDynamicSqlSupport.userBookshelf;
import static com.java2nb.novel.mapper.UserDynamicSqlSupport.*;
import static com.java2nb.novel.mapper.UserFeedbackDynamicSqlSupport.userFeedback;
@ -54,6 +54,8 @@ public class UserServiceImpl implements UserService {
private final UserFeedbackMapper userFeedbackMapper;
private final UserBuyRecordMapper userBuyRecordMapper;
@Override
@ -266,5 +268,40 @@ public class UserServiceImpl implements UserService {
}
@Override
public boolean queryIsBuyBookIndex(Long userId, Long bookIndexId) {
return userBuyRecordMapper.count(c ->
c.where(UserBuyRecordDynamicSqlSupport.userId, isEqualTo(userId))
.and(UserBuyRecordDynamicSqlSupport.bookIndexId,isEqualTo(bookIndexId))) > 0;
}
@Transactional
@Override
public void buyBookIndex(Long userId, UserBuyRecord buyRecord) {
//查询用户余额
long balance = userInfo(userId).getAccountBalance();
if(balance<10){
//余额不足
throw new BusinessException(ResponseStatus.USER_NO_BALANCE);
}
buyRecord.setUserId(userId);
buyRecord.setCreateTime(new Date());
buyRecord.setBuyAmount(10);
//生成购买记录
userBuyRecordMapper.insertSelective(buyRecord);
//减少用户余额
userMapper.update(update(user)
.set(UserDynamicSqlSupport.accountBalance)
.equalTo(balance-10)
.where(id,isEqualTo(userId))
.build()
.render(RenderingStrategies.MYBATIS3));
}
}

View File

@ -36,6 +36,9 @@
onselect="document.selection.empty()">
<input type="hidden" id="bookId" th:value="${book.id}"/>
<input type="hidden" id="bookName" th:value="${book.bookName}"/>
<input type="hidden" id="preIndexName" th:value="${bookIndex.indexName}"/>
<input type="hidden" id="preContentId" th:value="${bookIndex.id}"/>
<div th:replace="common/top :: top('10')">
@ -86,7 +89,7 @@
作者:<a th:href="'javascript:searchByK(\''+${book.authorName}+'\')'" th:text="${book.authorName}"></a><span th:text="'字数'+${bookIndex.wordCount}"></span><span th:text="'更新时间'+${#dates.format(bookIndex.updateTime, 'yy/MM/dd HH:mm:ss')}"></span>
</div>
</div>
<div class="txtwrap" th:if="${bookIndex.isVip == 1}">
<div class="txtwrap" th:if="${needBuy}">
<div id="showReading" class="readBox" style="font-size: 16px; font-family: microsoft yahei">
<p>
@ -112,7 +115,7 @@
</div>
<ul class="order_list">
<li>价格:<span class="red">10屋币(1元=100屋币)</span></li>
<li id="panelPay" class="btns"><a class="btn_red" href="/pay?cid=2052117" >购买</a></li>
<li id="panelPay" class="btns"><a class="btn_red" href="javascript:buyBookIndex()" >购买</a></li>
</ul>
<input type="hidden" name="HidCId" id="HidCId" value="2052117">
@ -121,7 +124,7 @@
</form>
</div>
</div>
<div class="txtwrap" th:if="${bookIndex.isVip != 1}">
<div class="txtwrap" th:if="${!needBuy}">
<div id="showReading" class="readBox" style="font-size: 16px; font-family: microsoft yahei" th:utext="${bookContent.content}">
@ -328,6 +331,33 @@
}
function buyBookIndex(){
$.ajax({
type: "POST",
url: "/user/buyBookIndex",
data: {'bookId':$("#bookId").val(),"bookName":$("#bookName").val(),
"bookIndexId":$("#preContentId").val(),"bookIndexName":$("#preIndexName").val()},
dataType: "json",
success: function (data) {
if (data.code == 200) {
location.reload();
} else if(data.code == 1001){
//未登录
}else {
layer.alert(data.msg);
}
},
error: function () {
layer.alert('网络异常');
}
})
}
$.post("/book/addVisitCount", {"bookId": $("#bookId").val()}, function () {
});
</script>