weboffice

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

WPS WebOffice 开放平台 Go SDK V3

依赖

  • go 1.16+
  • gin framework

How to use

go get github.com/mose-x/weboffice
package main

func InitRouter() {
	// 初始化路由
	e := gin.Default()

	// 注册wps web office服务
	provider := &Provider{}
	weboffice.NewServer(weboffice.Config{
		PreviewProvider:   provider,
		UserProvider:      provider,
		WatermarkProvider: provider,
		EditProvider:      provider,
		VersionProvider:   provider,
		Logger:            weboffice.DefaultLogger(),
		NotifyProvider:    provider,
	}, e)
	
	// 启动服务
	_ = e.Run(":8080")
}
实现接口
package main

type Provider struct {
}

func (*Provider) GetFileWatermark(_ weboffice.Context, _ string) (*weboffice.GetWatermarkReply, error) {
	return &weboffice.GetWatermarkReply{
		Type:       1,
		Value:      "mose",
		FillStyle:  "rgba(192,192,192,0.6)",
		Font:       "bold 20px Serif",
		Rotate:     0.5,
		Horizontal: 50,
		Vertical:   50,
	}, nil
}
实际效果

docx 在线预览/编辑


pptx 在线预览/编辑


xlsx 在线预览/编辑


pdf 在线预览/编辑


更多

关于接口的更多说明,请参考WebOffice开放平台-WebOffice回调配置

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized         = &Error{code: 40002, statusCode: http.StatusUnauthorized, message: "unauthorized"}
	ErrPermissionDenied     = &Error{code: 40003, statusCode: http.StatusForbidden, message: "permission denied"}
	ErrFileNotExists        = &Error{code: 40004, statusCode: http.StatusForbidden, message: "file not exists"}
	ErrInvalidArguments     = &Error{code: 40005, statusCode: http.StatusForbidden, message: "invalid arguments"}
	ErrSpaceFull            = &Error{code: 40006, statusCode: http.StatusForbidden, message: "space full"}
	ErrFileNameConflict     = &Error{code: 40008, statusCode: http.StatusForbidden, message: "filename conflict"}
	ErrFileVersionNotExists = &Error{code: 40009, statusCode: http.StatusForbidden, message: "file version not exists"}
	ErrUserNotExists        = &Error{code: 40010, statusCode: http.StatusForbidden, message: "user not exists"}
	ErrInternalError        = &Error{code: 50001, statusCode: http.StatusInternalServerError, message: "internal error"}
)

枚举错误码

View Source
var Referer = "https://solution.wps.cn"

Functions

func NewServer

func NewServer(config Config, e *gin.Engine)

NewServer 创建服务

Types

type Code

type Code int
const (
	OK Code = 0
)

OK 枚举成功码

type Config

type Config struct {
	// 注册预览服务
	PreviewProvider

	// 注册用户服务
	UserProvider

	// 注册水印服务
	WatermarkProvider

	// 注册编辑服务
	EditProvider

	// 注册版本服务
	VersionProvider

	// 路由前缀(上传用)
	Prefix string

	// 注册日志服务
	Logger

	// 注册通知服务
	NotifyProvider
}

Config 配置

type Context

type Context interface {
	context.Context

	// AppID 获取请求ID
	AppID() string

	// Token 获取请求Token
	Token() string

	// Query 获取请求参数
	Query() url.Values

	// RequestID 获取请求ID
	RequestID() string
}

Context 定义上下文接口

func ParseContext

func ParseContext(req *http.Request) Context

ParseContext 解析上下文

type EditProvider

type EditProvider interface {
	// UpdateFile 更新文件
	UpdateFile(ctx Context, fileID string, args *UpdateFilePhaseArgs) (*GetFileReply, error)

	// RenameFile 重命名文件
	RenameFile(ctx Context, fileID string, args *RenameFileArgs) error
}

EditProvider 定义编辑接口

type Empty

type Empty struct {
}

Empty 空结构体

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error 定义枚举错误结构体

func NewCustomError

func NewCustomError(message string) *Error

NewCustomError 创建自定义错误

func NewError

func NewError(code Code) *Error

NewError 创建枚举错误

func (*Error) Code

func (err *Error) Code() Code

Code 获取错误码

func (*Error) Error

func (err *Error) Error() string

Error 获取错误描述

func (*Error) Message

func (err *Error) Message() string

Message 获取错误描述

func (*Error) StatusCode

func (err *Error) StatusCode() int

StatusCode 获取错误码对应的HTTP状态码

func (*Error) WithMessage

func (err *Error) WithMessage(msg string) *Error

WithMessage 设置错误描述

type GetFileDownloadReply

type GetFileDownloadReply struct {
	URL     string            `json:"url"`
	Headers map[string]string `json:"headers"`
}

GetFileDownloadReply 获取文件下载地址返回信息

type GetFilePermissionReply

type GetFilePermissionReply struct {
	Comment  int    `json:"comment"`
	Copy     int    `json:"copy"`
	Download int    `json:"download"`
	History  int    `json:"history"`
	Print    int    `json:"print"`
	Read     int    `json:"read"`
	Rename   int    `json:"rename"`
	SaveAs   int    `json:"saveas"`
	Update   int    `json:"update"`
	UserId   string `json:"user_id"`
}

