Browse Source

登录完成

tangs 6 years ago
parent
commit
597d4b0d61

+ 156 - 0
bk/ddpf.sql

@@ -0,0 +1,156 @@
+/*==============================================================*/
+/* DBMS name:      MySQL 5.0                                    */
+/* Created on:     2017/8/15 0:17:29                            */
+/*==============================================================*/
+
+
+drop index LASTTIME on SESSION;
+
+drop index TIME on SESSION;
+
+drop index UID on SESSION;
+
+drop index ID on SESSION;
+
+drop table if exists SESSION;
+
+drop index College on User;
+
+drop index TIME on User;
+
+drop index Status on User;
+
+drop index Role on User;
+
+drop index Password on User;
+
+drop index Username on User;
+
+drop index Account on User;
+
+drop index ID on User;
+
+drop table if exists User;
+
+/*==============================================================*/
+/* Table: SESSION                                               */
+/*==============================================================*/
+create table SESSION
+(
+   ID                   varchar(64),
+   UID                  varchar(64),
+   TIME                 int,
+   LASTTIME             int
+);
+
+/*==============================================================*/
+/* Index: ID                                                    */
+/*==============================================================*/
+create index ID on SESSION
+(
+   ID
+);
+
+/*==============================================================*/
+/* Index: UID                                                   */
+/*==============================================================*/
+create index UID on SESSION
+(
+   UID
+);
+
+/*==============================================================*/
+/* Index: TIME                                                  */
+/*==============================================================*/
+create index TIME on SESSION
+(
+   TIME
+);
+
+/*==============================================================*/
+/* Index: LASTTIME                                              */
+/*==============================================================*/
+create index LASTTIME on SESSION
+(
+   LASTTIME
+);
+
+/*==============================================================*/
+/* Table: User                                                  */
+/*==============================================================*/
+create table User
+(
+   ID                   varchar(64) comment '用户id',
+   Account              varchar(64) comment '用户的系统账号',
+   Name                 varchar(512) comment '用户名',
+   Password             varchar(1024) comment '用户密码',
+   Role                 int comment '用户角色',
+   Status               varchar(32),
+   Time                 int comment '用户创建时间',
+   College              varchar(128) comment '用户所在的学院'
+);
+
+/*==============================================================*/
+/* Index: ID                                                    */
+/*==============================================================*/
+create index ID on User
+(
+   ID
+);
+
+/*==============================================================*/
+/* Index: Account                                               */
+/*==============================================================*/
+create index Account on User
+(
+   Account
+);
+
+/*==============================================================*/
+/* Index: Username                                              */
+/*==============================================================*/
+create index Username on User
+(
+   Name
+);
+
+/*==============================================================*/
+/* Index: Password                                              */
+/*==============================================================*/
+create index Password on User
+(
+   Password
+);
+
+/*==============================================================*/
+/* Index: Role                                                  */
+/*==============================================================*/
+create index Role on User
+(
+   Role
+);
+
+/*==============================================================*/
+/* Index: Status                                                */
+/*==============================================================*/
+create index Status on User
+(
+   Status
+);
+
+/*==============================================================*/
+/* Index: TIME                                                  */
+/*==============================================================*/
+create index TIME on User
+(
+   Time
+);
+
+/*==============================================================*/
+/* Index: College                                               */
+/*==============================================================*/
+create index College on User
+(
+   College
+);
+

BIN
pkg/windows_amd64/ddpf/model/dbm.a


BIN
pkg/windows_amd64/ddpf/model/session.a


BIN
pkg/windows_amd64/ddpf/model/user.a


BIN
pkg/windows_amd64/ddpf/response.a


BIN
pkg/windows_amd64/ddpf/test.a


BIN
pkg/windows_amd64/github.com/go-sql-driver/mysql.a


BIN
pkg/windows_amd64/github.com/tangs-drm/go-tool/dbm.a


BIN
pkg/windows_amd64/github.com/tangs-drm/go-tool/http.a


BIN
pkg/windows_amd64/github.com/tangs-drm/go-tool/log.a


BIN
pkg/windows_amd64/github.com/tangs-drm/go-tool/util.a


File diff suppressed because it is too large
+ 3386 - 0
src/ddpf.pdb


File diff suppressed because it is too large
+ 3386 - 0
src/ddpf.pdm


+ 156 - 0
src/ddpf.sql

@@ -0,0 +1,156 @@
+/*==============================================================*/
+/* DBMS name:      MySQL 5.0                                    */
+/* Created on:     2017/8/15 0:17:29                            */
+/*==============================================================*/
+
+
+drop index LASTTIME on SESSION;
+
+drop index TIME on SESSION;
+
+drop index UID on SESSION;
+
+drop index ID on SESSION;
+
+drop table if exists SESSION;
+
+drop index College on User;
+
+drop index TIME on User;
+
+drop index Status on User;
+
+drop index Role on User;
+
+drop index Password on User;
+
+drop index Username on User;
+
+drop index Account on User;
+
+drop index ID on User;
+
+drop table if exists User;
+
+/*==============================================================*/
+/* Table: SESSION                                               */
+/*==============================================================*/
+create table SESSION
+(
+   ID                   varchar(64),
+   UID                  varchar(64),
+   TIME                 int,
+   LASTTIME             int
+);
+
+/*==============================================================*/
+/* Index: ID                                                    */
+/*==============================================================*/
+create index ID on SESSION
+(
+   ID
+);
+
+/*==============================================================*/
+/* Index: UID                                                   */
+/*==============================================================*/
+create index UID on SESSION
+(
+   UID
+);
+
+/*==============================================================*/
+/* Index: TIME                                                  */
+/*==============================================================*/
+create index TIME on SESSION
+(
+   TIME
+);
+
+/*==============================================================*/
+/* Index: LASTTIME                                              */
+/*==============================================================*/
+create index LASTTIME on SESSION
+(
+   LASTTIME
+);
+
+/*==============================================================*/
+/* Table: User                                                  */
+/*==============================================================*/
+create table User
+(
+   ID                   varchar(64) comment '用户id',
+   Account              varchar(64) comment '用户的系统账号',
+   Name                 varchar(512) comment '用户名',
+   Password             varchar(1024) comment '用户密码',
+   Role                 int comment '用户角色',
+   Status               varchar(32),
+   Time                 int comment '用户创建时间',
+   College              varchar(128) comment '用户所在的学院'
+);
+
+/*==============================================================*/
+/* Index: ID                                                    */
+/*==============================================================*/
+create index ID on User
+(
+   ID
+);
+
+/*==============================================================*/
+/* Index: Account                                               */
+/*==============================================================*/
+create index Account on User
+(
+   Account
+);
+
+/*==============================================================*/
+/* Index: Username                                              */
+/*==============================================================*/
+create index Username on User
+(
+   Name
+);
+
+/*==============================================================*/
+/* Index: Password                                              */
+/*==============================================================*/
+create index Password on User
+(
+   Password
+);
+
+/*==============================================================*/
+/* Index: Role                                                  */
+/*==============================================================*/
+create index Role on User
+(
+   Role
+);
+
+/*==============================================================*/
+/* Index: Status                                                */
+/*==============================================================*/
+create index Status on User
+(
+   Status
+);
+
+/*==============================================================*/
+/* Index: TIME                                                  */
+/*==============================================================*/
+create index TIME on User
+(
+   Time
+);
+
+/*==============================================================*/
+/* Index: College                                               */
+/*==============================================================*/
+create index College on User
+(
+   College
+);
+

+ 48 - 17
src/ddpf/login/login.go

@@ -1,11 +1,14 @@
 package login
 
 import (
-	"net/http"
+	"ddpf/model/session"
+	"ddpf/model/user"
 	"ddpf/response"
+	"fmt"
 	"github.com/tangs-drm/go-tool/log"
 	"github.com/tangs-drm/go-tool/util"
-	"ddpf/model/user"
+	"net/http"
+	"strings"
 )
 
 func Login(w http.ResponseWriter, r *http.Request) {
@@ -14,16 +17,16 @@ func Login(w http.ResponseWriter, r *http.Request) {
 	var msg string
 	var data util.Map
 	defer func() {
+		msg = response.Message[code]
 		response.WriteResponse(w, code, msg, err, data)
 	}()
-	username := r.PostFormValue("username")
-	password := r.PostFormValue("password")
+	username := r.FormValue("account")
+	password := r.FormValue("password")
 
 	if len(username) < 1 || len(password) < 1 {
 		err = util.Error("username/password contain a empty value")
 		log.Error("Login receive invalid username/password ->(%v)", err)
-		code = 1
-		msg = "用户名/密码为空"
+		code = 100
 		return
 	}
 
@@ -33,13 +36,11 @@ func Login(w http.ResponseWriter, r *http.Request) {
 	if err != nil {
 		log.Error("Login receive username(%v) ExistUser error ->(%v)", username, err)
 		code = 2
-		msg = "系统错误"
 		return
 	}
 	if !isExist {
 		log.Debug("Login receive username(%v) is not exist", username)
-		code = 3
-		msg = "用户不存在"
+		code = 101
 		return
 	}
 
@@ -48,17 +49,47 @@ func Login(w http.ResponseWriter, r *http.Request) {
 	usr, err = user.CheckPassword(username, password)
 	if err != nil {
 		log.Error("Login receive username(%v) check password error ->(%v)", username, err)
-		code = 4
-		msg = "系统错误"
+		code = 2
 		return
 	}
-	if nil == usr {
+	if len(usr.Id) < 1 {
 		log.Debug("Login receive username(%v) password is incorrect", username)
-		code = 5
-		msg = "密码错误"
+		code = 102
 		return
 	}
 
-	// 写cookie
-	//session.CreateSession()
-}
+	// 创建session
+	sess, err := session.CreateSession(usr.Id)
+	if err != nil {
+		log.Error("Login create session by uid(%v) error ->(%v)", usr.Id, err)
+		code = 2
+		return
+	}
+
+	cookie := &http.Cookie{Name: "token", Value: sess.Id, Domain: "/"}
+	http.SetCookie(w, cookie)
+	log.Debug("Login with account(%v) success", username)
+	return
+}
+
+// DoLogin 为了测试的时候方便调用的方式,用户测试
+func DoLogin(username string, password string) (util.Map, error) {
+	url := fmt.Sprintf("%v/login?account=%v&password=%v", SrvAddr(), username, password)
+
+	var headers = map[string]string{
+		"Content-Type": "application/x-www-form-urlencoded",
+	}
+	var reqBody = util.Map{
+		"account":  username,
+		"password": password,
+	}
+	body := strings.NewReader(util.S2Json(reqBody))
+	res, err := util.HTTPPostMap(url, headers, body)
+	if err != nil {
+		return nil, err
+	}
+	if res.Int("code") != 0 {
+		return nil, util.Error("%v", res.Int("code"))
+	}
+	return res, nil
+}

+ 100 - 0
src/ddpf/login/login_test.go

@@ -0,0 +1,100 @@
+package login
+
+import (
+	"ddpf/model/dbm"
+	"ddpf/test"
+	"github.com/tangs-drm/go-tool/log"
+	"github.com/tangs-drm/go-tool/util"
+	"net/http"
+	"testing"
+	"time"
+)
+
+func init() {
+	test.SqlConf = "../../ddpf.sql"
+	test.Init()
+	mux := http.NewServeMux()
+	Router()
+	SrvAddr = func() string {
+		return "http://127.0.0.1:7890"
+	}
+	mux.HandleFunc("/login", Login)
+	go func() {
+		log.Debug("init start listen on 7890")
+		err := http.ListenAndServe(":7890", mux)
+		if err != nil {
+			panic(err)
+		}
+	}()
+	time.Sleep(3 * time.Second)
+}
+
+func TestDoLogin(t *testing.T) {
+	var times int = 3
+	for i := 0; i < times; i++ {
+		testDogin(t, i)
+	}
+}
+
+func testDogin(t *testing.T, times int) {
+	var err error
+
+	log.Debug("testDologin times %v --- START", times)
+	// 准备用户数据
+	// user 1
+	var sqlCommand = "INSERT INTO USER(ID, NAME, ACCOUNT, PASSWORD, ROLE, STATUS, COLLEGE) value(?, ?, ?, ?, ?, ?, ?);"
+	_, err = dbm.Db().Exec(sqlCommand, util.UUID(), "user1", "123", "123456", 1, "normal", "computer")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	// user 2
+	_, err = dbm.Db().Exec(sqlCommand, util.UUID(), "user2", "124", "123456", 1, "normal", "computer")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	// user 3
+	_, err = dbm.Db().Exec(sqlCommand, util.UUID(), "user4", "125", "123456", 1, "normal", "computer")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+
+	log.Debug("START to login")
+	// login
+	var username, password string
+	var result util.Map
+
+	// username/password is empty
+	result, err = DoLogin(username, password)
+	if err == nil {
+		t.Error(err)
+		return
+	}
+	log.Debug("result --- %v", util.S2Json(result))
+
+	// user is not exist
+	result, err = DoLogin("halo", "halo")
+	if err == nil {
+		t.Error(err)
+		return
+	}
+
+	// password is invalid
+	result, err = DoLogin("123", "halo")
+	if err == nil {
+		t.Error(err)
+		return
+	}
+
+	// success
+	result, err = DoLogin("123", "123456")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+
+	log.Debug("DoLogin success result --- %v", util.S2Json(result))
+	log.Debug("testDogin times %v --- END", times)
+}

+ 5 - 1
src/ddpf/login/router.go

@@ -2,6 +2,10 @@ package login
 
 import "net/http"
 
+var SrvAddr = func() string {
+	panic("nil")
+}
+
 func Router() {
-	http.HandleFunc("login", Login)
+	http.HandleFunc("/login", Login)
 }

+ 0 - 16
src/ddpf/login/session_test.go

@@ -1,16 +0,0 @@
-package login
-
-import "testing"
-
-func TestSession(t *testing.T) {
-	type testFunc func() error
-
-	var tf testFunc = func() error {
-		return nil
-	}
-	if tf == nil {
-		t.Error(nil)
-		return
-	}
-	return
-}

+ 2 - 2
src/ddpf/model/dbm/dbm.go

@@ -2,8 +2,8 @@ package dbm
 
 import (
 	"github.com/tangs-drm/go-tool/dbm"
-	"io/ioutil"
 	"github.com/tangs-drm/go-tool/log"
+	"io/ioutil"
 )
 
 var Db = func() *dbm.DB {
@@ -23,4 +23,4 @@ func Init(path string) error {
 
 	log.Debug("[Init] dbm init db with path(%v) success", path)
 	return nil
-}
+}

+ 3 - 20
src/ddpf/model/session/session_test.go

@@ -2,7 +2,6 @@ package session
 
 import (
 	"testing"
-	tdbm "github.com/tangs-drm/go-tool/dbm"
 	"ddpf/model/dbm"
 	"time"
 	"github.com/tangs-drm/go-tool/log"
@@ -11,28 +10,12 @@ import (
 	"github.com/tangs-drm/go-tool/http"
 	ghttp "net/http"
 	"io/ioutil"
+	"ddpf/test"
 )
 
 func init() {
-	// 开启服务
-	SessionLog = true
-	db, err := tdbm.Default("testUser:123@tcp(localhost:3306)/testdb?charset=utf8")
-	if err != nil {
-		panic(err)
-		return
-	}
-	err = db.Ping()
-	if err != nil {
-		panic(err)
-		return
-	}
-	// init order.sql
-	var sqlConf string = "../../../order.sql"
-	err = dbm.Init(sqlConf)
-	if err != nil {
-		panic(err)
-		return
-	}
+	test.SqlConf = "../../../ddpf.sql"
+	test.Init()
 }
 
 // TestSession 对session的单元测试

+ 1 - 1
src/ddpf/model/user/items.go

@@ -19,9 +19,9 @@ type User struct  {
 	Id string `json:"id"`		// 用户id
 	Name string `json:"name"`	// 用户名
 	Account string `json:"account"`	// 用户工号
+	Password string `json:"password"` //用户密码
 	Role int `json:"role"`	// 用户角色
 	Status string `json:"status"` // 用户状态
 	Time int64 `json:"time"`	// 创建时间
 	College string `json:"college"` // 所在学院
-	Attrs map[string]interface{} // 其他属性
 }

+ 12 - 2
src/ddpf/model/user/login.go

@@ -25,7 +25,17 @@ func ExistUser(account string) (bool, error) {
 	return true, nil
 }
 
-// CheckPassword 检查用户的账户密码是否正确
+/*
+CheckPassword
+arg
+	account:用户账号
+	password:用户密码
+desc
+	检查用户账号和密码是否匹配
+ret
+	0:返回用户信息
+	1:返回错误信息
+*/
 func CheckPassword(account, password string) (*User, error) {
 	var sqlString string = "SELECT ID FROM USER WHERE ACCOUNT = ? AND PASSWORD = ?;"
 	rows, err := dbm.Db().Query(sqlString, account, password)
@@ -36,7 +46,7 @@ func CheckPassword(account, password string) (*User, error) {
 	defer rows.Close()
 
 	if !rows.Next() {
-		return nil, nil
+		return &User{}, nil
 	}
 	var user = &User{}
 	err = rows.Scan(&user.Id)

+ 11 - 0
src/ddpf/response/message.go

@@ -0,0 +1,11 @@
+package response
+
+var Message = map[int]string{
+	0: "成功",
+	1: "参数错误",
+	2: "系统错误",
+
+	100: "用户名/密码为空",
+	101: "用户不存在",
+	102: "密码错误",
+}

+ 31 - 0
src/ddpf/test/test.go

@@ -0,0 +1,31 @@
+package test
+
+import (
+	"ddpf/model/dbm"
+	"ddpf/model/session"
+	tdbm "github.com/tangs-drm/go-tool/dbm"
+)
+
+var SqlConf = ""
+
+func Init() {
+	// 开启服务
+	session.SessionLog = true
+	db, err := tdbm.Default("testUser:123@tcp(localhost:3306)/testdb?charset=utf8")
+	if err != nil {
+		panic(err)
+		return
+	}
+	err = db.Ping()
+	if err != nil {
+		panic(err)
+		return
+	}
+	// init order.sql
+	var sqlConf string = SqlConf
+	err = dbm.Init(sqlConf)
+	if err != nil {
+		panic(err)
+		return
+	}
+}

+ 0 - 60
src/order.sql

@@ -1,60 +0,0 @@
-/*==============================================================*/
-/* DBMS name:      MySQL 5.0                                    */
-/* Created on:     2017/8/6 14:47:07                            */
-/*==============================================================*/
-
-
-DROP INDEX LASTTIME ON SESSION;
-
-DROP INDEX TIME ON SESSION;
-
-DROP INDEX UID ON SESSION;
-
-DROP INDEX ID ON SESSION;
-
-DROP TABLE IF EXISTS SESSION;
-
-/*==============================================================*/
-/* Table: SESSION                                               */
-/*==============================================================*/
-CREATE TABLE SESSION
-(
-   ID                   VARCHAR(64) NOT NULL,
-   UID                  VARCHAR(64),
-   TIME                 INT,
-   LASTTIME             INT,
-   PRIMARY KEY (ID)
-);
-
-/*==============================================================*/
-/* Index: ID                                                    */
-/*==============================================================*/
-CREATE INDEX ID ON SESSION
-(
-   ID
-);
-
-/*==============================================================*/
-/* Index: UID                                                   */
-/*==============================================================*/
-CREATE INDEX UID ON SESSION
-(
-   UID
-);
-
-/*==============================================================*/
-/* Index: TIME                                                  */
-/*==============================================================*/
-CREATE INDEX TIME ON SESSION
-(
-   TIME
-);
-
-/*==============================================================*/
-/* Index: LASTTIME                                              */
-/*==============================================================*/
-CREATE INDEX LASTTIME ON SESSION
-(
-   LASTTIME
-);
-