91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
![]() |
package api_conn
|
|||
|
|
|||
|
import (
|
|||
|
"all_platform/dbpool"
|
|||
|
"all_platform/tool"
|
|||
|
"fmt"
|
|||
|
"log"
|
|||
|
"net/http"
|
|||
|
"strconv"
|
|||
|
"time"
|
|||
|
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
machine_code "github.com/super-l/machine-code"
|
|||
|
)
|
|||
|
|
|||
|
// 定义一个路由组,应用于特定的中间件
|
|||
|
// 校验用户路由中间件
|
|||
|
|
|||
|
func authMiddleware(c *gin.Context) {
|
|||
|
if c.Request.Header.Get("key") != "" {
|
|||
|
if key := tool.Keyprin(c.Request.Header.Get("key")); key.Status == "1" {
|
|||
|
a, _ := strconv.ParseInt(key.End_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 {
|
|||
|
c.JSON(http.StatusOK, gin.H{"error": fmt.Sprintf("授权已到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)})
|
|||
|
log.Printf("授权已到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)
|
|||
|
c.Abort()
|
|||
|
} else {
|
|||
|
log.Printf("授权未到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)
|
|||
|
c.Next()
|
|||
|
}
|
|||
|
} else {
|
|||
|
c.JSON(http.StatusOK, gin.H{"error": "类型不匹配,api请求类型为1"})
|
|||
|
log.Println("类型不匹配,api请求类型为1")
|
|||
|
c.Abort()
|
|||
|
}
|
|||
|
} else {
|
|||
|
c.JSON(http.StatusOK, gin.H{"error": "密钥为空"})
|
|||
|
log.Println("密钥为空")
|
|||
|
c.Abort()
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func Auth_main(mobile_phone_number, status string) map[string]string {
|
|||
|
var auth_s dbpool.Douy_info
|
|||
|
fmt.Println("电脑序列号:", machine_code.Machine.BoardSerialNumber)
|
|||
|
if machine_code.Machine.BoardSerialNumber != "" {
|
|||
|
//查询sql 获取用户信息
|
|||
|
rows, err := dbpool.Query("SELECT * FROM `douy`.`douy_info` WHERE mobile_phone_number = ? and status = ?;", mobile_phone_number, status)
|
|||
|
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,
|
|||
|
}
|
|||
|
return aa
|
|||
|
}
|
|||
|
|
|||
|
// 认证参数
|
|||
|
type Auth struct {
|
|||
|
Msg string `json:"msg"` //输入手机号
|
|||
|
BoardSerialNumber string `json:"boardSerialNumber"` //输入主板序列号
|
|||
|
Status string `json:"status"` //输入1是api,2是客户端
|
|||
|
}
|