2024-11-20 22:49:49 +08:00

158 lines
6.5 KiB
Go

package tencentmeeting
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"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/tencentmeeting/model"
"shylinux.com/x/ice"
"shylinux.com/x/icebergs/base/web"
kit "shylinux.com/x/toolkits"
wemeet "github.com/TencentCloud/wemeet-openapi-sdk-go/wemeet_openapi"
wemeetcore "github.com/TencentCloud/wemeet-openapi-sdk-go/wemeet_openapi/core"
meetings "github.com/TencentCloud/wemeet-openapi-sdk-go/wemeet_openapi/service/meetings"
)
type tencentmeeting struct {
renzhengshouquan.Table
order string `data:"10"`
fields string `data:"auth_uid,user_uid,app_id,sdk_id,secret_id,secret_key,user_id"`
create string `name:"create app_id* sdk_id* secret_id* secret_key* user_id*" role:"leader"`
remove string `name:"remove" role:"leader"`
list string `name:"list auth_uid uid auto" role:"leader"`
config string `name:"config user_id" style:"notice" role:"leader"`
}
func (s tencentmeeting) Init(m *ice.Message, arg ...string) {
gonganxitong.MeetingVendorAdd(m.PrefixKey(), s)
s.Table.Init(m, arg...)
}
func (s tencentmeeting) Inputs(m *ice.Message, arg ...string) {
switch arg[0] {
case "user_id":
s.UserList(m, m.Option("auth_uid"))
m.Cut("userid", "username")
}
}
func (s tencentmeeting) List(m *ice.Message, arg ...string) {
if len(arg) == 1 {
s.ValueList(m, arg[:1]).Display("")
kit.If(m.Length() > 0, func() { m.Action() })
} else {
// s.UserList(m, arg[0])
// return
m.Cmdy(api.RENZHENGSHOUQUAN_AUTH, arg[0])
m.Table(func(value ice.Maps) {
if renzhengshouquan.AuthStatus(kit.Int(value["auth_status"])) == renzhengshouquan.AuthIssued {
m.PushButton(s.Config)
} else {
m.PushButton()
}
})
}
}
func (s tencentmeeting) Config(m *ice.Message, arg ...string) {
m.Cmdy(api.GONGANXITONG_MEETING, s.Config, m.OptionSimple(model.PLACE_UID, "user_id"), "vendor", m.PrefixKey())
}
func (s tencentmeeting) UserList(m *ice.Message, arg ...string) {
s.spide(m, arg[0], "", "users/list", "page", "1", "page_size", "10")
s.parseList(m, "users", "role_code", "role_name", "userid", "username", "avatar_url", "status", "account_version", "user_account_type").RenameAppend("avatar_url", model.AVATAR)
}
func (s tencentmeeting) MeetingList(m *ice.Message, arg ...string) {
s.spide(m, arg[0], "", "meetings", "userid", "$user_id", "instanceid", "1")
s.parseList(m, "meeting_info_list", "subject", "meeting_code", "start_time", "end_time", "join_url").RenameAppend("join_url", "link")
}
func init() { ice.TeamCtxCmd(tencentmeeting{}) }
func (s tencentmeeting) spide(m *ice.Message, auth_uid, method, api string, arg ...ice.Any) {
arg = append(arg, "operator_id", "$user_id", "operator_id_type", "1")
msg := s.Select(m.Spawn(), model.AUTH_UID, auth_uid)
for i := 0; i < len(arg); i += 2 {
kit.If(arg[i+1] == "$user_id", func() { arg[i+1] = kit.Select(msg.Append("user_id"), m.Option("user_id")) })
}
method = kit.Select(http.MethodGet, method)
uri := kit.MergeURL("/v1/"+api, arg...)
now := kit.Format(time.Now().Unix())
nonce := kit.Format(uint64(100000 + rand.New(rand.NewSource(time.Now().UnixNano())).Intn(900000)))
m.Options(web.SPIDE_HEADER, map[string]string{
"AppId": msg.Append("app_id"),
"SdkId": msg.Append("sdk_id"),
"X-TC-Key": msg.Append("secret_id"),
"X-TC-Nonce": nonce,
"X-TC-Timestamp": now,
"X-TC-Registered": "1",
"X-TC-Signature": doSignature(msg.Append("secret_id"), msg.Append("secret_key"), "GET", nonce, now, uri, ""),
}).Cmdy(web.SPIDE, "dev", web.SPIDE_RAW, http.MethodGet, "https://api.meeting.qq.com"+uri)
}
func (s tencentmeeting) parseList(m *ice.Message, key string, arg ...string) *ice.Message {
if m.IsErr() {
return m
}
kit.For(kit.Value(kit.UnMarshal(m.Result()), key), func(value ice.Map) {
kit.For(arg, func(key string) {
if strings.HasSuffix(key, "_time") {
m.Push(key, strings.TrimSuffix(time.Unix(kit.Int64(value[key]), 0).Format(ice.MOD_TIME), ":00.000"))
} else {
m.Push(key, value[key])
}
})
})
m.SetResult()
// m.Display("/plugin/story/json.js")
return m
}
func doSignature(secretId string, secretKey string, httpMethod string, headerNonce string, headerTimestamp string, requestUri string, requestBody string) string {
headSignStr := fmt.Sprintf("X-TC-Key=%s&X-TC-Nonce=%s&X-TC-Timestamp=%s", secretId, headerNonce, headerTimestamp)
signStr := fmt.Sprintf("%s\n%s\n%s\n%s", httpMethod, headSignStr, requestUri, requestBody)
h := hmac.New(sha256.New, []byte(secretKey))
h.Write([]byte(signStr))
sha := hex.EncodeToString(h.Sum([]byte{}))
signBase64 := base64.StdEncoding.EncodeToString([]byte(sha))
return signBase64
}
func (s tencentmeeting) meetingList(m *ice.Message, arg ...string) {
if client, auth, user_id := s.newClient(m, arg[0]); client != nil {
args := []string{user_id, "1", kit.Select("", arg, 1)}
response, err := client.MeetingsApi.V1MeetingsGet(m, &meetings.ApiV1MeetingsGetRequest{Userid: &args[0], Instanceid: &args[1], MeetingCode: &args[2]}, auth)
s.resTable(m, response.Data, err, "meeting_info_list", "subject", "meeting_code", "start_time", "end_time", "join_url").RenameAppend("join_url", "link")
}
}
func (s tencentmeeting) newClient(m *ice.Message, arg ...string) (*wemeet.Client, wemeetcore.RequestOptionFunc, string) {
msg := s.Select(m.Spawn(), model.AUTH_UID, arg[0])
if msg.Length() == 0 {
m.Echo("请联系公司管理员,配置腾讯会议的账号").Action()
return nil, nil, ""
}
client := wemeet.NewClient(wemeet.WithAppId(msg.Append("app_id")), wemeet.WithSdkId(msg.Append("sdk_id")), wemeet.WithSecret(msg.Append("secret_id"), msg.Append("secret_key")))
authenticator := &wemeetcore.JWTAuthenticator{Nonce: uint64(100000 + rand.New(rand.NewSource(time.Now().UnixNano())).Intn(900000)), Timestamp: strconv.Itoa(int(time.Now().Unix()))}
return client, wemeetcore.WithJWTAuth(authenticator), kit.Select(msg.Append("user_id"), m.Option("user_id"))
}
func (s tencentmeeting) resTable(m *ice.Message, data ice.Any, err error, key string, arg ...string) *ice.Message {
if m.SetAppend().SetResult(); err != nil {
return m.Echo(kit.Formats(err))
}
kit.For(kit.Value(kit.UnMarshal(kit.Formats(data)), key), func(value ice.Map) {
kit.For(arg, func(key string) {
if strings.HasSuffix(key, "_time") {
m.Push(key, strings.TrimSuffix(time.Unix(kit.Int64(value[key]), 0).Format(ice.MOD_TIME), ":00.000"))
} else {
m.Push(key, value[key])
}
})
})
return m
}