dingtalk待办任务 未测试
This commit is contained in:
parent
bfea1f94d2
commit
b156c8133e
|
|
@ -0,0 +1 @@
|
|||
{"errcode":0,"errmsg":"ok","access_token":"68071ea7408933b78557a9f78a26d20d","expires_in":7200,"Created":1649990138}
|
||||
90
main.go
90
main.go
|
|
@ -19,6 +19,7 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
"myschools.me/wyh/dingd.git/model"
|
||||
"myschools.me/wyh/dingd.git/service"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -31,46 +32,25 @@ func main() {
|
|||
}).Errorf("%s", err.Error())
|
||||
return
|
||||
}
|
||||
timestamp := strconv.FormatInt(time.Now().UnixNano()/1000000, 10) // 毫秒时间戳
|
||||
signature := EncodeSHA256(timestamp, viper.GetString("app.AppSecret")) // 加密签名 加密算法见我另一个函数
|
||||
url := fmt.Sprintf("https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=%s×tamp=%s&signature=%s",
|
||||
viper.GetString("app.AppKey"), timestamp, signature)
|
||||
p := struct {
|
||||
Tmp_auth_code string `json:"tmp_auth_code"`
|
||||
}{viper.GetString("app.AuthCode")} // post数据
|
||||
p1, _ := json.Marshal(p)
|
||||
p2 := string(p1)
|
||||
p3 := strings.NewReader(p2) //构建post数据
|
||||
uid := "351313653629177902"
|
||||
opid := "351313653629177902"
|
||||
service.DingTalkTaskCreate(&uid, &opid)
|
||||
// unionid:=UnionidByCode()
|
||||
|
||||
resp, err := http.Post(url, "application/json;charset=UTF-8", p3)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
i := make(map[string]interface{})
|
||||
_ = json.Unmarshal(body, &i)
|
||||
fmt.Println(i)
|
||||
errcode := i["errcode"].(float64)
|
||||
if errcode != 0 {
|
||||
log.Fatal("errcode ==0")
|
||||
}
|
||||
unionid := i["user_info"].(map[string]interface{})["unionid"].(string)
|
||||
accesstoken, err := GetAccesstoken()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
userid, err := GetUseridByUnionid(accesstoken, unionid)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
userinfo, err := GETUserInfo(accesstoken, userid)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(userinfo)
|
||||
// accesstoken, err := GetAccesstoken()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
|
||||
// userid, err := GetUseridByUnionid(accesstoken, unionid)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// userinfo, err := GETUserInfo(accesstoken, userid)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// fmt.Println(userinfo)
|
||||
}
|
||||
func EncodeSHA256(message, secret string) string {
|
||||
// 钉钉签名算法实现
|
||||
|
|
@ -168,3 +148,35 @@ func GETUserInfo(accesstoken, userid string) (*model.DingtalkUserInfoResponse, e
|
|||
}
|
||||
return userinfo, nil
|
||||
}
|
||||
|
||||
func UnionidByCode() string {
|
||||
timestamp := strconv.FormatInt(time.Now().UnixNano()/1000000, 10) // 毫秒时间戳
|
||||
signature := EncodeSHA256(timestamp, viper.GetString("app.AppSecret")) // 加密签名 加密算法见我另一个函数
|
||||
url := fmt.Sprintf("https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=%s×tamp=%s&signature=%s",
|
||||
viper.GetString("app.AppKey"), timestamp, signature)
|
||||
p := struct {
|
||||
Tmp_auth_code string `json:"tmp_auth_code"`
|
||||
}{viper.GetString("app.AuthCode")} // post数据
|
||||
p1, _ := json.Marshal(p)
|
||||
p2 := string(p1)
|
||||
p3 := strings.NewReader(p2) //构建post数据
|
||||
|
||||
resp, err := http.Post(url, "application/json;charset=UTF-8", p3)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
i := make(map[string]interface{})
|
||||
_ = json.Unmarshal(body, &i)
|
||||
fmt.Println(i)
|
||||
errcode := i["errcode"].(float64)
|
||||
if errcode != 0 {
|
||||
log.Fatal("errcode ==0")
|
||||
}
|
||||
unionid := i["user_info"].(map[string]interface{})["unionid"].(string)
|
||||
|
||||
return unionid
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,3 +67,48 @@ type DingtalkUnionEmpMapVo struct {
|
|||
UserID string `json:"userid"`
|
||||
CorpID string `json:"corpid"`
|
||||
}
|
||||
|
||||
type DingtalkTask struct {
|
||||
SourceID string `json:"sourceId"`
|
||||
Subject string `json:"subject"`
|
||||
CreatorID string `json:"creatorId"`
|
||||
Description string `json:"description"`
|
||||
DueTime int64 `json:"dueTime"`
|
||||
ExecutorIds []string `json:"executorIds"`
|
||||
ParticipantIds []string `json:"participantIds"`
|
||||
DetailURL DetailURL `json:"detailUrl"`
|
||||
IsOnlyShowExecutor bool `json:"isOnlyShowExecutor"`
|
||||
Priority int `json:"priority"`
|
||||
NotifyConfigs NotifyConfigs `json:"notifyConfigs"`
|
||||
}
|
||||
type DetailURL struct {
|
||||
AppURL string `json:"appUrl"`
|
||||
PcURL string `json:"pcUrl"`
|
||||
}
|
||||
type NotifyConfigs struct {
|
||||
DingNotify string `json:"dingNotify"`
|
||||
}
|
||||
|
||||
type DingtalkRes struct {
|
||||
ID string `json:"id"`
|
||||
Subject string `json:"subject"`
|
||||
Description string `json:"description"`
|
||||
StartTime int64 `json:"startTime"`
|
||||
DueTime int64 `json:"dueTime"`
|
||||
FinishTime int64 `json:"finishTime"`
|
||||
Done bool `json:"done"`
|
||||
ExecutorIds []string `json:"executorIds"`
|
||||
ParticipantIds []string `json:"participantIds"`
|
||||
DetailURL DetailURL `json:"detailUrl"`
|
||||
Source string `json:"source"`
|
||||
SourceID string `json:"sourceId"`
|
||||
CreatedTime int64 `json:"createdTime"`
|
||||
ModifiedTime int64 `json:"modifiedTime"`
|
||||
CreatorID string `json:"creatorId"`
|
||||
ModifierID string `json:"modifierId"`
|
||||
BizTag string `json:"bizTag"`
|
||||
RequestID string `json:"requestId"`
|
||||
IsOnlyShowExecutor bool `json:"isOnlyShowExecutor"`
|
||||
Priority int `json:"priority"`
|
||||
NotifyConfigs NotifyConfigs `json:"notifyConfigs"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Httpsetheader
|
||||
func HttpSetHeader(method, url *string, body interface{}) (*http.Request, error) {
|
||||
bb, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
reader := strings.NewReader(string(bb))
|
||||
//生成client 参数为默认
|
||||
// client := &http.Client{}
|
||||
|
||||
//提交请求
|
||||
reqest, err := http.NewRequest(*method, *url, reader)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "Httpsetheader",
|
||||
}).Warnf("%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
// //增加header选项
|
||||
// reqest.Header.Add("Cookie", "xxxxxx")
|
||||
// reqest.Header.Add("User-Agent", "xxx")
|
||||
// reqest.Header.Add("X-Requested-With", "xxxx")
|
||||
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// //处理返回结果
|
||||
// response, _ := client.Do(reqest)
|
||||
// defer response.Body.Close()
|
||||
return reqest, nil
|
||||
}
|
||||
|
||||
func HttpPost(uri string, body interface{}) (*[]byte, error) {
|
||||
bb, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader := strings.NewReader(string(bb))
|
||||
|
||||
url := fmt.Sprintf("%s%s", viper.GetString("srv.host"), uri)
|
||||
resp, err := http.Post(url, "application/json;charset=UTF-8", reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respbody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &respbody, nil
|
||||
}
|
||||
|
||||
func HttpGet(uri string) (*[]byte, error) {
|
||||
url := fmt.Sprintf("%s%s", viper.GetString("srv.host"), uri)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respbody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &respbody, nil
|
||||
}
|
||||
|
|
@ -116,3 +116,53 @@ func dingtalkWorkMessage(msg *model.Msg, toalluser bool, useridlist, reqid *stri
|
|||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 待办新增 未测试
|
||||
func DingTalkTaskCreate(unionid, operatorid *string) {
|
||||
client, err := dingtalkClient()
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "dingtalkDepartment",
|
||||
}).Warnf("dingtalkClient: %s", err.Error())
|
||||
return
|
||||
}
|
||||
// 扫码认证使用snsuser
|
||||
// res, err := client.SnsUser()
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
newtask := &model.DingtalkTask{
|
||||
Subject: "这是一个待办任务",
|
||||
}
|
||||
method := "post"
|
||||
uri := fmt.Sprintf("https://api.dingtalk.com/v1.0/todo/users/%s/tasks?operatorId=%s", *unionid, *operatorid)
|
||||
httprequest, err := HttpSetHeader(&method, &uri, newtask)
|
||||
httprequest.Header.Set("x-acs-dingtalk-access-token", client.AccessToken)
|
||||
|
||||
resp, err := client.HTTPClient.Do(httprequest)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "DingTalkTaskCreate",
|
||||
}).Warnf("cliemt.HTTPClient.Do: %s", err.Error())
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "DingTalkTaskCreate",
|
||||
}).Warnf("statuscode =%d", resp.StatusCode)
|
||||
}
|
||||
bb, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "DingTalkTaskCreate",
|
||||
}).Warnf("ioutil.ReadAll: %s", err.Error())
|
||||
}
|
||||
var result *model.DingtalkRes
|
||||
if err := json.Unmarshal(bb, &result); err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"func": "DingTalkTaskCreate",
|
||||
}).Warnf("json.Unmarshal: %s", err.Error())
|
||||
}
|
||||
logrus.Println(result)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue