更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -21,12 +21,11 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// BackendFileController 平台端文件管理(yz_system_files / yz_system_files_category)
|
||||
type BackendFileController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
const fileUploadMaxMB = 2048 // 2GB,适用于大型软件安装包
|
||||
const fileUploadMaxMB = 2048
|
||||
const fileUploadMaxBytes = fileUploadMaxMB * 1024 * 1024
|
||||
|
||||
var fileTypeByCategory = map[string]uint8{
|
||||
@@ -42,7 +41,6 @@ var allowedExtByCategory = map[string][]string{
|
||||
"document": {"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt"},
|
||||
"video": {"mp4", "webm", "mov"},
|
||||
"audio": {"mp3", "wav", "ogg"},
|
||||
// 安装包 / 软件升级(上传时 cate 选 appsupgrade 分类即可,扩展名在此放行)
|
||||
"appsupgrade": {"zip", "exe", "dmg", "msi", "msix", "apk", "deb", "rpm", "7z", "tar", "gz", "pkg"},
|
||||
}
|
||||
|
||||
@@ -50,23 +48,14 @@ func (c *BackendFileController) backendClaims() (*jwtutil.Claims, error) {
|
||||
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndBackend)
|
||||
}
|
||||
|
||||
// effectiveTid 取文件归属租户 ID:只认令牌 claims。
|
||||
// ?tid= 与 X-Tenant-Id 此前可覆盖登录态,等于让任意 backend 账号往别人的目录树写文件。
|
||||
func (c *BackendFileController) effectiveTid(claims *jwtutil.Claims) uint64 {
|
||||
_ = c.ParseForm(1 << 20) // 上传接口需要先解析表单字段
|
||||
_ = c.ParseForm(1 << 20)
|
||||
if claims != nil && claims.TenantId > 0 {
|
||||
return uint64(claims.TenantId)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// resolveTuid 解析归属用户 ID,返回 0 表示租户共享文件。
|
||||
//
|
||||
// 取值顺序:
|
||||
// 1. 表单/请求头的 tuid(代他人上传场景,如 HR 代员工上传票据)
|
||||
// 2. scope=user 时取当前登录的租户用户 UID(backend 端 JWT 的 UserID 即 yz_system_tenant_user.uid)
|
||||
// —— 强制取自身 UID,避免前端伪造他人 uid 把文件塞进别人目录
|
||||
// 3. 其余情况返回 0(租户共享)
|
||||
func (c *BackendFileController) resolveTuid(claims *jwtutil.Claims) uint64 {
|
||||
if v, err := c.GetUint64("tuid"); err == nil && v > 0 {
|
||||
return v
|
||||
@@ -84,7 +73,6 @@ func (c *BackendFileController) resolveTuid(claims *jwtutil.Claims) uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// currentTuid 当前登录者对应的租户用户 UID(backend 端 JWT 的 UserID 即 yz_system_tenant_user.uid)
|
||||
func (c *BackendFileController) currentTuid(claims *jwtutil.Claims) uint64 {
|
||||
if claims != nil && claims.UserID > 0 {
|
||||
return uint64(claims.UserID)
|
||||
@@ -92,9 +80,6 @@ func (c *BackendFileController) currentTuid(claims *jwtutil.Claims) uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// scopeFilter 按归属范围过滤:
|
||||
// - scope=user → 只看当前登录用户的个人文件
|
||||
// - 其它(默认)→ 只看租户共享文件
|
||||
func scopeFilter(qs orm.QuerySeter, scope string, tuid uint64) orm.QuerySeter {
|
||||
if scope == services.ScopeUser {
|
||||
return qs.Filter("tuid", tuid)
|
||||
@@ -171,7 +156,6 @@ func removePhysicalBySrc(webSrc string) {
|
||||
_ = os.Remove(webSrc)
|
||||
}
|
||||
|
||||
// GetAllFiles GET /backend/allfiles
|
||||
func (c *BackendFileController) GetAllFiles() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -226,7 +210,6 @@ func (c *BackendFileController) GetAllFiles() {
|
||||
})
|
||||
}
|
||||
|
||||
// GetUserCate GET /backend/usercate
|
||||
func (c *BackendFileController) GetUserCate() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -268,7 +251,6 @@ type createCateBody struct {
|
||||
Tuid *uint64 `json:"tuid"`
|
||||
}
|
||||
|
||||
// CreateFileCate POST /backend/createfilecate
|
||||
func (c *BackendFileController) CreateFileCate() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -316,7 +298,6 @@ type renameCateBody struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// RenameFileCate POST /backend/renamefilecate/:id
|
||||
func (c *BackendFileController) RenameFileCate() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -363,7 +344,6 @@ func (c *BackendFileController) RenameFileCate() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteFileCate DELETE /backend/deletefilecate/:id
|
||||
func (c *BackendFileController) DeleteFileCate() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -409,7 +389,6 @@ func (c *BackendFileController) DeleteFileCate() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetCateFiles GET /backend/catefiles/:id
|
||||
func (c *BackendFileController) GetCateFiles() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -468,7 +447,6 @@ func (c *BackendFileController) GetCateFiles() {
|
||||
})
|
||||
}
|
||||
|
||||
// GetFileByID GET /backend/file/:id
|
||||
func (c *BackendFileController) GetFileByID() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -496,7 +474,6 @@ func (c *BackendFileController) GetFileByID() {
|
||||
c.jsonOK(fileToMap(&f))
|
||||
}
|
||||
|
||||
// UploadFile POST /backend/uploadfile
|
||||
func (c *BackendFileController) UploadFile() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -509,7 +486,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
return
|
||||
}
|
||||
tid := c.effectiveTid(claims)
|
||||
// 归属用户:传了 tuid 或 scope=user 即用户个人文件,否则为租户共享文件
|
||||
tuid := c.resolveTuid(claims)
|
||||
|
||||
fh, header, err := c.GetFile("file")
|
||||
@@ -530,14 +506,12 @@ func (c *BackendFileController) UploadFile() {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取存储服务
|
||||
storageService, err := services.GetStorageService()
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "获取存储服务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 1. 先写入临时文件并计算 MD5(此时文件尚未进入正式目录)
|
||||
staged, err := storageService.Stage(fh, header)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "读取上传文件失败: "+err.Error())
|
||||
@@ -545,7 +519,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
// 去重命中或出错时清理临时文件,避免产生垃圾
|
||||
if !committed && staged != nil {
|
||||
_ = storageService.Discard(staged)
|
||||
}
|
||||
@@ -554,8 +527,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
ctx := services.UploadContext{Source: services.SourceBackend, Tid: tid, Tuid: tuid}
|
||||
scope := ctx.Scope()
|
||||
|
||||
// 2. 精确查重:同端 + 同归属范围 + 同租户 + 同归属用户 + 同 MD5 才算重复。
|
||||
// 因此租户已有 a 文件、用户 c 上传同样文件不会冲突。
|
||||
exist, err := services.FindDuplicate(services.SourceBackend, scope, tid, tuid, staged.MD5)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "查重失败: "+err.Error())
|
||||
@@ -575,8 +546,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
return
|
||||
}
|
||||
|
||||
// 2.1 附件空间校验:重复文件不占空间,所以放在查重之后、落盘之前。
|
||||
// 平台端上传(source=platform)不占租户额度,这里只管租户端上传。
|
||||
if qerr := services.EnsureTenantStorageQuota(tid, uint64(staged.Size)); qerr != nil {
|
||||
if respondStorageQuotaError(&c.Controller, qerr) {
|
||||
return
|
||||
@@ -585,7 +554,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 提交到正式目录:backend/{tid}[/{tuid}]/yyyy/MM/dd/
|
||||
result, err := storageService.Commit(staged, ctx)
|
||||
if err != nil {
|
||||
c.jsonErr(500, 500, "上传文件失败: "+err.Error())
|
||||
@@ -593,14 +561,12 @@ func (c *BackendFileController) UploadFile() {
|
||||
}
|
||||
committed = true
|
||||
|
||||
// 获取分类
|
||||
cateStr := c.GetString("cate")
|
||||
var cate uint64
|
||||
if cateStr != "" {
|
||||
cate, _ = strconv.ParseUint(cateStr, 10, 64)
|
||||
}
|
||||
|
||||
// 4. 保存文件记录到数据库
|
||||
id, err := services.CreateFileRecord(services.FileUploadParams{
|
||||
Source: services.SourceBackend,
|
||||
Scope: scope,
|
||||
@@ -617,7 +583,6 @@ func (c *BackendFileController) UploadFile() {
|
||||
MD5: result.MD5,
|
||||
})
|
||||
if err != nil {
|
||||
// 数据库插入失败,删除已提交的文件
|
||||
_ = storageService.Delete(result.Key)
|
||||
c.jsonErr(500, 500, "上传失败: "+err.Error())
|
||||
return
|
||||
@@ -653,7 +618,6 @@ type updateFileBody struct {
|
||||
Cate *uint64 `json:"cate"`
|
||||
}
|
||||
|
||||
// UpdateFile POST /backend/updatefile/:id
|
||||
func (c *BackendFileController) UpdateFile() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -708,7 +672,6 @@ func (c *BackendFileController) UpdateFile() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteFile DELETE /backend/deletefile/:id
|
||||
func (c *BackendFileController) DeleteFile() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -741,7 +704,6 @@ func (c *BackendFileController) DeleteFile() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteFilePermanently DELETE /backend/deletefilepermanently/:id
|
||||
func (c *BackendFileController) DeleteFilePermanently() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -780,7 +742,6 @@ func (c *BackendFileController) DeleteFilePermanently() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// MoveFile GET /backend/movefile/:id
|
||||
func (c *BackendFileController) MoveFile() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -819,7 +780,6 @@ type idsBody struct {
|
||||
Cate *uint64 `json:"cate"`
|
||||
}
|
||||
|
||||
// BatchDeleteFiles POST /backend/batchdeletefiles
|
||||
func (c *BackendFileController) BatchDeleteFiles() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -842,7 +802,6 @@ func (c *BackendFileController) BatchDeleteFiles() {
|
||||
c.jsonErr(400, 400, "请选择要删除的文件")
|
||||
return
|
||||
}
|
||||
// 软删除:只标记 delete_time,不删除物理文件(彻底删除接口才会删)
|
||||
now := time.Now()
|
||||
n, err := models.Orm.QueryTable(new(models.SystemFile)).
|
||||
Filter("id__in", body.IDs).
|
||||
@@ -860,7 +819,6 @@ func (c *BackendFileController) BatchDeleteFiles() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// BatchDeleteFilesPermanently POST /backend/batchDeleteFilesPermanently
|
||||
func (c *BackendFileController) BatchDeleteFilesPermanently() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -913,19 +871,13 @@ func (c *BackendFileController) BatchDeleteFilesPermanently() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// avatarUploadMaxMB 头像大小上限:头像场景 2MB 足够
|
||||
const avatarUploadMaxMB = 2
|
||||
const avatarUploadMaxBytes = avatarUploadMaxMB * 1024 * 1024
|
||||
|
||||
// avatarAllowedExts 头像允许的图片格式
|
||||
var avatarAllowedExts = map[string]bool{
|
||||
"jpg": true, "jpeg": true, "png": true, "gif": true, "webp": true, "bmp": true,
|
||||
}
|
||||
|
||||
// UploadAvatar POST /backend/uploadavatar
|
||||
// 上传并设置「当前登录用户」头像:
|
||||
// 复用文件存储链路(Stage → 查重 → Commit → 落文件表),
|
||||
// 并把最终 URL 写入 yz_auth_identity.avatar 供认证中心 userinfo 下发。
|
||||
func (c *BackendFileController) UploadAvatar() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -968,25 +920,20 @@ func (c *BackendFileController) UploadAvatar() {
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
// 去重命中或出错时清理临时文件,避免产生垃圾
|
||||
if !committed && staged != nil {
|
||||
_ = storageService.Discard(staged)
|
||||
}
|
||||
}()
|
||||
|
||||
tid := c.effectiveTid(claims)
|
||||
// 头像固定归入「我的个人文件」目录:既不会混进租户共享文件列表,
|
||||
// 又能让本人在文件管理里找到自己上传过的头像。
|
||||
tuid := uint64(claims.UserID)
|
||||
upCtx := services.UploadContext{Source: services.SourceBackend, Tid: tid, Tuid: tuid}
|
||||
|
||||
// 同一张图重复上传:直接复用已有文件,不再占一份空间
|
||||
if exist, e := services.FindDuplicate(services.SourceBackend, upCtx.Scope(), tid, tuid, staged.MD5); e == nil && exist != nil {
|
||||
c.saveAvatarAndReply(claims.UserID, exist.Src, exist.Name)
|
||||
return
|
||||
}
|
||||
|
||||
// 头像也占用租户附件空间(计入文件台账)
|
||||
if qerr := services.EnsureTenantStorageQuota(tid, uint64(staged.Size)); qerr != nil {
|
||||
if respondStorageQuotaError(&c.Controller, qerr) {
|
||||
return
|
||||
@@ -1002,7 +949,6 @@ func (c *BackendFileController) UploadAvatar() {
|
||||
}
|
||||
committed = true
|
||||
|
||||
// 文件台账落库失败不阻断头像设置(头像可用优先)
|
||||
if _, e := services.CreateFileRecord(services.FileUploadParams{
|
||||
Source: services.SourceBackend,
|
||||
Scope: upCtx.Scope(),
|
||||
@@ -1023,7 +969,6 @@ func (c *BackendFileController) UploadAvatar() {
|
||||
c.saveAvatarAndReply(claims.UserID, result.URL, header.Filename)
|
||||
}
|
||||
|
||||
// saveAvatarAndReply 写入身份头像并返回结果
|
||||
func (c *BackendFileController) saveAvatarAndReply(identityID int, url, name string) {
|
||||
if err := services.UpdateIdentityAvatar(uint64(identityID), url); err != nil {
|
||||
c.jsonErr(500, 500, "保存头像失败: "+err.Error())
|
||||
@@ -1032,8 +977,6 @@ func (c *BackendFileController) saveAvatarAndReply(identityID int, url, name str
|
||||
c.jsonOK(map[string]interface{}{"url": url, "name": name})
|
||||
}
|
||||
|
||||
// UpdateAvatar POST /backend/uploadavatar/:id
|
||||
// 兼容旧调用:把指定文件的地址设为当前登录用户头像(不重新上传文件)。
|
||||
func (c *BackendFileController) UpdateAvatar() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
@@ -1059,7 +1002,6 @@ func (c *BackendFileController) UpdateAvatar() {
|
||||
c.saveAvatarAndReply(claims.UserID, f.Src, f.Name)
|
||||
}
|
||||
|
||||
// BatchMoveFiles POST /backend/batchMoveFiles
|
||||
func (c *BackendFileController) BatchMoveFiles() {
|
||||
claims, err := c.backendClaims()
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user