Golang 雲盤開發之增加用戶系統

用戶表建表語句

CREATE TABLE `tbl_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '用戶名',
  `user_pwd` varchar(256) NOT NULL DEFAULT '' COMMENT '用戶encoded密碼',
  `email` varchar(64) DEFAULT '' COMMENT '郵箱',
  `phone` varchar(128) DEFAULT '' COMMENT '手機號',
  `email_validated` tinyint(1) DEFAULT 0 COMMENT '郵箱是否已驗證',
  `phone_validated` tinyint(1) DEFAULT 0 COMMENT '手機號是否已驗證',
  `signup_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '註冊日期',
  `last_active` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最後活躍時間戳',
  `profile` text COMMENT '用戶屬性',
  `status` int(11) NOT NULL DEFAULT '0' COMMENT '賬戶狀態(啓用/禁用/鎖定/標記刪除等)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_username` (`user_name`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Model 數據

// User : 用戶表model
type User struct {
	Username     string
	Email        string
	Phone        string
	SignupAt     string
	LastActiveAt string
	Status       int
}

 接口梳理

對於有登錄邏輯的系統,使用驗證邏輯

用戶註冊邏輯

// SignupHandler : 處理用戶註冊請求
func SignupHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet {
		// data, err := ioutil.ReadFile("./static/view/signup.html")
		// if err != nil {
		// 	w.WriteHeader(http.StatusInternalServerError)
		// 	return
		// }
		// w.Write(data)
		http.Redirect(w, r, "/static/view/signup.html", http.StatusFound)
		return
	}
	r.ParseForm()

	username := r.Form.Get("username")
	passwd := r.Form.Get("password")

	if len(username) < 3 || len(passwd) < 5 {
		w.Write([]byte("Invalid parameter"))
		return
	}

	// 對密碼進行加鹽及取Sha1值加密
	encPasswd := util.Sha1([]byte(passwd + pwdSalt))
	// 將用戶信息註冊到用戶表中
	suc := dblayer.UserSignup(username, encPasswd)
	if suc {
		w.Write([]byte("SUCCESS"))
	} else {
		w.Write([]byte("FAILED"))
	}
}

登錄業務邏輯

包括邏輯校驗,生成token,跳轉首頁三部分

// SignInHandler : 登錄接口
func SignInHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet {
		// data, err := ioutil.ReadFile("./static/view/signin.html")
		// if err != nil {
		// 	w.WriteHeader(http.StatusInternalServerError)
		// 	return
		// }
		// w.Write(data)
		http.Redirect(w, r, "/static/view/signin.html", http.StatusFound)
		return
	}

	r.ParseForm()
	username := r.Form.Get("username")
	password := r.Form.Get("password")

	encPasswd := util.Sha1([]byte(password + pwdSalt))

	// 1. 校驗用戶名及密碼
	pwdChecked := dblayer.UserSignin(username, encPasswd)
	if !pwdChecked {
		w.Write([]byte("FAILED"))
		return
	}

	// 2. 生成訪問憑證(token)
	token := GenToken(username)
	upRes := dblayer.UpdateToken(username, token)
	if !upRes {
		w.Write([]byte("FAILED"))
		return
	}

	// 3. 登錄成功後重定向到首頁
	//w.Write([]byte("http://" + r.Host + "/static/view/home.html"))
	resp := util.RespMsg{
		Code: 0,
		Msg:  "OK",
		Data: struct {
			Location string
			Username string
			Token    string
		}{
			Location: "http://" + r.Host + "/static/view/home.html",
			Username: username,
			Token:    token,
		},
	}
	w.Write(resp.JSONBytes())
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章