2024-12-06 09:00:24 +08:00

99 lines
3.6 KiB
Go

package tencentdocument
import (
"time"
"shylinux.com/x/ice"
"shylinux.com/x/icebergs/base/web"
kit "shylinux.com/x/toolkits"
"shylinux.com/x/community/src/api"
"shylinux.com/x/community/src/gonganxitong"
"shylinux.com/x/community/src/renzhengshouquan"
"shylinux.com/x/community/src/renzhengshouquan/external/tencentdocument/model"
)
type tencentdocument struct {
renzhengshouquan.Table
client_id string `data:""`
client_secret string `data:""`
order string `data:"13"`
fields string `data:"user_id,access_token,expired_time,user_uid"`
auth string `name:"auth" help:"授权" role:"worker"`
remove string `name:"remove" role:"worker"`
config string `name:"config folder_id*:select" style:"notice" role:"worker"`
}
func (s tencentdocument) Inputs(m *ice.Message, arg ...string) {
switch arg[0] {
case model.FOLDER_ID:
s.Folder(m, m.Option(model.AUTH_UID)).Cut(arg[0], model.NAME)
m.DisplayInputKeyNameIconTitle()
}
}
func (s tencentdocument) List(m *ice.Message, arg ...string) {
if len(arg) == 1 {
if s.ValueList(m, arg).Action(s.Auth).Display("").Length() == 0 {
s.Button(m.SetResult(), "请授权访问腾讯文档", s.Auth)
}
} else if len(arg) == 2 {
s.AuthList(m, arg[0], func(value ice.Maps, auth bool) {
if auth {
m.PushButton(s.Config)
} else {
m.PushButton()
}
})
}
}
func (s tencentdocument) Config(m *ice.Message, arg ...string) {
m.Cmdy(api.GONGANXITONG_DOCUMENT, s.Config, m.OptionSimple(model.PLACE_UID, model.USER_UID, model.FOLDER_ID), model.VENDOR, m.PrefixKey())
}
func (s tencentdocument) Folder(m *ice.Message, arg ...string) *ice.Message {
msg := s.Select(m.Spawn(), model.AUTH_UID, arg[0])
if msg.Length() == 0 {
m.Echo("请联系公司管理员, 授权访问腾讯文档")
return m
}
m.Options(web.SPIDE_HEADER, map[string]string{
"Client-Id": m.Config(model.CLIENT_ID), "Open-Id": msg.Append(model.USER_ID), "Access-Token": msg.Append(model.ACCESS_TOKEN),
}).Cmdy(web.SPIDE, ice.DEV, web.SPIDE_RAW, kit.MergeURL("https://docs.qq.com/openapi/drive/v2/folders/"+kit.Select("", arg, 1)))
kit.For(kit.Value(kit.UnMarshal(m.Result()), "data.list"), func(value ice.Map) {
value["browse_time"] = time.Unix(kit.Int64(value["lastBrowseTime"]), 0).Format(ice.MOD_TIME)
m.PushRecord(value, "browse_time", "type", "title", "ownerName", "url", "ID")
})
m.RenameAppend("title", model.NAME, "url", model.LINK, "ID", model.FOLDER_ID)
m.SetResult()
return m
}
func (s tencentdocument) Auth(m *ice.Message, arg ...string) {
if m.Option("code") == "" {
m.ProcessOpen(kit.MergeURL("https://docs.qq.com/oauth/v2/authorize",
model.CLIENT_ID, m.Config(model.CLIENT_ID),
"redirect_uri", m.MergePodCmd("", m.PrefixKey()+"/action/"+m.ActionKey()),
"response_type", "code", "scope", "all", "state", m.Option(model.AUTH_UID),
))
} else {
m.Cmdy(web.SPIDE, ice.DEV, kit.MergeURL("https://docs.qq.com/oauth/v2/token",
model.CLIENT_ID, m.Config(model.CLIENT_ID), model.CLIENT_SECRET, m.Config(model.CLIENT_SECRET),
"redirect_uri", m.MergePodCmd("", m.PrefixKey()+"/action/"+m.ActionKey()),
"grant_type", "authorization_code", "code", m.Option("code"),
))
if m.IsErr() {
return
}
m.Append(model.EXPIRED_TIME, m.Time(m.Append("expires_in")+"s"))
s.Insert(m, kit.Simple(model.AUTH_UID, m.Option("state"), m.OptionSimple(model.USER_UID),
m.AppendSimple(model.USER_ID, model.ACCESS_TOKEN, model.REFRESH_TOKEN, model.EXPIRED_TIME))...)
if m.IsWeixinUA() {
m.ProcessBack("-2")
} else {
m.ProcessClose()
}
}
}
func init() { ice.TeamCtxCmd(tencentdocument{}) }
func init() { gonganxitong.DocumentVendor = tencentdocument{} }