GetFilePermissionReply 获取文件权限返回信息

type GetFileReply

type GetFileReply struct {
	CreateTime int64  `json:"create_time"`
	CreatorId  string `json:"creator_id"`
	ID         string `json:"id"`
	ModifierId string `json:"modifier_id"`
	ModifyTime int64  `json:"modify_time"`
	Name       string `json:"name"`
	Size       int64  `json:"size"`
	Version    int32  `json:"version"`
}

GetFileReply 获取文件返回信息

type GetWatermarkReply

type GetWatermarkReply struct {
	Type       int     `json:"type"`
	Value      string  `json:"value"`
	FillStyle  string  `json:"fill_style"`
	Font       string  `json:"font"`
	Rotate     float64 `json:"rotate"`
	Horizontal int     `json:"horizontal"`
	Vertical   int     `json:"vertical"`
}

GetWatermarkReply 获取水印返回信息

type Logger

type Logger interface {
	Info(format string, args ...any)
	Error(format string, args ...any)
}

Logger 定义log接口

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger 默认logger

type NotifyArgs

type NotifyArgs struct {
	FileId  string        `json:"file_id,omitempty"`
	Type    string        `json:"type,omitempty"`
	Content NotifyContent `json:"content,omitempty"`
}

NotifyArgs 通知参数

type NotifyContent

type NotifyContent struct {
	SessionId       string `json:"session_id,omitempty"`
	InitVersion     int    `json:"init_version,omitempty"`
	Readonly        bool   `json:"readonly,omitempty"`
	UploadedVersion int    `json:"uploaded_version,omitempty"`
	LastModifierId  string `json:"last_modifier_id,omitempty"`
	ConnectionId    string `json:"connection_id,omitempty"`
	UserId          string `json:"user_id,omitempty"`
	Permission      string `json:"permission,omitempty"`
	Print           bool   `json:"print,omitempty"`
	Format          string `json:"format,omitempty"`
}

NotifyContent 通知内容

type NotifyProvider

type NotifyProvider interface {
	// OnNotify 触发通知
	OnNotify(ctx Context, args *NotifyArgs) error
}

NotifyProvider 定义通知接口

type PreviewProvider

type PreviewProvider interface {
	// GetFile 获取文件信息
	GetFile(ctx Context, fileID string) (*GetFileReply, error)

	// GetFileDownload 获取文件下载地址
	GetFileDownload(ctx Context, fileID string) (*GetFileDownloadReply, error)

	// GetFilePermission 获取文件权限信息
	GetFilePermission(ctx Context, fileID string) (*GetFilePermissionReply, error)
}

PreviewProvider 定义预览接口

type RenameFileArgs

type RenameFileArgs struct {
	Name string `json:"name"`
}

RenameFileArgs 重命名文件参数

type Reply

type Reply struct {
	Code    Code   `json:"code"`
	Message string `json:"message,omitempty"`
	Data    any    `json:"data"`
}

Reply 返回参数结构体

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 服务配置

func (*Server) Handler

func (srv *Server) Handler() http.Handler

Handler 获取http handler

func (*Server) Router

func (srv *Server) Router() gin.IRouter

Router 获取路由

func (*Server) Run

func (srv *Server) Run(addr string) error

Run 启动服务

type UpdateFilePhaseArgs

type UpdateFilePhaseArgs struct {
	Name     string
	Size     int64
	SHA1     string
	IsManual bool
	Content  io.Reader
}

UpdateFilePhaseArgs 更新文件阶段参数

type UserContext added in v1.0.3

type UserContext struct {
	context.Context
	// contains filtered or unexported fields
}

UserContext 用户上下文

func (*UserContext) AppID added in v1.0.3

func (uc *UserContext) AppID() string

func (*UserContext) Query added in v1.0.3

func (uc *UserContext) Query() url.Values

func (*UserContext) RequestID added in v1.0.3

func (uc *UserContext) RequestID() string

func (*UserContext) Token added in v1.0.3

func (uc *UserContext) Token() string

type UserProvider

type UserProvider interface {
	// GetUsers 获取用户信息
	GetUsers(ctx Context, userIDs []string) ([]*UserReply, error)
}

UserProvider 定义用户接口

type UserReply

type UserReply struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	AvatarURL string `json:"avatar_url"`
}

UserReply 用户信息

type VersionProvider

type VersionProvider interface {
	// GetFileVersions 获取文件版本列表
	GetFileVersions(ctx Context, fileID string, offset, limit int) ([]*GetFileReply, error)

	// GetFileVersion 获取文件指定版本信息
	GetFileVersion(ctx Context, fileID string, version int32) (*GetFileReply, error)

	// GetFileVersionDownload 获取文件指定版本下载地址
	GetFileVersionDownload(ctx Context, fileID string, version int32) (*GetFileDownloadReply, error)
}

VersionProvider 定义版本接口

type WatermarkProvider

type WatermarkProvider interface {
	// GetFileWatermark 获取文件水印信息
	GetFileWatermark(ctx Context, fileID string) (*GetWatermarkReply, error)
}

WatermarkProvider 定义水印接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL