1 Star 1 Fork 0

zhuyuns/basic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model.go 4.83 KB
一键复制 编辑 原始数据 按行查看 历史
wing 提交于 2021-12-18 11:42 . 添加基础包
/**
* @api post model.
*
* User: yunshengzhu
* Date: 2020/7/31
* Time: 3:04 下午
*/
package ding
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"strings"
)
type iDingMsg interface {
Marshaler() []byte
}
type atOption interface {
apply(model *atModel)
}
type funcAtOption struct {
f func(model *atModel)
}
func (fdo *funcAtOption) apply(do *atModel) {
fdo.f(do)
}
func newFuncAtOption(f func(model *atModel)) *funcAtOption{
return &funcAtOption{f:f}
}
func WithAtAll() atOption{
return newFuncAtOption(func(o *atModel) {
o.IsAtAll=true
})
}
func WithAtMobiles(mobiles []string) atOption{
return newFuncAtOption(func(o *atModel) {
o.AtMobiles=mobiles
})
}
type textMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Text textModel `json:"text,omitempty"`
At atModel `json:"at,omitempty"`
}
func (t textMsg) Marshaler() []byte {
b, _ := jsoniter.Marshal(t)
return b
}
func NewTextMsg(content string, opts ...atOption) *textMsg {
msg := &textMsg{MsgType: TEXT, Text:textModel{Content:content}}
for _,opt := range opts{
opt.apply(&msg.At)
}
return msg
}
type linkMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Link linkModel `json:"link,omitempty"`
}
func (l linkMsg) Marshaler() []byte {
b, _ := jsoniter.Marshal(l)
return b
}
func NewLinkMsg(title, text, picUrl, msgUrl string) *linkMsg {
return &linkMsg{MsgType: LINK, Link:linkModel{
Text: text,
Title: title,
PicUrl: picUrl,
MessageUrl: msgUrl,
}}
}
type markDownMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Markdown markDownModel `json:"markdown,omitempty"`
At atModel `json:"at,omitempty"`
}
func (m markDownMsg) Marshaler() []byte {
b, _ := jsoniter.Marshal(m)
return b
}
func NewMarkDownMsg(title string, text interface{}, opts ...atOption) *markDownMsg {
msg := &markDownMsg{MsgType: MARKDOWN, Markdown:markDownModel{Title:title, Text:text.(string)}}
for _,opt := range opts{
opt.apply(&msg.At)
}
return msg
}
type actionCardOption interface {
apply(model *actionCardModel)
}
type funcActionCardOption struct {
f func(model *actionCardModel)
}
func (fdo *funcActionCardOption) apply(do *actionCardModel) {
fdo.f(do)
}
func newFuncActionCardOption(f func(model *actionCardModel)) *funcActionCardOption{
return &funcActionCardOption{f:f}
}
func WithCardBtnVertical() actionCardOption{
return newFuncActionCardOption(func(o *actionCardModel) {
o.BtnOrientation=vertical
})
}
func WithCardSingleTitle(title string) actionCardOption{
return newFuncActionCardOption(func(o *actionCardModel) {
o.SingleTitle=title
})
}
func WithCardSingleURL(url string) actionCardOption{
return newFuncActionCardOption(func(o *actionCardModel) {
o.SingleURL=url
})
}
func WithCardBtns(btns []ActionCardMultiBtnModel)actionCardOption{
return newFuncActionCardOption(func(o *actionCardModel) {
o.Btns=btns
})
}
type actionCardMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
ActionCard actionCardModel `json:"actionCard,omitempty"`
}
func (a actionCardMsg) Marshaler() []byte {
b, _ := jsoniter.Marshal(a)
return b
}
func NewActionCardMsg(title, text string, opts ...actionCardOption) *actionCardMsg {
card := &actionCardMsg{MsgType: ACTION_CARD, ActionCard:actionCardModel{
Title: title,
Text: text,
BtnOrientation: horizontal,
}}
for _, opt := range opts{
opt.apply(&card.ActionCard)
}
return card
}
type feedCardMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
FeedCard feedCardModel `json:"feedCard,omitempty"`
}
func (f feedCardMsg) Marshaler() []byte {
b, _ := jsoniter.Marshal(f)
return b
}
func NewFeedCardMsg(feedCard []FeedCardLinkModel) *feedCardMsg {
return &feedCardMsg{MsgType: FEED_CARD, FeedCard:feedCardModel{Links:feedCard}}
}
type MarkType string
// 有序map
type dingMap struct {
m map[string]MarkType
l []string
}
func DingMap() *dingMap {
return &dingMap{m: make(map[string]MarkType), l: make([]string,0,0)}
}
func (d *dingMap) Set(val string, t MarkType) {
d.l = append(d.l, val)
d.m[val] = t
}
func (d *dingMap) Remove(val string) {
if _, ok := d.m[val]; ok {
for i, v := range d.l {
if v == val {
d.l = append(d.l[:i], d.l[i+1:]...)
break
}
}
delete(d.m, val)
}
}
func (d *dingMap) Slice() []string {
resList := make([]string, 0, len(d.l))
for _, val := range d.l {
content := d.formatVal(val, d.m[val])
resList = append(resList, content)
}
return resList
}
func (d *dingMap) formatVal(val string, t MarkType) (res string) {
var ok bool
if res, ok = hMap[t]; ok {
vl := strings.Split(val, formatSpliter)
if len(vl) == 3 {
res = fmt.Sprintf(res, vl[1])
res = vl[0] + res + vl[2]
} else {
res = fmt.Sprintf(res, val)
}
} else {
res = val
}
if !strings.HasPrefix(res, "- ") && !strings.HasPrefix(res, "#") {
res = "- " + res
}
return
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhuyuns/basic.git
git@gitee.com:zhuyuns/basic.git
zhuyuns
basic
basic
v0.0.35

搜索帮助