用户下载文档

dev
truthhun 1 year ago
parent b364dbbda2
commit 328a7793e8

3
.gitignore vendored

@ -12,4 +12,5 @@ dist
output
cache
uploads
documents
documents
watch.bat

@ -584,7 +584,7 @@ func (s *DocumentAPIService) DownloadDocument(ctx context.Context, req *pb.Docum
}
// 查询文档存不存在
doc, err := s.dbModel.GetDocument(req.Id, "id", "price", "status", "title", "ext")
doc, err := s.dbModel.GetDocument(req.Id, "id", "price", "status", "title", "ext", "user_id")
if err != nil || doc.Status == model.DocumentStatusDisabled {
return res, status.Errorf(codes.NotFound, "文档不存在")
}
@ -606,17 +606,23 @@ func (s *DocumentAPIService) DownloadDocument(ctx context.Context, req *pb.Docum
}
user, _ := s.dbModel.GetUser(userId)
if user.CreditCount < doc.Price {
if doc.UserId != userId && user.CreditCount < doc.Price {
return res, status.Errorf(codes.PermissionDenied, "魔豆不足,无法下载")
}
// 直接返回下载地址
err = s.dbModel.CreateDownload(&model.Download{
// 用户可以免费下载自己的文档
free := doc.UserId == userId || s.dbModel.CanIFreeDownload(userId, doc.Id)
down := &model.Download{
UserId: userId,
DocumentId: doc.Id,
Ip: ip,
IsPay: s.dbModel.CanIFreeDownload(userId, doc.Id),
})
IsPay: !free,
}
s.logger.Debug("下载文档", zap.Any("down", down), zap.Bool("canFreeDownload", free))
// 直接返回下载地址
err = s.dbModel.CreateDownload(down)
if err != nil {
return res, status.Errorf(codes.Internal, "创建下载失败:%s", err.Error())
}

@ -170,14 +170,20 @@ func (m *DBModel) GetDownloadList(opt OptionGetDownloadList) (downloadList []Dow
// CanIFreeDownload 判断用户是否可以免费下载
// 最后一次付费下载时间 + 免费下载时长 > 当前时间
func (m *DBModel) CanIFreeDownload(userId, documentId int64) (yes bool) {
func (m *DBModel) CanIFreeDownload(userId, documentId int64) bool {
var download Download
m.db.Where("user_id = ? and document_id = ? and is_pay = ?", userId, documentId, 1).Last(&download)
m.logger.Debug("CanIFreeDownload", zap.Any("Last Download", download))
if download.Id == 0 {
return false
}
cfg := m.GetConfigOfDownload(ConfigDownloadFreeDownloadDuration)
m.logger.Debug("CanIFreeDownload", zap.Int32("FreeDownloadDuration", cfg.FreeDownloadDuration))
if cfg.FreeDownloadDuration <= 0 {
return true
}
m.logger.Debug("CanIFreeDownload", zap.Any("CreatedAt", download.CreatedAt), zap.Time("Now", time.Now()), zap.Time("After", download.CreatedAt.Add(time.Duration(cfg.FreeDownloadDuration)*time.Hour*24)))
return download.CreatedAt.Add(time.Duration(cfg.FreeDownloadDuration) * time.Hour * 24).After(time.Now())
}

@ -23,7 +23,7 @@ const (
var UserPublicFields = []string{
"id", "username", "signature", "status", "avatar", "realname",
"doc_count", "follow_count", "fans_count", "favorite_count", "comment_count",
"created_at", "updated_at", "login_at",
"created_at", "updated_at", "login_at", "credit_count",
}
type User struct {

@ -64,6 +64,14 @@ export const searchDocument = (params) => {
})
}
export const downloadDocument = (params) => {
return service({
url: '/api/v1/document/download',
method: 'get',
params,
})
}
export const listRecycleDocument = (params) => {
return service({
url: '/api/v1/document/recycle',

@ -22,7 +22,7 @@
</div>
</el-col>
<el-col :span="8"
><div>财富</div>
><div>魔豆</div>
<div class="el-link el-link--primary">
{{ user.credit_count || 0 }}
</div>

@ -78,6 +78,10 @@ export default {
target: 'http://127.0.0.1:8880', // 目标服务器
changeOrigin: true,
},
'/download': {
target: 'http://127.0.0.1:8880', // 目标服务器
changeOrigin: true,
},
},
// PWA module configuration: https://go.nuxtjs.dev/pwa
pwa: {

@ -74,7 +74,11 @@
</div>
<div class="doc-page-more text-center">
<div>下载文档到电脑方便使用</div>
<el-button type="primary" icon="el-icon-download">
<el-button
type="primary"
icon="el-icon-download"
@click="downloadDocument"
>
下载文档({{ formatBytes(document.size) }})</el-button
>
<div v-if="document.preview - pages.length > 0">
@ -106,6 +110,7 @@
type="primary"
icon="el-icon-download"
class="float-right"
@click="downloadDocument"
>下载文档({{ formatBytes(document.size) }})</el-button
>
<el-button
@ -214,7 +219,10 @@
<el-button type="primary" icon="el-icon-coin" class="btn-coin"
>{{ document.price || 0 }} 个魔豆</el-button
>
<el-button type="primary" icon="el-icon-download"
<el-button
type="primary"
icon="el-icon-download"
@click="downloadDocument"
>下载文档({{ formatBytes(document.size) }})</el-button
>
</el-button-group>
@ -233,7 +241,7 @@
<script>
import { mapGetters } from 'vuex'
import DocumentSimpleList from '~/components/DocumentSimpleList.vue'
import { getDocument } from '~/api/document'
import { getDocument, downloadDocument } from '~/api/document'
import { getFavorite, createFavorite, deleteFavorite } from '~/api/favorite'
import { formatDatetime, formatBytes } from '~/utils/utils'
import FormComment from '~/components/FormComment.vue'
@ -393,6 +401,17 @@ export default {
commentSuccess() {
this.$refs.commentList.getComments()
},
async downloadDocument() {
const res = await downloadDocument({
id: this.documentId,
})
if (res.status === 200) {
//
window.location.href = res.data.url
return
}
this.$message.error(res.data.message || '下载失败')
},
prevPage() {
if (this.currentPage > 1) {
const currentPage = this.currentPage - 1

@ -93,15 +93,15 @@
<el-row class="text-center user-count">
<el-col :span="8">
<div><small>文档</small></div>
<span>22</span>
<span>{{ user.doc_count || 0 }}</span>
</el-col>
<el-col :span="8">
<div><small>收藏</small></div>
<span>32</span>
<span>{{ user.favorite_count || 0 }}</span>
</el-col>
<el-col :span="8">
<div><small>财富</small></div>
<span>12</span>
<div><small>魔豆</small></div>
<span>{{ user.credit_count || 0 }}</span>
</el-col>
</el-row>
<!-- <el-button class="btn-block" type="success">

Loading…
Cancel
Save