dt_automate/auth/util.go

82 lines
2.7 KiB
Go
Raw Normal View History

2025-03-11 16:04:26 +08:00
package auth
2025-02-19 18:30:19 +08:00
import (
"dt_automate/dbpool"
2025-03-20 16:44:25 +08:00
"dt_automate/tool"
2025-03-04 21:15:55 +08:00
"fmt"
2025-02-19 18:30:19 +08:00
"log"
2025-03-20 16:44:25 +08:00
"strconv"
"time"
2025-02-19 18:30:19 +08:00
machine_code "github.com/super-l/machine-code"
)
2025-03-20 16:44:25 +08:00
func auth_main() map[string]string {
2025-02-19 18:30:19 +08:00
var auth_s dbpool.Douy_info
if machine_code.Machine.BoardSerialNumber != "" {
//查询sql 获取用户信息
2025-03-11 15:07:17 +08:00
rows, err := dbpool.QueryRows("SELECT * FROM `douy`.`douy_info` WHERE serialNumber = ?;", machine_code.Machine.BoardSerialNumber)
2025-02-19 18:30:19 +08:00
if err != nil {
log.Println(err)
}
defer rows.Close()
// var typecho_userss []dbpool.Typecho_users
for rows.Next() {
2025-03-17 00:15:13 +08:00
if err := rows.Scan(&auth_s.Mobile_phone_number, &auth_s.SerialNumber, &auth_s.Username, &auth_s.Initial_time, &auth_s.End_time, &auth_s.Surplus_time, &auth_s.Key, &auth_s.Status); err != nil {
2025-02-19 18:30:19 +08:00
log.Println(err)
}
// typecho_userss = append(typecho_userss, users)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
// log.Println("查询到的结果:", auth_s)
} else {
log.Println("该设备未申请授权,该设备码为:", machine_code.Machine.BoardSerialNumber)
}
aa := map[string]string{
"mobile_phone_number": auth_s.Mobile_phone_number.String,
"serialNumber": auth_s.SerialNumber.String,
"username": auth_s.Username.String,
"initial_time": auth_s.Initial_time.String,
"end_time": auth_s.End_time.String,
"surplus_time": auth_s.Surplus_time.String,
"key": auth_s.Key.String,
2025-03-17 00:15:13 +08:00
"status": auth_s.Status.String,
2025-02-19 18:30:19 +08:00
}
// b, err := json.Marshal(aa)
// if err != nil {
// log.Println(err)
// }
// log.Println(string(b))
return aa
}
2025-03-20 16:44:25 +08:00
// 授权状态
func Auth() bool {
key := tool.Keyprin(auth_main()["key"])
if key.Status == "2" {
a, _ := strconv.ParseInt(key.End_time, 10, 64)
// b, _ := strconv.ParseInt(key.Initial_time, 10, 64) // 获取的不是当前时间
duration := time.Unix(a, 0).Sub(time.Unix(tool.Timestamp("second"), 0))
days := int(duration / (24 * time.Hour)) //天
hours := int(duration % (24 * time.Hour) / time.Hour) //小时
minutes := int(duration / time.Minute % 60) //分钟
secod := int(duration / time.Second % 60) //秒
if days <= 0 && hours <= 0 && minutes <= 0 {
fmt.Println("电脑序列号:", machine_code.Machine.BoardSerialNumber)
log.Printf("授权已到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)
return false
} else {
fmt.Println("电脑序列号:", machine_code.Machine.BoardSerialNumber)
log.Printf("授权未到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)
return true
}
} else {
fmt.Println("电脑序列号:", machine_code.Machine.BoardSerialNumber)
log.Println("没有授权,请联系管理员")
return false
}
}