增加文件流返回前的判断,日记文本状态bug,登录提示保存密码(尽力了Vuetify官方都无能为力)

This commit is contained in:
sysnix 2023-09-25 10:54:40 +08:00
parent 293318e25f
commit 5d3d26b907
7 changed files with 86 additions and 42 deletions

View File

@ -1,6 +1,7 @@
package controller package controller
import ( import (
"blog/internal/model/AjaxResult"
"blog/internal/service" "blog/internal/service"
"net/http" "net/http"
"net/url" "net/url"
@ -19,11 +20,14 @@ type FileController struct {
func (ctrl *FileController) ViewFile() { func (ctrl *FileController) ViewFile() {
id := ctrl.Ctx.Params().Get("id") id := ctrl.Ctx.Params().Get("id")
file, err := service.FileService.GetFile(id) file, err := service.FileService.GetFile(id)
if err != nil { if err != nil || file.Id == "" {
ctrl.Ctx.JSON(AjaxResult.Error("文件不存在"))
return return
} }
ctrl.Ctx.Header("Content-Disposition", "attachment;filename="+url.QueryEscape(file.FileName)) ctrl.Ctx.Header("Content-Disposition", "attachment;filename="+url.QueryEscape(file.FileName))
if file.FileSize != 0 {
ctrl.Ctx.Header("Content-Length", strconv.FormatInt(file.FileSize, 10)) ctrl.Ctx.Header("Content-Length", strconv.FormatInt(file.FileSize, 10))
}
ctrl.Ctx.Write(file.Data) ctrl.Ctx.Write(file.Data)
} }

View File

@ -43,7 +43,7 @@ func (ctrl *DiaryController) PostSubmit() {
database.WGorm().Transaction(func(tx *gorm.DB) error { database.WGorm().Transaction(func(tx *gorm.DB) error {
now := time.Now().UnixMilli() now := time.Now().UnixMilli()
diary := admin.AdminDiary{Id: diaryId, PublishTime: now, Del: 0, CreateTime: now, CreateBy: userId} diary := admin.AdminDiary{Id: diaryId, PublishTime: now, Del: 0, CreateTime: now, CreateBy: userId}
ctn := vo.CommonContent{Id: uuid.NewString(), RelId: diaryId, Content: content} ctn := vo.CommonContent{Id: uuid.NewString(), RelId: diaryId, Content: content, State: consts.CONTENT_STATE_PUBLISH}
err = tx.Table("common_contents").Create(ctn).Error err = tx.Table("common_contents").Create(ctn).Error
if err != nil { if err != nil {
return err return err
@ -57,7 +57,8 @@ func (ctrl *DiaryController) PostSubmit() {
for i, file := range files { for i, file := range files {
defer file.Close() defer file.Close()
header := headers[i] header := headers[i]
var bytes []byte = make([]byte, header.Size) fileSize := header.Size
var bytes []byte = make([]byte, fileSize)
_, err = file.Read(bytes) _, err = file.Read(bytes)
if err != nil { if err != nil {
return err return err
@ -65,7 +66,7 @@ func (ctrl *DiaryController) PostSubmit() {
fileName := header.Filename fileName := header.Filename
fileId := uuid.NewString() fileId := uuid.NewString()
fileIds = append(fileIds, fileId) fileIds = append(fileIds, fileId)
sysFile := admin.SysFile{Id: fileId, FileName: fileName, Data: bytes, Sort: i, Del: 0, CreateBy: userId, CreateTime: now} sysFile := admin.SysFile{Id: fileId, FileName: fileName, FileSize: fileSize, Data: bytes, Sort: i, Del: 0, CreateBy: userId, CreateTime: now}
err = tx.Table("common_files").Create(&sysFile).Error err = tx.Table("common_files").Create(&sysFile).Error
if err != nil { if err != nil {
return err return err

View File

@ -60,16 +60,20 @@ func (ctrl *LoginController) GetRefreshcaptcha() {
func (ctrl *LoginController) Post() { func (ctrl *LoginController) Post() {
var loginUser admin.SysUserLogin var loginUser admin.SysUserLogin
if err := ctrl.Ctx.ReadBody(&loginUser); err != nil { if err := ctrl.Ctx.ReadBody(&loginUser); err != nil {
result := AjaxResult.Error("参数错误") // result := AjaxResult.Error("参数错误")
ctrl.Ctx.JSON(result) // ctrl.Ctx.JSON(result)
ctrl.Ctx.Redirect("/admin")
return return
} }
session := sessions.Get(ctrl.Ctx) session := sessions.Get(ctrl.Ctx)
captcha := loginUser.Captcha captcha := loginUser.Captcha
sessionCaptcha := session.GetString("captcha") sessionCaptcha := session.GetString("captcha")
if !strings.EqualFold(captcha, sessionCaptcha) { if !strings.EqualFold(captcha, sessionCaptcha) {
result := AjaxResult.Error("验证码错误") // result := AjaxResult.Error("验证码错误")
ctrl.Ctx.JSON(result) // ctrl.Ctx.JSON(result)
ctrl.Ctx.ViewData("user", loginUser)
ctrl.Ctx.ViewData("errorMsg", "验证码错误")
ctrl.Ctx.View("/admin/login")
return return
} }
@ -77,12 +81,22 @@ func (ctrl *LoginController) Post() {
result := database.GormTemplate.Where("username = ?", loginUser.Username).First(&user) result := database.GormTemplate.Where("username = ?", loginUser.Username).First(&user)
rowsAffected := result.RowsAffected rowsAffected := result.RowsAffected
if rowsAffected > 1 { if rowsAffected > 1 {
ctrl.Ctx.JSON(AjaxResult.Error("数据异常,后台错误!")) // ctrl.Ctx.JSON(AjaxResult.Error("数据异常,后台错误!"))
ctrl.Ctx.Redirect("/admin")
return
}
if rowsAffected == 0 {
ctrl.Ctx.ViewData("user", loginUser)
ctrl.Ctx.ViewData("errorMsg", "账号不存在")
ctrl.Ctx.View("/admin/login")
return return
} }
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(loginUser.Password)) err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(loginUser.Password))
if err != nil { if err != nil {
ctrl.Ctx.JSON(AjaxResult.Error("密码错误!")) // ctrl.Ctx.JSON(AjaxResult.Error("密码错误!"))
ctrl.Ctx.ViewData("user", loginUser)
ctrl.Ctx.ViewData("errorMsg", "密码错误")
ctrl.Ctx.View("/admin/login")
return return
} }
@ -95,5 +109,6 @@ func (ctrl *LoginController) Post() {
xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For") xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For")
log.Println("用户:", user.Username, "登录,IP为:", xFrowardedFor) log.Println("用户:", user.Username, "登录,IP为:", xFrowardedFor)
ctrl.Ctx.JSON(AjaxResult.Ok("login")) // ctrl.Ctx.JSON(AjaxResult.Ok("login"))
ctrl.Ctx.Redirect("/admin")
} }

View File

@ -99,7 +99,7 @@ func (*articleService) CreateArticle(articel *admin.AdminArticle) (string, error
err := database.WGorm().Transaction(func(tx *gorm.DB) error { err := database.WGorm().Transaction(func(tx *gorm.DB) error {
contentId := uuid.NewString() contentId := uuid.NewString()
content := articel.Content content := articel.Content
commonContent := vo.CommonContent{Id: contentId, RelId: articleId, Content: content, State: "down"} commonContent := vo.CommonContent{Id: contentId, RelId: articleId, Content: content, State: consts.CONTENT_STATE_DOWN}
err := tx.Table("common_contents").Create(commonContent).Error err := tx.Table("common_contents").Create(commonContent).Error
if err != nil { if err != nil {
return err return err

View File

@ -102,8 +102,8 @@
axios.post('/admin/diary/submit', formData, {headers: {"Content-Type": "multipart/form-data"}}).then(res => { axios.post('/admin/diary/submit', formData, {headers: {"Content-Type": "multipart/form-data"}}).then(res => {
let data = res.data let data = res.data
console.log(data) console.log(data)
location.href = '/diary'
this.sending = false this.sending = false
location.href = '/diary'
}).catch(err => { }).catch(err => {
ths.error = err ths.error = err
console.error(err) console.error(err)

View File

@ -46,13 +46,16 @@
<v-alert color="error" type="error" color="red lighten-2" v-if="errorMsg"> <v-alert color="error" type="error" color="red lighten-2" v-if="errorMsg">
{{errorMsg}} {{errorMsg}}
</v-alert> </v-alert>
<v-form ref="form" v-model="valid" lazy-validation> <v-form ref="form" v-model="valid" lazy-validation action="/admin/login" method="post">
<v-text-field <v-text-field
v-model="username" v-model="username"
:rules="requiredRules" :rules="requiredRules"
label="账号" label="账号"
required required
name="username"
@keyup.enter="login" @keyup.enter="login"
autocomplete="iris-username"
id="iris-username"
></v-text-field> ></v-text-field>
<v-text-field <v-text-field
v-model="password" v-model="password"
@ -60,15 +63,20 @@
label="密码" label="密码"
required required
type="password" type="password"
name="password"
@keyup.enter="login" @keyup.enter="login"
></v-text-field> autocomplete="iris-password"
id="iris-password">
</v-text-field>
<v-text-field <v-text-field
v-model="captcha" v-model="captcha"
:rules="requiredRules" :rules="requiredRules"
label="验证码" label="验证码"
required required
name="captcha"
@keyup.enter="login" @keyup.enter="login"
></v-text-field> id="iris-captcha">
</v-text-field>
<img :src="captchaBase64" alt='' @click="refreshCaptcha"/> <img :src="captchaBase64" alt='' @click="refreshCaptcha"/>
<div><v-btn class="mr-4" @click="login">登录</v-btn></div> <div><v-btn class="mr-4" @click="login">登录</v-btn></div>
@ -85,7 +93,7 @@
template: '#app-template', template: '#app-template',
data: { data: {
captchaBase64: '#{.captchaBase64 }', captchaBase64: '#{.captchaBase64 }',
errorMsg: null, errorMsg: "#{.errorMsg}",
valid: true, valid: true,
requiredRules: [ requiredRules: [
v => !!v || '不能为空', v => !!v || '不能为空',
@ -97,6 +105,7 @@
}, },
methods: { methods: {
refreshCaptcha() { refreshCaptcha() {
console.log("获取验证码");
axios.get('/admin/login/refreshcaptcha') axios.get('/admin/login/refreshcaptcha')
.then((response) => { .then((response) => {
console.log(response); console.log(response);
@ -110,32 +119,47 @@
login() { login() {
let validate = this.$refs.form.validate() let validate = this.$refs.form.validate()
if (validate) { if (validate) {
axios.post('/admin/login', { // console.log(this.$refs.form);
username: this.username, localStorage.setItem("username", this.username)
password: this.password, localStorage.setItem("password", this.password)
captcha: this.captcha, this.$refs.form.$el.submit()
}) // axios.post('/admin/login', {
.then((response) => { // username: this.username,
// console.log(response); // password: this.password,
let data = response.data // captcha: this.captcha,
return data // })
}). // .then((response) => {
then((data) => { // // console.log(response);
console.log(data) // let data = response.data
if (data.code !== 200) { // return data
this.errorMsg = data.msg // }).
this.refreshCaptcha() // then((data) => {
return // console.log(data)
} // if (data.code !== 200) {
this.errorMsg = null // this.errorMsg = data.msg
location.href = "/admin" // this.refreshCaptcha()
// return
// }
// this.errorMsg = null
// location.href = "/admin"
}) // })
.catch(function (error) { // .catch(function (error) {
console.log(error); // console.log(error);
}); // });
} }
}, },
},
mounted() {
// console.log(this.captchaBase64);
if (!this.captchaBase64) {
this.refreshCaptcha()
}
if (!this.username) {
this.username = localStorage.getItem("username")
this.password = localStorage.getItem("password")
}
}, },
vuetify: new Vuetify(), vuetify: new Vuetify(),

View File

@ -86,7 +86,7 @@
<v-row> <v-row>
<template v-if="item.images.length === 1"> <template v-if="item.images.length === 1">
<v-col v-for="id in item.images" :key="id" <v-col v-for="id in item.images" :key="id"
class="d-flex child-flex" cols="6"> class="d-flex child-flex" cols="12">
<v-img :src="`/file/${id}`" aspect-ratio="1" <v-img :src="`/file/${id}`" aspect-ratio="1"
class="grey lighten-2"> class="grey lighten-2">
<template v-slot:placeholder> <template v-slot:placeholder>