guest.go 1.6 KB
Newer Older
Y
Your Name 已提交
1
package console_sqlite3
E
eoLinker API Management 已提交
2 3 4

import (
	SQL "database/sql"
Y
Your Name 已提交
5

黄孟柱 已提交
6 7
	"github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
8 9
)

Y
Your Name 已提交
10
//Login 登录
E
eoLinker API Management 已提交
11 12 13 14 15 16 17 18 19 20
func Login(loginCall, loginPassword string) (bool, int) {
	db := database.GetConnection()
	var userID int
	err := db.QueryRow("SELECT userID FROM goku_admin WHERE loginCall = ? AND loginPassword = ?;", loginCall, loginPassword).Scan(&userID)
	if err != nil {
		return false, 0
	}
	return true, userID
}

Y
Your Name 已提交
21
//CheckLogin 检查用户是否登录
E
eoLinker API Management 已提交
22 23 24 25 26 27 28 29 30 31
func CheckLogin(userToken string, userID int) bool {
	db := database.GetConnection()
	var loginPassword, loginCall string
	err := db.QueryRow("SELECT loginCall,loginPassword FROM goku_admin WHERE userID = ?;", userID).Scan(&loginCall, &loginPassword)
	if err != nil {
		return false
	}
	if utils.Md5(loginCall+loginPassword) == userToken {
		return true
	}
Y
Your Name 已提交
32
	return false
E
eoLinker API Management 已提交
33 34
}

Y
Your Name 已提交
35
//Register 用户注册
E
eoLinker API Management 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
func Register(loginCall, loginPassword string) bool {
	db := database.GetConnection()
	sql := "SELECT userID,loginPassword FROM goku_admin WHERE loginCall = ?;"
	password := ""
	userID := 0
	err := db.QueryRow(sql, loginCall).Scan(&userID, &password)
	if err != nil {
		if err == SQL.ErrNoRows {
			sql = "INSERT INTO goku_admin (loginPassword,loginCall) VALUES (?,?);"
		} else {
			return false
		}
	} else {
		if password != loginPassword {
			sql = "UPDATE goku_admin SET loginPassword = ? WHERE loginCall = ?;"
		} else {
			return true
		}
	}
	rows, err := db.Exec(sql, loginPassword, loginCall)
	if err != nil {
		return false
	}
	affectRow, _ := rows.RowsAffected()
	if affectRow > 0 {
		return true
	}
Y
Your Name 已提交
63
	return false
E
eoLinker API Management 已提交
64
}