dt_automate/auth/util.go
2025-03-20 16:44:25 +08:00

82 lines
2.7 KiB
Go

package auth
import (
"dt_automate/dbpool"
"dt_automate/tool"
"fmt"
"log"
"strconv"
"time"
machine_code "github.com/super-l/machine-code"
)
func auth_main() map[string]string {
var auth_s dbpool.Douy_info
if machine_code.Machine.BoardSerialNumber != "" {
//查询sql 获取用户信息
rows, err := dbpool.QueryRows("SELECT * FROM `douy`.`douy_info` WHERE serialNumber = ?;", machine_code.Machine.BoardSerialNumber)
if err != nil {
log.Println(err)
}
defer rows.Close()
// var typecho_userss []dbpool.Typecho_users
for rows.Next() {
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 {
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,
"status": auth_s.Status.String,
}
// b, err := json.Marshal(aa)
// if err != nil {
// log.Println(err)
// }
// log.Println(string(b))
return aa
}
// 授权状态
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
}
}