golang 实现简单的注册登陆 忘记修改密码

main.go

package main

import (
	"log"
	"net/http"
	"yj/url-encode/common"
	"yj/url-encode/login"
	"yj/url-encode/register"
	"yj/url-encode/update_password"
)

func main() {
	http.HandleFunc("/sendVerificationCode", common.Sendverifycode)
	http.HandleFunc("/register", register.Register)
	http.HandleFunc("/login", login.Login) //登录
	http.HandleFunc("/newPassword", update_password.UpdatePassword)
	log.Println("start")

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Println(err)
	}
}

注册
register.go

package register

import (
	"database/sql"
	"fmt"
	"net/http"
	"strconv"
	"yj/url-encode/common"

	_ "github.com/go-sql-driver/mysql" //注册MySQL驱动
)

type VerifyUser struct { //user who will verify verification code
	Email      string
	Verifycode int
}

//var AllUserVerifyCode = make([]VerifyUser, 0)

func VerifyUsername(user common.User) bool {
	if common.Alluserinfo[user.Email].Username == user.Username {
		return true
	}
	return true
}

func EmailExists(user common.User) bool {
	for k, _ := range common.Code {
		if k == user.Email {
			return true
		}

	}
	return true
}
func Register(w http.ResponseWriter, r *http.Request) {
	var user common.User
	r.ParseForm()
	user.Username = r.FormValue("Username")
	user.Password = r.FormValue("Password")
	user.Email = r.FormValue("Email")
	registerVerifycode, _ := strconv.Atoi(r.FormValue("Verifycode"))

	if registerVerifycode == common.Code[user.Email] {
		if VerifyUsername(user) == false {
			status_username := common.Status{true, "username is not existed"}
			w.Write(common.Status.Marshal(status_username))
		}
		if EmailExists(user) == false {
			status_email := common.Status{true, "Email is  not existed"}
			w.Write(common.Status.Marshal(status_email))
		}
		common.Alluserinfo[user.Email] = user
		fmt.Println(common.Alluserinfo)
		//用户名:密码@tcp(地址:3306)/数据库名
		db, err := sql.Open("mysql", "root:123456@tcp(192.168.2.132:3306)/test")
		if err != nil {
			fmt.Println(err)
		}
		//把数据存入数据库
		for _, v := range common.Alluserinfo {
			result, err := db.Exec("INSERT INTO Alluserinfo(Username,Password,Email)VALUES(?,?,?)", v.Username, v.Password, v.Email)
			if err != nil {
				fmt.Println(result, err)
				status_verifycode := common.Status{false, "用户名或用户邮箱已存在"}
				w.Write(common.Status.Marshal(status_verifycode))
			} else {
				status_verifycode := common.Status{true, "register successful"}
				w.Write(common.Status.Marshal(status_verifycode))

			}

		}

		//AllUserVerifyCode = append(AllUserVerifyCode, user_register)

	} else if registerVerifycode != common.Code[user.Email] {
		status_wrong := common.Status{false, "Verifycode is wrong"}
		w.Write(common.Status.Marshal(status_wrong))
	}
}

登陆
login.go

package login

import (
	"database/sql"
	"fmt"
	"net/http"
	"yj/url-encode/common"
)

type loginuser struct {
	Username string
	Password string
}

func Login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	var user common.User
	user.Username = r.FormValue("Username")
	user.Password = r.FormValue("Password")
	user.Email = r.FormValue("Email")
	//用户名:密码@tcp(地址:3306)/数据库名
	db, err := sql.Open("mysql", "root:123456@tcp(192.168.2.132:3306)/test")
	if err != nil {
		fmt.Println(err)
	}
	//查询数据库数据
	var tempuser common.User
	err = db.QueryRow("select * from Alluserinfo where Email = ?", user.Email).Scan(&tempuser.Username, &tempuser.Password, &tempuser.Email)
	if err != nil {
		fmt.Println("查询出错了", err)
	}
	common.Alluserinfo[user.Email] = tempuser
	fmt.Println(common.Alluserinfo)
	if !common.UsernameExists(user) {
		status_username := common.Status{false, "Username is not existed or password is wrong"}
		w.Write(common.Status.Marshal(status_username))
		return

	}
	if user.Username == common.Alluserinfo[user.Email].Username && user.Password != common.Alluserinfo[user.Email].Password {
		status_username := common.Status{false, "password is wrong"}
		w.Write(common.Status.Marshal(status_username))
		return
	}

	status_password := common.Status{true, "Password is true"}
	w.Write(common.Status.Marshal(status_password))

}

修改密码
updatepassword.go

package update_password

import (
	"database/sql"
	"fmt"
	"net/http"
	"strconv"
	"yj/url-encode/common"
	"yj/url-encode/register"

	_ "github.com/go-sql-driver/mysql" //注册MySQL驱动
)

