golang实现企业微信钉钉考勤打卡签到

最近在看golang开发

之前看同事用golang实现钉钉打卡

这两天下班了研究下如何企业微信打卡

原理很简单,就是用golang通过adb操作手机

手机通过数据线连接上电脑,如果手机上装有360手机助手或者别的手机助手,需要检查或者占用了端口

同时手机要开启开发者权限

以下为代码

```
package main

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/Comdex/imgo"
"gopkg.in/gomail.v2"
)

var (
Warning *log.Logger
Info *log.Logger
Error *log.Logger
CurrentPath string
)

func init() {

//获取当前目录
path, _ := GetCurrentPath()
CurrentPath = path

// 初始化 log 模块
defer func() {
if err := recover(); err != nil {
fmt.Println("反正就是发生错误了")
}
}()
s := fmt.Sprintf("%s%s%s", CurrentPath, time.Now().Format("2006-01-02"), ".txt")

file, err := os.OpenFile(s, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
panic("打开文件错误")
}
//警告信息 stdout 输出 stderr 输出错误
Warning = log.New(os.Stdout, "警告信息:", log.Ldate|log.Lmicroseconds)
//提示信息
Info = log.New(io.MultiWriter(file, os.Stderr), "Info:", log.LstdFlags)
//错误信息
//discard 所有的io 不会操作 但是会返回成功
Error = log.New(ioutil.Discard, "错误信息", log.Lmicroseconds)
//Info.Println("ssss")

}

func main() {

//随机休眠
rd := rand.New(rand.NewSource(time.Now().UnixNano()))
s := time.Duration(rd.Intn(180)) * time.Second
Info.Println(fmt.Sprintf("%s%s", "随机休眠 ", s))
//time.Sleep(s)

//检测工作日
if IsWorkDay() == true {
//fmt.Print("test")
mx6()
} else {
mx6()
log.Fatalf("no work")
}

//sendmail()
//mx6();

}

func mx6() {
//设备id
device := "a9675225"
//连接设备
runAdb("connect", device)
//点亮屏幕
runAdb("-s", device, "shell", fmt.Sprintf("input keyevent 26"))
time.Sleep(time.Duration(1) * time.Second)
//上滑
runAdb("-s", device, "shell", fmt.Sprintf("input swipe 300 1500 300 500"))
time.Sleep(time.Duration(2) * time.Second)
//解锁
runAdb("-s", device, "shell", fmt.Sprintf("input tap 240 680"))
// runAdb("-s", device, "shell", fmt.Sprintf("input tap 550 680"))
// runAdb("-s", device, "shell", fmt.Sprintf("input tap 550 960"))
// runAdb("-s", device, "shell", fmt.Sprintf("input tap 550 1200"))
time.Sleep(time.Duration(2) * time.Second)
Info.Println("解锁完毕")
// runAdb("-s", device, "shell", fmt.Sprintf("input keyevent 3"))
// kill
// runAdb("-s", device, "shell", fmt.Sprintf("am force-stop com.alibaba.android.rimet"))
runAdb("-s", device, "shell", fmt.Sprintf("am force-stop com.tencent.wework"))
time.Sleep(time.Duration(2) * time.Second)
Info.Println("kill")

if time.Now().Hour() > 10 {
Info.Println("进入晚上流程")

//唤醒应用
runAdb("-s", device, "shell", fmt.Sprintf("am start com.tencent.wework/com.tencent.wework.launch.LaunchSplashActivity"))
// runAdb("-s", device, "shell", fmt.Sprintf("am start -n com.alibaba.android.rimet/.biz.SplashActivity"))
Info.Println("唤醒应用")
time.Sleep(time.Duration(15) * time.Second)

// 中间
runAdb("-s", device, "shell", fmt.Sprintf("input tap 521 1459"))
Info.Println("中间")
time.Sleep(time.Duration(8) * time.Second)

//上滑
runAdb("-s", device, "shell", fmt.Sprintf("input swipe 90 495 49 100"))
Info.Println("上滑")
time.Sleep(time.Duration(4) * time.Second)

runAdb("-s", device, "shell", fmt.Sprintf("input tap 131 1095"))
Info.Println("进入打卡界面")
time.Sleep(time.Duration(15) * time.Second)

// runAdb("-s", device, "shell", fmt.Sprintf("input tap 365 990"))
// Info.Println("ding")

} else {
Info.Println("进入早晨流程")
//唤醒急速
// runAdb("-s", device, "shell", fmt.Sprintf("am start -n com.alibaba.android.rimet/.biz.SplashActivity"))
Info.Println("唤醒 急速ding")
time.Sleep(time.Duration(10) * time.Second)

Info.Println("进入晚上流程")

//唤醒应用
runAdb("-s", device, "shell", fmt.Sprintf("am start com.tencent.wework/com.tencent.wework.launch.LaunchSplashActivity"))
// runAdb("-s", device, "shell", fmt.Sprintf("am start -n com.alibaba.android.rimet/.biz.SplashActivity"))
Info.Println("唤醒应用")
time.Sleep(time.Duration(15) * time.Second)

// 中间
runAdb("-s", device, "shell", fmt.Sprintf("input tap 521 1459"))
Info.Println("中间")
time.Sleep(time.Duration(8) * time.Second)

//上滑
runAdb("-s", device, "shell", fmt.Sprintf("input swipe 90 495 49 100"))
Info.Println("上滑")
time.Sleep(time.Duration(4) * time.Second)

runAdb("-s", device, "shell", fmt.Sprintf("input tap 131 1095"))
Info.Println("进入打卡界面")
time.Sleep(time.Duration(15) * time.Second)

// runAdb("-s", device, "shell", fmt.Sprintf("input tap 365 990"))
// Info.Println("ding")
}

runAdb("-s", device, "shell", fmt.Sprintf("screencap -p /sdcard/screen.png"))
Info.Println("截屏")
runAdb("-s", device, "pull", "/sdcard/screen.png", fmt.Sprintf("%s%s", CurrentPath, "screen1.png"))
Info.Println("下载")
Info.Println("发送邮件")
sendmail()
Info.Println("邮件完毕")

}

//识别图片
func imgtest() bool {
// img := imgo.MustRead("E:\\adb\\go\\screen.png")
// r := img[1][1][0]
// g := img[1][1][1]
// b := img[1][1][2]
// a := img[1][1][3]

// fmt.Println(r, g, b, a)

cos, _ := imgo.CosineSimilarity("screen.png", "start.png")
fmt.Println(cos)
result := false

if cos > 0.98 {
result = true
//fmt.Println("是登录页面");
}

return result

}

//运行adb命令
func runAdb(args ...string) {
var b bytes.Buffer
cmd := exec.Command("adb", args...)
cmd.Stdout = &b
cmd.Stderr = &b
log.Printf("adb %s", strings.Join(args, " "))
err := cmd.Run()
if cmd.Process != nil {
cmd.Process.Kill()
}
if err != nil {
log.Fatalf("adb %s: %v", strings.Join(args, " cccc"), err.Error())
}
}

//检测工作日
func IsWorkDay() bool {
url := "http://tool.bitefu.net/jiari/?d=" + time.Now().Format("20060102")
// url := "http://tool.bitefu.net/jiari/?d=20190205";
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}

// fmt.Println("v1 type:", reflect.TypeOf(body))
fmt.Println("v1 type:", string(body))

bodystr := string(body)
result := false
//0:工作日 1:假日 2:节日
if bodystr == "0" {
result = true
// fmt.Println("is 0")
}
return result
}

func sendmail() {
m := gomail.NewMessage()
m.SetHeader("From", "test@163.com")
m.SetHeader("To", "test@163.com")
if time.Now().Hour() < 10 { m.SetHeader("Subject", fmt.Sprintf("早晨打卡记录 %v", time.Now().Format("2006-01-02 15:04:05"))) } else { m.SetHeader("Subject", fmt.Sprintf("晚上打卡记录 %v", time.Now().Format("2006-01-02 15:04:05"))) } m.SetBody("text/html", "打卡记录生成请查看")
s := fmt.Sprintf("%s%s%s", CurrentPath, time.Now().Format("2006-01-02"), ".txt")
m.Attach(fmt.Sprintf("%s%s", CurrentPath, "screen1.png"))

b, err := ioutil.ReadFile(s)
if err != nil {
fmt.Print(err)
}
str := string(b)
m.SetBody("text/plain", str)

d := gomail.NewDialer("smtp.163.com", 25, "test@163.com", "MRVMBVQRFLBXTLQC")

// Send
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}

func GetCurrentPath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
path, err := filepath.Abs(file)
if err != nil {
return "", err
}
//fmt.Println("path111:", path)
if runtime.GOOS == "windows" {
path = strings.Replace(path, "\\", "/", -1)
}
//fmt.Println("path222:", path)
i := strings.LastIndex(path, "/")
if i < 0 { return "", errors.New(`Can't find "/" or "\".`) } //fmt.Println("path333:", path) return string(path[0 : i+1]), nil } ```

    A+
发布日期:2022年12月03日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: