vue+go实现注册功能

发布时间 2023-12-26 21:01:23作者: 青烟绕指柔
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<style>
.container {
width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f5f5f5;
}

h1 {
text-align: center;
margin-bottom: 20px;
}

.form-group {
margin-bottom: 15px;
}

label {
display: block;
margin-bottom: 5px;
}

input[type="text"],
input[type="password"],
input[type="tel"] {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}

button {
display: block;
width: 100%;
padding: 10px;
border: none;
border-radius: 4px;
background-color: #4CAF50;
color: #fff;
font-size: 16px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<form id="registration-form" @submit.prevent="submitForm">
<h1>用户注册</h1>
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" v-model="username" placeholder="Enter your username" required>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" id="password" name="password" v-model="password" placeholder="Enter your password" required>
</div>
<div class="form-group">
<label for="phone">手机号:</label>
<input type="tel" id="phone" name="phone" v-model="phone" placeholder="Enter your phone number" required>
</div>
<button type="submit">点击注册</button>
</form>
</div>
</div>
</body>
</html>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
<script src="https://cdn.jsdelivr.net/npm/axios@0.24.0/dist/axios.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
username: '',
password: '',
phone: ''
},
methods: {
submitForm() {
// 使用 Axios 发送 GET 请求
axios({
method: "post",//请求方法
params: {
username: this.username,//请求参数
password: this.password,//请求参数
phone:this.phone
},
url: "http://localhost:8080/users/register",
}).then(res => {
if(res.data.code == 200) {
alert(res.data.message);
window.location.href = "list.html"
}else{
alert(res.data.message);
}
})
}
}
});
</script>

package main

import (
"encoding/json"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"net/http"
)

type Users struct {
Id int `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
Phone string `json:"phone"`
}

func Register(w http.ResponseWriter, r *http.Request) {

// 设置允许跨域的域名
w.Header().Set("Access-Control-Allow-Origin", "*")
// 初始化数据库连接
dsn := "root:root@tcp(127.0.0.1:8889)/2112a"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println("连接失败")
return
}

username := r.FormValue("username")
password := r.FormValue("password")
phone := r.FormValue("phone")
user := Users{
Username: username,
Password: password,
Phone: phone,
}
err = db.Create(&user).Error
if err != nil {
response := map[string]interface{}{"code": 500, "message": "添加失败"}
json.NewEncoder(w).Encode(response)
} else {
response := map[string]interface{}{"code": 200, "message": "添加成功"}
json.NewEncoder(w).Encode(response)
}

}
func main() {
http.HandleFunc("/users/register", Register)
http.ListenAndServe("localhost:8080", nil)
}