feat: now user are able to configure channels

This commit is contained in:
JustSong
2022-11-22 17:52:44 +08:00
parent e911c3fb7c
commit 2acc56e03c
10 changed files with 470 additions and 32 deletions
+14 -14
View File
@@ -40,9 +40,9 @@ func TokenStoreInit() {
}
if user.WeChatCorpAccountId != "" {
item := &WeChatCorpAccountTokenStoreItem{
CorpId: user.WeChatCorpAccountId,
CorpSecret: user.WeChatCorpAccountSecret,
AgentId: user.WeChatCorpAccountAgentId,
CorpId: user.WeChatCorpAccountId,
AgentSecret: user.WeChatCorpAccountAgentSecret,
AgentId: user.WeChatCorpAccountAgentId,
}
items = append(items, item)
}
@@ -121,21 +121,21 @@ func TokenStoreUpdateUser(cleanUser *model.User, originUser *model.User) {
if cleanUser.WeChatCorpAccountAgentId == originUser.WeChatCorpAccountAgentId {
cleanUser.WeChatCorpAccountAgentId = ""
}
if cleanUser.WeChatCorpAccountSecret == originUser.WeChatCorpAccountSecret {
cleanUser.WeChatCorpAccountSecret = ""
if cleanUser.WeChatCorpAccountAgentSecret == originUser.WeChatCorpAccountAgentSecret {
cleanUser.WeChatCorpAccountAgentSecret = ""
}
if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountSecret != "" {
if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountAgentSecret != "" {
oldWeChatCorpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
CorpId: cleanUser.WeChatCorpAccountId,
CorpSecret: cleanUser.WeChatCorpAccountSecret,
AgentId: cleanUser.WeChatCorpAccountAgentId,
CorpId: cleanUser.WeChatCorpAccountId,
AgentSecret: cleanUser.WeChatCorpAccountAgentSecret,
AgentId: cleanUser.WeChatCorpAccountAgentId,
}
newWeChatCorpAccountTokenStoreItem := oldWeChatCorpAccountTokenStoreItem
if cleanUser.WeChatCorpAccountId != "" {
newWeChatCorpAccountTokenStoreItem.CorpId = cleanUser.WeChatCorpAccountId
}
if cleanUser.WeChatCorpAccountSecret != "" {
newWeChatCorpAccountTokenStoreItem.CorpSecret = cleanUser.WeChatCorpAccountSecret
if cleanUser.WeChatCorpAccountAgentSecret != "" {
newWeChatCorpAccountTokenStoreItem.AgentSecret = cleanUser.WeChatCorpAccountAgentSecret
}
if cleanUser.WeChatCorpAccountAgentId != "" {
newWeChatCorpAccountTokenStoreItem.AgentId = cleanUser.WeChatCorpAccountAgentId
@@ -157,9 +157,9 @@ func TokenStoreRemoveUser(user *model.User) {
TokenStoreRemoveItem(&testAccountTokenStoreItem)
}
corpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
CorpId: user.WeChatCorpAccountId,
CorpSecret: user.WeChatCorpAccountSecret,
AgentId: user.WeChatCorpAccountAgentId,
CorpId: user.WeChatCorpAccountId,
AgentSecret: user.WeChatCorpAccountAgentSecret,
AgentId: user.WeChatCorpAccountAgentId,
}
if !corpAccountTokenStoreItem.IsShared() {
TokenStoreRemoveItem(&corpAccountTokenStoreItem)
+6 -6
View File
@@ -20,18 +20,18 @@ type wechatCorpAccountResponse struct {
type WeChatCorpAccountTokenStoreItem struct {
CorpId string
CorpSecret string
AgentSecret string
AgentId string
AccessToken string
}
func (i *WeChatCorpAccountTokenStoreItem) Key() string {
return i.CorpId + i.AgentId + i.CorpSecret
return i.CorpId + i.AgentId + i.AgentSecret
}
func (i *WeChatCorpAccountTokenStoreItem) IsShared() bool {
return model.DB.Where("wechat_corp_account_id = ? and wechat_corp_account_secret = ? and wechat_corp_account_agent_id = ?",
i.CorpId, i.CorpSecret, i.AgentId).Find(&model.User{}).RowsAffected != 1
return model.DB.Where("wechat_corp_account_id = ? and wechat_corp_account_agent_secret = ? and wechat_corp_account_agent_id = ?",
i.CorpId, i.AgentSecret, i.AgentId).Find(&model.User{}).RowsAffected != 1
}
func (i *WeChatCorpAccountTokenStoreItem) Token() string {
@@ -44,7 +44,7 @@ func (i *WeChatCorpAccountTokenStoreItem) Refresh() {
Timeout: 5 * time.Second,
}
req, err := http.NewRequest("GET", fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
i.CorpId, i.CorpSecret), nil)
i.CorpId, i.AgentSecret), nil)
if err != nil {
common.SysError(err.Error())
return
@@ -119,7 +119,7 @@ func SendWeChatCorpMessage(message *Message, user *model.User) error {
if err != nil {
return err
}
key := fmt.Sprintf("%s%s%s", user.WeChatCorpAccountId, user.WeChatCorpAccountAgentId, user.WeChatCorpAccountSecret)
key := fmt.Sprintf("%s%s%s", user.WeChatCorpAccountId, user.WeChatCorpAccountAgentId, user.WeChatCorpAccountAgentSecret)
accessToken := TokenStoreGetToken(key)
resp, err := http.Post(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s", accessToken), "application/json",
bytes.NewBuffer(jsonData))