guest.go 1.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package account

import (
	"errors"
	"github.com/eolinker/goku/console/controller"
	"github.com/eolinker/goku/console/module/account"
	"github.com/eolinker/goku/utils"
	"net/http"
	"strconv"
)

// 用户登录
func Login(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	//resultInfo := entity2.ResultInfo{}

	loginCall := httpRequest.PostFormValue("loginCall")
	loginPassword := httpRequest.PostFormValue("loginPassword")

	loginPassword = utils.Md5(loginPassword)
	flag, userID := account.Login(loginCall, loginPassword)
	if !flag {

		controller.WriteError(httpResponse,
			"100000",
			"guest",
			"[ERROR]Wrong username or password!",
			errors.New("Wrong username or password"))
		return
	}

	userCookie := &http.Cookie{Name: "userToken", Value: utils.Md5(loginCall + loginPassword), Path: "/", MaxAge: 86400}
	nameCookie := &http.Cookie{Name: "userID", Value: strconv.Itoa(userID), Path: "/", MaxAge: 86400}
	http.SetCookie(httpResponse, userCookie)
	http.SetCookie(httpResponse, nameCookie)

	controller.WriteResultInfo(httpResponse, "guest", "userID", userID)
	return
}