func UpdatePassword(w http.ResponseWriter, r *http.Request) {
	var user_updatepassword common.User
	r.ParseForm()
	user_updatepassword.Username = r.FormValue("Username")
	user_updatepassword.Email = r.FormValue("Email")
	user_updatepassword.Password = r.FormValue("Password")
	verifycode, _ := strconv.Atoi(r.FormValue("Verifycode"))

	if common.UsernameExists(user_updatepassword) == true && register.EmailExists(user_updatepassword) == true {
		num := common.Code[user_updatepassword.Email]
		if num == verifycode {
			status := common.Status{true, "verification is right"}
			w.Write(common.Status.Marshal(status))
			//用户名:密码@tcp(地址:3306)/数据库名
			db, err := sql.Open("mysql", "root:123456@tcp(192.168.2.132:3306)/test")
			if err != nil {
				fmt.Println(err)
			}
			//查询数据库数据
			var user common.User
			err = db.QueryRow("select * from Alluserinfo where Email = ?", user_updatepassword.Email).Scan(&user.Username, &user.Password, &user.Email)
			if err != nil {
				fmt.Println("查询出错了", err)
			}
			common.Alluserinfo[user_updatepassword.Email] = user
			if common.Alluserinfo[user_updatepassword.Email].Email == user_updatepassword.Email && common.Alluserinfo[user_updatepassword.Email].Username == user_updatepassword.Username {
				value := common.Alluserinfo[user_updatepassword.Email]
				value.Password = user_updatepassword.Password
				common.Alluserinfo[user_updatepassword.Email] = value
				status_update := common.Status{true, "update password successfully"}
				w.Write(common.Status.Marshal(status_update))
			}
			fmt.Println(common.Alluserinfo)

			//修改数据库数据
			for _, v := range common.Alluserinfo {
				result, err := db.Exec("update Alluserinfo SET Password=? WHERE Email=?", v.Password, v.Email)
				if err != nil {
					fmt.Println(result, err)
				}
			}
		} else if num != verifycode {
			status_wrong := common.Status{false, "verification is wrong"}
			w.Write(common.Status.Marshal(status_wrong))
		}
	} else if common.UsernameExists(user_updatepassword) == false || register.EmailExists(user_updatepassword) == true {
		statusupdate := common.Status{false, "username or email is not exist"}
		w.Write(common.Status.Marshal(statusupdate))
	}

}

common.go

package common

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"net/smtp"
	"time"

	_ "github.com/go-sql-driver/mysql" //注册MySQL驱动
)

type User struct {
	Username string
	Password string
	Email    string
}

type Status struct {
	Statuscode bool
	Details    string
}

type RegisterUser struct {
	RegisterEmail string
	Verifycode    int
}

var Code = make(map[string]int)         //存用户的Email
var Alluserinfo = make(map[string]User) //存用户数据,key为用户Email

func UsernameExists(user User) bool {
	for _, v := range Alluserinfo {
		if user.Username == v.Username {
			return true
		}
	}

	return true
}


func (s Status) Marshal() []byte {
	bytes, _ := json.Marshal(s)
	return bytes
}
func SendEmail(email string) int {
	auth := smtp.PlainAuth("", "[email protected]", "happy1314WyJ", "smtp.163.com")
	to := []string{"[email protected]"}
	rand.Seed(time.Now().Unix())
	num := rand.Intn(10000)
	str := fmt.Sprintf("From:[email protected]\r\nTo:[email protected]\r\nSubject:verifycode\r\n\r\nhaha,%d,\r\n.", num)
	msg := []byte(str)
	//msg := []byte("From:[email protected]\r\nTo:[email protected]\r\nSubject:verifycode\r\n\r\nhaha,num,\r\n.")
	err := smtp.SendMail("smtp.163.com:25", auth, "[email protected]", to, msg)
	if err != nil {
		log.Fatal(err)
	}
	return num
}

func Sendverifycode(w http.ResponseWriter, r *http.Request) {
	var registerUser RegisterUser
	r.ParseForm()
	registerUser.RegisterEmail = r.FormValue("Email")
	verifycode := SendEmail(registerUser.RegisterEmail)
	fmt.Println(verifycode)
	registerUser.Verifycode = verifycode
	for k, _ := range Code {
		if k == registerUser.RegisterEmail {
			Code[k] = registerUser.Verifycode
		}
	}
	statusemail := Status{true, "send verification code to your email"}
	w.Write(Status.Marshal(statusemail))
	Code[registerUser.RegisterEmail] = verifycode
	fmt.Println(Code)
}

你可能感兴趣的:(golang,项目,golang,注册登陆)